def detect(image_path): image = image_rescale(image_path) # transform bgr to rgb im = image[:,:,(2,1,0)] regions = ss.selective_search(im, color_spaces, ks, feature_masks) fig, ax = plt.subplots(figsize=(12, 12)) ax.imshow(image, aspect='equal') locations = [] for v, (i0, j0, i1, j1) in regions: w = j1 - j0 h = i1 - i0 # check if w < 10 or h < 10 or w > 60 or h > 60 or w/h > 1.2 or h/w > 1.2: continue # extract pathch patch = image[i0:i1,j0:j1,:] patch = cv2.resize(patch,im_size) # extract fv feature kp, des = extfeat.extract_sift_from_image(patch) if des is None or len(des) == 0: continue image_info = {'des':[des], 'img':[patch], 'kps':[kp]} spm_feats = spm.spm_transform(image_info, level=spm_level) # load classifier clf = joblib.load(osp.join(model_dir,'spm_rf.pkl')) pred = clf.predict_proba(spm_feats) if pred[0][1] > 0.50: locations.append((j0, i0, pred[0][1], w, h)) # nms locations = nms.nms(locations) # plot for (x, y, score, w, h) in locations: cv2.rectangle(image, (x, y), (x+w, y+h), (0, 0, 255), 2) img_name = image_path.split('/')[-1] cv2.imwrite(osp.join(output_dir,'spm_'+img_name), image)
def detect(image_path): image = image_rescale(image_path) # transform bgr to rgb im = image[:, :, (2, 1, 0)] regions = ss.selective_search(im, color_spaces, ks, feature_masks) fig, ax = plt.subplots(figsize=(12, 12)) ax.imshow(image, aspect='equal') locations = [] for v, (i0, j0, i1, j1) in regions: w = j1 - j0 h = i1 - i0 # check if w < 20 or h < 20 or w > 50 or h > 50 or w / h > 1.2 or h / w > 1.2: continue # extract pathch patch = image[i0:i1, j0:j1, :] patch = cv2.resize(patch, im_size) # extract bow feature _, des = extfeat.extract_sift_from_image(patch) if des is None or len(des) == 0: continue X_bow = extfeat.generate_bof([des], retrain=False, suffix='BOW') # load classifier clf = joblib.load(osp.join(model_dir, 'bow_rf.pkl')) pred = clf.predict_proba(X_bow) if pred[0][1] > 0.50: locations.append((j0, i0, pred[0][1], w, h)) # nms locations = nms.nms(locations) # plot for (x, y, score, w, h) in locations: cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 2) img_name = image_path.split('/')[-1] cv2.imwrite(osp.join(output_dir, 'bow_' + img_name), image)
def detect(image_path): image = image_rescale(image_path) # transform bgr to rgb im = image[:, :, (2, 1, 0)] regions = ss.selective_search(im, color_spaces, ks, feature_masks) fig, ax = plt.subplots(figsize=(12, 12)) ax.imshow(image, aspect='equal') locations = [] for v, (i0, j0, i1, j1) in regions: w = j1 - j0 h = i1 - i0 # check if w < 20 or h < 20 or w > 50 or h > 50 or w / h > 1.2 or h / w > 1.2: continue # extract pathch patch = image[i0:i1, j0:j1, :] patch = cv2.resize(patch, im_size) # extract hsv histogram hsv_hist = extfeat.colorHist(patch) hsv_hist = np.array([hsv_hist], dtype='float32') # load classifier clf = joblib.load(osp.join(model_dir, 'hsv_rf.pkl')) pred = clf.predict_proba(hsv_hist) if pred[0][1] > 0.50: locations.append((j0, i0, pred[0][1], w, h)) # nms locations = nms.nms(locations) # plot for (x, y, score, w, h) in locations: cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 2) img_name = image_path.split('/')[-1] cv2.imwrite(osp.join(output_dir, 'hsv_' + img_name), image)
def ClsKLSCoarseLoc(imgFile, paras): im = imread(imgFile) im_norm = np.array(im, dtype='f') / 255 if len(im.shape) == 2: img = skimage.color.gray2rgb(im) paras['img'] = img paras['im_norm'] = im_norm paras['im'] = im color_space = paras['color_space'] ks = paras['ks'] overlapThresh = paras['overlapThresh'] regionSizeRange = paras['regionSizeRange'] net = paras['net'] scoreThresh = paras['scoreThresh'] paras['sizeRange'] = (patchSize, patchSize) paras['patchSize'] = np.array([patchSize, patchSize]) paras['feaType'] = feaType if L == 0: paras['L'] = None else: paras['L'] = L region_set = ss.selective_search(img, color_spaces=color_space, ks=ks, feature_masks=feature_masks, eraseMap=eraseMap) boxes = regionSetToBoxes(region_set, overlapThresh, sizeRange=regionSizeRange, isVisualize=False) scores, boxes = im_detect(net, im, boxes) classHeatMap = generateRegionClassHeatMap(scores, boxes, scoreThresh) label = mapsToLabels(classHeatMap) return label, classHeatMap
def process_test_data(dataPath): image_data = create_image_data(dataPath) processed_images = [] for img_t in image_data: img = img_t[0] label = img_t[1] box_t = img_t[2] boxes = selective_search.selective_search(img, mode='fast', random=False) boxes = selective_search.box_filter(boxes, min_size=20, topN=400) postBox = [] boxIDs = [] boxID = 0 for box in boxes: new_img = img[box[0]:box[3], box[1]:box[2]] postBox.append(preprocess.preprocess(new_img)) boxIDs.append(boxID) boxID += 1 postBox = np.squeeze(postBox).transpose() processed_images.append(((img, label, box_t, boxes), postBox, boxIDs)) print("%d/%d processed" % (len(processed_images), len(image_data))) return processed_images
def __parameter_changed(self): # obtain parameters color_spaces = [color.lower() for color in self.chosen_colors] ks = [float(k) for k in self.chosen_ks] similarity_masks = [features.SimilarityMask('S' in mask, 'C' in mask, 'T' in mask, 'F' in mask) for mask in self.chosen_similarities] self.regions = selective_search.selective_search(self.ndimg, color_spaces, ks, similarity_masks) self.slider.setMaximum(len(self.regions)) self.slider.setValue(int(len(self.regions) / 4)) self.__draw()
def read_plate (net, transformer, plate_path): plate_image = cv2.imread(plate_path) w, h, shape = plate_image.shape print w print h t = time.time() search_results = selective_search.selective_search(plate_image) runtime = time.time() - t _, rois = zip(*search_results) # convert tuple to list and make sure they are valid ROIs bboxes = [] for i in xrange(len(rois)): roi = rois[i] if roi[3] - roi[1] != 0 and roi[2] - roi[0] != 0: bboxes.append(roi) print 'Got {} boxes in {} s'.format(len(bboxes), runtime) # some extra processing specifically for characters bboxes_processed = [] # Extra Restrictions for i in xrange(len(bboxes)): bbox = bboxes[i] width = bbox[2]-bbox[0] height = bbox[3] - bbox[1] if height/width > 1 and height/width < 3: if width > 5 and height > 10: bboxes_processed.append(bbox) bboxes = bboxes_processed class_names = [] scores = [] for i in xrange(len(bboxes)): bbox = bboxes[i] crop = plate_image[bbox[0]:bbox[2], bbox[1]:bbox[3]] class_name, score = process_image(net, transformer, crop) class_names.append(class_name) scores.append(score) vis_detections(plate_image, class_names, scores, bboxes, 0.9)
def __parameter_changed(self): # obtain parameters color_spaces = [color.lower() for color in self.chosen_colors] ks = [float(k) for k in self.chosen_ks] similarity_masks = [ features.SimilarityMask('S' in mask, 'C' in mask, 'T' in mask, 'F' in mask) for mask in self.chosen_similarities ] self.regions = selective_search.selective_search( self.ndimg, color_spaces, ks, similarity_masks) self.slider.setMaximum(len(self.regions)) self.slider.setValue(int(len(self.regions) / 4)) self.__draw()
def main(): # loading astronaut image #img = skimage.data.astronaut() path = os.path.abspath(os.path.dirname(__file__)) image = Image.open(path+"/seg_test.jpg",mode="r") img = np.asarray(image) #plt.imshow(img) #plt.savefig("origin.jpg") plt.hist(img[:,:,0],100, facecolor="red", edgecolor="black", alpha=0.7) plt.savefig("origin_his_red.jpg") plt.clf plt.hist(img[:,:,1],100, facecolor="red", edgecolor="black", alpha=0.7) plt.savefig("origin_his_green.jpg") plt.clf plt.hist(img[:,:,2],100, facecolor="red", edgecolor="black", alpha=0.7) plt.savefig("origin_his_blue.jpg") plt.clf # perform selective search img_lbl, regions = selective_search.selective_search( img, scale=500, sigma=0.9, min_size=10) candidates = set() for r in regions: # excluding same rectangle (with different segments) if r['rect'] in candidates: continue # excluding regions smaller than 2000 pixels if r['size'] < 2000: continue # distorted rects x, y, w, h = r['rect'] if w / h > 1.2 or h / w > 1.2: continue candidates.add(r['rect']) # draw rectangles on the original image fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(6, 6)) ax.imshow(img) for x, y, w, h in candidates: print(x, y, w, h) rect = mpatches.Rectangle( (x, y), w, h, fill=False, edgecolor='red', linewidth=1) ax.add_patch(rect)
def detect_objects(image): """Determine the label and location of objects in an image. Returns a list of tuples (label, x, y, w, h) corresponding to all object labels and locations with probability above the confidence threshold. """ candidates = list(selective_search(image)) sub_images = np.array([ *map( lambda i: np.array(Image.fromarray(image[i[1]:i[1] + i[3], i[0]:i[ 0] + i[2], :]).resize(image_shape), dtype=np.uint8) / 255, candidates) ]) predict_probabilities = predict(sub_images) predict_labels = np.argmax(predict_probabilities, axis=1) return [ (predict_labels[i], *candidates[i]) for i in range(len(candidates)) if predict_probabilities[i][predict_labels[i]] > CONFIDENCE_THRESHOLD ]
def generate_selective_search(image_dir): img = cv2.imread(image_dir) img_lbl, regions = selective_search(img, scale=500, sigma=0.8, min_size=10) candidates = set() for r in regions: # excluding same rectangle (with different segments) if r['rect'] in candidates: continue # excluding regions smaller than 220 pixels if r['size'] < 220: continue # distorted rects x, y, w, h = r['rect'] if w == 0 or h == 0: continue if w / h > 2.5 or h / w > 2.5: continue candidates.add(r['rect']) return candidates
def do_detection(mode, input_examples, minimalImage_size, output_path, limit=np.Inf): detection_indx = 0 chunck_size = min(1000, limit // 1000) if chunck_size == 0: chunck_size = limit print("Starting {} search..".format(mode)) for img_path in input_examples: if os.path.exists(img_path): print("processing {}".format(img_path)) image = cv2.imread(img_path) raw_name = os.path.splitext(os.path.basename(img_path))[0] if mode == 'selective': boxes = selective_search.selective_search(image, mode='fast') elif mode == 'sliding': boxes = sliding_window_boxes(image, minimalImage_size, limit=np.inf) else: os.error("incorrect mode name") random.shuffle(boxes) if limit < 10001: limit_per_image = 100 boxes = boxes[:limit_per_image] for x1, y1, x2, y2 in boxes: window = image[x1:x2, y1:y2, :] if window.size > 0: chunck_indx = detection_indx // chunck_size write_window_to_file(window, minimalImage_size, detection_indx, os.path.join(output_path, str(chunck_indx)), raw_name) detection_indx += 1 if detection_indx == limit: return # [Optional:] Draw window on image: # cv2.rectangle(tmp, (x, y), (x + w_width, y + w_height), (255, 0, 0), 2) # draw rectangle on image # plt.imshow(np.array(tmp).astype('uint8')) # plt.show() print("Total {} detected windows were saved to {}!".format(detection_indx, output_path))
def get_proposals(img_path, topN=80): # Proposals: (x1 y1 x2 y2) img = skimage.io.imread(img_path) proposals = selective_search.selective_search(img, mode='fast', random=False) proposals = selective_search.box_filter(proposals, min_size=20, topN=topN) # fig, ax = plt.subplots(figsize=(6, 6)) # ax.imshow(img) # for x1, y1, x2, y2 in proposals: # bbox = mpatches.Rectangle( # (x1, y1), (x2 - x1), (y2 - y1), fill=False, edgecolor='red', linewidth=1) # ax.add_patch(bbox) # # plt.axis('off') # plt.show() # # os.system('pause') return np.array(proposals)
def SelectiveSearchImg(className, imgName): image = io.imread(IMG_PATH + className + '/' + imgName + '.jpg') image = transform.resize(image, (224, 224)) boxes = selective_search.selective_search(image, mode='single') boxes_filter = selective_search.box_filter(boxes, min_size=30, topN=20) image = np.asarray(image) proposals = [] for box in boxes_filter: w, h = box[2] - box[0], box[3] - box[1] if w < 250 and h < 250: img = cv2.resize(image[box[0]:box[2], box[1]:box[3], :], (224, 224)) temp = np.empty((224, 224, 3)) temp[:, :, 0] = img[:, :, 0] temp[:, :, 1] = img[:, :, 1] temp[:, :, 2] = img[:, :, 2] img = temp proposals.append(img) return proposals
def region_class_heatMap(paras): img = paras['img'] im = paras['im'] color_space = paras['color_space'] ks = paras['ks'] feature_masks = paras['feature_masks'] eraseMap = paras['eraseMap'] overlapThresh = paras['overlapThresh'] regionSizeRange = paras['regionSizeRange'] net = paras['net'] scoreThresh = paras['scoreThresh'] is_showProposals = paras['is_showProposals'] region_set = ss.selective_search(img, color_spaces=color_space, ks=ks, feature_masks=feature_masks, eraseMap=eraseMap) boxes = regionSetToBoxes(region_set, overlapThresh, sizeRange=regionSizeRange, isVisualize=False) if is_showProposals: plf.showProposals(im, boxes) plt.savefig('../../Data/Results/proposals/'+paras['imgFile'][-20:-4]+'_proposals.jpg') plt.show() scores, boxes = im_detect(net, im, boxes) regionClassMap = generateRegionClassHeatMap(scores, boxes, scoreThresh) return regionClassMap
def selective_search_areas(img, scale=500, sigma=0.9, min_size=10, ratio=-1, min_area=1000, num_scores=0): # Workaround to import selectivesearch from its directory # Perform selective search boxes = [] img_lbl, regions = ss.selective_search(img, scale=scale, sigma=sigma, min_size=min_size) candidates = set() for r in regions: # Exclude same rectangle (with different segments) if r['rect'] in candidates: continue # Exclude small regions if r['size'] < min_area: continue # Exclude distorted rects min_x, min_y, max_x, max_y = r['rect'] if min_x > max_x: min_x, max_x = max_x, min_x if min_x > max_x: min_y, max_y = max_y, min_y width, height = max_x - min_x + 1, max_y - min_y + 1 #print('width=' + str(width) + '\theight=' + str(height)) if width == 0 or height == 0: continue if ratio > 0: if width / height > ratio or height / width > ratio: continue candidates.add(r['rect']) for min_x, min_y, max_x, max_y in candidates: width, height = max_x - min_x + 1, max_y - min_y + 1 boxes.append([1.0 for i in range(num_scores)] + [min_x, min_y, width, height]) return boxes
def save_ss_hdf5(vector_db, ids=None, resize_factor=5, ss_mode="fast", n_jobs=1): mul = lambda x: x * resize_factor ks = { "fast": [1000], "complete": [100, 500, 800, 1500, 2500], # 100, 150, 300, 500], } feature_masks = { "fast": [ SimilarityMask(size=1, color=1, texture=1, fill=1), SimilarityMask(size=1, color=0, texture=1, fill=1), ], "complete": [ SimilarityMask(size=0, color=1, texture=0, fill=0), SimilarityMask(size=0, color=0, texture=0, fill=1), SimilarityMask(size=1, color=1, texture=1, fill=1), SimilarityMask(size=1, color=0, texture=1, fill=1), ] } color_spaces = { "fast": ['hsv', 'lab', 'rgb'], "complete": ['hsv', 'lab', 'hue', 'rgb'] } #rgi if ids == None: ids = vector_db.keys() errors = [] i = 0 for img_id in ids: print(img_id) i += 1 # If problem reading, skip try: img = np.array( vector_db.retrieve_instance(img_id, groups=("Images", ))[0]) except: continue img = np.rollaxis(img, 0, 3) img = resize_pano(img=img, resize_factor=resize_factor) try: img_ss = selective_search(img, ks=ks[ss_mode], feature_masks=feature_masks[ss_mode], color_spaces=color_spaces[ss_mode], n_jobs=n_jobs) except: try: img_ss = selective_search(img, ks=[200, 700, 1500], feature_masks=feature_masks[ss_mode], color_spaces=color_spaces[ss_mode], n_jobs=n_jobs) except: errors.append(img_id) continue img_ss = np.array( [map(mul, (x0, y0, x1, y1)) for score, (y0, x0, y1, x1) in img_ss]) print("errors : {}".format(errors)) print("Number of errors: {} / {}".format(len(errors), i)) vector_db.save_ss(img_id, img_ss, replace=True) vector_db.flush() print(errors)
import numpy as np import matplotlib.pyplot as plt from matplotlib.patches import Rectangle from skimage import data from imageio import imread from datetime import datetime from selective_search import selective_search img = data.coffee() # img = data.astronaut() # img = data.chelsea() # img = imread("image1.jpg") t1 = datetime.now() segment_mask, R = selective_search(img, scale=150, sim_threshold=0.65) t2 = datetime.now() print(t2 - t1) fig = plt.figure() ax1 = plt.subplot(121) plt.imshow(img) for r in R: rect = Rectangle((r['x_min'], r['y_min']), r['width'], r['height'], fill=False, color='red', linewidth=1.5) ax1.add_patch(rect) ax2 = plt.subplot(122) plt.imshow(segment_mask)
def selective_search_detection(input_examples, output_path, minimalImage_size, gen_frame_rate=2, frame_intervals=20, limit=np.Inf): detection_indx = 0 chunck_size = min(1000, int(limit / 1000)) for img_path in input_examples: if os.path.exists(img_path): # read the image and define the stepSize and window size (width,height) print("processing %s" % img_path) #image = cv2.imread(img_path) # your image path cap = cv2.VideoCapture(img_path) raw_name = os.path.splitext(os.path.basename(img_path))[0] fps = cap.get(cv2.CAP_PROP_FPS) # Default resolutions of the frame are obtained.The default resolutions are system dependent. # We convert the resolutions from float to integer. frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) total_number_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) # for fram_indx in range(0, total_number_frames, frame_intervals): for ii in range(0, int(total_number_frames / frame_intervals)): fram_indx = np.random.randint(0, total_number_frames - 1) cap.set(cv2.CAP_PROP_POS_FRAMES, fram_indx) ret, first_frame = cap.read() #with np.errstate(divide='ignore', invalid='ignore'): boxes = selective_search.selective_search(first_frame, mode='fast') # boxes_filter (optional) boxes = selective_search.box_filter(boxes, min_size=20, topN=80) random.shuffle(boxes) if limit < 10001: limit_per_image = 100 boxes = boxes[:limit_per_image] for x1, y1, x2, y2 in boxes: window_frame1 = first_frame[ x1:x2, y1:y2, :] # [x:x + w_width, y:y + w_height, :] if window_frame1.size > 0: # randomize frame 2: second_fram_indx = np.random.randint( fram_indx, total_number_frames) cap.set(cv2.CAP_PROP_POS_FRAMES, second_fram_indx) ret, second_frame = cap.read() window_frame2 = second_frame[x1:x2, y1:y2, :] chunck_indx = int(detection_indx / chunck_size) #'v_MoppingFloor_g05_c01_size_30_bbox_29_207_37_215_inds_17_55_rate_2_O' video_file_name = raw_name + '_size_' + str(minimalImage_size) \ + '_bbox_' + str(x1)+'_' + str(x2)+'_' + str(y1)+'_' + str(y2)+'_' \ + '_inds_' + '_' + str(fram_indx) + '_' + str(second_fram_indx) + '_rate_' + str(gen_frame_rate) write_windows_to_file( window_frame1, window_frame2, minimalImage_size, gen_frame_rate, detection_indx, os.path.join(output_path, str(chunck_indx)), video_file_name) detection_indx += 1 if detection_indx > limit: return # # show all windows # plt.show() print("Total %d detected windows were saved to %s!" % (detection_indx, output_path))
if not os.path.exists(eraseMapPath): imSize = 440 eraseMap = np.zeros((imSize, imSize)) radius = imSize / 2 centers = np.array([219.5, 219.5]) for i in range(440): for j in range(440): if np.linalg.norm(np.array([i, j]) - centers) > 220: eraseMap[i, j] = 1 imsave(eraseMapPath, eraseMap) else: eraseMap = imread(eraseMapPath) / 255 im = skimage.io.imread(imgFile) if len(im.shape) == 2: img = skimage.color.gray2rgb(im) region_set = ss.selective_search(img, color_spaces=color_space, ks=ks, feature_masks=feature_masks, eraseMap=eraseMap) boxes = regionSetToBoxes(region_set, overlapThresh, sizeRange=regionSizeRange, isVisualize=False) caffe.set_mode_gpu() caffe.set_device(gpu_id) net = caffe.Net(regionModelPrototxt, regionModelWeights, caffe.TEST) scores, boxes = im_detect(net, im, boxes) regionClassMap = generateRegionClassHeatMap(scores, boxes, scoreThresh) class_names = ['background', 'arc', 'drapery', 'radial', 'hot-spot'] visRegionClassHeatMap(regionClassMap, class_names) vis_detections(im, scores, boxes, class_names) pass
import cv2 from selective_search import selective_search #from cnn_model import cnn, cnn_keras #from bbox_operator import like_nms, akurasi, akurasi2, merge_bb, evaluasi from saving import save_bbox, show_bbox, save_region conf = 3 thres = 0.5 nama = 'crop001512' path = 'coba_img/' image = cv2.imread(path + nama + '.png') selective = selective_search(image) bbox = selective.boundingbox img = selective.image_crop() #for new image #list_bbox = np.array(selective.boundingbox) #list_img = selective.image_crop #anotasi=[[0,0,0,0]] #for test image #list_bbox = np.load('bbox/{}_bbox.npy'.format(nama)) #list_img = np.load('list_img/{}_list.npy'.format(nama)) #read_anotasi = pd.read_csv('INRIAPerson/Test/annotations_new/{}.csv'.format(nama)) #read_anotasi = read_anotasi.values # #anotasi = np.zeros((read_anotasi.shape),dtype = int)
import skimage import selective_search image = skimage.data.astronaut() # Propose boxes boxes = selective_search.selective_search(image, mode='single') # Filter box proposals boxes_filter = selective_search.box_filter(boxes, min_size=20, topN=80)
def find_plate (net, transformer, plate_image): # plate_image = cv2.imread(plate_path) t = time.time() search_results = selective_search.selective_search(plate_image) runtime = time.time() - t # Debugging: Visualize output of Selective Search if needed visualize_search(plate_image, search_results, 1000) # convert tuple to list and make sure they are valid ROIs _, rois = zip(*search_results) bboxes = [] for i in xrange(len(rois)): roi = rois[i] if roi[3] - roi[1] != 0 and roi[2] - roi[0] != 0: bboxes.append(roi) print 'Got {} boxes in {} s'.format(len(bboxes), runtime) # Some restrictions for how the bounding box can look like. bboxes_processed = [] for i in xrange(len(bboxes)): bbox = bboxes[i] width = bbox[3]-bbox[1] height = bbox[2] - bbox[0] if width/float(height) > 1 and width/float(height) < 10: if width > 30 and height > 15: bboxes_processed.append(bbox) bboxes = bboxes_processed # Gets the values of any detections class_names = [] scores = [] for i in xrange(len(bboxes)): bbox = bboxes[i] crop = plate_image[bbox[0]:bbox[2], bbox[1]:bbox[3]] class_name, score = process_image(net, transformer, crop) class_names.append(class_name) scores.append(score) # Find the best guesses. BG = Best Guesses bg_bboxes = [] bg_class_names = [] bg_scores = [] orig_h, orig_w, _ = plate_image.shape for i in xrange(len(bboxes)): bbox = bboxes[i] h = bbox[2] - bbox[0] x = bbox[1] y = bbox[0] w = bbox[3] - bbox[1] ratio_width = w/float(orig_w) ratio_height = h/float(orig_h) # print ratio_width # print ratio_height isCorrectSize = ratio_width < 0.20 and ratio_width > 0.07 \ and ratio_height < 0.15 and ratio_height > 0.03 location_height = y/float(orig_h) isCorrectLocation = location_height > 0.4 if isCorrectSize and isCorrectLocation: bg_bboxes.append(bbox) bg_class_names.append(class_names[i]) bg_scores.append(scores[i]) DET_THRESH = 0.999 vis_detections(plate_image, bg_class_names, bg_scores, bg_bboxes, DET_THRESH, color='r') vis_detections(plate_image, class_names, scores, bboxes, DET_THRESH, color='g')
import skimage import selective_search image = skimage.data.astronaut() # Propose boxes boxes = selective_search.selective_search(image, mode='single', random_sort=True) # Filter box proposals boxes_filter = selective_search.box_filter(boxes, min_size=20, topN=80) print(boxes_filter)
import skimage import selective_search import cv2 image = skimage.data.astronaut() # Propose boxes # mode = ['single','fast','quality'] boxes = selective_search.selective_search(image, mode='quality', random_sort=True) # Filter box proposals boxes_filter = selective_search.box_filter(boxes, min_size=20, topN=80) print(boxes_filter) for box in boxes_filter: qw = cv2.rectangle(image, (box[0], box[1]), (box[2], box[3]), (255, 0, 0), 2) cv2.imshow('as', qw) cv2.waitKey(0) cv2.destroyAllWindows()
import sys sys.path.append('./python') from selective_search import selective_search import numpy as np import cv2 import time ''' boxes = selective_search(image, mode, min_size) >> INPUT image: src (str) or NumpyArray (3d uint8 tensor, H x W x 3). mode: 0 (fast mode) or 1 (quality mode). >> OUTPUT boxes: NumpyArray (2d tensor, N x 4) or None if no boxes are found. ''' # load from src is faster tic = time.time() boxes = selective_search('./demo/skimage_astronaut.jpg', 0, 20) toc = time.time() print('box.shape:', boxes.shape, 'time:', toc - tic) image = cv2.imread('./demo/skimage_astronaut.jpg') boxes2 = selective_search(image, 0, 20) toc = time.time() print('box.shape:', boxes2.shape, 'time:', toc - tic)