def model_predict(img_file, view): """ :param img_file: image file path :param view: true if you want show the result :return: """ res_obj = {"score": 0, "position": ""} prediction = "predict_image.png" img = cv2.imread(img_file) if view else cv2.imdecode( numpy.fromstring(img_file, numpy.uint8), 1) scale = 500 / img.shape[1] img = cv2.resize(img, (0, 0), fx=scale, fy=scale) gray, binary = get_binary_image(img) if get_gray_score(binary): mg_lbl, regions = selectivesearch.selective_search( binary, SCALE, SIGMA, MIN_SIZE) regions = get_proposal(regions, img.shape) rectangles, score_list = get_prediction(binary, regions) if len(score_list) > 0: score = round(max(score_list), 2) rect = rectangles[score_list.index(max(score_list))] position = get_pos(rect, scale) res_obj["score"] = float(score) res_obj["position"] = position cv2.drawContours(img, [rect], -1, (255, 145, 30), 2) if not os.path.exists(PREDICT_PATH): os.mkdir(PREDICT_PATH) cv2.imwrite(PREDICT_PATH + prediction, img) if view: print(res_obj) image_view(img) return res_obj
def create_proposals(image, config): img_lbl, regions = selectivesearch.selective_search(image, scale=100, sigma=0.9, min_size=10) candidates = set() images = [] vertices = [] for r in regions: # remove reduplicative region if r['rect'] in candidates: continue if r['size'] < config.proposal_min_size: continue if (r['rect'][2] * r['rect'][3]) < config.proposal_min_area: continue proposal_img, proposal_vertice = clip_pic(image, r['rect']) if len(proposal_img) == 0: continue x, y, w, h = r['rect'] if w == 0 or h == 0: continue [a, b, c] = np.shape(proposal_img) if a == 0 or b == 0 or c == 0: continue resized_proposal_img = resize_image(proposal_img, (config.sz, config.sz)) candidates.add(r['rect']) img_float = np.asarray(resized_proposal_img, dtype="float32") images.append(img_float) vertices.append(r['rect']) return images, vertices
def searchROI(path): image = cv2.imread(path) if image is None: return img_lbl, regions = selectivesearch.selective_search(image, scale=100, sigma=0.9, min_size=200) print(len(regions)) fig, ax = plt.subplots(figsize=(10, 10)) for reg in regions: x, y, w, h = reg['rect'] rect = cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2) ax.imshow(rect) plt.show()
def get_proposals(img): """ show generated proposals on input image :param img: input image :return: """ img = cv2.imread(img) scale = 500 / img.shape[1] img = cv2.resize(img, (0, 0), fx=scale, fy=scale) gray, binary = get_binary_image(img) mg_lbl, regions = selectivesearch.selective_search(img, min_size=80) regions = get_proposal(regions, img.shape) print("proposals:", len(regions)) cv2.drawContours(gray, regions, -1, (255, 145, 30), 2) image_view(gray)
def read_im(name): im = Image.open("D:/Statistical Learning/project/image/ours/" + name) a, b = im.size if (a > 600): out = im.resize((800, int((800 * b) / a)), Image.ANTIALIAS) out.save("D:/Statistical Learning/project/image/ours/" + name) im = out im = im.convert('RGB') img = cv2.imread("D:/Statistical Learning/project/image/ours/" + name) start_time = time.time() img_lbl, regions = ss.selective_search(img, scale=100, sigma=0.8, min_size=20) end_time = time.time() print('Time for SS: %ssecs' % (end_time - start_time)) return ([im, regions])
def load_train_proposals(datafile, num_clss, threshold=0.5, svm=False, save=False, save_path='dataset.pkl'): train_list = open(datafile, 'r') labels = [] images = [] for line in train_list: tmp = line.strip().split(' ') # tmp0 = image address # tmp1 = label # tmp2 = rectangle vertices img = skimage.io.imread(tmp[0]) img_lbl, regions = selectivesearch.selective_search(img, scale=500, sigma=0.9, min_size=10) candidates = set() print(tmp[0]) for r in regions: # excluding same rectangle (with different segments) if r['rect'] in candidates: continue if r['size'] < 220: continue # resize to 224 * 224 for input proposal_img, proposal_vertice = clip_pic(img, r['rect']) # Delete Empty array if len(proposal_img) == 0: continue # Ignore things contain 0 or not C contiguous array x, y, w, h = r['rect'] if w == 0 or h == 0: continue # Check if any 0-dimension exist [a, b, c] = np.shape(proposal_img) if a == 0 or b == 0 or c == 0: continue im = Image.fromarray(proposal_img) resized_proposal_img = resize_image(im, 224, 224) candidates.add(r['rect']) img_float = pil_to_nparray(resized_proposal_img) images.append(img_float) # IOU ref_rect = tmp[2].split(',') ref_rect_int = [int(i) for i in ref_rect] iou_val = IOU(ref_rect_int, proposal_vertice) # labels, let 0 represent default class, which is background index = int(tmp[1]) if svm == False: label = np.zeros(num_clss + 1) if iou_val < threshold: label[0] = 1 else: label[index] = 1 labels.append(label) else: if iou_val < threshold: labels.append(0) else: labels.append(index) if save: pickle.dump((images, labels), open(save_path, 'wb')) return images, labels
import selectivesearch.selectivesearch as ss import cv2 img = cv2.imread("./image/1.jpg") img_lbl, regions = ss.selective_search(img, scale=1000, sigma=0.8, min_size=50) for r in regions: x, y, h, w = r['rect'] cv2.rectangle(img, (x, y), (x + h, y + w), (0, 255, 0), 2) cv2.imshow("img", img) print("OK") cv2.waitKey(0)