def demo(sess, net, im_file): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image print(im_file) im = cv2.imread(im_file) # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, im) timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format( timer.total_time, boxes.shape[0])) # Visualize detections for each class CONF_THRESH = 0.8 NMS_THRESH = 0.3 results = [] for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] res = vis_detections(im, cls, dets, thresh=CONF_THRESH) if res != None: results.append(res) return results
def demo(sess, net, image_name): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image im_file = os.path.join(cfg.DATA_DIR, 'demo', image_name) im = cv2.imread(im_file) # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, im) timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format(timer.total_time, boxes.shape[0])) # Visualize detections for each class CONF_THRESH = 0.8 NMS_THRESH = 0.3 output_path = os.path.join(cfg.DATA_DIR,'test_output') for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] name = image_name.split('.')[0] + '.txt' with open(os.path.join(output_path,name),'a') as f: for item in dets: f.write(str(item[0]) + '\t' + str(item[1]) + '\t' + str(item[2])+ '\t' + str(item[3]) + '\t' +str(item[4]) + '\n')
def demo(sess, net, image_name): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image once_time = 0 im = cv2.imread(img_path) # print('>>>>>>>', im.shape[0], im.shape[1]) # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, im) timer.toc() once_time = timer.total_time print('Detection took {:.3f}s for {:d} object proposals'.format( timer.total_time, boxes.shape[0])) # Visualize detections for each class CONF_THRESH = 0.85 NMS_THRESH = 0.3 inds = np.where(scores[:, 0] > CONF_THRESH)[0] scores = scores[inds, 0] boxes = boxes[inds, :] dets = np.hstack((boxes, scores[:, np.newaxis])).astype(np.float32, copy=False) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] print('>>>>>num_faces:', dets.shape[0]) cv2_vis(im, CLASSES[1], dets) return once_time
def demo(sess, net, image_name, direction='Temp'): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image im_file = os.path.join(cfg.DATA_DIR, direction, image_name) im = cv2.imread(im_file) # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, im) timer.toc() print('Detection took {:.3f}s '.format(timer.total_time)) # Visualize detections for each class CONF_THRESH = 0.3 NMS_THRESH = 0.6 #numaximum surpression a = [0, 0, 0, 0] i = 0 for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] # num_vehicle = num_vehicle + num_detections(im, cls, dets, thresh=CONF_THRESH) a[i] = a[i] + num_detections(im, cls, dets, thresh=CONF_THRESH) return a
def demo(sess, net, image_name): """Detect object classes in an image using pre-computed object proposals.""" """使用训练好的模型进行目标检测与识别""" # Load the demo image 载入图片 im_file = os.path.join(cfg.DATA_DIR, 'demo', image_name) im = cv2.imread(im_file) # Detect all object classes and regress object bounds # 检测所有类别并回归目标边界 timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, im) #目标检测 timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format(timer.total_time, boxes.shape[0])) # Visualize detections for each class # 检测结构可视化 CONF_THRESH = 0.8 #类别确认阈值 NMS_THRESH = 0.3 #区域检测阈值 for cls_ind, cls in enumerate(CLASSES[1:]): #字典枚举 cls_ind += 1 # because we skipped background 因为跳过了背景类别 cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)] #获取包围框 cls_scores = scores[:, cls_ind] #获取对应的分值 dets = np.hstack((cls_boxes, #将包围框与对应的分值整合 cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) #包围框筛选 dets = dets[keep, :] vis_detections(im, cls, dets, thresh=CONF_THRESH)
def demo(sess, net, im_file, result_dir, viz=False, oriented=False): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image im = helper.read_rgb_img(im_file) # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes, resized_im_shape, im_scale = im_detect(sess, net, im) timer.toc() im = cv2.cvtColor(im, cv2.COLOR_RGB2BGR) img_name = im_file.split('/')[-1] draw_rpn_boxes(im, img_name, boxes, scores[:, np.newaxis], im_scale, True, result_dir) draw_rpn_boxes(im, img_name, boxes, scores[:, np.newaxis], im_scale, False, result_dir) # Run TextDetector to merge small box line_detector = TextDetector(oriented) # line_detector 的输入必须是在 scale 之后的图片上!!, # 如果还原了以后再进行行构建,原图可能太大,导致每个 anchor 的 width 很大,导致 MAX_HORIZONTAL_GAP 太小 # text_lines point order: left-top, right-top, left-bottom, right-bottom text_lines = line_detector.detect(boxes, scores[:, np.newaxis], resized_im_shape) print("Image %s, detect %d text lines in %.3fs" % (im_file, len(text_lines), timer.diff)) if len(text_lines) != 0: text_lines = recover_scale(text_lines, im_scale) save_result(im, img_name, text_lines, result_dir) # Visualize detections if viz: vis_detections(im, CLASSES[1], text_lines)
def process_img(net, image_name): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image im_file = os.path.join(cfg.DATA_DIR, 'face', image_name) im = cv2.imread(im_file) # Detect all object classes and regress object bounds scores, boxes = im_detect(net, im) # Visualize detections for each class CONF_THRESH = 0.7 NMS_THRESH = 0.3 cls_ind = 1 # because we skipped background cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] # 300 x 5 (x1,y1,x2,y2,scores) dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) # NMS keep = nms(torch.from_numpy(dets), NMS_THRESH) dets = dets[keep.numpy(), :] crop_imgs = extract_faces(im, dets, thresh=CONF_THRESH) return crop_imgs
def img_test(net,image_name): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image im_file = os.path.join(image_name) im = cv2.imread(im_file) # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(net, im) timer.toc() # Visualize detections for each class CONF_THRESH = 0.9 NMS_THRESH = 0.3 box_out = [] for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(torch.from_numpy(dets), NMS_THRESH) dets = dets[keep.numpy(), :] inds = np.where(dets[:, -1] >= CONF_THRESH)[0] dets = dets[inds].astype(int)[:,:4] box_out.append(dets) return box_out
def demo(sess, net, color_image, depth_colormap): """Detect object classes in an image using pre-computed object proposals.""" # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, color_image) timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format(timer.total_time, boxes.shape[0])) # Visualize detections for each class CONF_THRESH = 0.7 NMS_THRESH = 0.3 dets_col = [] cls_col = [] for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] dets_col.append(dets) cls_col.append(cls) vis_detections(color_image, depth_colormap, cls_col, dets_col, thresh=CONF_THRESH) depth_col, bbox_col = calc_histogram(depth_image, cls_col, dets_col, thresh=CONF_THRESH) print("box depth:", depth_col[0], "sucker depth:", depth_col[1]) print("box bbox:", bbox_col[0], "sucker bbox", bbox_col[1])
def demo(sess, net, im_file, result_dir, viz=False, oriented=False): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image im = cv2.imread(im_file) # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, im) timer.toc() img_name = im_file.split('/')[-1] draw_rpn_boxes(im, img_name, boxes, scores[:, np.newaxis], True, result_dir) draw_rpn_boxes(im, img_name, boxes, scores[:, np.newaxis], False, result_dir) # Run TextDetector to merge small box line_detector = TextDetector(oriented) # text_lines point order: left-top, right-top, left-bottom, right-bottom text_lines = line_detector.detect(boxes, scores[:, np.newaxis], im.shape[:2]) print("Image %s, detect %d text lines in %.3fs" % (im_file, len(text_lines), timer.diff)) save_result(im, img_name, text_lines, result_dir) # Visualize detections if viz: vis_detections(im, CLASSES[1], text_lines)
def demo(sess, net, image_name, CONF_THRESH): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image im_file = os.path.join(cfg.DATA_DIR, 'demo', 'Images', image_name) im = cv2.imread(im_file, cv2.IMREAD_UNCHANGED) scene_name = image_name[:10] # 'scene_0021' image_index = image_name[11:15] # '0003' # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, im) timer.toc() NMS_THRESH = 0.3 image_grasps = [] for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] add_predicted_grasps(cls, dets, image_grasps, thresh=CONF_THRESH) image_grasps = np.array(image_grasps) np.save( os.path.join('..', 'predicted_rectangle_grasp', scene_name, graspnet_config.CAMERA_NAME, image_index), image_grasps)
def demo(sess, net, im): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image # im_file = os.path.join(cfg.DATA_DIR, 'demo', image_name) # im = cv2.imread(im_file) # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, im) timer.toc() print(('Detection took {:.3f}s for ' '{:d} object proposals').format(timer.total_time, boxes.shape[0])) frameRate = 1.0 / timer.total_time print("fps: " + str(frameRate)) # Visualize detections for each class CONF_THRESH = 0.8 NMS_THRESH = 0.3 for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] vis_detections(im, cls, dets, thresh=CONF_THRESH) cv2.putText(im, '{:s} {:.2f}'.format("FPS:", frameRate), (1750, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255))
def im_Detect_Highscore(sess, net, image, CONF_THRESH=0.8, NMS_THRESH=0.3): """Detect object classes in an image using pre-computed object proposals.""" im = image # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, im) timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format( timer.total_time, boxes.shape[0])) print('okokzzqtestgithub') # get a list of all high score classbox # if the classbox's score > CONF_THRESH, than this classbox will add the imageAllClass imageAllClass = [] for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] rightDect = vis_detections(cls, dets, thresh=CONF_THRESH) if rightDect is None: pass else: for iti in rightDect: imageAllClass.append(iti) return imageAllClass
def detect_one(name, thresh=0.75): im = cv2.imread(osp.join('/home/hezheqi/data/tmp/p2', name)) scores, polys = im_detect(sess, net, im) print(scores) boxes = np.zeros((polys.shape[0], 8), dtype=polys.dtype) boxes[:, 0] = np.min(polys[:, 0:8:2], axis=1) boxes[:, 1] = np.min(polys[:, 1:8:2], axis=1) boxes[:, 2] = np.max(polys[:, 0:8:2], axis=1) boxes[:, 3] = np.max(polys[:, 1:8:2], axis=1) boxes[:, 4] = np.min(polys[:, 8::2], axis=1) boxes[:, 5] = np.min(polys[:, 9::2], axis=1) boxes[:, 6] = np.max(polys[:, 8::2], axis=1) boxes[:, 7] = np.max(polys[:, 9::2], axis=1) for j in range(1, NUM_CLASSES): inds = np.where(scores[:, j] > thresh)[0] cls_scores = scores[inds, j] cls_boxes = boxes[inds, j * 4:(j + 1) * 4] cls_polys = polys[inds, j * 8:(j + 1) * 8] cls_dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])) \ .astype(np.float32, copy=False) cls_dets_poly = cls_polys.astype(np.float32, copy=False) keep = nms(cls_dets, cfg.TEST.NMS) # cls_dets = cls_dets[keep, :] cls_dets = cls_boxes[keep, :] cls_dets_poly = cls_dets_poly[keep, :] cls_scores = cls_scores[:, np.newaxis] cls_scores = cls_scores[keep, :] cls_dets = np.hstack((cls_dets, cls_dets_poly, cls_scores)) print(cls_dets) vis_detections(im, cls_dets) cv2.imwrite(osp.join(out_dir, name), im) fout = open(osp.join(out_dir, 'txt', name[:-4]+'.txt'), 'w') for det in cls_dets: fout.write('{}\n'.format(' '.join(str(int(d)) for d in det[4:12])))
def extract(sess, net, image_path): im = cv2.imread(image_path) scores, boxes, feature_maps = im_detect(sess, net, im) CONF_THRESH = 0.8 NMS_THRESH = 0.3 results = [] for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background # take only the top predictions per image cls_scores = scores[:, cls_ind] score_thresh = max(np.sort(cls_scores)[::-1][cfg.TEST.top_scores_image], cfg.TEST.min_score_thresh) cls_scores_indices = cls_scores > score_thresh cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = cls_scores[cls_scores_indices] cls_boxes = cls_boxes[cls_scores_indices] dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] feature_maps_nms = feature_maps[keep, :] results.append((dets, feature_maps_nms)) # vis_detections(im, cls, dets, thresh=CONF_THRESH) detected_objects = build_det_dict(results) return detected_objects
def demo(sess, net, image_name): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image im_file = os.path.join(cfg.DATA_DIR, 'demo', image_name) im = cv2.imread(im_file) # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, im) timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format(timer.total_time, boxes.shape[0])) # Visualize detections for each class CONF_THRESH = 0.4 NMS_THRESH = 0.3 classes_list = list() for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] if cls_ind==1: coordinates_list,ax = vis_detections(im, cls, dets, thresh=CONF_THRESH) classes_list.append(coordinates_list) else: classes_list.append(vis_detections_target(ax, cls, dets, thresh=CONF_THRESH)) return classes_list
def run_on_fddb(sess, net, image_name): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image im = cv2.imread(image_name) # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, im) timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format( timer.total_time, boxes.shape[0])) # Visualize detections for each class CONF_THRESH = 0.5 NMS_THRESH = 0.3 inds = np.where(scores[:, 0] > CONF_THRESH)[0] scores = scores[inds, 0] boxes = boxes[inds, :] dets = np.hstack((boxes, scores[:, np.newaxis])).astype(np.float32, copy=False) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] return dets
def demo(sess, net, image_name): # Load the demo image im_path = os.path.join(cfg.DATA_DIR, 'horus-test', 'images') im_file = os.path.join(im_path, image_name) im = cv2.imread(im_file) # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, im) timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format( timer.total_time, boxes.shape[0])) # Visualize detections for each class CONF_THRESH = 0.6 for cls_ind, cls in enumerate(cfg.CLASSES[1:]): cls_ind += 1 # because we skipped background inds = np.where(scores[:, cls_ind] >= CONF_THRESH)[0] cls_boxes = boxes[inds, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[inds, cls_ind] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, cfg.TEST.NMS) dets = dets[keep, :] print(dets) # vis_detections(im, cls, dets, thresh=CONF_THRESH) # inds = np.where(dets[:, -1] >= CONF_THRESH)[0] if len(dets) != 0: im = draw_bounding_boxes(im, dets, cls_ind, cls) cv2.imwrite(cfg.DATA_DIR + '/output/output_' + image_name, im)
def demo(sess, net, image_name): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image im_file = os.path.join(cfg.DATA_DIR, 'demo', image_name) im = cv2.imread(im_file) # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, im) timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format(timer.total_time, boxes.shape[0])) # Visualize detections for each class CONF_THRESH = 0.8 NMS_THRESH = 0.3 im = im[:, :, (2, 1, 0)] fig, ax = plt.subplots(figsize=(12, 12)) ax.imshow(im, aspect='equal') for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] vis_detections(im, cls, dets, ax, thresh=CONF_THRESH) plt.axis('off') plt.tight_layout() plt.draw()
def demo(sess, net, image_name,xminarr,yminarr,xmaxarr,ymaxarr,results,score_file,index_file,num_boxes,imagedir, mode): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image im_file = os.path.join(imagedir, image_name) im = cv2.imread(im_file) # Detect all object classes and regress object bounds if mode == 'fast': scores, boxes = im_detect_fast(sess, net, im) else: scores, boxes = im_detect(sess, net, im) # Visualize detections for each class CONF_THRESH = 0.1 # Visualize people cls_ind = 1 cls = 'person' cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep=soft_nms(dets,method=2) dets=keep if(dets.shape[0]!=0): index_file.write("{} {} ".format(image_name,num_boxes+1)) num_boxes = vis_detections(im, image_name, cls, dets,xminarr,yminarr,xmaxarr,ymaxarr,results,score_file,index_file,num_boxes, thresh=CONF_THRESH) if(dets.shape[0]!=0): index_file.write("{}\n".format(num_boxes)) return num_boxes
def demo(sess, net, image_name): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image # im_file = os.path.join(cfg.ROOT_DIR, 'text_img_dataset', 'data', 'fake_imgs', image_name) im_file = os.path.join(cfg.ROOT_DIR, 'text_img_dataset', 'data', 'real_imgs', image_name) # im_file = os.path.join(cfg.ROOT_DIR, 'text_img_dataset', 'data', 'Images', image_name) print(im_file) im = cv2.imread(im_file) # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, im) timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format( timer.total_time, boxes.shape[0])) CONF_THRESH = 0.8 NMS_THRESH = 0.3 # My code all_objects(im, scores, boxes, image_name, thresh=CONF_THRESH, nms_tresh=NMS_THRESH)
def basademo(net, image_name, dataname, exptname): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image #im_file = os.path.join('/om/user/mcusi/nnInit/pytorch-faster-rcnn/data/bASA/JPEGImages', image_name) im_file = '/om/user/mcusi/nnInit/pytorch-faster-rcnn/data/' + dataname + '/demos/' + image_name + '.jpg' print(im_file) im = cv2.imread(im_file) # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(net, im) timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format( timer.total_time(), boxes.shape[0])) # Visualize detections for each class CONF_THRESH = [1, 0.4, 0.6] NMS_THRESH = [1, 0.5, 0.3] elements = [] for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(torch.from_numpy(dets), NMS_THRESH[cls_ind]) dets = dets[keep.numpy(), :] fn = '/om2/user/mcusi/bayesianASA/cogsci2018/fig/' + dataname + image_name + cls + exptname + '.png' vis_detections(im, cls, dets, fn, thresh=CONF_THRESH[cls_ind])
def detect(self, image): scores, boxes, rboxes, quadboxes = im_detect(self.sess, self.net, image) # print('Detection {:d} object proposals'.format( boxes.shape[0])) # skip the backgound, only keep the text boxes inds = np.where(scores[:, 1] > self.conf_thresh)[0] txt_scores = scores[inds, 1] txt_boxes = boxes[inds, 4:8] txt_rboxes = rboxes[inds, 5:10] txt_quadboxes = quadboxes[inds, 8:16] txt_dets = np.hstack( (txt_boxes, txt_scores[:, np.newaxis])).astype(np.float32, copy=False) keep = nms(txt_dets, self.nms_thresh) txt_dets = txt_dets[keep, :] txt_rboxes = txt_rboxes[keep, :] txt_quadboxes = txt_quadboxes[keep, :] txt_dets = np.hstack((txt_dets, txt_rboxes, txt_quadboxes)) # return txt_dets regions = [] for txt_det in txt_dets: overlap, idx = iou(np.asarray(regions), txt_det) if overlap < self.iou_thresh: regions.append(txt_det) return regions
def demo(sess, net, image): """Detect object classes in an image using pre-computed object proposals.""" # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, image) timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format(timer.total_time, boxes.shape[0])) fig, ax = plt.subplots(figsize=(12, 12)) # Visualize detections for each class CONF_THRESH = 0.1 NMS_THRESH = 0.3 for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] vis_detections(ax, image, cls, dets, thresh=CONF_THRESH) plt.axis('off') plt.tight_layout() plt.draw()
def demo(sess, net, image_name): # 参数:net 测试时使用的网络结构;image_name:图片名称 """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image im_file = os.path.join(cfg.DATA_DIR, 'demo', image_name) im = cv2.imread(im_file) # Detect all object classes and regress object bounds timer = Timer() timer.tic() # from model.test import im_detect # def im_detect(sess, net, im): return scores, pred_boxes scores, boxes = im_detect(sess, net, im) # scores为<class 'numpy.ndarray'>,存放了300个object proposals的得分 # boxes为这300个框 timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format( timer.total_time, boxes.shape[0])) # Visualize detections for each class CONF_THRESH = 0.8 # score 阈值,最后画出候选框时需要,>thresh才会被画出 NMS_THRESH = 0.3 # 非极大值抑制的阈值,剔除重复候选框 for cls_ind, cls in enumerate(CLASSES[1:]): # CLASSES为元组,其中0是背景,故从1开始,但此时的ind0对应第一个标签 cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] # 300个框,4个坐标 cls_scores = scores[:, cls_ind] # 300个分数 # 将分数和框合并到一起成为一个新的(300, 5)矩阵, np.hstack():在水平方向上平铺 dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) # 进行非极大值抑制,得到抑制后的 dets dets = dets[keep, :] # keep 为留下的索引 vis_detections(im, cls, dets, thresh=CONF_THRESH) # 从索引1开始到~每个 cls 存在的 dets 进行显示
def demo3(sess, net, im): scores, boxes = im_detect(sess, net, im) CONF_THRESH = 0.5 NMS_THRESH = 0.3 result = None classes = [] for cls_ind, cls in enumerate(CLASSES[1:]): #print('a') cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) # print(cls) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] boxess = get_box(dets, CONF_THRESH) if boxess is not None: classes.append(cls) if result is None: result = boxess else: result = np.vstack((result, boxess)) return result, classes
def demo(sess, net, image_name): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image im_file = os.path.join(cfg.DATA_DIR, 'demo', image_name) im = cv2.imread(im_file) # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, im) timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format(timer.total_time, boxes.shape[0])) # Visualize detections for each class CONF_THRESH = 0.8 NMS_THRESH = 0.3 for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] vis_detections(im, cls, dets, thresh=CONF_THRESH)
def demo(sess, net, image_name): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image im_path = os.path.join(cfg.DATA_DIR, 'demo_video', 'video_to_images') im_file = os.path.join(im_path, image_name + '.jpg') im = cv2.imread(im_file) # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, im) timer.toc() print("Object detection for frame: " + image_name) # Visualize detections for each class CONF_THRESH = 0.6 for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background inds = np.where(scores[:, cls_ind] >= CONF_THRESH)[0] cls_boxes = boxes[inds, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[inds, cls_ind] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, cfg.TEST.NMS) dets = dets[keep, :] if len(dets) != 0: im = draw_bounding_boxes(im, dets, cls_ind, cls) cv2.imwrite(cfg.DATA_DIR + '/output/output_' + image_name, im)
def demo(net, image_name): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image im_file = os.path.join(cfg.DATA_DIR, 'demo', image_name) im = cv2.imread(im_file) # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(net, im) timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format( timer.total_time(), boxes.shape[0])) # Visualize detections for each class CONF_THRESH = 0.8 NMS_THRESH = 0.3 for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(torch.from_numpy(cls_boxes), torch.from_numpy(cls_scores), NMS_THRESH) dets = dets[keep.numpy(), :] vis_detections(im, cls, dets, thresh=CONF_THRESH)
def getImageLabelNum(sess, net, image_name): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image im = cv2.imread(image_name) # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, im) timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format(timer.total_time, boxes.shape[0])) # Visualize detections for each class CONF_THRESH = 0.8 NMS_THRESH = 0.3 countNum=0 labelList=[] for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] # vis_detections(im, cls, dets, thresh=CONF_THRESH) count=len(np.where(dets[:, -1] >= CONF_THRESH)[0]) if count!=0: countNum += count labelList.append(cls) return countNum,labelList
def demo(sess, net, image_name): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image #im_file = os.path.join(folder, image_name) #im_file = "/data_raid5/weijun/android_data/PNGImages/" + image_name #im_file = "data/VOCdevkit/android_data/PNGImages/" + image_name im = cv2.imread(image_name) im = im[:, :, (2, 1, 0)] #fig, ax = plt.subplots(figsize=(12, 12)) #ax.imshow(im, aspect='equal') # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, im) timer.toc() #print('Detection took {:.3f}s for {:d} object proposals'.format(timer.total_time, boxes.shape[0])) # Visualize detections for each class CONF_THRESH = 0.8 NMS_THRESH = 0.3 for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] #vis_detections(im, cls, dets, thresh=CONF_THRESH) #vis_detections(fig, ax, cls, dets, thresh=CONF_THRESH) create_clippings(image_name, cls, dets, thresh=CONF_THRESH)
def demo(sess, net, im): """Detect object classes in an image using pre-computed object proposals.""" # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, im) timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format( timer.total_time, boxes.shape[0])) # Visualize detections for each class CONF_THRESH = 0.7 NMS_THRESH = 0.3 for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background if cls != 'person' and cls != 'plasticbag' and cls != 'bottle': continue cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] vis_detections(im, cls, dets, thresh=CONF_THRESH) # Display the image with bouding boxes cv2.imshow('', im)
def demo(sess, net, mode, im): """Detect object classes in an image using pre-computed object proposals.""" if mode == 'fast': scores, boxes = im_detect_fast(sess, net, im) else: multi_scales = size scores, boxes = im_detect(sess, net, im, multi_scales) # Visualize detections for each class CONF_THRESH = soft_thresh # Visualize people for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # cls = 'pedestrian' #image_name=image_name.split('/')[-1].split('.')[0] cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = soft_nms(dets, sigma=0.6, Nt=0.3, method=2) dets = keep #print(dets) inds = np.where(dets[:, -1] >= CONF_THRESH)[0] bbox_list = [] for i in inds: bbox = dets[i, :4] score = np.round(dets[i, -1] * 1000) / 1000 xmin = round(bbox[0], 1) ymin = round(bbox[1], 1) xmax = round(bbox[2], 1) ymax = round(bbox[3], 1) bbox_list.append([xmin, ymin, xmax, ymax, score]) return bbox_list
def demo(net, im): """Detect object classes in an image using pre-computed object proposals.""" # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(net, im) timer.toc() #print('Detection took {:.3f}s for {:d} object proposals'.format(timer.total_time(), boxes.shape[0])) # Visualize detections for each class CONF_THRESH = 0.8 NMS_THRESH = 0.3 nms_keep_indices = [None] * len(CLASSES) for cls_ind, cls in enumerate(CLASSES[:]): cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(torch.from_numpy(dets), NMS_THRESH) dets = dets[keep.numpy(), :] nms_keep_indices[cls_ind] = keep.numpy().tolist() return scores, boxes, nms_keep_indices
def demo(sess, net, im_file, icdar_dir, oriented=False, ltrb=False): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image im = helper.read_rgb_img(im_file) # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes, resized_im_shape, im_scale = im_detect(sess, net, im) timer.toc() # Run TextDetector to merge small box line_detector = TextDetector(oriented) # text_lines point order: left-top, right-top, left-bottom, right-bottom text_lines = line_detector.detect(boxes, scores[:, np.newaxis], resized_im_shape) print("Image %s, detect %d text lines in %.3fs" % (im_file, len(text_lines), timer.diff)) if len(text_lines) != 0: text_lines = recover_scale(text_lines, im_scale) return save_result_txt(text_lines, icdar_dir, im_file, ltrb)
def demo(net, image_name): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image im_file = os.path.join(cfg.DATA_DIR, 'demo', image_name) im = cv2.imread(im_file) # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(net, im) timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format(timer.total_time(), boxes.shape[0])) # Visualize detections for each class thresh = 0.8 # CONF_THRESH NMS_THRESH = 0.3 im = im[:, :, (2, 1, 0)] fig, ax = plt.subplots(figsize=(12, 12)) ax.imshow(im, aspect='equal') cntr = -1 for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(torch.from_numpy(dets), NMS_THRESH) dets = dets[keep.numpy(), :] inds = np.where(dets[:, -1] >= thresh)[0] if len(inds) == 0: continue else: cntr += 1 for i in inds: bbox = dets[i, :4] score = dets[i, -1] ax.add_patch( plt.Rectangle((bbox[0], bbox[1]), bbox[2] - bbox[0], bbox[3] - bbox[1], fill=False, edgecolor=COLORS[cntr % len(COLORS)], linewidth=3.5) ) ax.text(bbox[0], bbox[1] - 2, '{:s} {:.3f}'.format(cls, score), bbox=dict(facecolor='blue', alpha=0.5), fontsize=14, color='white') ax.set_title('All detections with threshold >= {:.1f}'.format(thresh), fontsize=14) plt.axis('off') plt.tight_layout() plt.savefig('demo_' + image_name) print('Saved to `{}`'.format(os.path.join(os.getcwd(), 'demo_' + image_name)))
def demo(sess, net, im_file, icdar_dir, oriented=False, ltrb=False): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image im = helper.read_rgb_img(im_file) # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes, resized_im_shape, im_scale = im_detect(sess, net, im) timer.toc() # Run TextDetector to merge small box line_detector = TextDetector(oriented) # text_lines point order: left-top, right-top, left-bottom, right-bottom text_lines = line_detector.detect(boxes, scores[:, np.newaxis], resized_im_shape) print("Image %s, detect %d text lines in %.3fs" % (im_file, len(text_lines), timer.diff)) if len(text_lines) != 0: text_lines = recover_scale(text_lines, im_scale) return save_result_txt(text_lines, icdar_dir, im_file, ltrb)