def generate(ident, sp=SQ_SIZE): size = (Square.GRID_SIZE + 4) * sp img = cv.CreateImage((size, size), 8, 1) rim = [(0, 0), (size, 0), (size, size), (0, size)] border = [(sp, sp), (size - sp, sp), (size - sp, size - sp), (sp, size - sp)] inside = [(sp * 2, sp * 2), (sp * 5, sp * 2), (sp * 5, sp * 5), (sp * 2, sp * 5)] square = lambda (x, y): [((2 + x) * sp, (2 + y) * sp), ((3 + x) * sp, (2 + y) * sp), ((3 + x) * sp, (3 + y) * sp), ((2 + x) * sp, (3 + y) * sp)] corners = [square((0, 0))] #square(size-sp*2,sp*2), #square(size-sp*2,size-sp*2), square(sp*2,size-sp*2)] code = [] id = ident for p in Square.code_points: if ident % 2 == 0: code.append(square(p)) ident /= 2 cv.FillPoly(img, [rim], (255, 255, 255)) cv.FillPoly(img, [border], (0, 0, 0)) cv.FillPoly(img, [inside], (255, 255, 255)) cv.FillPoly(img, corners + code, (0, 0, 0)) font = cv.InitFont(cv.CV_FONT_HERSHEY_COMPLEX_SMALL, 0.7, 0.7) cv.PutText(img, "%d" % id, (0, 15), font, (50, 50, 50)) return img
def rects_intersection(rects, maxwidth, maxheight): if not rects: return intersection = cv.CreateImage((maxwidth, maxheight), cv.IPL_DEPTH_8U,1) cv.FillPoly(intersection, [rects[0]], im.color.blue) for r in rects: canvas = cv.CreateImage((maxwidth, maxheight), cv.IPL_DEPTH_8U,1) cv.FillPoly(canvas, [r], im.color.blue) cv.And(canvas, intersection, intersection) return im.find_contour(intersection)
def drawToImage(self, img): pts = [pt.toTuple() for pt in self.getShape().vertices()] cv.FillPoly(img, [pts], self.getDrawColor()) for side in self.getShape().sides(): cv.Line(img, side.start().toTuple(), side.end().toTuple(), cv.RGB(150, 150, 150), 1)
def show(self, col): point_list = [[(0, 0), (self.width, 0), (self.width, self.height), (0, self.height)]] cv.FillPoly( self.img, point_list, cv.CV_RGB(color_array[col][0], color_array[col][1], color_array[col][2])) cv.ShowImage("test", self.img)
def get_mask(w, h): img = cv.CreateImage((w, h), 8, 1) cv.Zero(img) t = int(w / 5) k = int(w / 15) p = w - k - 1 poly = ((k, t), (t + k, 0), (p - t, 0), (p, t), (p, h - t), (p - t, h), (t + k, h), (k, h - t)) cv.FillPoly(img, (poly, ), 255) return img
def sub_intersection(amap, apoly, maxwidth, maxheight): polymap = cv.CreateImage((maxwidth, maxheight), cv.IPL_DEPTH_8U,1) cv.FillPoly(polymap, [apoly], im.color.blue) intersection = cv.CreateImage((maxwidth, maxheight), cv.IPL_DEPTH_8U,1) cv.And(polymap, amap, intersection) cv.Sub(amap, intersection, amap)
def has_intersection(amap, apoly, maxwidth, maxheight): polymap = cv.CreateImage((maxwidth, maxheight), cv.IPL_DEPTH_8U,1) cv.FillPoly(polymap, [apoly], im.color.blue) intersection = cv.CreateImage((maxwidth, maxheight), cv.IPL_DEPTH_8U,1) cv.And(polymap, amap, intersection) m=cv.Moments(cv.GetMat(intersection), True) return bool(cv.GetSpatialMoment(m, 0, 0))
def process_image(self, msg): try: cvim = self.bridge.imgmsg_to_cv(msg, 'bgr8') except CvBridgeError as err: rospy.logerr(e) return if self.boxes: cv.PolyLine(cvim, self.boxes, True, (0, 0, 255)) if self.selected_obj_id: cv.FillPoly(cvim, [self.boxes[self.selected_obj_id]], (0, 0, 255, 0.5)) # display object names if self.object_centers_msg: for center, color, category in zip( self.object_centers_msg.centers, self.object_centers_msg.color_labels, self.object_centers_msg.category_labels): cv.PutText(cvim, color + ' ' + category, (center.pixel.x, center.pixel.y), self.font, (0, 0, 255)) # display trajector if self.trajector_location: cv.Circle(cvim, self.trajector_location, 3, (255, 0, 0), thickness=2, lineType=cv.CV_AA, shift=0) cv.PutText(cvim, 'trajector', self.trajector_location, self.font, (255, 0, 0)) # display selected landmark if self.meaning: lmk = self.meaning.args[0] p_arr = self.lmk_geo_to_img_geo(lmk) if isinstance(lmk.representation, PointRepresentation): cv.Circle(cvim, tuple(map(int, p_arr[0])), 3, (0, 255, 0), thickness=2, lineType=cv.CV_AA, shift=0) else: pl = [[tuple(map(int, l)) for l in p_arr]] cv.PolyLine(cvim, pl, True, (0, 255, 0), thickness=3) cv.ShowImage('img', cvim) cv.WaitKey(10)
def backproject_depth(im, depth, corners): # Use 'extrinsic rectification' to find plane equation global obj, cam, distcc, rvec, tvec, bp obj = np.mgrid[:6, :8, :1].astype('f').reshape(3, -1) * 0.0254 f = 583.0 cx = 321 cy = 249 distcc = np.zeros((4, 1), 'f') rvec = np.zeros((3, 1), 'f') tvec = np.zeros((3, 1), 'f') cam = np.array([[f, 0, cx], [0, f, cy], [0, 0, 1]]) cv.FindExtrinsicCameraParams2(obj, corners, cam, distcc, rvec, tvec) # Back project to see how it went bp = np.array(corners) cv.ProjectPoints2(obj, rvec, tvec, cam, distcc, bp) global pts1 # Show the points in a point cloud, using rvec and tvec RT = np.eye(4).astype('f') RT[:3, :3] = expmap.axis2rot(rvec.squeeze()) RT[:3, 3] = tvec.squeeze() #RT = np.linalg.inv(RT) pts1 = np.dot(RT[:3, :3], obj) + RT[:3, 3].reshape(3, 1) pts1[1, :] *= -1 pts1[2, :] *= -1 rgb1 = np.zeros((pts1.shape[1], 4), 'f') rgb1[:, :] = [0, 0, 0, 1] # Also show the points in the region, using the calib image global mask, pts2 bounds = corners[[0, 7, 5 * 8 + 7, 5 * 8], :] polys = (tuple([tuple(x) for x in tuple(bounds)]), ) mask = np.zeros((480, 640), 'f') cv.FillPoly(mask, polys, [1, 0, 0]) mask = (mask > 0) & (depth < 2047) v, u = np.mgrid[:480, :640].astype('f') pts2 = np.vstack(project(depth[mask].astype('f'), u[mask], v[mask])) rgb2 = np.zeros((pts2.shape[1], 4), 'f') rgb2[:, :] = [0, 1, 0, 1] if np.any(np.isnan(pts2.mean(1))): return window.update_points( np.hstack((pts1, pts2)).transpose(), np.vstack((rgb1, rgb2))) window.lookat = pts1.mean(1) window.Refresh() return
def init_empty_img(self): # calculate Union{ near_regions } for all building nearby_region_polys = [bd.near_region_poly for bd in self.buildings] all_near_regions = cv.CreateImage( (self.label_img.width, self.label_img.height), cv.IPL_DEPTH_8U, 1) cv.FillPoly(all_near_regions, [nearby_region_polys[0]], im.color.blue) for poly in nearby_region_polys: tmp_canvas = cv.CreateImage( (self.label_img.width, self.label_img.height), cv.IPL_DEPTH_8U, 1) cv.FillPoly(tmp_canvas, [poly], im.color.blue) cv.Or(tmp_canvas, all_near_regions, all_near_regions) # find the "empty" region empty_region = cv.CreateImage( (self.label_img.width, self.label_img.height), cv.IPL_DEPTH_8U, 1) cv.CmpS(all_near_regions, 0, empty_region, cv.CV_CMP_EQ) for ele in it.nonzero_indices(cv.GetMat(empty_region)): y, x = ele y, x = int(y), int(x) nearest_bd = self.get_nearest_building(x, y) cv.Set2D(empty_region, y, x, nearest_bd.bid) return empty_region
def drawToImage(self,img,imgtype): pts = [] if not imgtype=='temp': pts = [pt.toTuple() for pt in self.getShape().vertices()] else: pts = [(int(pt.x() + 400),int(pt.y())) for pt in self.getShape().vertices()] cv.FillPoly(img,[pts],self.getDrawColor()) for side in self.getShape().sides(): pt1 = side.start() pt2 = side.end() if imgtype == 'temp': dx = 400 dy = 0 pt1.translate(dx,dy) pt2.translate(dx,dy) cv.Line(img,pt1.toTuple(),pt2.toTuple(),cv.RGB(150,150,150),1)
def motion_bbox(output, motion): #global muestra, prom global bbox_list #INICIO = time.time() contour = cv.FindContours(motion, mem_storage, cv.CV_RETR_CCOMP, cv.CV_CHAIN_APPROX_SIMPLE) bbox_list = [] #lista para almacenar los bounding box de las manchas average_box_area = 0 #variable para obtener el area en promedio de las bounding box while contour: #recorrido de los contornos/manchas bbox = cv.BoundingRect( list(contour)) #obtencion del bounding box del contorno actual pt1 = (bbox[0], bbox[1]) #punto 1 del bounding box pt2 = (bbox[0] + bbox[2], bbox[1] + bbox[3]) #punto 2 del bounding box w, h = abs(pt1[0] - pt2[0]), abs( pt1[1] - pt2[1]) #ancho y largo del bounding box #obtencion de puntos del contorno para crear un wire-frame polygon_points = cv.ApproxPoly(list(contour), mem_storage, cv.CV_POLY_APPROX_DP) #mostrar o las manchas de movimiento if SHOW_MOVEMENT_AREA: cv.FillPoly(output, [ list(polygon_points), ], cv.CV_RGB(255, 255, 255), 0, 0) #mostrar o no los contornos de las manchas de movimiento if SHOW_MOVEMENT_CONTOUR and w * h > AREA * MIN_PERCENT: cv.PolyLine(output, [ polygon_points, ], 0, cv.CV_RGB(255, 255, 255), 1, 0, 0) average_box_area += w * h #acumulacion de totales de areas bbox_list.append((pt1, pt2)) #lista con todos los bounding box contour = contour.h_next() #lectura del siguiente contorno, si hay if len(bbox_list) > 0: #si hubo movimiento average_box_area = average_box_area / float( len(bbox_list)) #area promedio de bounding box new_bbox_list = [ ] #nueva lista de bounding box, eliminando los menores al area promedio for i in range(len(bbox_list)): #recorrido de los bounding box pt1, pt2 = bbox_list[i] #separacion en dos puntos del bounding box w, h = abs(pt1[0] - pt2[0]), abs( pt1[1] - pt2[1]) #obtencion del ancho y largo if w * h >= average_box_area and w * h > AREA * MIN_PERCENT: #comparacion del area del bounding box con el promedio new_bbox_list.append( (pt1, pt2)) #si es mayor o igual, se queda en la nueva lista bbox_list = get_collided_bboxes( new_bbox_list ) #combinacion de varios bounding box en uno si estan en contacto
def run(self): while not self._stop.isSet(): task = self.q.get() if task != None: obj, image = task rect = cv.BoundingRect(obj.cont) siftimage = siftfastpy.Image(rect[2], rect[3]) cv.CvtColor(image, self.gray, cv.CV_BGR2GRAY) cv.SetZero(self.mask) cv.FillPoly(self.mask, [obj.cont], cv.Scalar(255)) cv.And(self.gray, self.mask, self.gray) gnp = np.asarray(cv.GetSubRect(self.gray, rect)) siftimage.SetData(gnp) t0 = time.time() # compute keypoints and time how long it takes frames,desc = siftfastpy.GetKeypoints(siftimage) self.stats.append((rect[2]*rect[3], time.time() - t0)) # compute feature vector tmp = np.concatenate((frames[:,2:4], desc), axis=1).astype('float64') # search in the flann tree for the feature vectors n = 2 thr = 1.5 result, dists = self.flann.nn_index(tmp, n, checks=32) # ismatch contains the indices in the testset for which a match is found ismatch = dists[:,1] > thr * dists[:,0] # meta contains the index to object-ID mapping obj.ids = [] for i, res in enumerate(result): if ismatch[i]: obj.ids.append(self.meta[res[0]][0]) # obj.ids = [self.meta[res][0] for i, res in enumerate(result) if ismatch[i]] # transfer keypoints back to full frame coordinates frames[:,0] += rect[0] frames[:,1] += rect[1] obj.frames = frames obj.desc = desc
def get_near_region_mask(self, blds): regions = [bd.near_region_poly for bd in blds] c = cg.rects_intersection(regions, self.label_img.width, self.label_img.height) # subtract buildings equ_class_region = cv.CreateImage( (self.label_img.width, self.label_img.height), cv.IPL_DEPTH_8U, 1) canvas = cv.CloneImage(self.label_img) cv.FillPoly(equ_class_region, [c], im.color.blue) cv.CmpS(canvas, 0, canvas, cv.CV_CMP_EQ) cv.And(equ_class_region, canvas, equ_class_region) # subtract near of near's neighbor near_of_near = set(self.buildings) for bd in blds: near_of_near = near_of_near.union(bd.near_set) near_of_near.difference_update(set(blds)) near_regions = [bd.near_region_poly for bd in near_of_near] for reg, bd in zip(near_regions, near_of_near): if cg.has_intersection(equ_class_region, reg, self.label_img.width, self.label_img.height): cg.sub_intersection(equ_class_region, reg, self.label_img.width, self.label_img.height) # equ_class_region -= near of near via nearest for bd in near_of_near: eq_region = self.get_equivalent_region_via_nearest(bd) c = im.find_contour(eq_region) while c: if cg.has_intersection(equ_class_region, list(c), self.label_img.width, self.label_img.height): cg.sub_intersection(equ_class_region, list(c), self.label_img.width, self.label_img.height) c = c.h_next() return equ_class_region
contour = contour.h_next() s_contours = [(abs(cv.ContourArea(c)), c) for c in cwalker(contours)] def aspect(c): ((x, y), (w, h), th) = cv.MinAreaRect2(c, cv.CreateMemStorage()) return max([w / h, h / w]) if 1: big_contours = [ c for a, c in s_contours if (a > 100) and (aspect(c) < 5.0) ] for c in big_contours: cv.FillPoly(rgbdi, [c], (255, 0, 0)) all_rects = [cv.BoundingRect(c) for c in big_contours] if all_rects != []: mr = all_rects[0] for r in all_rects[1:]: mr = cv.MaxRect(mr, r) (x, y, w, h) = mr cv.Rectangle(rgbdi, (x, y), (x + w, y + h), cv.RGB(255, 0, 0)) cv.ShowImage("diff", rgbdi) else: cv.ShowImage("diff", di) cv.WaitKey(10) chain.append(f1) chain = chain[-6:]
def run(self): global centroid frame = cv.QueryFrame(self.capture) while not frame: cv.WaitKey(10) 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) while not display_image: cv.WaitKey(10) 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 = 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) ############################### ### Face detection stuff haar_cascade = cv.Load('haarcascades/haarcascade_frontalface_alt.xml') # Set this to the max number of targets to look for (passed to k-means): max_targets = 3 while True: # Capture frame from webcam camera_image = cv.QueryFrame(self.capture) while not camera_image: cv.WaitKey(10) camera_image = cv.QueryFrame(self.capture) frame_count += 1 print 'frame_count = ', frame_count 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: 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) # 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] image_name = "display" # for black and white: Threshold #for colored = display # 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) if self.writer: cv.WriteFrame(self.writer, image) arr = numpy.asarray(image[:, :]) move_thresh = 100 counter = 0 # 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() centroid = get_centroid(trimmed_center_points) print 'centroid = ', centroid # 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) 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)
def run(self): 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 = ["display", "difference", "threshold", "camera", "faces"] 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) haar_cascade = cv.Load( '/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml' ) max_targets = 3 while True: 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.420, 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) cv.Dilate(grey_image, grey_image, None, 18) cv.Erode(grey_image, grey_image, None, 20) 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: levels = 0 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) if frame_t0 - last_target_change_t < .650: # 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) points = non_black_coords_array center_points = [] if len(points): 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) 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) # 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] if nearest_possible_entity in this_frame_entity_list: continue # 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) 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) # Now "delete" any not-found entities which have expired: entity_ttl = 1.0 # 1 sec. ent_count = 0 for entity in last_frame_entity_list: last_time_seen = entity[2] if frame_t0 - last_time_seen > entity_ttl: pass else: # Save it for next time... not expired yet: this_frame_entity_list.append(entity) ent_count += 1 # For next frame: last_frame_entity_list = this_frame_entity_list # Draw the found entities to screen: count = 0 if ent_count != 0: entity = this_frame_entity_list[0] center_point = entity[3] c = entity[1] # RGB color tuple # print '%s %d %d %d' % (entity[0], count, center_point[0], center_point[1]) 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) 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) x = 50 + (center_point[0] * 80 / 320) y = 20 + (center_point[1] * 80 / 240) if self.have_eye: self.ServoMove(0, int(x)) self.ServoMove(1, int(y)) s = '%3.0d %3.0d' % (x, y) cv.PutText(display_image, str(s), text_coord, text_font, text_color) #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 == "display": image = display_image # cv.PutText( image, "AABBs and contours", text_coord, text_font, text_color ) elif image_name == "camera": image = camera_image cv.PutText(image, "No overlay", 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 == "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) 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) cv.ShowImage("Target", image) if self.writer: cv.WriteFrame(self.writer, image) frame_t1 = time.time() 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)
if ((cur_value < perc * hist[end]) & (1.1 * cur_value < next_value)): # cut out a certain depth layer cv.Threshold(for_thresh, min_thresh, bins[start], 255, cv.CV_THRESH_BINARY) cv.Threshold(for_thresh, max_thresh, bins[i], 255, cv.CV_THRESH_BINARY_INV) cv.And(min_thresh, max_thresh, and_thresh) # erode the layer and find contours if erode: cv.Erode(and_thresh, and_thresh, elem) conts = cv.FindContours(and_thresh, storage, cv.CV_RETR_EXTERNAL, cv.CV_CHAIN_APPROX_SIMPLE) # collect all interesting contours in a list while conts: if draw_cluster: cv.FillPoly(contours, [conts], color_tab[c]) if len(conts) > cont_length and min_cont_area < cv.ContourArea(conts) < max_cont_area: conts_list.append(list(conts)) conts = conts.h_next() # prepare for search for next hill start, end = i, i c += 1 x_scale = 1 # draw current value in histogram try: pts = [(int(i * x_scale), hist_height), (int(i * x_scale + x_scale), hist_height), (int(i * x_scale + x_scale), int(hist_height - next_value * hist_height / max_hist)),
def draw_filled_hull(self, im, rgb=(255, 255, 255)): cv.FillPoly(im, [self.hull], cv.RGB(*rgb), cv.CV_AA)
def draw_region(equ_class_region, color=im.color.red): c = im.find_contour(equ_class_region) while c: cv.FillPoly(self.show_img, [list(c)], color) c = c.h_next()
def doRun(tdc): # Create directory to put images into... if not sweep: try: os.makedirs('junction') except: pass # Create a corpus... c = rlda.Corpus(10,4) c.setIdentWordCounts(identCount(),4) for i in xrange(tdc): dic, abn = genDoc(False) doc = rlda.Document(dic) doc.abn = abn c.add(doc) if not sweep: prob = numpy.zeros((6,6,4),dtype=numpy.float_) for key,item in dic.iteritems(): x,y = identToCoord(key[0]) prob[x,y,key[1]] = item multProb = 255.0/prob.max() img = cv.CreateImage((6*25,6*25),cv.IPL_DEPTH_32F,3) for y in xrange(6): for x in xrange(6): coords = [(x*25,y*25),((x+1)*25,y*25),((x+1)*25,(y+1)*25),(x*25,(y+1)*25)] centre = (x*25+12,y*25+12) for d in xrange(4): if d%2==0: col = cv.RGB(0.0,prob[x,y,d]*multProb,0.0) else: col = cv.RGB(prob[x,y,d]*multProb,0.0,0.0) cv.FillPoly(img, [(coords[d],coords[(d+1)%4],centre)], col) cv.SaveImage('junction/xdoc_%i_%s.png'%(i,str(abn)),img) # Fit a model... params = rlda.Params() params.setRuns(16) print 'Fitting model...' p = ProgBar() c.fit(params,p.callback) del p ir = c.getIR() wrt = c.getWRT() # Visualise the regions... if not sweep: mult = 255.0/ir.max() for r in xrange(ir.shape[1]): rend = numpy.zeros((6,6),dtype=numpy.float_) for i in xrange(ir.shape[0]): rend[identToCoord(i)] = ir[i,r] * mult rend = numpy.repeat(numpy.repeat(rend,25,axis=0),25,axis=1) cv.SaveImage('junction/region_%i.png'%r,array2cv(rend)) # Visualise the topics... if not sweep: for t in xrange(wrt.shape[2]): prob = numpy.zeros((6,6,4),dtype=numpy.float_) for i in xrange(ir.shape[0]): x,y = identToCoord(i) for r in xrange(wrt.shape[1]): for w in xrange(wrt.shape[0]): prob[x,y,w] += ir[i,r] * wrt[w,r,t] multProb = 255.0/prob.max() img = cv.CreateImage((6*25,6*25),cv.IPL_DEPTH_32F,3) for y in xrange(6): for x in xrange(6): coords = [(x*25,y*25),((x+1)*25,y*25),((x+1)*25,(y+1)*25),(x*25,(y+1)*25)] centre = (x*25+12,y*25+12) for d in xrange(4): if d%2==0: col = cv.RGB(0.0,prob[x,y,d]*multProb,0.0) else: col = cv.RGB(prob[x,y,d]*multProb,0.0,0.0) cv.FillPoly(img, [(coords[d],coords[(d+1)%4],centre)], col) cv.SaveImage('junction/topic_%i.png'%t,img) # Test on a bunch of documents, creating a list of abnormality score/actually an abnormality pairs... ab_gt = [] print 'Testing...' p = ProgBar() for i in xrange(testDocCount): p.callback(i,testDocCount) dic, abn = genDoc() doc = rlda.Document(dic) doc.fit(ir,wrt) ab_gt.append((doc.negLogLikeRegionVec().max(),abn)) del p ab_gt.sort(reverse=True) # Use the pairs to construct a roc... posCount = len(filter(lambda p:p[1]==True,ab_gt)) negCount = len(ab_gt) - posCount print 'positive samples = ',posCount print 'negative samples = ',negCount truePos = 0 falsePos = 0 trueNeg = negCount falseNeg = posCount roc = [] for p in ab_gt: if p[1]: truePos += 1 falseNeg -= 1 else: falsePos +=1 trueNeg -= 1 pnt = (float(falsePos)/float(falsePos+trueNeg), float(truePos)/float(truePos+falseNeg)) roc.append(pnt) # Save the roc to disk... if not sweep: f = open('junction_roc.txt','w') f.write('0.0 0.0\n') for pnt in roc: f.write('%f %f\n'%pnt) f.close() # Calculate and print out the area under the roc... area = 0.0 for i in xrange(1,len(roc)): area += 0.5*(roc[i-1][1]+roc[i][1]) * (roc[i][0]-roc[i-1][0]) print 'area under roc =',area, '(above',(1.0-area),')' return area
def doRun(tdc): # Create a corpus... vlda = lda.VLDA(4, identCount()*4) abnDict = dict() for i in xrange(tdc): dic, abn = genDoc() nDic = dict() for key,item in dic.iteritems(): nDic[key[0]*4+key[1]] = item doc = vlda.add(nDic) abnDict[doc] = abn # Fit a model... print 'Fitting model...' p = ProgBar() vlda.solve() del p # Visualise the topics... if not sweep: for t in xrange(vlda.numTopics()): prob = numpy.zeros((6,6,4),dtype=numpy.float_) beta = vlda.getBeta(t) for i in xrange(beta.shape[0]): x,y = identToCoord(i//4) w = i%4 prob[x,y,w] += beta[i] multProb = 255.0/prob.max() img = cv.CreateImage((6*25,6*25),cv.IPL_DEPTH_32F,3) for y in xrange(6): for x in xrange(6): coords = [(x*25,y*25),((x+1)*25,y*25),((x+1)*25,(y+1)*25),(x*25,(y+1)*25)] centre = (x*25+12,y*25+12) for d in xrange(4): if d%2==0: col = cv.RGB(0.0,prob[x,y,d]*multProb,0.0) else: col = cv.RGB(prob[x,y,d]*multProb,0.0,0.0) cv.FillPoly(img, [(coords[d],coords[(d+1)%4],centre)], col) cv.SaveImage('junction_topic_%i.png'%t,img) # Test on a bunch of documents, creating a list of abnormality score/actually an abnormality pairs... ab_gt = [] print 'Testing...' p = ProgBar() for i in xrange(testDocCount): p.callback(i,testDocCount) dic, abn = genDoc() nDic = dict() for key,item in dic.iteritems(): nDic[key[0]*4+key[1]] = item nll = vlda.getNewNLL(nDic) ab_gt.append((nll,abn)) del p ab_gt.sort(reverse=True) # Use the pairs to construct a roc... posCount = len(filter(lambda p:p[1]==True,ab_gt)) negCount = len(ab_gt) - posCount print 'positive samples = ',posCount print 'negative samples = ',negCount truePos = 0 falsePos = 0 trueNeg = negCount falseNeg = posCount roc = [] for p in ab_gt: if p[1]: truePos += 1 falseNeg -= 1 else: falsePos +=1 trueNeg -= 1 pnt = (float(falsePos)/float(falsePos+trueNeg), float(truePos)/float(truePos+falseNeg)) roc.append(pnt) # Save the roc to disk... if not sweep: f = open('junction_roc.csv','w') f.write('0.0, 0.0\n') for pnt in roc: f.write('%f, %f\n'%pnt) f.close() # Calculate and print out the area under the roc... area = 0.0 for i in xrange(1,len(roc)): area += 0.5*(roc[i-1][1]+roc[i][1]) * (roc[i][0]-roc[i-1][0]) print 'area under roc =',area, '(above',(1.0-area),')' return area
cv.PolyLine(image, pt, 1, random_color(random), random.randrange(1, 9), line_type, 0) cv.ShowImage(window_name, image) cv.WaitKey(delay) # draw some filled polylines for i in range(number): for a in range(nb_polylines): for b in range(polylines_size): pt [a][b] = (random.randrange(-width, 2 * width), random.randrange(-height, 2 * height)) cv.FillPoly(image, pt, random_color(random), line_type, 0) cv.ShowImage(window_name, image) cv.WaitKey(delay) # draw some circles for i in range(number): pt1 = (random.randrange(-width, 2 * width), random.randrange(-height, 2 * height)) cv.Circle(image, pt1, random.randrange(0, 300), random_color(random), random.randrange(-1, 9), line_type, 0) cv.ShowImage(window_name, image)
def mapper(self, _, line): photo_label = "photo:" if line[0:len(photo_label)] == photo_label: (label, id, secret, server) = line.split(' ') unique = "candidate_flickr_" + server + "_" + id + "_" + secret + "_m.jpg" url = 'http://static.flickr.com/' + server + "/" + id + "_" + secret + '_m.jpg' (filename, headers) = urllib.urlretrieve(url) width = self.options.processing_size height = width flickrImage = Image.open(filename) candidateImage = ImageOps.fit(flickrImage, (width, height), Image.ANTIALIAS, 0, (0.5, 0.5)) os.remove(filename) candidateImage.save(filename, "JPEG") #imageName=os.path.basename(sys.argv[1]) #dirName=`dirname $1` #echo Finding superpixels ... berkeleySegFile = '/tmp/' + unique + '.bse' #subprocess.call(['segment', '-image', filename, '-segfile', berkeleySegFile, '-numsuperpixels', str(number_of_superpixels)]) subprocess.call([ '/home/stolrsky/Desktop/LTPM/Libraries/BSE-1.2/segment', '-image', filename, '-segfile', berkeleySegFile, '-numsuperpixels', str(number_of_superpixels) ]) #echo Merging superpixels ... segmentationString = subprocess.check_output([ '/home/stolrsky/Desktop/LTPM/SuperPixelsToSegmentation/build/SuperPixelsToSegmentation', filename, berkeleySegFile, '0' ]) #print segmentationString segmentationFilePath = '/tmp/' + unique + '.realseg' segmentationFile = open(segmentationFilePath, 'w') segmentationFile.write(segmentationString) segmentationFile.close() cvImage = cv.CreateImageHeader(candidateImage.size, cv.IPL_DEPTH_8U, 3) cv.SetData(cvImage, candidateImage.tostring()) #cv.ShowImage(cvImage) #cv.WaitKey() polys = parseRealseg(segmentationString) #sys.stderr.write("polysString: " + segmentationString) sys.stderr.write("polysCount: " + str(len(polys))) p = 0 for poly in polys: segmentMask = cv.CreateImage(candidateImage.size, cv.IPL_DEPTH_8U, 3) cv.SetZero(segmentMask) cv.FillPoly(segmentMask, [poly], cv.RGB(1, 1, 1)) segment = cv.CreateImage(candidateImage.size, cv.IPL_DEPTH_8U, 3) cv.Mul(segmentMask, cvImage, segment) cv.SaveImage('/tmp/' + unique + '_' + str(p) + '.jpg', segment) p = p + 1 cv.SaveImage('/tmp/' + unique + '.jpg', cvImage) os.remove(berkeleySegFile) os.remove(filename) """ db = MySQLdb.connect(user="******", db="LTPM") c = db.cursor() LTPM_name = self.options.target_image_name candidate_name = unique rows = self.options.rows cols = self.options.cols for target_row in range(rows): for target_col in range(cols): target_realseg_path = self.get_target_seg_path(target_row, target_col) call = '/home/stolrsky/Desktop/LTPM/PlaceImage/build/PlaceImage ' + target_realseg_path + ' ' + segmentationFilePath #print call #score = subprocess.check_output(['/home/stolrsky/Desktop/LTPM/PlaceImage/build/PlaceImage', target_realseg_path, segmentationFilePath]) score = subprocess.check_output(shlex.split(call)) #print "scored " + unique + ' vs. ' + LTPM_name + ':' + str(target_row) + ':' + str(target_col) #print score #print " " # insert in db c.execute("insert into scores(LTPM_name, target_row, target_col, candidate_name, score) VALUES('"+LTPM_name+"', '"+str(target_row)+"', '"+str(target_col)+"', '"+url+"', "+str(score)+")") c.close() """ os.remove(segmentationFilePath) #(filename, headers) = urllib.urlretrieve(url, '/tmp/' + unique) #s3conn = S3Connection() #LTPM = Bucket(s3conn, 'ltpm') #image = Key(LTPM) #image.key = unique #image.set_contents_from_filename(filename) yield (unique, url)
def detect_motion(self, sensitivity='medium'): #Finding Video Size from the first frame frame = cv.QueryFrame(self.video_handle) frame_size = cv.GetSize(frame) '''Initializing Image Variables(to be used in motion detection) with required types and sizes''' # Image containg instantaneous moving rectangles color_image = cv.CreateImage(frame_size, 8, 3) # Resizing to window size color_output = cv.CreateImage(self.window_size, 8, 3) # Grey Image used for contour detection grey_image = cv.CreateImage(frame_size, cv.IPL_DEPTH_8U, 1) # Image storing background (moving pixels are averaged over small time window) moving_average = cv.CreateImage(frame_size, cv.IPL_DEPTH_32F, 3) # Image for storing tracks resized to window size track_output = cv.CreateImage(self.window_size, cv.IPL_DEPTH_8U, 3) track_image, track_win = self.init_track_window(frame) def totuple(a): try: return tuple(totuple(i) for i in a) except TypeError: return a first = True # Infinite loop for continuous detection of motion while True: '''########## Pixelwise Detection of Motion in a frame ###########''' # Capturing Frame color_image = cv.QueryFrame(self.video_handle) ##### Sensitivity Control 1 ##### if (sensitivity == 'medium') or (sensitivity == 'low'): # Gaussian Smoothing cv.Smooth(color_image, color_image, cv.CV_GAUSSIAN, 3, 0) if first: difference = cv.CloneImage(color_image) temp = cv.CloneImage(color_image) cv.ConvertScale(color_image, moving_average, 1.0, 0.0) first = False else: cv.RunningAvg(color_image, moving_average, .020, None) # Convert the scale of the moving average. cv.ConvertScale(moving_average, temp, 1, 0.0) # Minus the current frame from the moving average. cv.AbsDiff(color_image, temp, difference) #cv.ShowImage("BG",difference) # Convert the image to grayscale. cv.CvtColor(difference, grey_image, cv.CV_RGB2GRAY) ##### Sensitivity Control 2 ##### sens_thres = 90 if (sensitivity == 'low') or (self.opt == 'cam') else 40 # Convert the image to black and white. cv.Threshold(grey_image, grey_image, sens_thres, 255, cv.CV_THRESH_BINARY) '''### Blobing moved adjacent pixels, finding closed contours and bounding rectangles ###''' ##### Sensitivity Control 3 ##### if (sensitivity == 'medium') or (sensitivity == 'low'): # Dilate and erode to get people blobs ker_size = 20 if self.opt == 'file' else 50 cv.Dilate(grey_image, grey_image, None, ker_size) cv.Erode(grey_image, grey_image, None, 3) storage = cv.CreateMemStorage(0) contour = cv.FindContours(grey_image, storage, cv.CV_RETR_CCOMP, cv.CV_CHAIN_APPROX_SIMPLE) points = [] while contour: bound_rect = cv.BoundingRect(list(contour)) polygon_points = cv.ApproxPoly(list(contour), storage, cv.CV_POLY_APPROX_DP) pt1 = (bound_rect[0], bound_rect[1]) pt2 = (bound_rect[0] + bound_rect[2], bound_rect[1] + bound_rect[3]) if (self.opt == 'file'): points.append(pt1) points.append(pt2) elif (bound_rect[0] - bound_rect[2] > 20) and (bound_rect[1] - bound_rect[3] > 20): points.append(pt1) points.append(pt2) box = cv.MinAreaRect2(polygon_points) box2 = cv.BoxPoints(box) box3 = np.int0(np.around(box2)) box4 = totuple(box3) box5 = box4 + (box4[0], ) # Filling the contours in the greyscale image (visual blobs instead of just contours) cv.FillPoly(grey_image, [ list(polygon_points), ], cv.CV_RGB(255, 255, 255), 0, 0) # Following line to draw detected contours as well #cv.PolyLine( color_image, [ polygon_points, ], 0, cv.CV_RGB(255,0,0), 1, 0, 0 ) # Drawing Rectangle around the detected contour cv.PolyLine(color_image, [list(box5)], 0, (0, 255, 255), 2) if len(points): # (self.opt == 'file') and center1 = (pt1[0] + pt2[0]) / 2 center2 = (pt1[1] + pt2[1]) / 2 cv.Circle(color_image, (center1, center2), 5, cv.CV_RGB(0, 255, 0), -1) rad = 3 if self.opt == 'file' else 5 cv.Circle(track_image, (center1, center2), rad, cv.CV_RGB(255, 128, 0), -1) contour = contour.h_next() # Uncomment to track centroid of all the moved boxes (only for WebCam) ''' if (self.opt == 'cam') and len(points): center_point = reduce(lambda a, b: ((a[0] + b[0]) / 2, (a[1] + b[1]) / 2), points) cv.Circle(track_image, center_point, 15, cv.CV_RGB(255, 128, 0), -1) ''' cv.Resize(color_image, color_output, cv.CV_INTER_AREA) cv.ShowImage("Original", color_output) cv.Resize(track_image, track_output, cv.CV_INTER_AREA) cv.ShowImage(track_win, track_output) # Listen for ESC key c = cv.WaitKey(7) % 0x100 if (0xFF & c == 27): cv.SaveImage('Tracks_img_042_' + sensitivity + '.jpeg', track_output) break
cv.CV_CHAIN_APPROX_SIMPLE) while contour: boundingRect = cv.BoundingRect(list(contour)) p1 = (boundingRect[0], boundingRect[1]) p2 = (boundingRect[0] + boundingRect[2], boundingRect[1] + boundingRect[3]) boundingBoxList.append((p1, p2)) polygonPoints = cv.ApproxPoly(list(contour), memStorage, cv.CV_POLY_APPROX_DP) #Show the contours cv.FillPoly(greyImage, [ list(polygonPoints), ], cv.CV_RGB(255, 255, 255), 0, 0) cv.PolyLine(displayImage, [ polygonPoints, ], 0, cv.CV_RGB(255, 255, 255), 1, 0, 0) contour = contour.h_next() # Find the average size of the bounding box targets and remove ones that are 5% or less than the average as noise boxAreas = [] for box in boundingBoxList: boxWidth = box[right][0] - box[left][0] boxHeight = box[bottom][0] - box[top][0] boxAreas.append(boxWidth * boxHeight) averageBoxArea = 0.0
def fillme(self, canvas, color=im.color.red): cv.FillPoly(canvas, [list(self.contour)], color)