def gen_annot_file_lines(annot): lines = [] # Get original annot line img_fn, class_name, train_subset_class = util.parse_annot(annot) annot_rect = util.get_annot_rect(annot) lines.append( gen_annot_file_line(img_fn, class_name, train_subset_class, annot_rect)) # Load image img = skimage.io.imread(os.path.join(common.TRAIN_IMAGE_DIR, img_fn)) # Selective search object_proposals = util.get_object_proposals(img) if len(object_proposals) == 0: return lines # Background proposals bg_proposals = get_bg_proposals(object_proposals, annot) if len(bg_proposals) == 0: return lines # Select bg proposal bg_proposal = bg_proposals[np.random.choice( np.array(bg_proposals).shape[0])] x1, y1, x2, y2 = bg_proposal[0], bg_proposal[ 1], bg_proposal[0] + bg_proposal[2], bg_proposal[1] + bg_proposal[3] lines.append( gen_annot_file_line(img_fn, common.CLASS_NAME[-1], train_subset_class, [x1, y1, x2, y2])) return lines
def main(): args = parse_cmdline() img_fn = os.path.abspath(args.img_fn) save_img = args.save_img if not os.path.exists(img_fn): print('Not found: {}'.format(img_fn)) sys.exit(-1) else: print('Target image: {}'.format(img_fn)) # Loaa target image target_image = cv2.imread(img_fn) # Get object proposals object_proposals = util.get_object_proposals(target_image) # Setup computation graph graph_params = setup_graph() # Model initialize sess = tf.Session(graph=graph_params['graph']) tf.global_variables_initializer() if os.path.exists('models'): save_path = os.path.join('models', 'deep_traffic_sign_model') graph_params['saver'].restore(sess, save_path) print('Model restored') else: print('Initialized') # traffic sign recognition results = [] for obj_proposal in object_proposals: x, y, w, h = obj_proposal crop_image = target_image[y:y + h, x:x + w] results.append( traffic_sign_recognition(sess, crop_image, obj_proposal, graph_params)) """ del_idx = [] for i, result in enumerate(results): if result['pred_class'] == common.CLASS_NAME[-1]: del_idx.append(i) results = np.delete(results, del_idx) """ # Non-max suppression nms_results = util.nms(results, pred_prob_th=0.999999, iou_th=0.4) # Draw rectangles on the target image fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(6, 6)) ax.imshow(cv2.cvtColor(target_image, cv2.COLOR_BGR2RGB)) for result in nms_results: print(result) (x, y, w, h) = result['obj_proposal'] ax.text(x, y, cls2name(result['pred_class']), fontsize=13, bbox=dict(facecolor='red', alpha=0.7)) rect = mpatches.Rectangle((x, y), w, h, fill=False, edgecolor='red', linewidth=1) ax.add_patch(rect) plt.show() # save the target image save_fname = os.path.splitext(os.path.basename(img_fn))[0] + '_result.jpg' if save_img: fig.savefig(save_fname, bbox_inches='tight', pad_inches=0.0)
def main(): args = parse_cmdline() img_fn = os.path.abspath(args.img_fn) if not os.path.exists(img_fn): print('Not found: {}'.format(img_fn)) sys.exit(-1) else: print('Target image: {}'.format(img_fn)) # Loaa target image target_image = util.load_target_image(img_fn) # Get object proposals object_proposals = util.get_object_proposals(target_image) # Setup computation graph graph_params = setup_graph() # Model initialize sess = tf.Session(graph=graph_params['graph']) tf.global_variables_initializer() if os.path.exists('models'): save_path = os.path.join('models', 'deep_logo_model') graph_params['saver'].restore(sess, save_path) print('Model restored') else: print('Initialized') # Logo recognition results = [] for obj_proposal in object_proposals: x, y, w, h = obj_proposal crop_image = target_image[y:y + h, x:x + w] results.append( logo_recognition(sess, crop_image, obj_proposal, graph_params)) del_idx = [] for i, result in enumerate(results): if result['pred_class'] == common.CLASS_NAME[-1]: del_idx.append(i) results = np.delete(results, del_idx) # Non-max suppression nms_results = util.nms(results, pred_prob_th=0.999999, iou_th=0.4) # Draw rectangles on the target image fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(6, 6)) ax.imshow(target_image) for result in nms_results: print(result) (x, y, w, h) = result['obj_proposal'] ax.text(x, y, result['pred_class'], fontsize=13, bbox=dict(facecolor='red', alpha=0.7)) rect = mpatches.Rectangle((x, y), w, h, fill=False, edgecolor='red', linewidth=1) ax.add_patch(rect) plt.show()
def main(file_name, graph_params, sess): img_fn = os.path.join("images", file_name) if not os.path.exists(img_fn): print('Not found: {}'.format(img_fn)) sys.exit(-1) else: print('Target image: {}'.format(img_fn)) # Load target image target_image = util.load_target_image(img_fn) #cv.normalize(target_image, target_image, 0, 255, cv.NORM_MINMAX) # limg = np.arcsinh(target_image) # limg /= limg.max() # low = np.percentile(limg, 0.25) # high = np.percentile(limg, 99.5) # opt_img = skie.exposure.rescale_intensity(limg, in_range=(low, high)) # target_image = opt_img # target_image = target_image.astype(np.float64) # Get object proposals object_proposals = util.get_object_proposals(target_image) # Logo recognition results = [] for obj_proposal in object_proposals: x, y, w, h = obj_proposal crop_image = target_image[y:y + h, x:x + w] results.append( logo_recognition(sess, crop_image, obj_proposal, graph_params)) del_idx = [] for i, result in enumerate(results): if result['pred_class'] == common.CLASS_NAME[-1]: del_idx.append(i) results = np.delete(results, del_idx) # Non-max suppression nms_results = util.nms(results, pred_prob_th=0.9, iou_th=0.4) # Draw rectangles on the target image fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(6, 6)) ax.imshow(target_image) for result in nms_results: print(result) (x, y, w, h) = result['obj_proposal'] ax.text(x, y, "{} {:.2f}".format(result['pred_class'], result['pred_prob']), fontsize=13, bbox=dict(facecolor='red', alpha=0.7)) rect = mpatches.Rectangle((x, y), w, h, fill=False, edgecolor='red', linewidth=1) ax.add_patch(rect) #img = BytesIO() plt.tight_layout() plt.savefig(os.path.join("results", file_name), bbox_inches='tight', pad_inches=0)