def demo(net, image_name, gt_boxes, result_dir, conf=0.75): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image im_file = image_name im = cv2.imread(im_file) im_height = im.shape[0] im_width = im.shape[1] # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = r_im_detect(net, im) # print "gt_margin: ", cfg.TEST.GT_MARGIN, cfg.TRAIN.GT_MARGIN # print "img_padding: ", cfg.IMG_PADDING 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 = conf NMS_THRESH = 0.3 for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 5 * cls_ind:5 * (cls_ind + 1)] # D cls_scores = scores[:, cls_ind] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = rotate_gpu_nms(dets, NMS_THRESH) # D dets = dets[keep, :] #dets = dets[0:20] #dets[:, 4] = dets[:, 4] * 0.45 dets[:, 2] = dets[:, 2] / cfg.TEST.GT_MARGIN dets[:, 3] = dets[:, 3] / cfg.TEST.GT_MARGIN #if imdb_name == "icdar13": #write_result_ICDAR2013(im_file, dets, CONF_THRESH, ori_result_dir, im_height, im_width) #result_file = write_result_ICDAR2013(im_file, dets, CONF_THRESH, result_dir, im_height, im_width) #print dets #if imdb_name == "icdar15": # write_result_ICDAR(im_file, dets, CONF_THRESH, ori_result_dir, im_height, im_width) results = write_result_ICDAR(im_file, dets, CONF_THRESH, result_dir, im_height, im_width) # write_result(im_file, dets, CONF_THRESH, ori_result_dir, im_height, im_width) # result_file = write_result(im_file, dets, CONF_THRESH, result_dir, im_height, im_width) # print "write done" # post_merge(result_file) # print "merge done" # #print "merge done" # vis_detections(im, cls, dets, gt_boxes, thresh=CONF_THRESH) return results
def detect(self, image_file, thresh=0.5): """ return format: [ label score x1 y1 x2 y2 x3 y3 x4 y4 ] """ # Load the demo image im = cv2.imread(image_file) im_height = im.shape[0] im_width = im.shape[1] # Detect all object classes and regress object bounds scores, boxes = r_im_detect(self.net, im) NMS_THRESH = 0.3 #################################### ### formate: [x_ctr y_ctr w h an score class] predections = np.empty((0, 7)) # the last all classes result ####################################### for cls_ind, cls in enumerate(self.classnames[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 5 * cls_ind:5 * (cls_ind + 1)] # D (300,5) cls_scores = scores[:, cls_ind] # (300,) cls_scores[:, np.newaxis] ===> (300,1) dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype( np.float32) # (300,5) + (300,1) ===> (300,6) keep = rotate_gpu_nms(dets, NMS_THRESH) # D dets = dets[keep, :] # only one class ############################ add the other axis to contain the class label #################### ############### (n,6) ===> (n,7) [ box(5) , score ] ===> [ box(5) , score , class ] label = np.ones_like(keep) dets = np.hstack((dets, label[:, np.newaxis] * cls_ind)) ########################### contact with different class ####################################### ############## (n1,7) + (n2,7) + (n3,7) + (n4,7) ===> (n1+n2+n3+n4,7) predections = np.vstack((predections, dets)) order = predections[:, 5] > thresh predections = predections[order] # get score > 0.5 dets[:, 2] = dets[:, 2] / 1.4 dets[:, 3] = dets[:, 3] / 1.4 predections[:, 2] = predections[:, 2] / 1.4 predections[:, 3] = predections[:, 3] / 1.4 pre = predections[:, 0:6] results = self.formatResult(predections, thresh, im_height, im_width) # [ label score x1 y1 x2 y2 x3 y3 x4 y4 ] return results
def demo(net, image_name, output_name):#, result_dir, ori_result_dir): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image im_file = image_name im = cv2.imread(im_file) im_height = im.shape[0] im_width = im.shape[1] # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = r_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.9 NMS_THRESH = 0.3 file_ret = "" for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 5*cls_ind:5*(cls_ind + 1)] # D cls_scores = scores[:, cls_ind] dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = rotate_gpu_nms(dets, NMS_THRESH) # D dets = dets[keep, :] #dets = dets[0:20] #dets[:, 4] = dets[:, 4] * 0.45 dets[:, 2] = dets[:, 2] / 1.4 dets[:, 3] = dets[:, 3] / 1.4 file_ret = vis_detections(im, cls, dets, output_name, thresh=CONF_THRESH) return file_ret
def demo(net, image_name, conf=0.1): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image im_file = image_name im = cv2.imread(im_file) #print(type(im),im.shape) im.resize((400, 300, 3)) #print(type(im),im.shape) im_height = im.shape[0] im_width = im.shape[1] # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = r_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 = conf NMS_THRESH = 0.3 #################################### ### formate: [x_ctr y_ctr w h an score class] predections = np.empty((0, 7)) # the last all classes result ####################################### for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 5 * cls_ind:5 * (cls_ind + 1)] # D (300,5) cls_scores = scores[:, cls_ind] # (300,) cls_scores[:, np.newaxis] ===> (300,1) dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype( np.float32) # (300,5) + (300,1) ===> (300,6) keep = rotate_gpu_nms(dets, NMS_THRESH) # D dets = dets[keep, :] # only one class ############################ add the other axis to contain the class label #################### ############### (n,6) ===> (n,7) [ box(5) , score ] ===> [ box(5) , score , class ] label = np.ones_like(keep) dets = np.hstack((dets, label[:, np.newaxis] * cls_ind)) ########################### contact with different class ####################################### ############## (n1,7) + (n2,7) + (n3,7) + (n4,7) ===> (n1+n2+n3+n4,7) predections = np.vstack((predections, dets)) order = predections[:, 5] > 0.5 predections = predections[order] # get score > 0.5 dets[:, 2] = dets[:, 2] / cfg.TEST.GT_MARGIN dets[:, 3] = dets[:, 3] / cfg.TEST.GT_MARGIN predections[:, 2] = predections[:, 2] / cfg.TEST.GT_MARGIN predections[:, 3] = predections[:, 3] / cfg.TEST.GT_MARGIN pre = predections[:, 0:6] return timer.total_time
if args.cpu_mode: caffe.set_mode_cpu() else: caffe.set_mode_gpu() caffe.set_device(args.gpu_id) cfg.GPU_ID = args.gpu_id net = caffe.Net(prototxt, caffemodel, caffe.TEST) print '\n\nLoaded network {:s}'.format(caffemodel) # Warmup on a dummy image im = 128 * np.ones((300, 500, 3), dtype=np.uint8) for i in xrange(2): _, _ = r_im_detect(net, im) im_names = [] gt_boxes = [] demo_dir = './data' for img in os.listdir(demo_dir): im_names.append(os.path.join(demo_dir, img)) gt_boxes.append([0, 0, 0, 0, 0]) # The detection results will save in cood_dir in txt cood_dir = "./result" # for im_idx in range(len(im_names)): # print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' # print 'Demo for data/demo/{}'.format(im_names[im_idx])
def demo(net, image_name, gt_boxes, result_dir, conf=0.1): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image im_file = image_name im = cv2.imread(im_file) im_height = im.shape[0] im_width = im.shape[1] # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = r_im_detect(net, im) # print "gt_margin: ", cfg.TEST.GT_MARGIN, cfg.TRAIN.GT_MARGIN # print "img_padding: ", cfg.IMG_PADDING #print("score'size:",scores.shape) # (300,5) class_num is 5 #print("\n\n\nboxes'size:",boxes.shape) # (300,25) each box has 5 info to locate the box,so 5 * class_num = 25 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 = conf NMS_THRESH = 0.3 #################################### ### formate: [x_ctr y_ctr w h an score class] predections = np.empty((0, 7)) # the last all classes result ####################################### for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 5 * cls_ind:5 * (cls_ind + 1)] # D (300,5) cls_scores = scores[:, cls_ind] # (300,) cls_scores[:, np.newaxis] ===> (300,1) dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype( np.float32) # (300,5) + (300,1) ===> (300,6) #print("dets111:",dets) keep = rotate_gpu_nms(dets, NMS_THRESH) # D dets = dets[keep, :] # only one class #print('keep:',keep) #print("cls_boxes:",cls_boxes.shape) #print("cls_scores:",cls_scores.shape) #print("dets:",dets) #print("dets:",dets.shape) #print type(cls_boxes) ############################ add the other axis to contain the class label #################### ############### (n,6) ===> (n,7) [ box(5) , score ] ===> [ box(5) , score , class ] label = np.ones_like(keep) dets = np.hstack((dets, label[:, np.newaxis] * cls_ind)) #print(dets) ########################### contact with different class ####################################### ############## (n1,7) + (n2,7) + (n3,7) + (n4,7) ===> (n1+n2+n3+n4,7) predections = np.vstack((predections, dets)) #print("predections:",predections) #print("class %d predections.shape:" % cls_ind,dets.shape) #dets = dets[0:20] #dets[:, 4] = dets[:, 4] * 0.45 #dets[:,:] = predections[:,0:6] #print("all predections.shape:",predections.shape) order = predections[:, 5] > 0.5 predections = predections[order] # get score > 0.5 # print("score > 0.5 predections.shape:",predections.shape) # print("result0:",predections) # print("confidence : ",predections[:,5]) dets[:, 2] = dets[:, 2] / cfg.TEST.GT_MARGIN dets[:, 3] = dets[:, 3] / cfg.TEST.GT_MARGIN predections[:, 2] = predections[:, 2] / cfg.TEST.GT_MARGIN predections[:, 3] = predections[:, 3] / cfg.TEST.GT_MARGIN pre = predections[:, 0:6] lab = predections[:, 6].astype(int).tolist() #print(type(lab),lab) # print("classes:",predections[:,6]) # print("labels:",CLASSES[lab[0]]) # print("result1:",predections) # print("rato h/w : ",predections[:,3]/predections[:,2]) # print("rato w/h : ",predections[:,2]/predections[:,3]) #if imdb_name == "icdar13": #write_result_ICDAR2013(im_file, dets, CONF_THRESH, ori_result_dir, im_height, im_width) #result_file = write_result_ICDAR2013(im_file, dets, CONF_THRESH, result_dir, im_height, im_width) #print dets #if imdb_name == "icdar15": # write_result_ICDAR(im_file, dets, CONF_THRESH, ori_result_dir, im_height, im_width) results = write_result_ICDAR(im_file, pre, CONF_THRESH, result_dir, im_height, im_width) # write_result(im_file, dets, CONF_THRESH, ori_result_dir, im_height, im_width) # result_file = write_result(im_file, dets, CONF_THRESH, result_dir, im_height, im_width) # print "write done" # post_merge(result_file) # print "merge done" # #print "merge done" #vis_detections(im, cls, dets, gt_boxes, thresh=CONF_THRESH) return results
def demo(net, image_name, gt_boxes, result_dir, ori_result_dir, conf = 0.75): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image im_file = image_name im = cv2.imread(im_file) im_height = im.shape[0] im_width = im.shape[1] # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = r_im_detect(net, im) print "gt_margin: ", cfg.TEST.GT_MARGIN, cfg.TRAIN.GT_MARGIN print "img_padding: ", cfg.IMG_PADDING 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 = conf NMS_THRESH = 0.3 for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 5*cls_ind:5*(cls_ind + 1)] # D cls_scores = scores[:, cls_ind] dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = rotate_gpu_nms(dets, NMS_THRESH) # D dets = dets[keep, :] #dets = dets[0:20] #dets[:, 4] = dets[:, 4] * 0.45 dets[:, 2] = dets[:, 2] / cfg.TEST.GT_MARGIN dets[:, 3] = dets[:, 3] / cfg.TEST.GT_MARGIN #if imdb_name == "icdar13": #write_result_ICDAR2013(im_file, dets, CONF_THRESH, ori_result_dir, im_height, im_width) #result_file = write_result_ICDAR2013(im_file, dets, CONF_THRESH, result_dir, im_height, im_width) #if imdb_name == "icdar15": write_result_ICDAR(im_file, dets, CONF_THRESH, ori_result_dir, im_height, im_width) result_file = write_result_ICDAR(im_file, dets, CONF_THRESH, result_dir, im_height, im_width) #write_result(im_file, dets, CONF_THRESH, ori_result_dir, im_height, im_width) #result_file = write_result(im_file, dets, CONF_THRESH, result_dir, im_height, im_width) print "write done" #post_merge(result_file) print "merge done" # #print "merge done" #vis_detections(im, cls, dets, gt_boxes, thresh=CONF_THRESH) return result_file