def get_rects(feat, mask, boundary_offset = 0): hard_mask = np.round(mask + 0.25) bb = np.copy(feat) for c in range(4): bb[c, :, :] *= hard_mask y_offset = np.array([np.arange(0, 480, 4)]).T y_offset = np.tile(y_offset, (1, 160)) x_offset = np.arange(0, 640, 4) x_offset = np.tile(x_offset, (120, 1)) bb[0, :, :] += x_offset bb[2, :, :] += x_offset bb[1, :, :] += y_offset bb[3, :, :] += y_offset selected_rects = hard_mask > 0 num_rects = np.sum(selected_rects) rects = np.empty((num_rects, 4)) for i in range(4): rects[:, i] = bb[i, selected_rects] + boundary_offset rects = rects[np.logical_and((rects[:, 2] - rects[:, 0]) > 0, (rects[:, 3] - rects[:, 1]) > 0), :] rects[:, (2, 3)] -= rects[:, (0, 1)] rects = np.clip(rects, 0, 640) rects = [rects[i, :] for i in range(rects.shape[0])] rects, scores = cv2.groupRectangles(rects, 2, 0.15) rectangles = [] if len(rects) == 0: return rectangles for i in range(rects.shape[0]): rectangles.append(Rect(rects[i, 0], rects[i, 1], rects[i, 0] + rects[i, 2], rects[i, 1] + rects[i, 3])) return rectangles
def group_overlapping_regions(map_of_regions, eps=0.9): """ Clusters regions with same classe basing it on the rectangle similarity Args: map_of_regions: {"label": [[rect1, p1], [rect2, p2], ..], "label2"...} eps: used only if CLUSTERING is used. Is the region similarity threshold """ grouped_map = defaultdict(list) for label, rect_prob_list in map_of_regions.items(): # extract rectangles from the array of pairs rects_only = np.array([value[0] for value in rect_prob_list]) # group them rect_list, _ = cv2.groupRectangles(rects_only.tolist(), 1, eps=eps) if len(rect_list) > 0: # for every merged rectangle, calculate the avg prob and # the number of intersection merged_rect_infos = [] for merged_rect in rect_list: sum_of_prob = 0.0 merged_count = 0 for idx, original_rect in enumerate(rects_only): if intersect(original_rect, merged_rect): sum_of_prob += rect_prob_list[idx][1] merged_count += 1 if merged_count > 0: avg_prob = sum_of_prob / merged_count merged_rect_infos.append( (merged_rect, avg_prob, merged_count)) grouped_map[label] = merged_rect_infos return grouped_map
def run(self): ''' 1. Find proposals using ViolaJones 2. Resize the window and classify it 3. Net returns a list of the roof coordinates of each type - saved in roof_coords ''' neural_time = 0 for i, img_name in enumerate(self.img_names): print '***************** Image {0}: {1}/{2} *****************'.format(img_name, i, len(self.img_names)-1) #VIOLA if self.pickle_viola is None: self.viola.detect_roofs(img_name=img_name) #this next line will fail because you dont get the image shape! self.viola.evaluation.score_img(img_name, img_shape[:2]) self.viola.evaluation.save_images(img_name, fname='beforeNeural') current_viola_detections = self.viola.viola_detections viola_time = self.viola.evaluation.detections.total_time else:#use the pickled detections for speed in testing the neural network current_viola_detections = self.viola_evaluation.detections viola_time = self.viola_evaluation.detections.total_time proposal_patches, proposal_coords, img_shape = self.find_viola_proposals(current_viola_detections, img_name=img_name) #NEURALNET with Timer() as t: classified_detections = self.neural_classification(proposal_patches, proposal_coords) #set detections and score for roof_type in utils.ROOF_TYPES: if self.groupThreshold > 0 and roof_type == 'metal': #need to covert to rectangles boxes = utils.get_bounding_boxes(np.array(classified_detections[roof_type])) grouped_boxes, weights = cv2.groupRectangles(np.array(boxes).tolist(), self.groupThreshold) classified_detections[roof_type] = utils.convert_detections_to_polygons(grouped_boxes) #convert back to polygons elif self.groupBounds and roof_type == 'metal': #grouping with the minimal bound of all overlapping rects classified_detections[roof_type] = self.group_min_bound(classified_detections[roof_type], img_shape[:2], erosion=self.erosion) elif self.suppress and roof_type == 'metal': #proper non max suppression from Felzenszwalb et al. classified_detections[roof_type] = self.non_max_suppression(classified_detections[roof_type]) self.detections_after_neural.set_detections(img_name=img_name, roof_type=roof_type, detection_list=classified_detections[roof_type]) neural_time += t.secs self.evaluation_after_neural.score_img(img_name, img_shape[:2], contours=self.groupBounds) self.evaluation_after_neural.save_images(img_name, 'posNeural') if self.pickle_viola is None: self.viola.evaluation.print_report(print_header=True, stage='viola') else: self.viola_evaluation.print_report(print_header=True, stage='viola') self.evaluation_after_neural.detections.total_time = (neural_time) self.evaluation_after_neural.print_report(print_header=False, stage='neural')
def group_regions(self, threshold = GROUP_THRESHOLD): """ Groups the present rectangles with similar sizes and similar locations. Package cv2 function also returns weight (w) of grouped rectangles; however, those values are of no use in current program, and are ignored. """ futureRectangles, w = cv2.groupRectangles(self.rectangles, threshold) # sanity check in case cv2 grouping removes all rectangles if len(futureRectangles) != 0: self.rectangles = futureRectangles
def advance_frame(self, frame, proto_objects_map): """Advances the algorithm by a single frame This method tracks all objects via the following steps: - adds all bounding boxes from saliency map as potential targets - finds bounding boxes from previous frame in current frame via mean-shift tracking - combines the two lists by removing duplicates certain targets are discarded: - targets that are too small - targets that don't move :param frame: New input RGB frame :param proto_objects_map: corresponding proto-objects map of the frame :returns: frame annotated with bounding boxes around all objects that are being tracked """ self.tracker = copy.deepcopy(frame) # build a list of all bounding boxes box_all = [] # append to the list all bounding boxes found from the # current proto-objects map box_all = self._append_boxes_from_saliency(proto_objects_map, box_all) # find all bounding boxes extrapolated from last frame # via mean-shift tracking box_all = self._append_boxes_from_meanshift(frame, box_all) # only keep those that are both salient and in mean shift if len(self.object_roi) == 0: group_thresh = 0 # no previous frame: keep all form saliency else: group_thresh = 1 # previous frame + saliency box_grouped, _ = cv2.groupRectangles(box_all, group_thresh, 0.1) # update mean-shift bookkeeping for remaining boxes self._update_mean_shift_bookkeeping(frame, box_grouped) # draw remaining boxes for (x, y, w, h) in box_grouped: cv2.rectangle(self.tracker, (x, y), (x + w, y + h), (0, 255, 0), 2) return self.tracker
def detect_jpg(detector, jpg_path): if not os.path.exists(jpg_path): raise Exception("Image not exist:"%(jpg_path)) src_img = Image.open(jpg_path) if 'L' != src_img.mode: img = src_img.convert("L") else: img = src_img maxSide = 200.0 w, h = img.size scale = max(1, max(w/maxSide, h/maxSide)) ws = int(w/scale + 0.5) hs = int(h/scale + 0.5) img = img.resize((ws,hs), Image.NEAREST) imgArr = np.asarray(img).astype(np.uint8) time_b = time.time() rects, confs = detector.detect(imgArr, 1.1, 2) t = getTimeByStamp(time_b, time.time(), 'sec') ### TODO: Use NMS to merge the candidate rects and show the landmark, Now merge the rects with opencv, res = cv2.groupRectangles(rects, 3, 0.2) rects = res[0] ### Show Result draw = ImageDraw.Draw(img) for i in xrange(len(rects)): draw.rectangle((rects[i][0], rects[i][1], rects[i][0]+rects[i][2], rects[i][1]+rects[i][3]), outline = "red") print("Detect time : %f s"%t) img.show() ######################################### if len(rects) > 0: num, _ = rects.shape for i in xrange(num): rects[i][0] = int(rects[i][0]*scale +0.5) rects[i][1] = int(rects[i][1]*scale +0.5) rects[i][2] = int(rects[i][2]*scale +0.5) rects[i][3] = int(rects[i][3]*scale +0.5) return
def nms_average(boxes, groupThresh=2, overlapThresh=0.2): rects = [] temp_boxes = [] weightslist = [] new_rects = [] #print 'boxes: ', boxes for i in range(len(boxes)): if boxes[i][4] > 0.2: rects.append([boxes[i,0], boxes[i,1], boxes[i,2]-boxes[i,0], boxes[i,3]-boxes[i,1]]) #print 'rects: ', rects # for i in range(len(rects)): # rects.append(rects[i]) rects, weights = cv2.groupRectangles(rects, groupThresh, overlapThresh) #######################test######### rectangles = [] for i in range(len(rects)): # A______ # | | # -------B # A B testRect = Rect( Point(rects[i,0], rects[i,1]), Point(rects[i,0]+rects[i,2], rects[i,1]+rects[i,3])) rectangles.append(testRect) clusters = [] for rect in rectangles: matched = 0 for cluster in clusters: if (rect_merge( rect, cluster , 0.2) ): matched=1 cluster.left = (cluster.left + rect.left )/2 cluster.right = ( cluster.right+ rect.right )/2 cluster.top = ( cluster.top+ rect.top )/2 cluster.bottom = ( cluster.bottom+ rect.bottom )/2 if ( not matched ): clusters.append( rect ) # print "Clusters:" # for c in clusters: # print c ################################### result_boxes = [] for i in range(len(clusters)): # result_boxes.append([rects[i,0], rects[i,1], rects[i,0]+rects[i,2], rects[i,1]+rects[i,3], 1]) result_boxes.append([clusters[i].left, clusters[i].bottom, clusters[i].right, clusters[i].top, 1]) #print 'result_boxes: ', result_boxes return result_boxes
def AdvanceFrame(self, frame, protoObjectsMap): """ tracks all objects via the following steps: - adds all bounding boxes from saliency map as potential targets - finds bounding boxes from previous frame in current frame via mean-shift tracking - combines the two lists by removing duplicates certain targets are discarded: - targets that are too small - targets that don't move """ self.tracker = copy.deepcopy(frame) # build a list of all bounding boxes box_all = [] # append to the list all bounding boxes found from the # current proto-objects map box_all = self.__AppendBoxesFromSaliency(protoObjectsMap, box_all) # find all bounding boxes extrapolated from last frame # via mean-shift tracking box_all = self.__AppendBoxesFromMeanShift(frame, box_all) # only keep those that are both salient and in mean shift if len(self.object_roi)==0: group_thresh = 0 # no previous frame: keep all form saliency else: group_thresh = 1 # previous frame + saliency box_grouped,_ = cv3.groupRectangles(box_all, group_thresh, 0.1) # update mean-shift bookkeeping for remaining boxes self.__UpdateMeanShiftBookkeeping(frame, box_grouped) # draw remaining boxes for (x,y,w,h) in box_grouped: cv3.rectangle(self.tracker,(x,y),(x+w,y+h),(0,255,0),2) return self.tracker
def smoothContours(contours, thresh=1): ''' Combines multiple bounding boxes into a single one. Parameters ---------- @param: contour: List @param: thresh: Int @return contour: List Array of smoothed contours. ''' if contours is None: return try: contours, weights = cv2.groupRectangles(contours, thresh) except Exception as e: pass # if width of the contour is very high than it's # height then it is probably an error. contours = list(x for x in contours if cv2.boundingRect(x)[2] / 2.5 <= cv2.boundingRect(x)[3] and cv2.contourArea(x) > 50) return contours
def make_bounding_boxes(image): raw_image = image # cv2.imshow('Original Image', raw_image) # cv2.waitKey(0) bilateral_filtered_image = cv2.bilateralFilter(raw_image, 5, 175, 175) # cv2.imshow('Bilateral', bilateral_filtered_image) # cv2.waitKey(0) edge_detected_image = cv2.Canny(bilateral_filtered_image, 75, 200) # cv2.imshow('Edge', edge_detected_image) # cv2.waitKey(0) _, contours, _ = cv2.findContours(edge_detected_image, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) drawing = raw_image.copy() color = (25, 25, 25) bounding_boxes = np.zeros((len(contours), 4)) for i in range(len(contours)): cnt = contours[i] x, y, w, h = cv2.boundingRect(cnt) boxed_img = raw_image[y:y + h, x:x + w] if np.size(boxed_img) > 0: bounding_boxes[i] = [x, y, w, h] # print(bounding_boxes.shape) # print(bounding_boxes) bounding_boxes = list(bounding_boxes) max_bound_boxes, _ = cv2.groupRectangles(bounding_boxes, 1, 0.9) # return bounding_boxes return np.array(max_bound_boxes).astype(int)
def find(self, haystack_img, threshold=0.5, max_results=10): # run the OpenCV algorithm result = cv.matchTemplate(haystack_img, self.needle_img, self.method) # Get the all the positions from the match result that exceed our threshold locations = np.where(result >= threshold) locations = list(zip(*locations[::-1])) #print(locations) # if we found no results, return now. this reshape of the empty array allows us to # concatenate together results without causing an error if not locations: return np.array([], dtype=np.int32).reshape(0, 4) # You'll notice a lot of overlapping rectangles get drawn. We can eliminate those redundant # locations by using groupRectangles(). # First we need to create the list of [x, y, w, h] rectangles rectangles = [] for loc in locations: rect = [int(loc[0]), int(loc[1]), self.needle_w, self.needle_h] # Add every box to the list twice in order to retain single (non-overlapping) boxes rectangles.append(rect) rectangles.append(rect) # Apply group rectangles. # The groupThreshold parameter should usually be 1. If you put it at 0 then no grouping is # done. If you put it at 2 then an object needs at least 3 overlapping rectangles to appear # in the result. I've set eps to 0.5, which is: # "Relative difference between sides of the rectangles to merge them into a group." rectangles, weights = cv.groupRectangles(rectangles, groupThreshold=1, eps=0.5) #print(rectangles) # for performance reasons, return a limited number of results. # these aren't necessarily the best results. if len(rectangles) > max_results: print('Warning: too many results, raise the threshold.') rectangles = rectangles[:max_results] return rectangles
def extract_face(image, fd, inspect=False): image_pyr = image_pyramid(image) faces_all = [] for lvl in range(len(image_pyr)): pyr = image_pyr[lvl] for patch, pos in sliding_window(pyr): faces = fd.detect(patch) if len(faces) > 0: faces_all.append((faces + np.array([pos[0], pos[1], 0, 0])) * 2 ** lvl) if len(faces_all) > 0: faces_all = np.vstack(faces_all) faces_all, _ = cv2.groupRectangles(faces_all.tolist(), 1) if inspect: out = image.copy() for (x, y, w, h) in faces_all: cv2.rectangle(out, (x, y), (x + w, y + h), (0, 255, 0), 3) show_image(scale_image(out, 1000)) face_patches = [] for (x, y, w, h) in faces_all: face_patch = cv2.resize(image[y : y + h, x : x + w, :], (160, 160)) face_patches.append(face_patch) return np.array(face_patches)
def vote_boxes(propose_boxes, propose_cvgs, mask, self): """ Vote amongst the boxes using openCV's built-in clustering routine. """ detections_per_image = [] if not propose_boxes.any(): return detections_per_image ###################################################################### # GROUP RECTANGLES Clustering ###################################################################### nboxes, weights = cv.groupRectangles( np.array(propose_boxes).tolist(), self.gridbox_rect_thresh, self.gridbox_rect_eps) if len(nboxes): for rect, weight in zip(nboxes, weights): if (rect[3] - rect[1]) >= self.min_height: confidence = math.log(weight[0]) detection = [rect[0], rect[1], rect[2], rect[3], confidence] detections_per_image.append(detection) return detections_per_image
def get_rects_legacy(feat, mask, boundary_offset = 0): hard_mask = np.round(mask + 0.25) bb = np.empty((4, 60, 80)) for y in range(15): for x in range(20): for c in range(4): bb[c, y*4:(y+1)*4, x*4:(x+1)*4] = feat[c*16:(c+1)*16, y, x].reshape((4, 4)) for c in range(4): bb[c, :, :] *= hard_mask y_offset = np.array([np.arange(16, 480, 32)]).T y_offset = np.tile(y_offset, (1, 20)) x_offset = np.arange(16, 640, 32) x_offset = np.tile(x_offset, (15, 1)) y_offset = scipy.ndimage.zoom(y_offset, 4, order=0) x_offset = scipy.ndimage.zoom(x_offset, 4, order=0) bb[0, :, :] += x_offset bb[2, :, :] += x_offset bb[1, :, :] += y_offset bb[3, :, :] += y_offset selected_rects = hard_mask > 0 num_rects = np.sum(selected_rects) rects = np.empty((num_rects, 4)) for i in range(4): rects[:, i] = bb[i, selected_rects] + boundary_offset rects = rects[np.logical_and((rects[:, 2] - rects[:, 0]) > 0, (rects[:, 3] - rects[:, 1]) > 0), :] rects[:, (2, 3)] -= rects[:, (0, 1)] rects = np.clip(rects, 0, 640) rects = [rects[i, :] for i in range(rects.shape[0])] rects, scores = cv2.groupRectangles(rects, 2, 0.15) rectangles = [] if len(rects) == 0: return rectangles for i in range(rects.shape[0]): rectangles.append(Rect(rects[i, 0], rects[i, 1], rects[i, 0] + rects[i, 2], rects[i, 1] + rects[i, 3])) return rectangles
def run(self): ''' 1. Sliding window proposals 2. Resize the window and classify it 3. Net returns a list of the roof coordinates of each type - saved in roof_coords ''' neural_time = 0 for i, img_name in enumerate(self.img_names): print '***************** Image {0}: {1}/{2} *****************'.format(img_name, i, len(self.img_names)-1) #NEURALNET with Timer() as t: classified_detections = self.neural_classification(proposal_patches, proposal_coords) #set detections and score for roof_type in utils.ROOF_TYPES: if self.groupThreshold > 0 and roof_type == 'metal': #need to covert to rectangles boxes = utils.get_bounding_boxes(np.array(classified_detections[roof_type])) grouped_boxes, weights = cv2.groupRectangles(np.array(boxes).tolist(), self.groupThreshold) classified_detections[roof_type] = utils.convert_detections_to_polygons(grouped_boxes) #convert back to polygons elif self.groupBounds and roof_type == 'metal': #grouping with the minimal bound of all overlapping rects classified_detections[roof_type] = self.group_min_bound(classified_detections[roof_type], img_shape[:2], erosion=self.erosion) elif self.suppress and roof_type == 'metal': #proper non max suppression from Felzenszwalb et al. classified_detections[roof_type] = self.non_max_suppression(classified_detections[roof_type]) self.detections_after_neural.set_detections(img_name=img_name, roof_type=roof_type, detection_list=classified_detections[roof_type]) neural_time += t.secs self.evaluation.score_img(img_name, img_shape[:2], contours=self.groupBounds) self.evaluation.save_images(img_name, 'posNeural') self.evaluation_after_neural.detections.total_time = (neural_time)
def process_video(img): history = deque(maxlen=30) skimage.io.imsave('c2.PNG', img) im2, boxes = test_image('c2.PNG', False) skimage.io.imsave('c1.PNG', im2) img_lanes = laneDetection('c1.PNG') # Iterate through all detected cars for b in range(1, boxes[1]+1): # Find pixels with each car_number label value nonzero = (boxes[0] == b).nonzero() # Identify x and y values of those pixels nonzeroy = np.array(nonzero[0]) nonzerox = np.array(nonzero[1]) # Append current boxe to history history.append([np.min(nonzerox),np.min(nonzeroy),np.max(nonzerox),np.max(nonzeroy)]) # Get recent boxes for the last 30 fps r_boxes = np.array(history).tolist() # Groups the object candidate rectangles with difference of 10% boxes = cv2.groupRectangles(r_boxes, 10, .1) # Draw rectangles if found if len(boxes[0]) != 0: for box in boxes[0]: cv2.rectangle(img_lanes, (box[0], box[1]), (box[2],box[3]), (0,0,255), 6) # Return image with found cars and lanes return img_lanes
def advance_frame(self, frame, proto_objects_map): self.tracker = copy.deepcopy(frame) box_all = [] box_all = self._append_boxes_from_saliency(proto_objects_map, box_all) box_all = self._append_boxes_from_meanshift(frame, box_all) if len(self.object_roi) == 0: group_thresh = 0 else: group_thresh = 1 box_grouped, _ = cv2.groupRectangles(box_all, group_thresh, 0.1) self._update_mean_shift_bookkeeping(frame, box_grouped) for (x, y, w, h) in box_grouped: cv2.rectangle(self.tracker, (x, y), (x + w, y + h), (0, 255, 0), 2) return self.tracker
def find(self, haystack_img, threshold=0.5, max_results=10): result = cv.matchTemplate(haystack_img, self.needle_img, self.method) locations = np.where(result >= threshold) locations = list(zip(*locations[::-1])) if not locations: return np.array([], dtype=np.int32).reshape(0, 4) # Remove redundant rectangles (using groupRectangles) after creating a group of rectangles rectangles = [] for loc in locations: rect = [int(loc[0]), int(loc[1]), self.needle_w, self.needle_h] # Add each rectangle twice to retain some non-overlapping rectangles rectangles.append(rect) rectangles.append(rect) rectangles, weights = cv.groupRectangles(rectangles, groupThreshold=1, eps=0.5) # limit number of results found on screen if len(rectangles) > max_results: print('Reached max number of results : ' + str(max_results)) rectangles = rectangles[:max_results] return rectangles
def get_rects(feat, mask, boundary_offset=0): hard_mask = np.round(mask + 0.25) bb = np.copy(feat) for c in range(4): bb[c, :, :] *= hard_mask y_offset = np.array([np.arange(0, 480, 4)]).T y_offset = np.tile(y_offset, (1, 160)) x_offset = np.arange(0, 640, 4) x_offset = np.tile(x_offset, (120, 1)) bb[0, :, :] += x_offset bb[2, :, :] += x_offset bb[1, :, :] += y_offset bb[3, :, :] += y_offset selected_rects = hard_mask > 0 num_rects = np.sum(selected_rects) rects = np.empty((num_rects, 4)) for i in range(4): rects[:, i] = bb[i, selected_rects] + boundary_offset rects = rects[np.logical_and((rects[:, 2] - rects[:, 0]) > 0, (rects[:, 3] - rects[:, 1]) > 0), :] rects[:, (2, 3)] -= rects[:, (0, 1)] rects = np.clip(rects, 0, 640) rects = [rects[i, :] for i in range(rects.shape[0])] rects, scores = cv2.groupRectangles(rects, 2, 0.15) rectangles = [] if len(rects) == 0: return rectangles for i in range(rects.shape[0]): rectangles.append( Rect(rects[i, 0], rects[i, 1], rects[i, 0] + rects[i, 2], rects[i, 1] + rects[i, 3])) return rectangles
def find(self, haystack_img, threshhold=0.35, max_result=5): result = cv.matchTemplate(haystack_img, self.needle_img, self.method) locations = np.where(result >= threshhold) locations = list(zip(*locations[::-1])) if not locations: return np.array([], dtype=np.int32).reshape(0, 4) rectangles = [] for loc in locations: rect = [int(loc[0]), int(loc[1]), self.needle_w, self.needle_h] rectangles.append(rect) rectangles.append(rect) rectangles, weight = cv.groupRectangles(rectangles, groupThreshold=1, eps=0.5) if len(rectangles) > max_result: print('Warning: too many results, raise the threshold') rectangles = rectangles[:max_result] return rectangles
def combine(boxes): # see https://stackoverflow.com/questions/21303374/example-for-opencv-grouprectangle-in-python before remove # the double line! double = boxes.copy() boxes.extend(double) boxes, weights =cv2.groupRectangles(boxes, 1) if type(boxes) is tuple: return [] boxes = boxes.tolist() while True: found = 0 for ra, rb in itertools.combinations(boxes, 2): if intersection(ra, rb): if ra in boxes: boxes.remove(ra) if rb in boxes: boxes.remove(rb) boxes.append((union(ra, rb))) found = 1 break if found == 0: break return boxes
def find_ore_deposit(self, source_img, threshold=0.35): print("here") #source_img = cv2.imread(source_img_path, cv2.IMREAD_UNCHANGED) # object_img = cv2.imread(object_img_path, cv2.IMREAD_UNCHANGED) # object_w = object_img.shape[1] # object_h = object_img.shape[0] # method = cv2.TM_CCOEFF_NORMED result = cv2.matchTemplate(source_img, self.object_img, self.method) locations = np.where(result >= threshold) locations = list(zip(*locations[::-1])) rectangles = [] for loc in locations: rect = [int(loc[0]), int(loc[1]), self.object_w, self.object_h] rectangles.append(rect) rectangles.append(rect) print("first for loop done") rectangles, weights = cv2.groupRectangles(rectangles, 1, 0.5) #print(rectangles) return rectangles
y = 0 while y + size <= h: if size < 100: break x = 0 while x + size <= w: rects.append([x, y, size, size]) x += 10 y += 10 scale *= scaling_factor size = int(size * scaling_factor) if size >= 712: break # Non maximum suppression rects = list(cv2.groupRectangles(rects, 1, 0.5)[0]) index = i + 1 img_name = str(index) + '.jpg' l = np.array(rects).tolist() for box in l: element = {"iname": img_name, "bbox": box} json_list.append(element) output_json = "results.json" with open(output_json, 'w') as f: json.dump(json_lists, f)
def findClickPositions(needle_img_path, haystack_img_path, threshold=0.5, debug_mode=None): # https://docs.opencv.org/4.2.0/d4/da8/group__imgcodecs.html haystack_img = cv.imread(haystack_img_path, cv.IMREAD_UNCHANGED) needle_img = cv.imread(needle_img_path, cv.IMREAD_UNCHANGED) # Save the dimensions of the needle image needle_w = needle_img.shape[1] needle_h = needle_img.shape[0] # There are 6 methods to choose from: # TM_CCOEFF, TM_CCOEFF_NORMED, TM_CCORR, TM_CCORR_NORMED, TM_SQDIFF, TM_SQDIFF_NORMED method = cv.TM_CCOEFF_NORMED result = cv.matchTemplate(haystack_img, needle_img, method) # Get the all the positions from the match result that exceed our threshold locations = np.where(result >= threshold) locations = list(zip(*locations[::-1])) # print(locations) # You'll notice a lot of overlapping rectangles get drawn. We can eliminate those redundant # locations by using groupRectangles(). # First we need to create the list of [x, y, w, h] rectangles rectangles = [] for loc in locations: rect = [int(loc[0]), int(loc[1]), needle_w, needle_h] # Add every box to the list twice in order to retain single (non-overlapping) boxes rectangles.append(rect) rectangles.append(rect) # Apply group rectangles. # The groupThreshold parameter should usually be 1. If you put it at 0 then no grouping is # done. If you put it at 2 then an object needs at least 3 overlapping rectangles to appear # in the result. I've set eps to 0.5, which is: # "Relative difference between sides of the rectangles to merge them into a group." rectangles, weights = cv.groupRectangles(rectangles, groupThreshold=1, eps=0.5) # print(rectangles) points = [] if len(rectangles): #print('Found needle.') line_color = (0, 255, 0) line_type = cv.LINE_4 marker_color = (255, 0, 255) marker_type = cv.MARKER_CROSS # Loop over all the rectangles for (x, y, w, h) in rectangles: # Determine the center position center_x = x + int(w / 2) center_y = y + int(h / 2) # Save the points points.append((center_x, center_y)) if debug_mode == 'rectangles': # Determine the box position top_left = (x, y) bottom_right = (x + w, y + h) # Draw the box cv.rectangle(haystack_img, top_left, bottom_right, color=line_color, lineType=line_type, thickness=2) elif debug_mode == 'points': # Draw the center point cv.drawMarker(haystack_img, (center_x, center_y), color=marker_color, markerType=marker_type, markerSize=40, thickness=2) if debug_mode: cv.imshow('Matches', haystack_img) cv.waitKey() #cv.imwrite('result_click_point.jpg', haystack_img) return points
def detect_bus_color(img,file_name_with_extension): #file_path='D:/mask_positive_front/mask_positive_front/12_20160106_174205_result_3_mask.jpg' path = os.path.abspath('.') #file_name_with_extension = sys.argv[1] file_type = '.jpg' color_dict = { 'orange' : [(7,115,138),(12,217,182)], 'yellow' : [(23,68,94),(43,136,247)], 'blue' : [(102,57,96),(110,168,177)] } #frame = True, cv2.imread(path+'/images_haar/'+file_name_with_extension,1) frame = img hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) for color_name in ['orange','yellow','blue']: #file_name in os.listdir('D:/mask_sample_mini/'):#'D:/mask_positive_front/mask_positive_front/'): print file_name_with_extension color = color_dict[color_name] #file_path=path+'/'+'images_color'+'/'+file_name_with_extension.split('.')[0]+'_'+color+file_type #'D:/mask_positive_front/mask_positive_front/'+file_name im = cv2.inRange(hsv, np.array(color[lower]), np.array(color[upper])) #mask cv2.imwrite(path+'/images_color/'+file_name_with_extension.split('.')[0]+"_"+color_name+".jpg",im) #im = cv2.imread(file_path,1) im = cv2.imread(path+'/images_color/'+file_name_with_extension.split('.')[0]+"_"+color_name+".jpg",1) gray=cv2.cvtColor(im,cv2.COLOR_BGR2GRAY) blur=cv2.GaussianBlur(gray,(5,5),0) ret,binario=cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) kernel = np.ones((5,5),np.uint8) dilation = cv2.dilate(im,kernel,iterations = 1) #opening = cv2.morphologyEx(binario, cv2.MORPH_OPEN, kernel) #closing = cv2.morphologyEx(binario, cv2.MORPH_CLOSE, kernel) #gradient = cv2.morphologyEx(binario, cv2.MORPH_GRADIENT, kernel) #tophat = cv2.morphologyEx(binario, cv2.MORPH_TOPHAT, kernel) #blackhat = cv2.morphologyEx(binario, cv2.MORPH_BLACKHAT, kernel) bordes=cv2.Canny(dilation,100,100) contours,hierarchy = cv2.findContours(bordes,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE) #""" thresh = 1 #rectList, weights = cv2.groupRectangles(contours, thresh) # eps is optional rectangles=[] for cnt in contours: #cv2.imwrite(file_name.split('.')[0]+"_shape_contour.jpg",cnt) x,y,w,h = cv2.boundingRect(cnt) rectangles.append([x,y,w,h]) rectangles.append([x,y,w,h]) print w*h #if w*h<100: continue #cv2.rectangle(im,(x,y),(x+w,y+h),(200,0,0),2) #""" #cv2.imshow('img',im) #cv2.drawContours(im, contours, -1, (0,255,0), 3) rectList, weights = cv2.groupRectangles(rectangles, 1,1.175) print '----' for rect in rectList: x,y,w,h=rect print w*h if w*h<1000: continue cv2.rectangle(im,(x,y),(x+w,y+h),(200,0,0),2) cv2.imwrite(path+'/images_color/'+file_name_with_extension.split('.')[0]+"_"+color_name+"_"+"_dilate.jpg",im) return True return False
def reduce_boxes(boxes): if len(boxes) == 0: return [] reduced_boxes = cv2.groupRectangles( [list(b) for b in itertools.chain(boxes, boxes)], 1, 0.2)[0] return [tuple(b) for b in reduced_boxes]
def find(self, threshold=0.5, debug_mode=None, method=cv2.TM_CCOEFF_NORMED): points = [] haystack_img = self.haystack_img count = 0 for needle_img in self.needle_img_ar: needle_w = needle_img.shape[1] needle_h = needle_img.shape[0] # run the OpenCV algorithm result = cv2.matchTemplate(self.haystack_img, needle_img, method) # Get the all the positions from the match result that exceed our threshold locations = np.where(result >= threshold) locations = list(zip(*locations[::-1])) # print(locations) rectangles = [] for loc in locations: rect = [int(loc[0]), int(loc[1]), needle_w, needle_h] # Add every box to the list twice in order to retain single (non-overlapping) boxes rectangles.append(rect) rectangles.append(rect) # Stolen from: https://github.com/learncodebygaming/opencv_tutorials/tree/master/005_real_time # Apply group rectangles. # The groupThreshold parameter should usually be 1. If you put it at 0 then no grouping is # done. If you put it at 2 then an object needs at least 3 overlapping rectangles to appear # in the result. I've set eps to 0.5, which is: # "Relative difference between sides of the rectangles to merge them into a group." rectangles, weights = cv2.groupRectangles( rectangles, groupThreshold=1, eps=0.5) if len(rectangles): #print('Found needle.') line_color = (count*155, 255, 0) line_type = cv2.LINE_4 marker_color = (255, 0, 255) marker_type = cv2.MARKER_CROSS # Loop over all the rectangles for (x, y, w, h) in rectangles: # Determine the center position center_x = x + int(w/2) center_y = y + int(h/2) points.append((center_x, center_y)) if debug_mode == 'rectangles': # Determine the box position top_left = (x, y) bottom_right = (x + w, y + h) haystack_img = cv2.rectangle(haystack_img, top_left, bottom_right, color=line_color, lineType=line_type, thickness=2) elif debug_mode == 'points': haystack_img = cv2.drawMarker(haystack_img, (center_x, center_y), color=marker_color, markerType=marker_type, markerSize=40, thickness=2) count += 1 self.points = points self.food_vis_img = haystack_img
def callback(self, data): pts = [] if not self.cam: return if self.weedtype is None: self.image_pub.publish(data) return 0 try: cv_image = self.bridge.imgmsg_to_cv2(data, "bgr8") except CvBridgeError as e: print(e) # The code has been divided into three steps, so that it can be furthur improvd for each kind of weeds individually # Like applying color sorting techniques # filtering # morphological transformations # for first row of simple weeds if self.weedtype == 'simple': hsv = cv2.cvtColor(cv_image, cv2.COLOR_BGR2HSV) hsv = cv2.GaussianBlur(hsv, ksize=(17, 17), sigmaX=10) lower_filter = np.array([40, 0, 0]) upper_filter = np.array([180, 120, 255]) mask = cv2.inRange(hsv, lower_filter, upper_filter) res = cv2.bitwise_and(cv_image, cv_image, mask=mask) gray_res = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY) ret, edged = cv2.threshold(gray_res, 10, 255, 0) im2, contours, hierarchy = cv2.findContours( edged, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) print(contours) cv2.drawContours(cv_image, contours, -1, (0, 255, 0), 3) threshold_area = 500 filtered_contours = [] for count in contours: area = cv2.contourArea(count) print(area) if area > threshold_area: filtered_contours.append(count) cv2.drawContours(cv_image, filtered_contours, -1, (0, 255, 0), 3) rects = [] for count in filtered_contours: x, y, w, h = cv2.boundingRect(count) rects.append([x, y, w, h]) rects.append([x, y, w, h]) # cv2.rectangle(cv_image,(x,y),(x+w,y+h),(0,255,0),8) contours_points = [] contours_boxes, weights = cv2.groupRectangles(rects, 1, 0.2) for rect in contours_boxes: middle = (x + w / 2, y + h / 2) if self.cam: xa, ya, za = self.cam.projectPixelTo3dRay(middle) print(xa, ya, za) pts.append(Point32(xa / 2, ya / 2, za / 2)) contours_points.append(middle) cv2.circle(cv_image, middle, 7, (255, 255, 255), -1) x, y, w, h = rect cv2.rectangle(cv_image, (x, y), (x + w, y + h), (255, 0, 0), 4) cv_image_s = cv2.resize(cv_image, (0, 0), fx=0.5, fy=0.5) self.image_pub.publish( self.bridge.cv2_to_imgmsg(cv_image_s, encoding="bgr8")) cv2.imshow('cv_image', cv_image) # cv2.imshow('Contours', cv_image_s) # cv2.imshow('mask', mask) cv2.waitKey(1) # publishing 3d pointcloud(x, y, z coordinates) time = rospy.Time(0) self.point_msg.points = pts self.point_msg.header.frame_id = 'map' self.point_msg.header.stamp = time self.point_pub.publish(self.point_msg) # for second row of realeasy weed elif self.weedtype == 'realeasy': hsv = cv2.cvtColor(cv_image, cv2.COLOR_BGR2HSV) hsv = cv2.blur(hsv, (40, 40)) hsv = cv2.GaussianBlur(hsv, ksize=(17, 17), sigmaX=10) lower_filter = np.array([30, 30, 0]) upper_filter = np.array([130, 80, 40]) mask = cv2.inRange(hsv, lower_filter, upper_filter) res = cv2.bitwise_and(cv_image, cv_image, mask=mask) gray_res = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY) ret, edged = cv2.threshold(gray_res, 10, 255, 0) im2, contours, hierarchy = cv2.findContours( edged, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) cv2.drawContours(cv_image, contours, -1, (0, 255, 0), 3) threshold_area = 500 filtered_contours = [] for count in contours: area = cv2.contourArea(count) print(area) if area > threshold_area: filtered_contours.append(count) cv2.drawContours(cv_image, filtered_contours, -1, (0, 255, 0), 3) rects = [] for count in filtered_contours: x, y, w, h = cv2.boundingRect(count) rects.append([x, y, w, h]) rects.append([x, y, w, h]) # cv2.rectangle(cv_image,(x,y),(x+w,y+h),(0,255,0),8) contours_points = [] contours_boxes, weights = cv2.groupRectangles(rects, 1, 0.2) for rect in contours_boxes: middle = (x + w / 2, y + h / 2) if self.cam: xa, ya, za = self.cam.projectPixelTo3dRay(middle) print(xa, ya, za) pts.append(Point32(xa / 2, ya / 2, za / 2)) contours_points.append(middle) cv2.circle(cv_image, middle, 7, (255, 255, 255), -1) x, y, w, h = rect cv2.rectangle(cv_image, (x, y), (x + w, y + h), (255, 0, 0), 4) cv_image_s = cv2.resize(cv_image, (0, 0), fx=0.5, fy=0.5) self.image_pub.publish( self.bridge.cv2_to_imgmsg(cv_image_s, encoding="bgr8")) cv2.imshow('cv_image', cv_image) # cv2.imshow('Contours', cv_image_s) # cv2.imshow('mask', mask) cv2.waitKey(1) # publishing 3d pointcloud(x, y, z coordinates) time = rospy.Time(0) self.point_msg.points = pts self.point_msg.header.frame_id = 'map' self.point_msg.header.stamp = time self.point_pub.publish(self.point_msg) # for third row of realhard weed elif self.weedtype == 'realhard': hsv = cv2.cvtColor(cv_image, cv2.COLOR_BGR2HSV) hsv = cv2.blur(hsv, (10, 10)) hsv = cv2.GaussianBlur(hsv, ksize=(17, 17), sigmaX=10) lower_filter = np.array([0, 90, 0]) upper_filter = np.array([255, 100, 255]) mask = cv2.inRange(hsv, lower_filter, upper_filter) res = cv2.bitwise_and(cv_image, cv_image, mask=mask) gray_res = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY) ret, edged = cv2.threshold(gray_res, 10, 255, 0) im2, contours, hierarchy = cv2.findContours( edged, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) cv2.drawContours(cv_image, contours, -1, (0, 255, 0), 3) threshold_area = 500 filtered_contours = [] for count in contours: area = cv2.contourArea(count) print(area) if area > threshold_area: filtered_contours.append(count) cv2.drawContours(cv_image, filtered_contours, -1, (0, 255, 0), 3) rects = [] for count in filtered_contours: x, y, w, h = cv2.boundingRect(count) rects.append([x, y, w, h]) rects.append([x, y, w, h]) # cv2.rectangle(cv_image,(x,y),(x+w,y+h),(0,255,0),8) contours_points = [] contours_boxes, weights = cv2.groupRectangles(rects, 1, 0.2) for rect in contours_boxes: middle = (x + w / 2, y + h / 2) if self.cam: xa, ya, za = self.cam.projectPixelTo3dRay(middle) print(xa, ya, za) pts.append(Point32(xa / 2, ya / 2, za / 2)) contours_points.append(middle) cv2.circle(cv_image, middle, 7, (255, 255, 255), -1) x, y, w, h = rect cv2.rectangle(cv_image, (x, y), (x + w, y + h), (255, 0, 0), 4) cv_image_s = cv2.resize(cv_image, (0, 0), fx=0.5, fy=0.5) self.image_pub.publish( self.bridge.cv2_to_imgmsg(cv_image_s, encoding="bgr8")) cv2.imshow('cv_image', cv_image) # cv2.imshow('Contours', cv_image_s) # cv2.imshow('mask', mask) cv2.waitKey(1) # publishing 3d pointcloud(x, y, z coordinates) time = rospy.Time(0) self.point_msg.points = pts self.point_msg.header.frame_id = 'map' self.point_msg.header.stamp = time self.point_pub.publish(self.point_msg)
def group_bboxes(bboxes): grouped_rectangles = cv2.groupRectangles(bboxes, 1, eps=0.05)[0] # remove rectangles with small area grouped_rectangles = [r for r in grouped_rectangles if get_rectangle_area(r) > 200] return grouped_rectangles
print label_rects t = time.time() positives = detector.detect(image, container_rect) print time.time() - t print len(positives) plot_rects_on_image(image, [container_rect]+positives, ['red', 'green'], thickness=1) print positives detector.set_classifier(classifier2) t = time.time() positives = detector.detect(image, container_rect, windows=positives) print positives print time.time() - t t = time.time() print len(positives) plot_rects_on_image(image, [container_rect]+positives, ['red', 'green'], thickness=2) positives = cv2.groupRectangles(positives, 1) np = [] for p in positives[0]: print '1',p x, y, w, h = p np.append((x, y, w, h)) positives = np print positives plot_rects_on_image(image, [container_rect]+list(positives), ['red', 'green'], thickness=2) exit()
def reduceRectanglesList(self, listOfRectangles, threshold, eps=0.2): return cv2.groupRectangles(listOfRectangles, threshold, eps)
sumlbp[0]=sumlbp[0]+x sumlbp[1]=sumlbp[1]+y cv2.rectangle(img,(x,y),(x+w,y+h),(255,255,255),1) print "white is lbp individual cascades" for (x,y,h,w) in mixedresultshaar: sumhaar[0]=sumhaar[0]+x sumhaar[1]=sumhaar[1]+y cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,255),1) print "yellow is haar individual cascades" sumlbp[0]=sumlbp[0]/len(mixedresultslbp) sumlbp[1]=sumlbp[1]/len(mixedresultslbp) sumhaar[0]=sumhaar[0]/len(mixedresultshaar) sumhaar[1]=sumhaar[1]/len(mixedresultshaar) (locations,weight)=cv2.groupRectangles(mixedresultslbp,5) for (x,y,h,w) in locations: cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2) cv2.circle(img,(x,y),10,(0,255,0),2) print str(sp[1]/2)+", "+str(sp[0]/2)+" vs "+str(x+w/2)+", "+str(y+h/2) #locations=tuple([sumlbp[0],sumlbp[1],max(mixedresultslbp[:][2]),max(mixedresultslbp[:][3])]) cv2.circle(img,(sumlbp[0],sumlbp[1]),15,(0,255,0),5) #cv2.rectangle(img,(locations[0],locations[1]),(locations[0]+locations[2],locations[1]+locations[3]),(0,255,0),2) print "green is clustered lbp cascades" i=19 (locations,weight)=cv2.groupRectangles(mixedresultshaar,i) while len(locations)==0: i=i-1 (locations,weight)=cv2.groupRectangles(mixedresultshaar,i) #locations=tuple([sumhaar[0],sumhaar[1],max(mixedresultshaar[:][2]),max(mixedresultshaar[:][3])])
yield (x, y, image[y:y + windowSize[1], x:x + windowSize[0],:]) all_rec = [] for (x, y, window) in sliding_window(image, stepSize=32, windowSize=(winW, winH)): if window.shape[0] != winH or window.shape[1] != winW: continue img=np.reshape(window,(1,100,100,3)) y_predict = model.predict(np.array(img,dtype=np.float32)) label = np.argmax(y_predict) if label!= 3: all_rec.append([x,y,x+winW,y+winH]) print(np.array(all_rec).shape) gr_rect= cv2.groupRectangles(all_rec,1,0.4) print(np.array(all_rec).shape) print(np.array(gr_rect[0]).shape) while(True): for i in range(np.array(gr_rect[0]).shape[0]): cv2.imshow("Window", image) cv2.rectangle(image, (gr_rect[0][i][0], gr_rect[0][i][1]), (gr_rect[0][i][2], gr_rect[0][i][3]), (0, 255, 0), 2) if cv2.waitKey(1) & 0xFF == ord('q'): break cv2.destroyAllWindows() gr_rect= cv2.groupRectangles(all_rec,1,0.1) print(np.array(all_rec).shape) print(np.array(gr_rect[0]).shape)
rmv_box.sort(reverse=True) return list, rmv_box #Dibujo de los rectangulos originales for i in range(0, len(list_box)): box_data = list_box[i] x1_draw = box_data[0] x2_draw = box_data[1] y1_draw = box_data[2] y2_draw = box_data[3] cv2.rectangle(Imagergb, (x1_draw, y1_draw), (x2_draw, y2_draw), (0, 255, 0), 1) #Dibujo de los rectangulos obtenidos por el metodo de clustering rectList, weights = cv2.groupRectangles(list_box, 1, 0.2) for i in range(0, len(rectList)): box_data = rectList[i] x1_draw = box_data[0] x2_draw = box_data[1] y1_draw = box_data[2] y2_draw = box_data[3] cv2.rectangle(Imagergb, (x1_draw, y1_draw), (x2_draw, y2_draw), (255, 0, 0), 2) #Dibujo de los rectangulos originales k, indxs = 0, len(list_box) while k < indxs: list_box, remv_box = iou_rectangle_merge(list_box, k, 0.2) for index in remv_box: del list_box[index]
IMAGEDIR = "/home/james/Code/faceevaluation/facesInTheWild/" cv2.namedWindow("win") cv2.startWindowThread() fd = cv2.CascadeClassifier("/home/james/Code/webcv/scripts/lbpcascade_frontalface.xml") for fold in folddata: outfile = open(fold + "-out.txt", 'w') print fold for image in folddata[fold]: print image outfile.write(image + "\n") im = cv2.imread(IMAGEDIR + image + ".jpg") rawrects = fd.detectMultiScale(im, 1.2, 0, 0) rects, weights = cv2.groupRectangles(np.array(rawrects).tolist(), 1) print "nrects %d" % len(rects) outfile.write("%d\n" % len(rects)) for i, r in enumerate(rects): weight = weights[i] rectline = "%f %f %f %f %d\n" % (r[0], r[1], r[2], r[3], weight) print rectline outfile.write(rectline) p1 = (r[0], r[1]) p2 = (r[0]+r[2], r[1]+r[3]) cv2.rectangle(im, p1, p2, (0,0,255)) cv2.imshow("win", im) outfile.close()
def getText(img,arg2,arg3): y = np.shape(img)[0] x = np.shape(img)[1] #negate document img = cv2.resize(img,(0,0),fx=1,fy=1) img = 255-img #create copy of image vis2 = img.copy() #dilate image kernel = np.ones((20,0),np.uint(8)) vis2 = cv2.dilate(vis2,kernel,iterations=21) cv2.imshow('Dilated Image.',vis2) #scipy.misc.imsave('imgDilated'+'.png',vis2) cv2.waitKey(0) #find mser regions for big rectangles in dilated image #10k is min area #max area = 0.5 of area of image #min area = 0.01 area of image mser = cv2.MSER_create(1, int(0.01*y*x), int(0.5 * y*x), 0.05, 0.02, 200, 1.01, 0.003, 5) regions = mser.detectRegions(vis2, None) #store the big rectangles detected in rectList2 rectList2 = [] for i in range(len(regions)): x,y,w,h = cv2.boundingRect(regions[i]) if(w*h>int(arg3)): rectList2.append([x,y,w,h]) #group the rectangles to pair similar rectangle rectList2,weights = cv2.groupRectangles(rectList2,int(arg2))#,float(arg3)) WordRectangleArray = [] #create copy of original image vis = img.copy() vis = cv2.cvtColor(vis,cv2.COLOR_GRAY2BGR) #draw the rectangles on vis for i in range(len(rectList2)): x = rectList2[i][0] y = rectList2[i][1] w = rectList2[i][2] h = rectList2[i][3] cv2.rectangle(vis,(x,y),(x+w,y+h),(255,0,0),2) WordRectangleArray.append(WordRectangle(x,y,w,h)) #have big rectangles #find small rectangles for i in range(len(rectList2)): mser = cv2.MSER_create(1, 0, 94400, 0.05, 0.02, 200, 1.01, 0.003, 5) x0 = rectList2[i][0] y0 = rectList2[i][1] w0 = rectList2[i][2] h0 = rectList2[i][3] WordRectangleArray[i].regions = mser.detectRegions(img[y0:y0+h0,x0:x0+w0], None) rectList = [] for j in range(len( WordRectangleArray[i].regions)): x,y,w,h = cv2.boundingRect( WordRectangleArray[i].regions[j]) #0.9 added to remove the big rectangle if(w*h>int(arg3) and w*h<0.9*w0*h0): rectList.append([x0+x,y0+y,w,h]) rectList.append([x0+x,y0+y,w,h]) rectList,weights = cv2.groupRectangles(rectList,int(arg2),0.1) avg = 0 sums = 0 for k in range(len(rectList)): sums+= rectList[k][2]*rectList[k][3] if(len(rectList)>0): avg = sums/len(rectList) rectList = filter(lambda x: x[2]*x[3]>0.8*avg ,rectList) rectList = sorted(rectList,key=itemgetter(0)) WordRectangleArray[i].characterRectangle = rectList def compare(x,y): if(x<0.9*y): return -1 elif(x>0.9*y): return 1 else: return 0 #modified sort that allows for variance WordRectangleArray = sorted(WordRectangleArray,cmp=compare,key=(operator.attrgetter('y'))) #no need to sort in x, already sorted count=0 for i in range(len(WordRectangleArray)): x0 = WordRectangleArray[i].x y0 = WordRectangleArray[i].y w0 = WordRectangleArray[i].w h0 = WordRectangleArray[i].h tmp = img[y0:y0+h0,x0:x0+w0] for j in range(len(WordRectangleArray[i].characterRectangle)): count+=1 x = WordRectangleArray[i].characterRectangle[j][0] y = WordRectangleArray[i].characterRectangle[j][1] w = WordRectangleArray[i].characterRectangle[j][2] h = WordRectangleArray[i].characterRectangle[j][3] cv2.rectangle(vis,(x,y),(x+w,y+h),(255,0,0),2) cv2.putText(vis,str(count),(x,y),font, 1,(255,255,255),2,cv2.LINE_AA) cv2.imshow('imgWithTextBoxes', vis) cv2.waitKey(0) #scipy.misc.imsave('imgWithTextBoxes'+'.png',vis) #cv2.destroyAllWindows() return WordRectangleArray
def main(inference_type: str = "K", batch_size: int = 1, test_path: str = None, weights: str = None, merge: bool = False, stage: str = "test", limit: int = 20, confidence: float = 0.1, visualize: bool = True): keras_model = MobileDetectNetModel.complete_model() if weights is not None: keras_model.load_weights(weights, by_name=True) images_done = 0 if test_path is not None: import cv2 if stage != 'test': from generator import MobileDetectNetSequence seq = MobileDetectNetSequence.create_augmenter(stage) else: seq = None images_full = [] images_input = [] images_scale = [] for r, d, f in os.walk(test_path): for file in f: image_full = cv2.imread(os.path.join(r, file)) image_input = cv2.resize(image_full, (224, 224)) scale_width = image_full.shape[1] / 224 scale_height = image_full.shape[0] / 224 images_scale.append((scale_width, scale_height)) if stage != 'test': seq_det = seq.to_deterministic() image_aug = (seq_det.augment_image(image_input).astype(np.float32) / 127.5) - 1. else: image_aug = image_input.astype(np.float32) / 127.5 - 1. images_full.append(image_full) images_input.append(image_aug) images_done += 1 if images_done == limit: break if images_done == limit: break x_test = np.array(images_input) else: x_test = np.random.random((limit, 224, 224, 3)) x_cold = np.random.random((batch_size, 224, 224, 3)) if inference_type == 'K': keras_model.predict(x_cold) t0 = time.time() model_outputs = keras_model.predict(x_test) t1 = time.time() elif inference_type == 'TF': tf_engine = keras_model.tf_engine() tf_engine.infer(x_cold) t0 = time.time() model_outputs = tf_engine.infer(x_test) t1 = time.time() elif inference_type == 'FP32': tftrt_engine = keras_model.tftrt_engine(precision='FP32', batch_size=batch_size) tftrt_engine.infer(x_cold) t0 = time.time() model_outputs = tftrt_engine.infer(x_test) t1 = time.time() elif inference_type == 'FP16': tftrt_engine = keras_model.tftrt_engine(precision='FP16', batch_size=batch_size) tftrt_engine.infer(x_cold) t0 = time.time() model_outputs = tftrt_engine.infer(x_test) t1 = time.time() elif inference_type == 'INT8': tftrt_engine = keras_model.tftrt_engine(precision='INT8', batch_size=batch_size) tftrt_engine.infer(x_cold) t0 = time.time() model_outputs = tftrt_engine.infer(x_test) t1 = time.time() else: raise ValueError("Invalid inference type") print('Time: ', t1 - t0) print('FPS: ', x_test.shape[0]/(t1 - t0)) if not visualize: return if len(model_outputs) == 2: classes, bboxes = model_outputs # TF / TensorRT models won't output regions (not useful for production) elif len(model_outputs) == 3: regions, bboxes, classes = model_outputs else: raise ValueError("Invalid model length output") if test_path is not None: import matplotlib.pyplot as plt from matplotlib.colors import LinearSegmentedColormap # get colormap ncolors = 256 color_array = plt.get_cmap('viridis')(range(ncolors)) # change alpha values color_array[:, -1] = np.linspace(0.0, 1.0, ncolors) # create a colormap object map_object = LinearSegmentedColormap.from_list(name='viridis_alpha', colors=color_array) # register this new colormap with matplotlib plt.register_cmap(cmap=map_object) for idx in range(0, len(images_full)): rectangles = [] for y in range(0, 7): for x in range(0, 7): if classes[idx, y, x, 0] >= confidence: rect = [ int(bboxes[idx, int(y), int(x), 0] * 224), int(bboxes[idx, int(y), int(x), 1] * 224), int(bboxes[idx, int(y), int(x), 2] * 224), int(bboxes[idx, int(y), int(x), 3] * 224)] rectangles.append(rect) if merge: rectangles, merges = cv2.groupRectangles(rectangles, 1, eps=0.75) scale_width, scale_height = images_scale[idx] for rect in rectangles: cv2.rectangle(images_full[idx], (int(rect[0]*scale_width), int(rect[1]*scale_height)), (int(rect[2]*scale_width), int(rect[3]*scale_height)), (0, 255, 0), 5) plt.imshow(cv2.cvtColor(images_full[idx], cv2.COLOR_BGR2RGB), alpha=1.0, aspect='auto') plt.imshow( cv2.resize(classes[idx].reshape((7, 7)), (images_full[idx].shape[1], images_full[idx].shape[0])), interpolation='nearest', alpha=0.5, cmap='viridis_alpha', aspect='auto') plt.show()
def callback(self, data): pts = [] if not self.cam: return if self.inputimage is None: self.image_pub.publish(data) return 0 # project a point in camera coordinates into the pixel coordinates # uv = self.camera_model.project3dToPixel((0, 0, 0.5)) # print('Running with image: {}'.format(self.inputimage)) # print 'Pixel coordinates: ', uv # print '' try: cv_image = self.bridge.imgmsg_to_cv2(data, "bgr8") except CvBridgeError as e: print(e) if self.inputimage == 'simple': hsv = cv2.cvtColor(cv_image, cv2.COLOR_BGR2HSV) hsv = cv2.GaussianBlur(hsv, ksize=(17, 17), sigmaX=10) lower_filter = np.array([40, 0, 0]) upper_filter = np.array([180, 120, 255]) mask = cv2.inRange(hsv, lower_filter, upper_filter) res = cv2.bitwise_and(cv_image, cv_image, mask=mask) gray_res = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY) ret, edged = cv2.threshold(gray_res, 10, 255, 0) im2, contours, hierarchy = cv2.findContours(edged, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) print (contours) cv2.drawContours(cv_image, contours, -1, (0, 255, 0), 3) threshold_area = 500 filtered_contours = [] for count in contours: area = cv2.contourArea(count) print(area) if area > threshold_area: filtered_contours.append(count) cv2.drawContours(cv_image, filtered_contours, -1, (0, 255, 0), 3) rects=[] for count in filtered_contours: x, y, w, h = cv2.boundingRect(count) rects.append([x, y, w, h]) rects.append([x, y, w, h]) cv2.rectangle(cv_image,(x,y),(x+w,y+h),(0,255,0),8) contours_points=[] contours_boxes, weights = cv2.groupRectangles(rects, 1, 0.2) for rect in contours_boxes: middle = (x + w / 2, y + h / 2) if self.cam: xa, ya, za = self.cam.projectPixelTo3dRay(middle) print (xa, ya, za) pts.append(Point32(xa/2, ya/2, za/2)) contours_points.append(middle) cv2.circle(cv_image, middle, 7, (255, 255, 255), -1) x, y, w, h = rect cv2.rectangle(cv_image, (x, y), (x + w, y + h), (255, 0, 0), 4) cv_image_s = cv2.resize(cv_image, (0, 0), fx=0.5, fy=0.5) self.image_pub.publish(self.bridge.cv2_to_imgmsg(cv_image_s, encoding="bgr8")) cv2.imshow('cv_image', cv_image) # cv2.imshow('Contours', cv_image_s) # cv2.imshow('mask', mask) cv2.waitKey(1) time = rospy.Time(0) self.point_msg.points = pts # self.point_msg.header.frame_id = self.cam.tfFrame() self.point_msg.header.frame_id = 'map' self.point_msg.header.stamp = time # tf_points = self.tflistener.transformPointCloud('map', self.points_msg) # self.point_pub.publish(tf_points) self.point_pub.publish(self.point_msg) elif self.inputimage == 'realeasy': hsv = cv2.cvtColor(cv_image, cv2.COLOR_BGR2HSV) hsv = cv2.blur(hsv, (40, 40)) hsv = cv2.GaussianBlur(hsv, ksize=(17, 17), sigmaX=10) lower_filter = np.array([30, 30, 0]) upper_filter = np.array([130, 80, 40]) mask = cv2.inRange(hsv, lower_filter, upper_filter) res = cv2.bitwise_and(cv_image, cv_image, mask=mask) gray_res = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY) ret, edged = cv2.threshold(gray_res, 10, 255, 0) im2, contours, hierarchy = cv2.findContours(edged, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) cv2.drawContours(cv_image, contours, -1, (0, 255, 0), 3) threshold_area = 500 filtered_contours = [] for count in contours: area = cv2.contourArea(count) print(area) if area > threshold_area: filtered_contours.append(count) cv2.drawContours(cv_image, filtered_contours, -1, (0, 255, 0), 3) rects=[] for count in filtered_contours: x, y, w, h = cv2.boundingRect(count) rects.append([x, y, w, h]) rects.append([x, y, w, h]) cv2.rectangle(cv_image,(x,y),(x+w,y+h),(0,255,0),8) contours_points=[] contours_boxes, weights = cv2.groupRectangles(rects, 1, 0.2) for rect in contours_boxes: middle = (x + w / 2, y + h / 2) if self.cam: xa, ya, za = self.cam.projectPixelTo3dRay(middle) print (xa, ya, za) pts.append(Point32(xa/2, ya/2, za/2)) contours_points.append(middle) cv2.circle(cv_image, middle, 7, (255, 255, 255), -1) x, y, w, h = rect cv2.rectangle(cv_image, (x, y), (x + w, y + h), (255, 0, 0), 4) cv_image_s = cv2.resize(cv_image, (0, 0), fx=0.5, fy=0.5) self.image_pub.publish(self.bridge.cv2_to_imgmsg(cv_image_s, encoding="bgr8")) cv2.imshow('cv_image', cv_image) # cv2.imshow('Contours', cv_image_s) # cv2.imshow('mask', mask) cv2.waitKey(1) elif self.inputimage == 'realhard': hsv = cv2.cvtColor(cv_image, cv2.COLOR_BGR2HSV) hsv = cv2.blur(hsv, (10, 10)) hsv = cv2.GaussianBlur(hsv, ksize=(17, 17), sigmaX=10) lower_filter = np.array([0, 90, 0]) upper_filter = np.array([255, 100, 255]) mask = cv2.inRange(hsv, lower_filter, upper_filter) res = cv2.bitwise_and(cv_image, cv_image, mask=mask) gray_res = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY) ret, edged = cv2.threshold(gray_res, 10, 255, 0) im2, contours, hierarchy = cv2.findContours(edged, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) cv2.drawContours(cv_image, contours, -1, (0, 255, 0), 3) threshold_area = 500 filtered_contours = [] for count in contours: area = cv2.contourArea(count) print(area) if area > threshold_area: filtered_contours.append(count) cv2.drawContours(cv_image, filtered_contours, -1, (0, 255, 0), 3) rects=[] for count in filtered_contours: x, y, w, h = cv2.boundingRect(count) rects.append([x, y, w, h]) rects.append([x, y, w, h]) cv2.rectangle(cv_image,(x,y),(x+w,y+h),(0,255,0),8) contours_points=[] contours_boxes, weights = cv2.groupRectangles(rects, 1, 0.2) for rect in contours_boxes: middle = (x + w / 2, y + h / 2) if self.cam: xa, ya, za = self.cam.projectPixelTo3dRay(middle) print (xa, ya, za) pts.append(Point32(xa/2, ya/2, za/2)) contours_points.append(middle) cv2.circle(cv_image, middle, 7, (255, 255, 255), -1) x, y, w, h = rect cv2.rectangle(cv_image, (x, y), (x + w, y + h), (255, 0, 0), 4) cv_image_s = cv2.resize(cv_image, (0, 0), fx=0.5, fy=0.5) self.image_pub.publish(self.bridge.cv2_to_imgmsg(cv_image_s, encoding="bgr8")) cv2.imshow('cv_image', cv_image) # cv2.imshow('Contours', cv_image_s) # cv2.imshow('mask', mask) cv2.waitKey(1)
bBoxes = hotPoints mask = sampleMask for box in bBoxes: topY = box[0][1] bottomY = box[1][1] leftX = box[0][0] rightX = box[1][0] mask[topY:bottomY, leftX:rightX] += 1 mask = np.clip(mask, 0, 255) #print('haalaa2') boxes, _ = cv2.groupRectangles( rectList=np.array(vehicleBoxesHistory).tolist(), groupThreshold=gdroupThr, eps=groupDiff) img = frame drawBoxes(img, boxes) cv2.imshow('box', img) # Press Q on keyboard to exit if cv2.waitKey(25) & 0xFF == ord('q'): break # Break the loop else: break # When everything done, release the video capture object cap.release()
def agrupar_rec_humo(reconocedores): grupos, pesos = cv2.groupRectangles(np.array(reconocedores).tolist(),1,0.2) return grupos
gt = 1 eps = 0.8 use_aux = True for t in range(num_trials): opencv_gr = [] my_gr = [] for r in range(num_rectangles): e = np.array([ random.random() * arena_size, random.random() * arena_size, random.random() * square_size, random.random() * square_size]) ec = np.concatenate([e, np.array([random.random(), random.random()])]) opencv_gr.append(e) my_gr.append(ec if use_aux else e) output1, scores1 = cv2.groupRectangles(opencv_gr, gt, eps) output2, aux2, scores2 = group_rectangles_with_aux.execute(my_gr, gt, eps) if len(output1) == 0: assert len(output2) == 0 else: try: assert (output1 == output2).all() except: print 'opencv' print output1 print 'mine' print output2 print 'diff' print output1.shape, output2.shape print output1 - output2 raise
is_convex = cv2.isContourConvex(approx) cv2.drawContours(temp_img, approx, -1, (0, 255, 255), 12) # cv2.imshow(f"contour {i}, sides {len(approx)}, convex {is_convex}", temp_img) # cv2.waitKey(1) ### drawing bboxes ### # draw bboxes of all contours in green bboxes = utils.generate_bboxes(contours) print("all bboxes:", bboxes) display_utils.draw_bboxes(target_img, bboxes, (0, 255, 0), 2) # draw common bboxes of contours in blue # the groupRectangles doesn't work well, my function works better common_bboxes, weights = cv2.groupRectangles(bboxes, 1, 0.2) print("common bboxes:", common_bboxes) display_utils.draw_bboxes(target_img, common_bboxes, (255, 0, 0), 2) # draw iou common bboxes of contours in white iou_bboxes = utils.get_distinct_bboxes(bboxes, 0.8) print("iou bboxes:", common_bboxes) display_utils.draw_bboxes(target_img, iou_bboxes, (255, 255, 255), 2) ### drawing centeriods ### # draw centeriods of contours in red contour_centeriods = utils.get_contour_centers(contours) display_utils.draw_circles(target_img, contour_centeriods, color=(0, 0, 255))
def detect(self, image): max_scale = int(min(image.shape) / (2 * 24)) print('max scale: {}'.format(max_scale)) self.scales = [i for i in range(3, max_scale + 1, 2)] cur = time.time() img_integral = integral(image)[1:-1, 1:-1] print('compute integral using: {}s'.format(time.time() - cur)) y, x = image.shape bounding_boxes = [] for scale in self.scales: scale_boxes = [] num_subset = [] for move_y in range(0, y - self.wnd_y * scale, self.shift * scale): for move_x in range(0, x - self.wnd_x * scale, self.shift * scale): wnd_integral = np.pad( img_integral[move_y:move_y + self.wnd_y * scale, move_x:move_x + self.wnd_x * scale], 1, mode='constant', constant_values=0).astype(np.int) if self.cascade_cls.classify(wnd_integral, scale, self.kernels): # print('detected, scale: {}'.format(scale)) # print(move_x, move_y, self.wnd_x, self.wnd_y, scale) if len(scale_boxes) == 0: # print('*' * 10, 'add new bbox', '*' * 10) # scale_boxes.append([move_x, move_y, self.wnd_x, self.wnd_y, scale]) scale_boxes.append([ move_x, move_y, move_x + self.wnd_x * scale, move_y + self.wnd_y * scale ]) num_subset.append(1) else: add = True # for i, box in enumerate(scale_boxes): # if abs(box[0] - move_x) <= 0.9 * self.wnd_x * box[4] and abs( # box[1] - move_y) <= 0.9 * self.wnd_y * box[4]: # scale_boxes[i][0] = int((box[0] * num_subset[i] + move_x) / (num_subset[i] + 1)) # scale_boxes[i][1] = int((box[1] * num_subset[i] + move_y) / (num_subset[i] + 1)) # num_subset[i] += 1 # add = False # break if add: # print('*' * 10, 'add new bbox', '*' * 10) # scale_boxes.append([move_x, move_y, self.wnd_x, self.wnd_y, scale]) scale_boxes.append([ move_x, move_y, move_x + self.wnd_x * scale, move_y + self.wnd_y * scale ]) num_subset.append(1) tmp, _ = cv2.groupRectangles(scale_boxes, 2, 0.5) scale_boxes = [] for t in tmp: scale_boxes.append([ int(round(t[0])), int(round(t[1])), self.wnd_x, self.wnd_y, ((t[2] - t[0]) / self.wnd_x + (t[3] - t[1]) / self.wnd_y) / 2 ]) print('*' * 10, 'Scale: {}, bbox: {}'.format(scale, len(scale_boxes)), '*' * 10) if len(bounding_boxes) == 0: bounding_boxes = bounding_boxes + scale_boxes else: # merge variant scale bbox deleted_small = 0 for i, new_box in enumerate(scale_boxes): j = 0 while j < len(bounding_boxes): box = bounding_boxes[j] if is_overlap(box, new_box, 24) >= 0.3: s1 = box[-1] s2 = new_box[-1] sum_scales = s1 + s2 scale_boxes[i][0] = round( (s1 * box[0] + s2 * new_box[0]) / sum_scales) scale_boxes[i][1] = round( (s1 * box[1] + s2 * new_box[1]) / sum_scales) scale_boxes[i][4] = (s1 * box[-1] + s2 * new_box[-1]) / sum_scales new_box = scale_boxes[i] bounding_boxes.pop(j) deleted_small += 1 else: j += 1 print('*' * 10, 'deleted {} small bboxes'.format(deleted_small), '*' * 10) bounding_boxes = bounding_boxes + scale_boxes return bounding_boxes
def find(self, haystack_img, threshold=0.72, debug_mode=None): # Can use IMREAD flags to do different pre-processing of image files. # Like making them grayscale or reducing the size. # https://docs.opencv.org/4.2.0/d4/da8/group__imgcodecs.html # haystack_img = cv.imread(haystack_img_path, cv.IMREAD_UNCHANGED) # No longer using haystack_img_path, passing in screen cap now! # We're turning needle_img into a property of self! # needle_img = cv.imread(needle_img_path, cv.IMREAD_UNCHANGED) # Get dimensions of the needle image # needle_w = needle_img.shape[1] # needle_h = needle_img.shape[0] # There are 6 comparison methods to choose from: # TM_CCOEFF, TM_CCOEFF_NORMED, TM_CCORR, TM_CCORR_NORMED, # TM_SQDIFF, TM_SQDIFF_NORMED # You can see the differences at a glance here: # https://docs.opencv.org/master/d4/dc6/tutorial_py_template_matching.html # Note that the values are inverted for TM_SQDIFF and TM_SQDIFF_NORMED # Best results were with CCOEFF_NORMED. # method = cv.TM_CCOEFF_NORMED # Apply property tags # Making result a property lets us find best match outside of class. self.result = cv.matchTemplate(haystack_img, self.needle_img, self.method) # print(self.result) # We get the coordinates of the values under a given threshold # >= or <= depending on method used in cv.matchTemplate locations = np.where(self.result >= threshold) # we can zip these values up into tuple values locations = list(zip(*locations[::-1])) # print(locations) # Let's create the list of [x, y, w ,h] rectangles rectangles = [] for loc in locations: rect = [int(loc[0]), int(loc[1]), self.needle_w, self.needle_h] rectangles.append(rect) # Twice to combat cv.groupRectangles problem. rectangles.append(rect) # second parameter determines how many rectanlges must overlap to group them. # third param - EPS - determines how close in order to group. # Returns inaccurate detections for single objects. # We can combat this by having at least one overlap. rectangles, weights = cv.groupRectangles(rectangles, groupThreshold=1, eps=0.5) # print(rectangles) # Now we check by rectangles instead of location coordinates. # (if rectangles: is ambiguous), so we use the length points = [] if len(rectangles): # if locations: print('Found speedchat button.') # Rectangle details line_color = (0, 255, 0) line_type = cv.LINE_4 marker_color = (255, 0, 255) marker_type = cv.MARKER_CROSS # Move this up top. # needle_w = needle_img.shape[1] # needle_h = needle_img.shape[0] # Loop over all locations and draw rectangle # for loc in locations: for (x, y, w, h) in rectangles: # Selecting the middle point of the box positions. center_x = x + int(w / 2) center_y = y + int(h / 2) # Save the points points.append((center_x, center_y)) if debug_mode == 'rectangles': # Determine the box positions # we gave top_left/right and needle_w/h variable names, so use those. top_left = (x, y) # bottom_right = (top_left[0] + needle_w, top_left[1] + needle_h) bottom_right = (x + w, y + h) # Draw the Box cv.rectangle(haystack_img, top_left, bottom_right, line_color, line_type) elif debug_mode == 'points': cv.drawMarker(haystack_img, (center_x, center_y), marker_color, marker_type) # Tabbing over 1 will still give video stream, outside if rectangles stmt, # even if we dont find any results. if debug_mode: cv.imshow('Matches', haystack_img) # cv.waitKey() # Wait key stuff done in Main while loop now. # cv.imwrite('result.png', haystack_img) return points else: print('Speedchat button not found.')
for t in range(num_trials): opencv_gr = [] my_gr = [] for r in range(num_rectangles): e = np.array([ random.random() * arena_size, random.random() * arena_size, random.random() * square_size, random.random() * square_size ]) ec = np.concatenate([e, np.array([random.random(), random.random()])]) opencv_gr.append(e) my_gr.append(ec if use_aux else e) output1, scores1 = cv2.groupRectangles(opencv_gr, gt, eps) output2, aux2, scores2 = group_rectangles_with_aux.execute(my_gr, gt, eps) if len(output1) == 0: assert len(output2) == 0 else: try: assert (output1 == output2).all() except: print 'opencv' print output1 print 'mine' print output2 print 'diff' print output1.shape, output2.shape print output1 - output2 raise
import numpy as np import cv2 rl = [[0,0,100,100],[2,2,102,102],[200,200,500,500]] rl = np.vstack((rl,rl)) nl,w = cv2.groupRectangles(np.array(rl).tolist(),1,0.2) print nl
resut = cv.matchTemplate(haystack_img, needle_img, cv.TM_SQDIFF_NORMED) threshold = 0.17 locations = np.where(resut <= threshold) locations = list(zip(*locations[::-1])) print(locations) rectangles = [] for loc in locations: rect = [int(loc[0]), int(loc[1]), needle_w, needle_h] rectangles.append(rect) rectangles.append(rect) rectangles, weights = cv.groupRectangles(rectangles, 1, 0.5) print(rectangles) if len(rectangles): print("found Needle") marker_color = (255, 0, 255) marker_type = cv.MARKER_CROSS for (x, y, w, k) in rectangles: ''' top_left = (x, y) bottom_right = (x + w, y + k) cv.rectangle(haystack_img, top_left, bottom_right, line_color, line_type) ''' center_x = x + int(w / 2)
elif approx.shape[0] > 12: num_shapes["squiggles"] += 1 else: num_shapes["ovals"] += 1 total_num_shapes = num_shapes["ovals"] + num_shapes["squiggles"] + num_shapes["diamonds"] shape_type = max(num_shapes.iteritems(), key=operator.itemgetter(1))[0] shapes = namedtuple("shapes", ["type", "number", "color", "shade"]) return shapes(shape_type, total_num_shapes, "UNKNOWN", "UNKNOWN") if __name__ == '__main__': from glob import glob for fn in glob('gameboard2.jpg'): img = cv2.imread(fn) cards = find_cards(img) cards = cv2.groupRectangles(cards, 1)[0] shape_count = {"squiggles": 0, "diamonds": 0, "ovals": 0} for card in cards: card_img = img[card[1]:card[1]+card[3], card[0]:card[0]+card[2]] shapes = find_shapes(card_img) shape_count[shapes.type] += shapes.number cv2.rectangle(img, (card[0], card[1]), (card[0]+card[2], card[1]+card[3]), (255, 0, 0), thickness=2) print "Found {num} cards and {shapes} shapes (d={d}, o={o}, s={s}).".format( num=len(cards), shapes=shape_count, s=shape_count["squiggles"], o=shape_count["ovals"], d=shape_count["diamonds"] ) cv2.imshow('squares', img) ch = 0xFF & cv2.waitKey()
def detect(self, cur_frame_id, buffer): logger.info('Detecting Image') v_max = 10 v_min = 1 # flow_list = np.array(self.flow_list[cur_frame_id - 50:cur_frame_id]).transpose() # move_distances = np.sum(np.sqrt(flow_list[0] ** 2 + flow_list[1] ** 2)) # if move_distances > 5: # self.onDetectSuccess.emit(cur_frame_id,[], [], []) # return verticals, horizontals = extractLines(buffer[0], threshold=0.66) shape = buffer[0].shape center = (int(shape[1] / 2), int(shape[0] / 2)) x_bound = [0, shape[1]] y_bound = [0, shape[0]] vs = extend_verticals(verticals, x_bound, y_bound) hs = extend_horizontals(horizontals, x_bound, y_bound) area_vec = calculateBoundingPoints(center, vs, hs) # area_vec = self.find_count_area(buffer[0]) frameDiff = np.abs(np.diff(buffer, axis=0)) frameDiffSum = np.sum(frameDiff, axis=0) av = (frameDiffSum / len(frameDiff)) av[av > v_max] = v_max av[av < v_min] = v_min normframe = (((av - v_min) / (v_max - v_min)) * 255).astype('uint8') if self.mode == RESNET: # preprocess image = np.stack((normframe, ) * 3, axis=-1) image = preprocess_image(image) image, scale = resize_image(image) boxes, scores, labels = self.model.predict_on_batch( np.expand_dims(image, axis=0)) boxes /= scale cells = [] sc = [] for cell, score in zip(boxes[0], scores[0]): if score > 0.5: l, t, r, b = cell cells.append([int(l), int(t), int(r - l), int(b - t)]) # cells.append(cell) sc.append(score) # min cluster size = 2, min distance = 0.5: cells.extend(cells) cells, weights = cv2.groupRectangles(cells, 1, 1.0) if len(cells) == 0: self.onDetectSuccess.emit(cur_frame_id, area_vec, [], []) return self.onDetectSuccess.emit(cur_frame_id, area_vec, cells.tolist(), sc) if self.mode == PROPER_REGION: image = normframe thresh = threshold_yen(image) binary = image >= thresh closed = binary_closing(binary) # plt.title("move distance:{}".format(move_distances)) # plt.imshow(closed) # plt.show() label_img = label(closed) cell_locs = regionprops(label_img) # tlbr to ltrb cells = [] for cell in cell_locs: t, l, b, r = cell.bbox if cell.area > 50: cells.append([l, t, r - l, b - t]) if len(cells) > 20: self.onDetectSuccess.emit(cur_frame_id, area_vec, [], []) return cells.extend(cells) cells, weights = cv2.groupRectangles(cells, 1, 1.0) # cell_locs = [p.bbox for p in cell_locs if p.area > 100] sc = [1.0] * len(cells) self.onDetectSuccess.emit(cur_frame_id, area_vec, list(cells), sc)
read_img_name = '/home/anson/FDDB/originalPics/' + array[current_image].rstrip() + '.jpg' img = cv2.imread(read_img_name) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # detect faces faces = face_cascade.detectMultiScale(gray, 1.2, 0) # don't group faces, and see confidence by finding how many # iterations of grouping till the face is still there rectList = faces[:] # rectList changes every iteration of grouping number_of_faces = len(faces) confidences = [] confidence_denominator = 55.0 group_threshold = 1 # start from 3, and see how far it goes last_number_of_faces = len(rectList) # store last number of faces, so that we know how many faces have disappeared while len(rectList) != 0: rectList, weights = cv2.groupRectangles(list(faces), group_threshold) number_of_faces_gone = last_number_of_faces - len(rectList) if number_of_faces_gone > 0: # if any faces were deleted at last grouping, append confidence for i in range(number_of_faces_gone): confidences.append( min( (group_threshold - 1) / confidence_denominator , 1)) last_number_of_faces = len(rectList) group_threshold += 1 for i in range(last_number_of_faces): # append confidences of faces in last round confidences.append( int(group_threshold - 1) / confidence_denominator ) # for i in range(number_of_faces): # print str(faces[i][0]) + ' ' + str(faces[i][1]) + ' ' + str(faces[i][2]) + ' ' + str(faces[i][3]), # print str(' ' + str(confidences[i])) # write to file