def cvRectangle(img, rect, colortup=None): if not colortup: color = cv.Scalar(255,255,255) else: color = cv.Scalar(colortup[0], colortup[1], colortup[2]) cv.Rectangle(img, (rect[0], rect[1]), (rect[0]+rect[2], rect[1]+rect[3]), color)
indexY = valuex.index(valuey) newList =[] newList.append(eyesList[indexX]) newList.append(eyesList[indexY]) finalEyesList=newList except: finalEyesList=None else: finalEyesList=None else: finalEyesList=None if finalEyesList: for eye in finalEyesList: cv.Rectangle(imcolor,eye[0], eye[1],cv.RGB(0, 255, 0),2) eyesList.append((pt1,pt2)); eye1=finalEyesList[0]; eye2=finalEyesList[1]; x1eye1=eye1[0][0] x2eye1=eye1[1][0] y1eye1=eye1[0][1] y2eye1=eye1[1][1] x1eye2=eye2[0][0] x2eye2=eye2[1][0] y1eye2=eye2[0][1] y2eye2=eye2[1][1] Area1 = round(abs(x1eye1-x2eye1)*abs(y1eye1-y2eye1),3) Area2 = round(abs(x1eye2-x2eye2)*abs(y1eye2-y2eye2),3) Area = round((image_size[0]*image_size[1]),2)
def detect_and_draw(img, cascade): # allocate temporary images gray = cv.CreateImage((img.width,img.height), 8, 1) #create a image with smaller size small_img = cv.CreateImage((cv.Round(img.width / image_scale), cv.Round (img.height / image_scale)), 8, 1) # convert color input image to grayscale cv.CvtColor(img, gray, cv.CV_BGR2GRAY) # scale input image for faster processing cv.Resize(gray, small_img, cv.CV_INTER_LINEAR) #if algorithm is present if(cascade): #to get the current time t = cv.GetTickCount() #create memory for calculation(createMemStorage) faces = cv.HaarDetectObjects(small_img, cascade, cv.CreateMemStorage(0), haar_scale, min_neighbors, haar_flags, min_size) #previous time minus current time t = cv.GetTickCount() - t print "detection time = %gms" % (t/(cv.GetTickFrequency()*1000.)) i=0 #if more then one faces detected if faces: #getting all the coordinates of face for ((x, y, w, h), n) in faces: i=1; # the input to cv.HaarDetectObjects was resized, so scale the # bounding box of each face and convert it to two CvPoints pt1 = (int(x * image_scale), int(y * image_scale)) pt2 = (int((x + w) * image_scale), int((y + h) * image_scale)) #draw rectangle (imagename,topleft,bottomright,color,size) cv.Rectangle(img,pt1,pt2,(0,230,0),1) #crop the image var1 = img[y: y + h, x: x + w] cv.SaveImage("face/database/image.png",var1) name="face/database/image.png" img=Image.open(name).convert('LA') img.save(name) break; cv.DestroyAllWindows() if i == 1: os.system("python resize.py") if i == 0: os.remove("face/database/image.png")
#cv.InRangeS(hue_img, (100,120,60), (200,255,255), threshold_img) # color code blue #cv.InRangeS(hue_img, (0,100,60), (10,255,255), threshold_img) # color code red storage = cv.CreateMemStorage(0) contour = cv.FindContours(threshold_img, storage, cv.CV_RETR_CCOMP, cv.CV_CHAIN_APPROX_SIMPLE) points = [] while contour: rect = cv.BoundingRect(list(contour)) contour = contour.h_next() size = (rect[2] * rect[3]) if size > 100: pt1 = (rect[0], rect[1]) pt2 = (rect[0] + rect[2], rect[1] + rect[3]) cv.Rectangle(img, pt1, pt2, cv.CV_RGB(255, 0, 0), 3) ######################################## contour center #################################################### cx = (rect[2] / 2 + rect[0]) #(0,0)################ cy = (rect[3] / 2 + rect[1]) ###################### print cx, cy ######################<--160x120 pix ###################### ############(160,120)# #将文字框加入到图片中,(5,30)定义了文字框左顶点在窗口中的位置,最后参数定义文字颜色 if cx <= (width * 4 / 7) and cx >= (width * 3 / 7) and cy <= ( height * 4 / 7) and cy >= (height * 3 / 7): TestStr = "Locking" cv.PutText(img, TestStr, (5, 30), font, (0, 0, 255)) else: TestStr = "serching...."
def run(self): # Initialize log_file_name = "tracker_output.log" log_file = open( log_file_name, 'a' ) #fps = 25 #cap = cv2.VideoCapture( '../000104-.avi' frame = cv.QueryFrame( self.capture ) frame_size = cv.GetSize( frame ) foreground = cv.CreateImage(cv.GetSize(frame),8,1) foremat = cv.GetMat(foreground) Nforemat = numpy.array(foremat, dtype=numpy.float32) gfilter=sys.argv[2] gfilter_string=gfilter gfilter=float(gfilter) print "Processing Tracker with filter: " + str(gfilter) # Capture the first frame from webcam for image properties display_image = cv.QueryFrame( self.capture ) # Create Background Subtractor fgbg = cv2.BackgroundSubtractorMOG() # Greyscale image, thresholded to create the motion mask: grey_image = cv.CreateImage( cv.GetSize(frame), cv.IPL_DEPTH_8U, 1 ) # The RunningAvg() function requires a 32-bit or 64-bit image... running_average_image = cv.CreateImage( cv.GetSize(frame), cv.IPL_DEPTH_32F, 3 ) # ...but the AbsDiff() function requires matching image depths: running_average_in_display_color_depth = cv.CloneImage( display_image ) # RAM used by FindContours(): mem_storage = cv.CreateMemStorage(0) # The difference between the running average and the current frame: difference = cv.CloneImage( display_image ) target_count = 1 last_target_count = 1 last_target_change_t = 0.0 k_or_guess = 1 codebook=[] frame_count=0 last_frame_entity_list = [] fps = 25 t0 = 165947 # For toggling display: image_list = [ "camera", "shadow", "white", "threshold", "display", "yellow" ] image_index = 0 # Index into image_list # Prep for text drawing: text_font = cv.InitFont(cv.CV_FONT_HERSHEY_COMPLEX, .5, .5, 0.0, 1, cv.CV_AA ) text_coord = ( 5, 15 ) text_color = cv.CV_RGB(255,255,255) text_coord2 = ( 5, 30 ) text_coord3 = ( 5, 45 ) ############################### ### Face detection stuff #haar_cascade = cv.Load( 'haarcascades/haarcascade_frontalface_default.xml' ) #haar_cascade = cv.Load( 'C:/OpenCV2.2/data/haarcascades/haarcascade_frontalface_alt.xml' ) #haar_cascade = cv.Load( 'haarcascades/haarcascade_frontalface_alt2.xml' ) #haar_cascade = cv.Load( 'haarcascades/haarcascade_mcs_mouth.xml' ) #haar_cascade = cv.Load( 'haarcascades/haarcascade_eye.xml' ) #haar_cascade = cv.Load( 'haarcascades/haarcascade_frontalface_alt_tree.xml' ) #haar_cascade = cv.Load( 'haarcascades/haarcascade_upperbody.xml' ) #haar_cascade = cv.Load( 'haarcascades/haarcascade_profileface.xml' ) # Set this to the max number of targets to look for (passed to k-means): max_targets = 20 while True: # Capture frame from webcam camera_image = cv.QueryFrame( self.capture ) #ret, frame = cap.read() frame_count += 1 frame_t0 = time.time() mat = cv.GetMat(camera_image) Nmat = numpy.array(mat, dtype=numpy.uint8) # Create an image with interactive feedback: display_image = cv.CloneImage( camera_image ) # NEW INSERT - FGMASK fgmask = fgbg.apply(Nmat,Nforemat,-1) fgmask = cv.fromarray(fgmask) # Create a working "color image" to modify / blur color_image = cv.CloneImage( display_image ) # Smooth to get rid of false positives cv.Smooth( color_image, color_image, cv.CV_GAUSSIAN, 19, 0 ) #Changed from 19 AND MADE MEDIAN FILTER # Smooth to get rid of false positives # color_image = numpy.asarray( cv.GetMat( color_image ) ) # (mu, sigma) = cv2.meanStdDev(color_image) # edges = cv2.Canny(color_image, mu - sigma, mu + sigma) # lines = cv2.HoughLines(edges, 1, pi / 180, 70) # Use the Running Average as the static background # a = 0.020 leaves artifacts lingering way too long. # a = 0.320 works well at 320x240, 15fps. (1/a is roughly num frames.) cv.RunningAvg( color_image, running_average_image, gfilter, None ) # Convert the scale of the moving average. cv.ConvertScale( running_average_image, running_average_in_display_color_depth, 1.0, 0.0 ) # Subtract the current frame from the moving average. cv.AbsDiff( color_image, running_average_in_display_color_depth, difference ) # Convert the image to greyscale. cv.CvtColor( difference, grey_image, cv.CV_RGB2GRAY ) # Smooth Before thresholding cv.Smooth( grey_image, grey_image, cv.CV_GAUSSIAN, 19, 19 ) # Threshold the image to a black and white motion mask: cv.Threshold( grey_image, grey_image, 2, 255, cv.CV_THRESH_BINARY ) # Smooth and threshold again to eliminate "sparkles" #cv.Smooth( grey_image, grey_image, cv.CV_GAUSSIAN, 19, 0 ) #changed from 19 - AND put smooth before threshold cv.Threshold( grey_image, grey_image, 240, 255, cv.CV_THRESH_BINARY) grey_image_as_array = numpy.asarray( cv.GetMat( grey_image ) ) non_black_coords_array = numpy.where( grey_image_as_array > 3 ) # Convert from numpy.where()'s two separate lists to one list of (x, y) tuples: non_black_coords_array = zip( non_black_coords_array[1], non_black_coords_array[0] ) frame_hsv = cv.CreateImage(cv.GetSize(color_image),8,3) cv.CvtColor(color_image,frame_hsv,cv.CV_BGR2HSV) imgthreshold_yellow=cv.CreateImage(cv.GetSize(color_image),8,1) imgthreshold_white=cv.CreateImage(cv.GetSize(color_image),8,1) imgthreshold_white2=cv.CreateImage(cv.GetSize(color_image),8,1) cv.InRangeS(frame_hsv,cv.Scalar(0,0,196),cv.Scalar(255,255,255),imgthreshold_white) # changed scalar from 255,15,255 to 255,255,255 cv.InRangeS(frame_hsv,cv.Scalar(41,43,224),cv.Scalar(255,255,255),imgthreshold_white2) cv.InRangeS(frame_hsv,cv.Scalar(20,100,100),cv.Scalar(30,255,255),imgthreshold_yellow) #cvCvtColor(color_image, yellowHSV, CV_BGR2HSV) #lower_yellow = np.array([10, 100, 100], dtype=np.uint8) #upper_yellow = np.array([30, 255, 255], dtype=np.uint8) #mask_yellow = cv2.inRange(yellowHSV, lower_yellow, upper_yellow) #res_yellow = cv2.bitwise_and(color_image, color_image, mask_yellow = mask_yellow) points = [] # Was using this to hold either pixel coords or polygon coords. bounding_box_list = [] # Now calculate movements using the white pixels as "motion" data contour = cv.FindContours( grey_image, mem_storage, cv.CV_RETR_CCOMP, cv.CV_CHAIN_APPROX_SIMPLE ) i=0 while contour: # c = contour[i] # m = cv2.moments(c) # Area = m['m00'] # print( Area ) bounding_rect = cv.BoundingRect( list(contour) ) point1 = ( bounding_rect[0], bounding_rect[1] ) point2 = ( bounding_rect[0] + bounding_rect[2], bounding_rect[1] + bounding_rect[3] ) bounding_box_list.append( ( point1, point2 ) ) polygon_points = cv.ApproxPoly( list(contour), mem_storage, cv.CV_POLY_APPROX_DP ) # To track polygon points only (instead of every pixel): #points += list(polygon_points) # Draw the contours: ###cv.DrawContours(color_image, contour, cv.CV_RGB(255,0,0), cv.CV_RGB(0,255,0), levels, 3, 0, (0,0) ) cv.FillPoly( grey_image, [ list(polygon_points), ], cv.CV_RGB(255,255,255), 0, 0 ) cv.PolyLine( display_image, [ polygon_points, ], 0, cv.CV_RGB(255,255,255), 1, 0, 0 ) #cv.Rectangle( display_image, point1, point2, cv.CV_RGB(120,120,120), 1) # if Area > 3000: # cv2.drawContours(imgrgb,[cnt],0,(255,255,255),2) # print(Area) i=i+1 contour = contour.h_next() # Find the average size of the bbox (targets), then # remove any tiny bboxes (which are prolly just noise). # "Tiny" is defined as any box with 1/10th the area of the average box. # This reduces false positives on tiny "sparkles" noise. box_areas = [] for box in bounding_box_list: box_width = box[right][0] - box[left][0] box_height = box[bottom][0] - box[top][0] box_areas.append( box_width * box_height ) #cv.Rectangle( display_image, box[0], box[1], cv.CV_RGB(255,0,0), 1) average_box_area = 0.0 if len(box_areas): average_box_area = float( sum(box_areas) ) / len(box_areas) trimmed_box_list = [] for box in bounding_box_list: box_width = box[right][0] - box[left][0] box_height = box[bottom][0] - box[top][0] # Only keep the box if it's not a tiny noise box: if (box_width * box_height) > average_box_area*0.1: trimmed_box_list.append( box ) # Draw the trimmed box list: #for box in trimmed_box_list: # cv.Rectangle( display_image, box[0], box[1], cv.CV_RGB(0,255,0), 2 ) bounding_box_list = merge_collided_bboxes( trimmed_box_list ) # Draw the merged box list: for box in bounding_box_list: cv.Rectangle( display_image, box[0], box[1], cv.CV_RGB(0,255,0), 1 ) # Here are our estimate points to track, based on merged & trimmed boxes: estimated_target_count = len( bounding_box_list ) # Don't allow target "jumps" from few to many or many to few. # Only change the number of targets up to one target per n seconds. # This fixes the "exploding number of targets" when something stops moving # and the motion erodes to disparate little puddles all over the place. if frame_t0 - last_target_change_t < .35: # 1 change per 0.35 secs estimated_target_count = last_target_count else: if last_target_count - estimated_target_count > 1: estimated_target_count = last_target_count - 1 if estimated_target_count - last_target_count > 1: estimated_target_count = last_target_count + 1 last_target_change_t = frame_t0 # Clip to the user-supplied maximum: estimated_target_count = min( estimated_target_count, max_targets ) # The estimated_target_count at this point is the maximum number of targets # we want to look for. If kmeans decides that one of our candidate # bboxes is not actually a target, we remove it from the target list below. # Using the numpy values directly (treating all pixels as points): points = non_black_coords_array center_points = [] if len(points): # If we have all the "target_count" targets from last frame, # use the previously known targets (for greater accuracy). k_or_guess = max( estimated_target_count, 1 ) # Need at least one target to look for. if len(codebook) == estimated_target_count: k_or_guess = codebook #points = vq.whiten(array( points )) # Don't do this! Ruins everything. codebook, distortion = vq.kmeans( array( points ), k_or_guess ) # Convert to tuples (and draw it to screen) for center_point in codebook: center_point = ( int(center_point[0]), int(center_point[1]) ) center_points.append( center_point ) #cv.Circle(display_image, center_point, 10, cv.CV_RGB(255, 0, 0), 2) #cv.Circle(display_image, center_point, 5, cv.CV_RGB(255, 0, 0), 3) # Now we have targets that are NOT computed from bboxes -- just # movement weights (according to kmeans). If any two targets are # within the same "bbox count", average them into a single target. # # (Any kmeans targets not within a bbox are also kept.) trimmed_center_points = [] removed_center_points = [] for box in bounding_box_list: # Find the centers within this box: center_points_in_box = [] for center_point in center_points: if center_point[0] < box[right][0] and center_point[0] > box[left][0] and \ center_point[1] < box[bottom][1] and center_point[1] > box[top][1] : # This point is within the box. center_points_in_box.append( center_point ) # Now see if there are more than one. If so, merge them. if len( center_points_in_box ) > 1: # Merge them: x_list = y_list = [] for point in center_points_in_box: x_list.append(point[0]) y_list.append(point[1]) average_x = int( float(sum( x_list )) / len( x_list ) ) average_y = int( float(sum( y_list )) / len( y_list ) ) trimmed_center_points.append( (average_x, average_y) ) # Record that they were removed: removed_center_points += center_points_in_box if len( center_points_in_box ) == 1: trimmed_center_points.append( center_points_in_box[0] ) # Just use it. # If there are any center_points not within a bbox, just use them. # (It's probably a cluster comprised of a bunch of small bboxes.) for center_point in center_points: if (not center_point in trimmed_center_points) and (not center_point in removed_center_points): trimmed_center_points.append( center_point ) # Draw what we found: #for center_point in trimmed_center_points: # center_point = ( int(center_point[0]), int(center_point[1]) ) # cv.Circle(display_image, center_point, 20, cv.CV_RGB(255, 255,255), 1) # cv.Circle(display_image, center_point, 15, cv.CV_RGB(100, 255, 255), 1) # cv.Circle(display_image, center_point, 10, cv.CV_RGB(255, 255, 255), 2) # cv.Circle(display_image, center_point, 5, cv.CV_RGB(100, 255, 255), 3) # Determine if there are any new (or lost) targets: actual_target_count = len( trimmed_center_points ) last_target_count = actual_target_count # Now build the list of physical entities (objects) this_frame_entity_list = [] # An entity is list: [ name, color, last_time_seen, last_known_coords ] for target in trimmed_center_points: # Is this a target near a prior entity (same physical entity)? entity_found = False entity_distance_dict = {} for entity in last_frame_entity_list: entity_coords= entity[3] delta_x = entity_coords[0] - target[0] delta_y = entity_coords[1] - target[1] distance = sqrt( pow(delta_x,2) + pow( delta_y,2) ) entity_distance_dict[ distance ] = entity # Did we find any non-claimed entities (nearest to furthest): distance_list = entity_distance_dict.keys() distance_list.sort() for distance in distance_list: # Yes; see if we can claim the nearest one: nearest_possible_entity = entity_distance_dict[ distance ] # Don't consider entities that are already claimed: if nearest_possible_entity in this_frame_entity_list: #print "Target %s: Skipping the one iwth distance: %d at %s, C:%s" % (target, distance, nearest_possible_entity[3], nearest_possible_entity[1] ) #Commented Out 3/20/2016 continue #print "Target %s pixel(b,g,r) : USING the one iwth distance: %d at %s, C:%s" % (target, distance, nearest_possible_entity[3] , nearest_possible_entity[1]) # Commented Out 3/20/2016 # Found the nearest entity to claim: entity_found = True nearest_possible_entity[2] = frame_t0 # Update last_time_seen nearest_possible_entity[3] = target # Update the new location this_frame_entity_list.append( nearest_possible_entity ) #log_file.write( "%.3f MOVED %s %d %d\n" % ( frame_count, nearest_possible_entity[0], nearest_possible_entity[3][0], nearest_possible_entity[3][1] ) ) break if entity_found == False: # It's a new entity. color = ( random.randint(0,255), random.randint(0,255), random.randint(0,255) ) name = hashlib.md5( str(frame_t0) + str(color) ).hexdigest()[:6] last_time_seen = frame_t0 if imgthreshold_white[target[1],target[0]] == 0.0: # It's a real detect (not a line marker) new_entity = [ name, color, last_time_seen, target ] this_frame_entity_list.append( new_entity ) log_file.write( "%.3f %.3f FOUND %s %d %d\n" % ( frame_count/fps, frame_count, new_entity[0], new_entity[3][0], new_entity[3][1] ) ) filedrive = "C:/Users/525494/New_folder/000216/run_096/" filename = "img"+str(name) #print "gfilter is: %.2f" + gfilter cv.SaveImage("image-test%s-%3f.png"%(new_entity[0],gfilter), display_image) elif imgthreshold_white[target[1],target[0]] == 255.0: # It's a white line detect new_entity = [ name, color, last_time_seen, target ] this_frame_entity_list.append( new_entity ) log_file.write( "%.3f %.3f FOUND %s %d %d\n" % ( frame_count/fps, frame_count, new_entity[0], new_entity[3][0], new_entity[3][1] ) ) filedrive = "C:/Users/525494/New_folder/000216/run_096/" filename = "img"+str(name) #print "gfilter is: %.2f" + gfilter cv.SaveImage("white-line-image-test%s-%3f.png"%(new_entity[0],gfilter), display_image) elif imgthreshold_yellow[target[1],target[0]] == 255.0: # It's a yellow line detect new_entity = [ name, color, last_time_seen, target ] this_frame_entity_list.append( new_entity ) log_file.write( "%.3f %.3f FOUND %s %d %d\n" % ( frame_count/fps, frame_count, new_entity[0], new_entity[3][0], new_entity[3][1] ) ) filedrive = "C:/Users/525494/New_folder/000216/run_096/" filename = "img"+str(name) cv.SaveImage("yellow-line-image-test%s.png"%(new_entity[0]), camera_image) # Now "delete" any not-found entities which have expired: entity_ttl = 1.0 # 1 sec. for entity in last_frame_entity_list: last_time_seen = entity[2] if frame_t0 - last_time_seen > entity_ttl: # It's gone. #log_file.write( "%.3f STOPD %s %d %d\n" % ( frame_count, entity[0], entity[3][0], entity[3][1] ) ) pass else: # Save it for next time... not expired yet: this_frame_entity_list.append( entity ) # For next frame: last_frame_entity_list = this_frame_entity_list # Draw the found entities to screen: for entity in this_frame_entity_list: center_point = entity[3] c = entity[1] # RGB color tuple cv.Circle(display_image, center_point, 20, cv.CV_RGB(c[0], c[1], c[2]), 1) cv.Circle(display_image, center_point, 15, cv.CV_RGB(c[0], c[1], c[2]), 1) cv.Circle(display_image, center_point, 10, cv.CV_RGB(c[0], c[1], c[2]), 2) cv.Circle(display_image, center_point, 5, cv.CV_RGB(c[0], c[1], c[2]), 3) #print "min_size is: " + str(min_size) # Listen for ESC or ENTER key c = cv.WaitKey(7) % 0x100 if c == 27 or c == 10: break # Toggle which image to show if chr(c) == 'd': image_index = ( image_index + 1 ) % len( image_list ) image_name = image_list[ image_index ] # Display frame to user if image_name == "camera": image = camera_image cv.PutText( image, "Camera (Normal)", text_coord, text_font, text_color ) elif image_name == "shadow": image = fgmask cv.PutText( image, "No Shadow", text_coord, text_font, text_color ) elif image_name == "white": #image = difference image = imgthreshold_white cv.PutText( image, "White Threshold", text_coord, text_font, text_color ) elif image_name == "display": #image = display_image image = display_image cv.PutText( image, "Targets (w/AABBs and contours)", text_coord, text_font, text_color ) cv.PutText( image, str(frame_t0), text_coord2, text_font, text_color ) cv.PutText( image, str(frame_count), text_coord3, text_font, text_color ) elif image_name == "threshold": # Convert the image to color. cv.CvtColor( grey_image, display_image, cv.CV_GRAY2RGB ) image = display_image # Re-use display image here cv.PutText( image, "Motion Mask", text_coord, text_font, text_color ) elif image_name == "yellow": # Do face detection #detect_faces( camera_image, haar_cascade, mem_storage ) image = imgthreshold_yellow # Re-use camera image here cv.PutText( image, "Yellow Threshold", text_coord, text_font, text_color ) #cv.ShowImage( "Target", image ) Commented out 3/19 # self.writer.write( image ) # out.write( image ); # cv.WriteFrame( self.writer, image ); # if self.writer: # cv.WriteFrame( self.writer, image ); # video.write( image ); log_file.flush() # If only using a camera, then there is no time.sleep() needed, # because the camera clips us to 15 fps. But if reading from a file, # we need this to keep the time-based target clipping correct: frame_t1 = time.time() # If reading from a file, put in a forced delay: # if not self.writer: # delta_t = frame_t1 - frame_t0 # if delta_t < ( 1.0 / 15.0 ): time.sleep( ( 1.0 / 15.0 ) - delta_t ): if frame_count == 155740: cv2.destroyWindow("Target") # cv.ReleaseVideoWriter() # self.writer.release() # log_file.flush() break t1 = time.time() time_delta = t1 - t0 processed_fps = float( frame_count ) / time_delta print "Got %d frames. %.1f s. %f fps." % ( frame_count, time_delta, processed_fps )
storage = cv.CreateMemStorage(0) contour = cv.FindContours(threshold_img, storage, cv.CV_RETR_CCOMP, cv.CV_CHAIN_APPROX_SIMPLE) points = [] while contour: rect = cv.BoundingRect(list(contour)) contour = contour.h_next() size = (rect[2] * rect[3]) mid = (rect[0]+rect[2]/2) diag = (rect[2]**2 + rect[3]**2)**0.5 if (size > 100): pt1 = (rect[0], rect[1]) #left top pt2 = (rect[0]+rect[2], rect[1]+rect[3]) #right bottom cv.Rectangle(img, pt1, pt2, (255,0,0),3) #print(str(pt1)+str(pt2)+str(size)+' '+str(diag)) #print(str(diag)) #if (rect[0]+rect[2]/2) > 80: sleep(0.01) if mid > 100:#turn right motorright() print('right') elif mid < 60:#turn left motorleft() print('left') elif diag > 100:#back motorbackword() print('backword') elif diag < 80:#for
import time capture = cv.CaptureFromCAM(0) cv.SetCaptureProperty(capture, 3, 720) cv.SetCaptureProperty(capture, 4, 480) while True: img = cv.QueryFrame(capture) cv.Smooth(img, img, cv.CV_BLUR, 3) hue_img = cv.CreateImage(cv.GetSize(img), 8, 3) cv.CvtColor(img, hue_img, cv.CV_BGR2HSV) threshold_img = cv.CreateImage(cv.GetSize(hue_img), 8, 1) cv.InRangeS(hue_img, (38, 120, 60), (75, 255, 255), threshold_img) storage = cv.CreateMemStorage(0) contour = cv.FindContours(threshold_img,storage,cv.CV_RETR_CCOMP,\ cv.CV_CHAIN_APPROX_SIMPLE) points = [] while contour: rect = cv.BoundingRect(list(contour)) contour = contour.h_next() size = (rect[2] * rect[3]) if size > 100: pt1 = (rect[0], rect[1]) pt2 = (rect[0] + rect[2], rect[1] + rect[3]) cv.Rectangle(img, pt1, pt2, (0, 255, 0)) cv.ShowImage("Colour Tracking", img) if cv.WaitKey(10) == 27: break
GPIO.output(24, True) if rect[0]>213.33 and rect[0]<426.67 and rect[1]>240.0 and rect[1]<480.0: print ('DIAM') GPIO.output(14, False) GPIO.output(15, False) GPIO.output(23, False) GPIO.output(24, False) if rect[0]>0.0 and rect[0]<213.33 and rect[1]>0.0 and rect[1]<240.0: print ('BELOK KIRI SEDANG') GPIO.output(14, False) GPIO.output(15, False) GPIO.output(23, False) GPIO.output(24, True) if rect[0]>0.0 and rect[0]<213.33 and rect[1]>240.0 and rect[1]<480.0: print ('BELOK KIRI CURAM') GPIO.output(14, True) GPIO.output(15, False) GPIO.output(23, False) GPIO.output(24, True) contour = contour.h_next() size = (rect[2]*rect[3]) if size > 100: pt1 = (rect[0],rect[1]) pt2 = (rect[0]+rect[2],rect[1]+rect[3]) cv.Rectangle(img,pt1,pt2,(38,160,60)) cv.ShowImage("Colour Tracking",img) if cv.WaitKey(10)==27: break
def run(self): # Initialize # log_file_name = "tracker_output.log" # log_file = file( log_file_name, 'a' ) print "hello" frame = cv.QueryFrame(self.capture) frame_size = cv.GetSize(frame) # Capture the first frame from webcam for image properties display_image = cv.QueryFrame(self.capture) # Greyscale image, thresholded to create the motion mask: grey_image = cv.CreateImage(cv.GetSize(frame), cv.IPL_DEPTH_8U, 1) # The RunningAvg() function requires a 32-bit or 64-bit image... running_average_image = cv.CreateImage(cv.GetSize(frame), cv.IPL_DEPTH_32F, 3) # ...but the AbsDiff() function requires matching image depths: running_average_in_display_color_depth = cv.CloneImage(display_image) # RAM used by FindContours(): mem_storage = cv.CreateMemStorage(0) # The difference between the running average and the current frame: difference = cv.CloneImage(display_image) target_count = 1 last_target_count = 1 last_target_change_t = 0.0 k_or_guess = 1 codebook = [] frame_count = 0 last_frame_entity_list = [] t0 = time.time() # For toggling display: image_list = [ "camera", "difference", "threshold", "display", "faces" ] image_index = 3 # Index into image_list # Prep for text drawing: text_font = cv.InitFont(cv.CV_FONT_HERSHEY_COMPLEX, .5, .5, 0.0, 1, cv.CV_AA) text_coord = (5, 15) text_color = cv.CV_RGB(255, 255, 255) ############################### # ## Face detection stuff # haar_cascade = cv.Load( 'haarcascades/haarcascade_frontalface_default.xml' ) haar_cascade = cv.Load('E:\\Softwares\\opencv\\data\\haarcascades\\haarcascade_frontalface_alt.xml') # haar_cascade = cv.Load( 'haarcascades/haarcascade_frontalface_alt2.xml' ) # haar_cascade = cv.Load( 'haarcascades/haarcascade_mcs_mouth.xml' ) # haar_cascade = cv.Load( 'haarcascades/haarcascade_eye.xml' ) # haar_cascade = cv.Load( 'haarcascades/haarcascade_frontalface_alt_tree.xml' ) # haar_cascade = cv.Load( 'haarcascades/haarcascade_upperbody.xml' ) # haar_cascade = cv.Load( 'haarcascades/haarcascade_profileface.xml' ) # Set this to the max number of targets to look for (passed to k-means): max_targets = 5 while True: # Capture frame from webcam camera_image = cv.QueryFrame(self.capture) frame_count += 1 frame_t0 = time.time() # Create an image with interactive feedback: display_image = cv.CloneImage(camera_image) # Create a working "color image" to modify / blur color_image = cv.CloneImage(display_image) # Smooth to get rid of false positives cv.Smooth(color_image, color_image, cv.CV_GAUSSIAN, 19, 0) # Use the Running Average as the static background # a = 0.020 leaves artifacts lingering way too long. # a = 0.320 works well at 320x240, 15fps. (1/a is roughly num frames.) cv.RunningAvg(color_image, running_average_image, 0.320, None) # Convert the scale of the moving average. cv.ConvertScale(running_average_image, running_average_in_display_color_depth, 1.0, 0.0) # Subtract the current frame from the moving average. cv.AbsDiff(color_image, running_average_in_display_color_depth, difference) # Convert the image to greyscale. cv.CvtColor(difference, grey_image, cv.CV_RGB2GRAY) # Threshold the image to a black and white motion mask: cv.Threshold(grey_image, grey_image, 2, 255, cv.CV_THRESH_BINARY) # Smooth and threshold again to eliminate "sparkles" cv.Smooth(grey_image, grey_image, cv.CV_GAUSSIAN, 19, 0) cv.Threshold(grey_image, grey_image, 240, 255, cv.CV_THRESH_BINARY) grey_image_as_array = numpy.asarray(cv.GetMat(grey_image)) non_black_coords_array = numpy.where(grey_image_as_array > 3) # Convert from numpy.where()'s two separate lists to one list of (x, y) tuples: non_black_coords_array = zip(non_black_coords_array[1], non_black_coords_array[0]) points = [] # Was using this to hold either pixel coords or polygon coords. bounding_box_list = [] # Now calculate movements using the white pixels as "motion" data contour = cv.FindContours(grey_image, mem_storage, cv.CV_RETR_CCOMP, cv.CV_CHAIN_APPROX_SIMPLE) while contour: bounding_rect = cv.BoundingRect(list(contour)) point1 = (bounding_rect[0], bounding_rect[1]) point2 = (bounding_rect[0] + bounding_rect[2], bounding_rect[1] + bounding_rect[3]) bounding_box_list.append((point1, point2)) polygon_points = cv.ApproxPoly(list(contour), mem_storage, cv.CV_POLY_APPROX_DP) # To track polygon points only (instead of every pixel): # points += list(polygon_points) # Draw the contours: # ##cv.DrawContours(color_image, contour, cv.CV_RGB(255,0,0), cv.CV_RGB(0,255,0), levels, 3, 0, (0,0) ) cv.FillPoly(grey_image, [ list(polygon_points), ], cv.CV_RGB(255, 255, 255), 0, 0) cv.PolyLine(display_image, [ polygon_points, ], 0, cv.CV_RGB(255, 255, 255), 1, 0, 0) # cv.Rectangle( display_image, point1, point2, cv.CV_RGB(120,120,120), 1) contour = contour.h_next() # Find the average size of the bbox (targets), then # remove any tiny bboxes (which are prolly just noise). # "Tiny" is defined as any box with 1/10th the area of the average box. # This reduces false positives on tiny "sparkles" noise. box_areas = [] for box in bounding_box_list: box_width = box[right][0] - box[left][0] box_height = box[bottom][0] - box[top][0] box_areas.append(box_width * box_height) # cv.Rectangle( display_image, box[0], box[1], cv.CV_RGB(255,0,0), 1) average_box_area = 0.0 if len(box_areas): average_box_area = float(sum(box_areas)) / len(box_areas) trimmed_box_list = [] for box in bounding_box_list: box_width = box[right][0] - box[left][0] box_height = box[bottom][0] - box[top][0] # Only keep the box if it's not a tiny noise box: if (box_width * box_height) > average_box_area * 0.1: trimmed_box_list.append(box) # Draw the trimmed box list: # for box in trimmed_box_list: # cv.Rectangle( display_image, box[0], box[1], cv.CV_RGB(0,255,0), 2 ) bounding_box_list = merge_collided_bboxes(trimmed_box_list) # Draw the merged box list: for box in bounding_box_list: cv.Rectangle(display_image, box[0], box[1], cv.CV_RGB(0, 255, 0), 1) # Here are our estimate points to track, based on merged & trimmed boxes: estimated_target_count = len(bounding_box_list) # Don't allow target "jumps" from few to many or many to few. # Only change the number of targets up to one target per n seconds. # This fixes the "exploding number of targets" when something stops moving # and the motion erodes to disparate little puddles all over the place. if frame_t0 - last_target_change_t < .350: # 1 change per 0.35 secs estimated_target_count = last_target_count else: if last_target_count - estimated_target_count > 1: estimated_target_count = last_target_count - 1 if estimated_target_count - last_target_count > 1: estimated_target_count = last_target_count + 1 last_target_change_t = frame_t0 # Clip to the user-supplied maximum: estimated_target_count = min(estimated_target_count, max_targets) # The estimated_target_count at this point is the maximum number of targets # we want to look for. If kmeans decides that one of our candidate # bboxes is not actually a target, we remove it from the target list below. # Using the numpy values directly (treating all pixels as points): points = non_black_coords_array center_points = [] if len(points): # If we have all the "target_count" targets from last frame, # use the previously known targets (for greater accuracy). k_or_guess = max(estimated_target_count, 1) # Need at least one target to look for. if len(codebook) == estimated_target_count: k_or_guess = codebook # points = vq.whiten(array( points )) # Don't do this! Ruins everything. codebook, distortion = vq.kmeans(array(points), k_or_guess) # Convert to tuples (and draw it to screen) for center_point in codebook: center_point = (int(center_point[0]), int(center_point[1])) center_points.append(center_point) # cv.Circle(display_image, center_point, 10, cv.CV_RGB(255, 0, 0), 2) # cv.Circle(display_image, center_point, 5, cv.CV_RGB(255, 0, 0), 3) # Now we have targets that are NOT computed from bboxes -- just # movement weights (according to kmeans). If any two targets are # within the same "bbox count", average them into a single target. # # (Any kmeans targets not within a bbox are also kept.) trimmed_center_points = [] removed_center_points = [] for box in bounding_box_list: # Find the centers within this box: center_points_in_box = [] for center_point in center_points: if center_point[0] < box[right][0] and center_point[0] > box[left][0] and \ center_point[1] < box[bottom][1] and center_point[1] > box[top][1] : # This point is within the box. center_points_in_box.append(center_point) # Now see if there are more than one. If so, merge them. if len(center_points_in_box) > 1: # Merge them: x_list = y_list = [] for point in center_points_in_box: x_list.append(point[0]) y_list.append(point[1]) average_x = int(float(sum(x_list)) / len(x_list)) average_y = int(float(sum(y_list)) / len(y_list)) trimmed_center_points.append((average_x, average_y)) # Record that they were removed: removed_center_points += center_points_in_box if len(center_points_in_box) == 1: trimmed_center_points.append(center_points_in_box[0]) # Just use it. # If there are any center_points not within a bbox, just use them. # (It's probably a cluster comprised of a bunch of small bboxes.) for center_point in center_points: if (not center_point in trimmed_center_points) and (not center_point in removed_center_points): trimmed_center_points.append(center_point) # Draw what we found: # for center_point in trimmed_center_points: # center_point = ( int(center_point[0]), int(center_point[1]) ) # cv.Circle(display_image, center_point, 20, cv.CV_RGB(255, 255,255), 1) # cv.Circle(display_image, center_point, 15, cv.CV_RGB(100, 255, 255), 1) # cv.Circle(display_image, center_point, 10, cv.CV_RGB(255, 255, 255), 2) # cv.Circle(display_image, center_point, 5, cv.CV_RGB(100, 255, 255), 3) # Determine if there are any new (or lost) targets: actual_target_count = len(trimmed_center_points) last_target_count = actual_target_count # Now build the list of physical entities (objects) this_frame_entity_list = [] # An entity is list: [ name, color, last_time_seen, last_known_coords ] for target in trimmed_center_points: # Is this a target near a prior entity (same physical entity)? entity_found = False entity_distance_dict = {} for entity in last_frame_entity_list: entity_coords = entity[3] delta_x = entity_coords[0] - target[0] delta_y = entity_coords[1] - target[1] distance = sqrt(pow(delta_x, 2) + pow(delta_y, 2)) entity_distance_dict[ distance ] = entity # Did we find any non-claimed entities (nearest to furthest): distance_list = entity_distance_dict.keys() distance_list.sort() for distance in distance_list: # Yes; see if we can claim the nearest one: nearest_possible_entity = entity_distance_dict[ distance ] # Don't consider entities that are already claimed: if nearest_possible_entity in this_frame_entity_list: # print "Target %s: Skipping the one iwth distance: %d at %s, C:%s" % (target, distance, nearest_possible_entity[3], nearest_possible_entity[1] ) continue # print "Target %s: USING the one iwth distance: %d at %s, C:%s" % (target, distance, nearest_possible_entity[3] , nearest_possible_entity[1]) # Found the nearest entity to claim: entity_found = True nearest_possible_entity[2] = frame_t0 # Update last_time_seen nearest_possible_entity[3] = target # Update the new location this_frame_entity_list.append(nearest_possible_entity) # log_file.write( "%.3f MOVED %s %d %d\n" % ( frame_t0, nearest_possible_entity[0], nearest_possible_entity[3][0], nearest_possible_entity[3][1] ) ) break if entity_found == False: # It's a new entity. color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) name = hashlib.md5(str(frame_t0) + str(color)).hexdigest()[:6] last_time_seen = frame_t0 new_entity = [ name, color, last_time_seen, target ] this_frame_entity_list.append(new_entity) # log_file.write( "%.3f FOUND %s %d %d\n" % ( frame_t0, new_entity[0], new_entity[3][0], new_entity[3][1] ) ) # Now "delete" any not-found entities which have expired: entity_ttl = 1.0 # 1 sec. for entity in last_frame_entity_list: last_time_seen = entity[2] if frame_t0 - last_time_seen > entity_ttl: # It's gone. # log_file.write( "%.3f STOPD %s %d %d\n" % ( frame_t0, entity[0], entity[3][0], entity[3][1] ) ) pass else: # Save it for next time... not expired yet: this_frame_entity_list.append(entity) # For next frame: last_frame_entity_list = this_frame_entity_list # Draw the found entities to screen: for entity in this_frame_entity_list: center_point = entity[3] c = entity[1] # RGB color tuple cv.Circle(display_image, center_point, 20, cv.CV_RGB(c[0], c[1], c[2]), 1) cv.Circle(display_image, center_point, 15, cv.CV_RGB(c[0], c[1], c[2]), 1) cv.Circle(display_image, center_point, 10, cv.CV_RGB(c[0], c[1], c[2]), 2) cv.Circle(display_image, center_point, 5, cv.CV_RGB(c[0], c[1], c[2]), 3) # print "min_size is: " + str(min_size) # Listen for ESC or ENTER key c = cv.WaitKey(7) % 0x100 if c == 27 or c == 10: break # Toggle which image to show # if chr(c) == 'd': # image_index = ( image_index + 1 ) % len( image_list ) # # image_name = image_list[ image_index ] # # # Display frame to user # if image_name == "camera": # image = camera_image # cv.PutText( image, "Camera (Normal)", text_coord, text_font, text_color ) # elif image_name == "difference": # image = difference # cv.PutText( image, "Difference Image", text_coord, text_font, text_color ) # elif image_name == "display": # image = display_image # cv.PutText( image, "Targets (w/AABBs and contours)", text_coord, text_font, text_color ) # elif image_name == "threshold": # # Convert the image to color. # cv.CvtColor( grey_image, display_image, cv.CV_GRAY2RGB ) # image = display_image # Re-use display image here # cv.PutText( image, "Motion Mask", text_coord, text_font, text_color ) # elif image_name == "faces": # # Do face detection # detect_faces( camera_image, haar_cascade, mem_storage ) # image = camera_image # Re-use camera image here # cv.PutText( image, "Face Detection", text_coord, text_font, text_color ) # cv.ShowImage( "Target", image ) image1 = display_image detect_faces(camera_image, haar_cascade, mem_storage) image2 = camera_image cv.ShowImage("Target 1", image1) cv.ShowImage("Target 2", image2) # if self.writer: # cv.WriteFrame( self.writer, image ); # log_file.flush() # If only using a camera, then there is no time.sleep() needed, # because the camera clips us to 15 fps. But if reading from a file, # we need this to keep the time-based target clipping correct: frame_t1 = time.time() # If reading from a file, put in a forced delay: if not self.writer: delta_t = frame_t1 - frame_t0 if delta_t < (1.0 / 15.0): time.sleep((1.0 / 15.0) - delta_t) [rows, cols] = cv.GetSize(frame) image = cv.CreateImage(cv.GetSize(frame), cv.IPL_DEPTH_8U, frame.nChannels) cv.Copy(frame, image) cv.ShowImage("camera", frame) leftImage = cv.CreateImage((image.width, image.height), 8, 1) cv.CvtColor(image, leftImage, cv.CV_BGR2GRAY) frame2 = cv.QueryFrame(self.capture2) print type(frame2) image2 = cv.CreateImage(cv.GetSize(frame2), cv.IPL_DEPTH_8U, frame2.nChannels) cv.Copy(frame2, image2) cv.ShowImage("camera2", frame2) rightImage = cv.CreateImage((image2.width, image2.height), 8, 1) cv.CvtColor(image2, rightImage, cv.CV_BGR2GRAY) disparity_left = cv.CreateMat(leftImage.height, leftImage.width, cv.CV_16S) disparity_right = cv.CreateMat(rightImage.height, rightImage.width, cv.CV_16S) # data structure initialization state = cv.CreateStereoGCState(16, 2) # print leftImage.width # print leftImage.height # print rightImage.width # print rightImage.height # running the graph-cut algorithm cv.FindStereoCorrespondenceGC(leftImage, rightImage, disparity_left, disparity_right, state) disp_left_visual = cv.CreateMat(leftImage.height, leftImage.width, cv.CV_8U) cv.ConvertScale(disparity_left, disp_left_visual, -16); # cv.Save("disparity.pgm", disp_left_visual); # save the map # # cutting the object farthest of a threshold (120) cut(disp_left_visual, leftImage, 120) # cv.NamedWindow('Disparity map', cv.CV_WINDOW_AUTOSIZE) cv.ShowImage('Disparity map', disp_left_visual) # # minimum value for the intensity maxValue = 0 maxPoint = None # now for all the moving object centers get the average intensity value for entity in this_frame_entity_list: center_point = entity[3] c = entity[1] # RGB color tuple # cv.Circle(display_image, center_point, 20, cv.CV_RGB(c[0], c[1], c[2]), 1) # cv.Circle(display_image, center_point, 15, cv.CV_RGB(c[0], c[1], c[2]), 1) # cv.Circle(display_image, center_point, 10, cv.CV_RGB(c[0], c[1], c[2]), 2) # cv.Circle(display_image, center_point, 5, cv.CV_RGB(c[0], c[1], c[2]), 3) # cv.Avg(arr) print center_point print type(disp_left_visual) print size(disp_left_visual) print center_point print "value " + str(cv.Get2D(disp_left_visual, center_point[1], center_point[0])) value = cv.Get2D(disp_left_visual, center_point[1], center_point[0])[0] + cv.Get2D(disp_left_visual, center_point[1], center_point[0])[1] + cv.Get2D(disp_left_visual, center_point[1], center_point[0])[2] if value > maxValue: maxValue = value maxPoint = center_point print "min value is " + str(maxValue) print " min point is " + str(maxPoint) if maxPoint != None: cv.Circle(display_image, maxPoint, 17, cv.CV_RGB(100, 100, 100), 1) #distance = 1 / maxValue # find if it is right or left or in the middle if maxValue > 100: middle = width / 2 if maxPoint[0] > middle + 20: message = "look left" elif maxPoint[0] < middle - 20: message = "look right" else: message = "look middle" print "message " + message t1 = time.time() time_delta = t1 - t0 processed_fps = float(frame_count) / time_delta print "Got %d frames. %.1f s. %f fps." % (frame_count, time_delta, processed_fps)
import cv2.cv as cv capture = cv.CaptureFromCAM(0) hc = cv.Load("../haarcascades/haarcascade_frontalface_alt.xml") while True: frame = cv.QueryFrame(capture) faces = cv.HaarDetectObjects(frame, hc, cv.CreateMemStorage(), 1.2, 2, cv.CV_HAAR_DO_CANNY_PRUNING, (0, 0)) for ((x, y, w, h), stub) in faces: cv.Rectangle(frame, (int(x), int(y)), (int(x) + w, int(y) + h), (0, 255, 0), 2, 0) cv.ShowImage("Window", frame) c = cv.WaitKey(1) if c == 27 or c == 1048603: #If Esc entered break
def detect_and_draw(img, cascade): # allocate temporary images gray = cv.CreateImage((img.width,img.height), 8, 1) small_img = cv.CreateImage((cv.Round(img.width / image_scale), cv.Round (img.height / image_scale)), 8, 1) # convert color input image to grayscale cv.CvtColor(img, gray, cv.CV_BGR2GRAY) # scale input image for faster processing cv.Resize(gray, small_img, cv.CV_INTER_LINEAR) cv.EqualizeHist(small_img, small_img) ####### 若解析度有改變,下面劃線座標亦隨之改變############## ''' cv.Line(img, (210,0),(210,480), (0,255,255),1) cv.Line(img, (420,0),(420,480), (0,255,255),1) cv.Line(img, (0,160),(640,160), (0,255,255),1) cv.Line(img, (0,320),(640,320), (0,255,255),1) ''' cv.Line(img, (width/2,0),(width/2,height), (0,10,255),3) cv.Line(img, ((width/2-20),(height/2-10)),((width/2-20),(height/2+10)), (0,10,255),2) cv.Line(img, ((width/2+20),(height/2-10)),((width/2+20),(height/2+10)), (0,10,255),2) cv.Line(img, (0,height/2),(width,height/2), (0,10,255),3) cv.Line(img, ((width/2-10),(height/2-20)),((width/2+10),(height/2-20)), (0,10,255),2) cv.Line(img, ((width/2-10),(height/2+20)),((width/2+10),(height/2+20)), (0,10,255),2) if(cascade): t = cv.GetTickCount() faces = cv.HaarDetectObjects(small_img, cascade, cv.CreateMemStorage(0), haar_scale, min_neighbors, haar_flags, min_size) t = cv.GetTickCount() - t print "detection time = %gms" % (t/(cv.GetTickFrequency()*1000.)) if faces: for ((x, y, w, h), n) in faces: # the input to cv.HaarDetectObjects was resized, so scale the # bounding box of each face and convert it to two CvPoints pt1 = (int(x * image_scale), int(y * image_scale)) pt2 = (int((x + w) * image_scale), int((y + h) * image_scale)) cv.Rectangle(img, pt1, pt2, cv.RGB(255, 0, 0), 3, 8, 0) ##################################################################################################3333 cx = (int(x * image_scale) + int((x + w) * image_scale)) / 2 cy = (int(y * image_scale) + int((y + h) * image_scale)) / 2 print cx, cy #################################################### if cx < img.width*3/ 7 : arduino.write('4') print '4' if cx < img.width*2/ 7 : arduino.write('44') print '4' if cx < img.width/ 7 : arduino.write('4444') print '44' if cx > img.width*4 / 7 : arduino.write('6') print '6' if cx > img.width*5/ 7 : arduino.write('66') print '6' if cx > img.width*6/ 7 : arduino.write('6666') print '66' if cy < img.height*3/ 7: arduino.write('2') print '2' if cy < img.height*2/ 7: arduino.write('22') print '2' if cy < img.height/ 7: arduino.write('2222') print '222' if cy > img.height*4 / 7: arduino.write('8') print '8' if cy > img.height*5 / 7: arduino.write('88') print '8' if cy > img.height*6 / 7: arduino.write('8888') print '888' break ###################################################### cv.ShowImage("result", img)
def match_template(self, search_image, detect_box): if detect_box: try: [center, size, angle] = detect_box pt1 = [ int(center[0] - size[0] / 2), int(center[1] - size[1] / 2) ] pt2 = [ int(center[0] + size[0] / 2), int(center[1] + size[1] / 2) ] w = pt2[0] - pt1[0] h = pt2[1] - pt1[1] except: [x, y, w, h] = detect_box pt1 = [x, y] pt2 = [x + w, y + h] self.search_factor = 4.0 w_search = int(w * self.search_factor) h_search = int(h * self.search_factor) pt1[0] = min(self.frame_size[0], max(0, pt1[0] - int((w_search - w) / 2))) pt1[1] = min(self.frame_size[1], max(0, pt1[1] - int((h_search - h) / 2))) pt2[0] = min(self.frame_size[0], pt1[0] + w_search) pt2[1] = min(self.frame_size[1], pt1[1] + h_search) try: cv.Rectangle(self.marker_image, [pt1], [pt2], cv.RGB(0, 0, 255)) search_image = search_image[pt1[1]:pt2[1], pt1[0]:pt2[0]] except: return None frame = np.array(search_image, dtype=np.uint8) # cv.NamedWindow("Search Image", cv.CV_WINDOW_AUTOSIZE) # cv.MoveWindow("Search Image", 800, 200) # cv.ShowImage("Search Image", search_image) H, W = frame.shape[0], frame.shape[1] h, w = self.template.shape[0], self.template.shape[1] # Make sure that the template image is smaller than the source if W < w or H < h: #rospy.loginfo( "Template image must be smaller than video frame." ) return None if frame.dtype != self.template.dtype: #rospy.loginfo("Template and video frame must have same depth and number of channels.") return None # Create a copy of the frame to modify frame_copy = frame.copy() for i in range(self.n_pyr): frame_copy = cv2.pyrDown(frame_copy) template_height, template_width = self.template.shape[:2] # Cycle through all scales starting with the last successful scale scales = self.scales[self.last_scale:] + self.scales[:self.last_scale - 1] # Track which scale and rotation gives the best match maxScore = -1 best_s = 1 best_r = 0 best_x = 0 best_y = 0 for s in self.scales: for r in self.rotations: # Scale the template by s template_copy = cv2.resize( self.template, (int(template_width * s), int(template_height * s))) # Rotate the template through r degrees rotation_matrix = cv2.getRotationMatrix2D( (template_copy.shape[1] / 2, template_copy.shape[0] / 2), r, 1.0) template_copy = cv2.warpAffine( template_copy, rotation_matrix, (template_copy.shape[1], template_copy.shape[0]), borderMode=cv2.BORDER_REPLICATE) # Use pyrDown() n_pyr times on the scaled and rotated template for i in range(self.n_pyr): template_copy = cv2.pyrDown(template_copy) # Create the results array to be used with matchTempate() h, w = template_copy.shape[:2] H, W = frame_copy.shape[:2] result_width = W - w + 1 result_height = H - h + 1 try: result_mat = cv.CreateMat(result_height, result_width, cv.CV_32FC1) result = np.array(result_mat, dtype=np.float32) except: continue # Run matchTemplate() on the reduced images cv2.matchTemplate(frame_copy, template_copy, cv.CV_TM_CCOEFF_NORMED, result) # Find the maximum value on the result map (minValue, maxValue, minLoc, maxLoc) = cv2.minMaxLoc(result) if maxValue > maxScore: maxScore = maxValue best_x, best_y = maxLoc best_s = s best_r = r best_template = template_copy.copy() self.last_scale = self.scales.index(s) best_result = result.copy() # Transform back to original image sizes best_x *= int(pow(2.0, self.n_pyr)) best_y *= int(pow(2.0, self.n_pyr)) h, w = self.template.shape[:2] h = int(h * best_s) w = int(w * best_s) best_result = cv2.resize( best_result, (int(pow(2.0, self.n_pyr)) * best_result.shape[1], int(pow(2.0, self.n_pyr)) * best_result.shape[0])) cv2.imshow("Result", best_result) best_template = cv2.resize( best_template, (int(pow(2.0, self.n_pyr)) * best_template.shape[1], int(pow(2.0, self.n_pyr)) * best_template.shape[0])) cv2.imshow("Best Template", best_template) #match_box = ((best_x + w/2, best_y + h/2), (w, h), -best_r) if maxScore > 0.7: return (best_x, best_y, w, h) else: return self.detect_box
def draw_rects(img, rects, color): if rects: for i in rects: cv.Rectangle(img, (int(rects[0][0]), int(rects[0][1])), (int(rects[0][2]), int(rects[0][3])), cv.CV_RGB(0, 255, 0), 1, 8, 0) #画一个绿色的矩形框
# files = os.listdir('data/examples') image_path = '/home/ivan/dev/pydev/lab/labtrans/plotter/data/cam/1372180045710-ocr.jpg' counter = 0 for f in [image_path]: image = cv.LoadImage(f) for plate in anpr.detect_plates(image): zzz = cv.CreateImage(cv.GetSize(plate), cv.IPL_DEPTH_8U, 3) cv.Smooth(plate, zzz) # cv.PyrMeanShiftFiltering(plate, zzz, 40, 15) foo = anpr.greyscale(plate) segmented = cv.CreateImage(cv.GetSize(plate), cv.IPL_DEPTH_8U, 1) bar = cv.CreateImage(cv.GetSize(plate), cv.IPL_DEPTH_8U, 1) cv.EqualizeHist(foo, segmented) cv.AdaptiveThreshold( segmented, bar, 255, cv.CV_ADAPTIVE_THRESH_GAUSSIAN_C, cv.CV_THRESH_BINARY_INV, plate.height % 2 == 0 and (plate.height + 1) or plate.height, plate.height / 2) baz = cv.CreateImage(cv.GetSize(plate), cv.IPL_DEPTH_8U, 1) el = cv.CreateStructuringElementEx(1, 2, 0, 0, cv.CV_SHAPE_RECT) cv.Erode(bar, baz, el) quick_show(plate) quick_show(segmented) quick_show(bar) quick_show(baz) for char in anpr.find_characters(foo, baz): cv.Rectangle(plate, (int(char.x1), int(char.y1)), (int(char.x2), int(char.y2)), (255, 0, 0)) quick_show(plate)
def showRectangle(imcolor, obj, color): cv.Rectangle( imcolor, (obj[0][0],obj[0][1]), (obj[0][0]+obj[0][2],obj[0][1]+obj[0][3]), color) cv.NamedWindow('Face Detection', cv.CV_WINDOW_NORMAL) cv.ShowImage('Face Detection', imcolor)
cv.Line(image, pt1, pt2, random_color(random), random.randrange(0, 10), line_type, 0) cv.ShowImage(window_name, image) cv.WaitKey(delay) # draw some rectangles for i in range(number): pt1 = (random.randrange(-width, 2 * width), random.randrange(-height, 2 * height)) pt2 = (random.randrange(-width, 2 * width), random.randrange(-height, 2 * height)) cv.Rectangle(image, pt1, pt2, random_color(random), random.randrange(-1, 9), line_type, 0) cv.ShowImage(window_name, image) cv.WaitKey(delay) # draw some ellipes for i in range(number): pt1 = (random.randrange(-width, 2 * width), random.randrange(-height, 2 * height)) sz = (random.randrange(0, 200), random.randrange(0, 200)) angle = random.randrange(0, 1000) * 0.180 cv.Ellipse(image, pt1, sz, angle, angle - 100, angle + 200, random_color(random), random.randrange(-1, 9),
t = cv.GetTickCount() # HaarDetectObjects takes 0.02s faces = cv.HaarDetectObjects(small_img, cascade, cv.CreateMemStorage(0), haar_scale, min_neighbors, haar_flags, min_size) t = cv.GetTickCount() - t if faces: lights(50 if len(faces) == 0 else 0, 50 if len(faces) > 0 else 0, 0, 50) for ((x, y, w, h), n) in faces: # the input to cv.HaarDetectObjects was resized, so scale the # bounding box of each face and convert it to two CvPoints pt1 = (int(x * image_scale), int(y * image_scale)) pt2 = (int((x + w) * image_scale), int((y + h) * image_scale)) cv.Rectangle(frame, pt1, pt2, cv.RGB(100, 220, 255), 1, 8, 0) # get the xy corner co-ords, calc the midFace location x1 = pt1[0] x2 = pt2[0] y1 = pt1[1] y2 = pt2[1] midFaceX = x1 + ((x2 - x1) / 2) midFaceY = y1 + ((y2 - y1) / 2) midFace = (midFaceX, midFaceY) offsetX = midFaceX / float(frame.width / 2) offsetY = midFaceY / float(frame.height / 2) offsetX -= 1 offsetY -= 1