def test(): import os im_file = 'demo/00001.jpg' # im_file = 'data/VOCdevkit2007/VOC2007/JPEGImages/009036.jpg' # im_file = '/media/longc/Data/data/2DMOT2015/test/ETH-Crossing/img1/000100.jpg' image = cv2.imread(im_file) model_file = './model/VGGnet_fast_rcnn_iter_70000.h5' # model_file = '/media/longc/Data/models/faster_rcnn_pytorch3/faster_rcnn_100000.h5' # model_file = '/media/longc/Data/models/faster_rcnn_pytorch2/faster_rcnn_2000.h5' detector = FasterRCNN() network.load_net(model_file, detector) detector.cuda() detector.eval() print('load model successfully!') # network.save_net(r'/media/longc/Data/models/VGGnet_fast_rcnn_iter_70000.h5', detector) # print('save model succ') t = Timer() t.tic() # image = np.zeros(shape=[600, 800, 3], dtype=np.uint8) + 255 dets, scores, classes = detector.detect(image, 0.7) runtime = t.toc() print('total spend: {}s'.format(runtime)) im2show = np.copy(image) for i, det in enumerate(dets): det = tuple(int(x) for x in det) cv2.rectangle(im2show, det[0:2], det[2:4], (255, 205, 51), 2) cv2.putText(im2show, '%s: %.3f' % (classes[i], scores[i]), (det[0], det[1] + 15), cv2.FONT_HERSHEY_PLAIN, 1.0, (0, 0, 255), thickness=1) cv2.imwrite(os.path.join('demo', 'out.jpg'), im2show)
def test(): import os im_file = 'demo/004545.jpg' image = cv2.imread(im_file) detector = FasterRCNN() network.load_net('/media/longc/Data/models/VGGnet_fast_rcnn_iter_70000.h5', detector) detector.cuda() print('load model successfully!') # network.save_net(r'/media/longc/Data/models/VGGnet_fast_rcnn_iter_70000.h5', detector) # print('save model succ') t = Timer() t.tic() # image = np.zeros(shape=[600, 800, 3], dtype=np.uint8) + 255 dets, scores, classes = detector.detect(image, 0.3) runtime = t.toc() print('total spend: {}s'.format(runtime)) im2show = np.copy(image) for i, det in enumerate(dets): if scores[i] < 0.3: continue det = tuple(int(x) for x in det) cv2.rectangle(im2show, det[0:2], det[2:4], (255, 205, 51), 2) cv2.putText(im2show, '%s: %.3f' % (classes[i], scores[i]), (det[0], det[1] + 15), cv2.FONT_HERSHEY_PLAIN, 1.0, (0, 0, 255), thickness=1) cv2.imwrite(os.path.join('demo', 'out.jpg'), im2show)
def test_net(net, dataset, max_per_image=300, thresh=0.05, vis=False, data_dir='./'): """Test a Fast R-CNN network on an image database.""" num_images = len(dataset) classes = ['__backround__', 'building'] # timers _t = {'im_detect': Timer(), 'misc': Timer()} all_boxes = [] total_time = 0.0 for i in range(num_images): im = cv2.imread(os.path.join(data_dir, dataset[i]['image_path'])) _t['im_detect'].tic() scores, boxes, current_time = im_detect(net, im) total_time += current_time detect_time = _t['im_detect'].toc(average=False) _t['misc'].tic() if vis: # im2show = np.copy(im[:, :, (2, 1, 0)]) im2show = np.copy(im) # skip j = 0, because it's the background class for j in xrange(1, len(classes)): current = np.concatenate([ boxes[:, j * 4:(j + 1) * 4], np.expand_dims(scores[:, 1], 1), np.ones((len(boxes), 1)) * i ], axis=1) all_boxes.extend(current[py_cpu_nms(current.astype(np.float32), 0.3)]) inds = np.where(scores[:, j] > thresh)[0] cls_scores = scores[inds, j] cls_boxes = boxes[inds, j * 4:(j + 1) * 4] cls_dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])) \ .astype(np.float32, copy=False) keep = py_cpu_nms(cls_dets, cfg.TEST.NMS) cls_dets = cls_dets[keep, :] if vis: im2show = vis_detections(im2show, classes[j], cls_dets) cv2.imwrite('samples/image_%d.jpg' % i, im2show) nms_time = _t['misc'].toc(average=False) print 'im_detect: {:d}/{:d} {:.3f}s {:.3f}s' \ .format(i + 1, num_images, detect_time, nms_time) df = pandas.DataFrame(all_boxes) df.columns = ['x1', 'y1', 'x2', 'y2', 'score', 'image_id'] df.to_csv('predictions.csv', index=False) print('Total time: %.4f, per image: %.4f' % (total_time, total_time / num_images))
def image_test(net, image_file, anno_file): tree = ET.parse(anno_file) size = tree.find('size') img_w = int(size.find('width').text) img_h = int(size.find('height').text) objs = tree.findall('object') num_objs = len(objs) boxes = np.zeros((num_objs, 4), dtype=np.int32) for ix, obj in enumerate(objs): bbox = obj.find('bndbox') cx = int(bbox.find('cx').text) cy = int(bbox.find('cy').text) wid = int(bbox.find('wid').text) hei = int(bbox.find('hei').text) theta = float(bbox.find('theta').text) #boxes[ix, :] = [cx, cy, wid, hei, theta] if theta > 0: rect = ((cx, cy), (wid, hei), -theta) else: rect = ((cx, cy), (hei, wid), -90 - theta) pts = cv2.boxPoints(rect) pts = np.array(pts, np.int32) xymin = np.min(pts, axis=0).tolist() xymax = np.max(pts, axis=0).tolist() xmin = max(0, xymin[0]) ymin = max(0, xymin[1]) xmax = min(img_w - 1, xymax[0]) ymax = min(img_h - 1, xymax[1]) boxes[ix, :] = [xmin, ymin, xmax, ymax] image = cv2.imread(image_file) t = Timer() t.tic() # image = np.zeros(shape=[600, 800, 3], dtype=np.uint8) + 255 dets, scores, classes = net.detect(image, 0.7) runtime = t.toc() print('total spend: {}s'.format(runtime)) im2show = np.copy(image) for box in boxes: box = tuple(int(x) for x in box) cv2.rectangle(im2show, box[0:2], box[2:4], (0, 0, 255), 2) for i, det in enumerate(dets): det = tuple(int(x) for x in det) cv2.rectangle(im2show, det[0:2], det[2:4], (255, 205, 51), 2) cv2.putText(im2show, '%s: %.3f' % (classes[i], scores[i]), (det[0], det[1] + 15), cv2.FONT_HERSHEY_PLAIN, 1.0, (0, 0, 255), thickness=1) im_name = os.path.basename(image_file) print(os.path.join('demo/det_results', im_name)) cv2.imwrite(os.path.join('demo/det_results', im_name), im2show)
def test_net_sg(name, net, imdb, mode='sg_det', max_per_image=100): """ Tests the network the stanford scene-graph way. :param net_name: :param weight_name: :param imdb: :param mode: :param max_per_image: :return: """ num_images = len(imdb.image_index) # timers _t = {'im_detect': Timer(), 'evaluate': Timer()} if mode == 'all': eval_modes = ['pred_cls', 'sg_cls', 'sg_det'] else: eval_modes = [mode] # initialize evaluator for each task evaluators = {} for m in eval_modes: evaluators[m] = SceneGraphEvaluator(imdb, mode=m) for im_i in range(num_images): im = imdb.im_getter(im_i) for m in eval_modes: # if mode == 'pred_cls' or mode == 'sg_cls': # use ground truth object locations # bbox_reg = False # box_proposals = gt_rois(roidb[im_i]) _t['im_detect'].tic() scores, boxes = im_detect(net, im, test_bbox_reg=True) if boxes.size == 0: continue _t['im_detect'].toc() _t['evaluate'].tic() evaluators[m].evaluate_scene_graph_entry( { 'boxes': boxes, 'scores': scores }, im_i, iou_thresh=0.5) _t['evaluate'].toc() print('im_detect: {:d}/{:d} {:.3f}s {:.3f}s' \ .format(im_i + 1, num_images, _t['im_detect'].average_time, _t['evaluate'].average_time)) # print out evaluation results for mode in eval_modes: evaluators[mode].print_stats()
def test(): import os im_file = 'demo/004545.jpg' # im_file = 'data/VOCdevkit2007/VOC2007/JPEGImages/009036.jpg' # im_file = '/media/longc/Data/data/2DMOT2015/test/ETH-Crossing/img1/000100.jpg' image = cv2.imread(im_file) # model_file = './VGGnet_fast_rcnn_iter_70000.h5' # model_file = '/media/longc/Data/models/faster_rcnn_pytorch3/faster_rcnn_100000.h5' # model_file = '/media/longc/Data/models/faster_rcnn_pytorch2/faster_rcnn_2000.h5' model_file = './models/saved_model_max/faster_rcnn_100000.h5' detector = FasterRCNN() network.load_net(model_file, detector) detector.cuda() detector.eval() print('load model successfully!') # network.save_net(r'/media/longc/Data/models/VGGnet_fast_rcnn_iter_70000.h5', detector) # print('save model succ') t = Timer() t.tic() # image = np.zeros(shape=[600, 800, 3], dtype=np.uint8) + 255 dets, scores, classes = detector.detect(image, 0.7) runtime = t.toc() print('total spend: {}s'.format(runtime)) im2show = np.copy(image) img = mpimg.imread(im_file) # Create figure and axes fig, ax = plt.subplots(1) # Display the image ax.imshow(img) # Create a Rectangle patch for i, det in enumerate(dets): w = det[2] - det[0] h = det[3] - det[1] rect = patches.Rectangle(det[0:2], w, h, linewidth=1, edgecolor='r', facecolor='none') # text plt.text(det[0], det[1], '%s: %.3f' % (classes[i], scores[i])) # Add the patch to the Axes ax.add_patch(rect) plt.show() print('aa')
def test(visualize=False): import os im_file = 'data/cervix/train/Type_2/1381.jpg' im_name = im_file.split('/')[-1] image = cv2.imread(im_file) # model_file = 'models/VGGnet_fast_rcnn_iter_70000.h5' model_file = 'models/saved_model3/faster_rcnn_100000.h5' expm = model_file.split('/')[-1].split('.')[0] expm_dir = os.path.join('demo', expm) if not os.path.exists(expm_dir): os.makedirs(expm_dir) detector = FasterRCNN() network.load_net(model_file, detector) detector.cuda() detector.eval( ) # set model in evaluation mode, has effect on Dropout and Batchnorm. Use train() to set train mode. print('load model successfully!') # network.save_net(r'/media/longc/Data/models/VGGnet_fast_rcnn_iter_70000.h5', detector) # print('save model succ') t = Timer() t.tic() # image = np.zeros(shape=[600, 800, 3], dtype=np.uint8) + 255 dets, scores, classes = detector.detect(image, 0.7) runtime = t.toc() print('total spend: {}s'.format(runtime)) im2show = np.copy(image) for i, det in enumerate(dets): det = tuple(int(x) for x in det) cv2.rectangle(im2show, det[0:2], det[2:4], (255, 205, 51), 4) cv2.putText(im2show, '%s: %.3f' % (classes[i], scores[i]), (det[0], det[1] + 15), cv2.FONT_HERSHEY_PLAIN, 1.0, (0, 0, 255), thickness=1) cv2.imwrite(os.path.join('demo', expm, im_name), im2show) if visualize: im2show = cv2.resize(im2show, None, None, fx=0.15, fy=0.15, interpolation=cv2.INTER_LINEAR) cv2.imshow('demo', im2show) cv2.waitKey(0)
def test(): import os img_file = 'demo/images.jpeg' image = cv2.imread(img_file) #imdb_name = 'CaltechPedestrians_train' imdb_name = 'coco_2017_train' #imdb_name = 'voc_2007_trainval' imdb = get_imdb(imdb_name) cfg_file = 'experiments/cfgs/faster_rcnn_end2end.yml' model_dir = 'data/pretrained_model/' #pre_model_name = 'VGGnet_fast_rcnn_iter_70000.h5' pre_model_name = 'coco_2017_train_10_vgg16_0.7_b1.h5' #pre_model_name = 'CaltechPedestrians_train_1_vgg16_0.7_b1.h5' pretrained_model = model_dir + pre_model_name cfg_from_file(cfg_file) print(imdb.classes) if 'vgg16' in pre_model_name.split('_'): detector = FasterRCNN_VGG(classes=imdb.classes, debug=False) elif 'resnet50' in pre_model_name.split('_'): detector = FasterRCNN_RES(classes=imdb.classes, debug=False) else: detector = FasterRCNN_VGG(classes=imdb.classes, debug=False) network.load_net(pretrained_model, detector) detector.cuda() detector.eval() print('load model successfully!') blob = init_data(is_cuda=True) t = Timer() t.tic() dets, scores, classes = detector.detect(image, blob, thr=0.7, nms_thresh=0.3) runtime = t.toc() print('total spend: {}s'.format(runtime)) im2show = np.copy(image) for i, det in enumerate(dets): det = tuple(int(x) for x in det) cv2.rectangle(im2show, det[0:2], det[2:4], (255, 205, 51), 2) cv2.putText(im2show, '%s: %.3f' % (classes[i], scores[i]), (det[0], det[1] + 15),\ cv2.FONT_HERSHEY_PLAIN, 1.0, (0, 0, 255), thickness=1) cv2.imwrite(os.path.join('demo', 'out.jpg'), im2show) cv2.imshow('demo', im2show) cv2.waitKey(0)
def imdb_proposals(net, imdb): """Generate RPN proposals on all images in an imdb.""" _t = Timer() imdb_boxes = [[] for _ in range(imdb.num_images)] for i in range(imdb.num_images): im = cv2.imread(imdb.image_path_at(i)) _t.tic() imdb_boxes[i], scores = im_proposals(net, im) _t.toc() print ('im_proposals: {:d}/{:d} {:.3f}s' \ .format(i + 1, imdb.num_images, _t.average_time)) if 0: dets = np.hstack((imdb_boxes[i], scores)) # from IPython import embed; embed() _vis_proposals(im, dets[:3, :], thresh=0.9) plt.show() return imdb_boxes
detector = FasterRCNN_VGG(classes=imdb.classes, debug=_DEBUG) network.load_net(pretrained_model, detector) blob = init_data(is_cuda=True) detector.cuda() detector.eval() name_blocks = pre_model_name.split('_') batch_size = imdb.num_triplet_test_images test_num = len(roidb) blob = init_data(is_cuda=True) features = [] bg_features = [] ids = [] print('Extracting features...') t = Timer() t.tic() for i in range(test_num): image = cv2.imread(roidb[i]['image']) gt_boxes = roidb[i]['boxes'].astype(np.float32) relu = True if 'relu' in name_blocks else False features.append( detector.extract_feature_vector(image, blob, gt_boxes, relu=relu).data.cpu().numpy()) ids.append(roidb[i]['ids'][0]) if BG_SHOW: bg_features.append( detector.extract_background_features(image, blob, gt_boxes, relu=relu).data.cpu().numpy())
def test_net(name, net, imdb, max_per_image=300, thresh=0.05, vis=False, vis_gt=False, vis_proposals=False): """Test a Fast R-CNN network on an image database.""" num_images = len(imdb.image_index) # all detections are collected into: # all_boxes[cls][image] = N x 5 array of detections in # (x1, y1, x2, y2, score) all_boxes = [[[] for _ in range(num_images)] for _ in range(imdb.num_classes)] output_dir = get_output_dir(imdb, name) # timers _t = {'im_detect': Timer(), 'misc': Timer()} det_file = os.path.join(output_dir, 'detections.pkl') for i in range(num_images): im = cv2.imread(imdb.image_path_at(i)) _t['im_detect'].tic() scores, boxes = im_detect(net, im) detect_time = _t['im_detect'].toc(average=False) if vis: # im2show = np.copy(im[:, :, (2, 1, 0)]) im2show = np.copy(im) if vis and vis_proposals: for j in range(scores.shape[0]): bbox_1 = tuple(x_1 for x_1 in boxes[j, :4]) cv2.rectangle(im2show, bbox_1[0:2], bbox_1[2:4], (0, j / 300 * 255, j / 300 * 255), 1) _t['misc'].tic() # skip j = 0, because it's the background class for j in range(1, imdb.num_classes): inds = np.where(scores[:, j] > thresh)[0] cls_scores = scores[inds, j] cls_boxes = boxes[inds, j * 4:(j + 1) * 4] cls_dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])) \ .astype(np.float32, copy=False) keep = nms(cls_dets, cfg.TEST.NMS) cls_dets = cls_dets[keep, :] if vis: im2show = vis_detections(im2show, imdb.classes[j], cls_dets, thresh) all_boxes[j][i] = cls_dets # Limit to max_per_image detections *over all classes* if max_per_image > 0: image_scores = np.hstack( [all_boxes[j][i][:, -1] for j in range(1, imdb.num_classes)]) if len(image_scores) > max_per_image: image_thresh = np.sort(image_scores)[-max_per_image] for j in range(1, imdb.num_classes): keep = np.where(all_boxes[j][i][:, -1] >= image_thresh)[0] all_boxes[j][i] = all_boxes[j][i][keep, :] nms_time = _t['misc'].toc(average=False) print('im_detect: {:d}/{:d} {:.3f}s {:.3f}s' \ .format(i + 1, num_images, detect_time, nms_time)) if vis_gt and vis: annopath_1 = os.path.join(imdb._devkit_path, 'VOC' + imdb._year, 'Annotations', '{:s}.xml') recs_1 = parse_rec(annopath_1.format(imdb._image_index[i])) bbox_2 = np.array([x['bbox'] for x in recs_1]) for d in range(len(recs_1)): bbox_1 = tuple(x_1 for x_1 in bbox_2[d, :4]) cv2.rectangle(im2show, bbox_1[0:2], bbox_1[2:4], (0, 0, 255), 1) if vis: cv2.imshow('test', im2show) cv2.waitKey(0) with open(det_file, 'wb') as f: pickle.dump(all_boxes, f, pickle.HIGHEST_PROTOCOL) print('Evaluating detections') imdb.evaluate_detections(all_boxes, output_dir)
# with open(idx_file, 'r') as f: # lines = f.readlines() # img_paths = [os.path.join(data_dir, l.strip().split()[0]) for l in lines] model_file = 'models/B300_FG03/faster_rcnn_100000.h5' expm = model_file.split('/')[-1].split('.')[0] expm_dir = os.path.join('demo', expm) if not os.path.exists(expm_dir): os.makedirs(expm_dir) detector = FasterRCNN() network.load_net(model_file, detector) detector.cuda() detector.eval() # set model in evaluation mode, has effect on Dropout and Batchnorm. Use train() to set train mode. print('load model successfully!\nConverting... please wait') t = Timer() raw_img_types = set() num_total = 0 for directory in directories: num_total_in_class = 0 dest_subdir = os.path.join(dest_dir, os.path.basename(directory)) for filename in os.listdir(directory): t.tic() suffix = filename.split('.')[-1] raw_img_types.add(suffix) path = os.path.join(directory, filename) try: img = cv2.imread(path) img_name = filename[:-len(suffix)] + dest_type dets, scores, classes = detector.detect(img, 0.7)
def test(): import os im_file = 'demo/004545.jpg' # im_file = 'data/VOCdevkit2007/VOC2007/JPEGImages/009036.jpg' # im_file = '/disk2/data/ILSVRC2015/DET/Data/DET/val/ILSVRC2013_val_00004599.JPEG' image = cv2.imread(im_file) model_file = '/disk2/data/pytorch_models/trained_models/resnet152_imgsize1000/saved_model3/faster_rcnn_200000.h5' # model_file = '/media/longc/Data/models/faster_rcnn_pytorch3/faster_rcnn_100000.h5' # model_file = '/media/longc/Data/models/faster_rcnn_pytorch2/faster_rcnn_2000.h5' classes = np.array(['__background__',\ 'n02672831', 'n02691156', 'n02219486', 'n02419796', 'n07739125', 'n02454379',\ 'n07718747', 'n02764044', 'n02766320', 'n02769748', 'n07693725', 'n02777292',\ 'n07753592', 'n02786058', 'n02787622', 'n02799071', 'n02802426', 'n02807133',\ 'n02815834', 'n02131653', 'n02206856', 'n07720875', 'n02828884', 'n02834778',\ 'n02840245', 'n01503061', 'n02870880', 'n02879718', 'n02883205', 'n02880940',\ 'n02892767', 'n07880968', 'n02924116', 'n02274259', 'n02437136', 'n02951585', 'n02958343', 'n02970849', 'n02402425', 'n02992211', 'n01784675', 'n03000684',\ 'n03001627', 'n03017168', 'n03062245', 'n03063338', 'n03085013', 'n03793489',\ 'n03109150', 'n03128519', 'n03134739', 'n03141823', 'n07718472', 'n03797390',\ 'n03188531', 'n03196217', 'n03207941', 'n02084071', 'n02121808', 'n02268443',\ 'n03249569', 'n03255030', 'n03271574', 'n02503517', 'n03314780', 'n07753113',\ 'n03337140', 'n03991062', 'n03372029', 'n02118333', 'n03394916', 'n01639765',\ 'n03400231', 'n02510455', 'n01443537', 'n03445777', 'n03445924', 'n07583066',\ 'n03467517', 'n03483316', 'n03476991', 'n07697100', 'n03481172', 'n02342885',\ 'n03494278', 'n03495258', 'n03124170', 'n07714571', 'n03513137', 'n02398521',\ 'n03535780', 'n02374451', 'n07697537', 'n03584254', 'n01990800', 'n01910747',\ 'n01882714', 'n03633091', 'n02165456', 'n03636649', 'n03642806', 'n07749582',\ 'n02129165', 'n03676483', 'n01674464', 'n01982650', 'n03710721', 'n03720891',\ 'n03759954', 'n03761084', 'n03764736', 'n03770439', 'n02484322', 'n03790512',\ 'n07734744', 'n03804744', 'n03814639', 'n03838899', 'n07747607', 'n02444819',\ 'n03908618', 'n03908714', 'n03916031', 'n00007846', 'n03928116', 'n07753275',\ 'n03942813', 'n03950228', 'n07873807', 'n03958227', 'n03961711', 'n07768694',\ 'n07615774', 'n02346627', 'n03995372', 'n07695742', 'n04004767', 'n04019541',\ 'n04023962', 'n04026417', 'n02324045', 'n04039381', 'n01495701', 'n02509815',\ 'n04070727', 'n04074963', 'n04116512', 'n04118538', 'n04118776', 'n04131690',\ 'n04141076', 'n01770393', 'n04154565', 'n02076196', 'n02411705', 'n04228054',\ 'n02445715', 'n01944390', 'n01726692', 'n04252077', 'n04252225', 'n04254120',\ 'n04254680', 'n04256520', 'n04270147', 'n02355227', 'n02317335', 'n04317175',\ 'n04330267', 'n04332243', 'n07745940', 'n04336792', 'n04356056', 'n04371430',\ 'n02395003', 'n04376876', 'n04379243', 'n04392985', 'n04409515', 'n01776313',\ 'n04591157', 'n02129604', 'n04442312', 'n06874185', 'n04468005', 'n04487394',\ 'n03110669', 'n01662784', 'n03211117', 'n04509417', 'n04517823', 'n04536866',\ 'n04540053', 'n04542943', 'n04554684', 'n04557648', 'n04530566', 'n02062744',\ 'n04591713', 'n02391049']) detector = FasterRCNN(classes) network.load_net(model_file, detector) detector.cuda() detector.eval() print('load model successfully!') # network.save_net(r'/media/longc/Data/models/VGGnet_fast_rcnn_iter_70000.h5', detector) # print('save model succ') t = Timer() t.tic() # image = np.zeros(shape=[600, 800, 3], dtype=np.uint8) + 255 dets, scores, classes = detector.detect(image, 0.) print "classes:{},scores:{}".format(classes, scores) runtime = t.toc() print('total spend: {}s'.format(runtime)) im2show = np.copy(image) for i, det in enumerate(dets): det = tuple(int(x) for x in det) cv2.rectangle(im2show, det[0:2], det[2:4], (255, 205, 51), 2) cv2.putText(im2show, '%s: %.3f' % (classes[i], scores[i]), (det[0], det[1] + 15), cv2.FONT_HERSHEY_PLAIN, 1.0, (0, 0, 255), thickness=1) cv2.imwrite(os.path.join('demo', 'out.jpg'), im2show)
def test_net(net, imdb, max_per_image=300, thresh=0.05, vis=False): """Test a Fast R-CNN network on an image database.""" num_images = len(imdb.image_index) print 'num_images',num_images # all detections are collected into: # all_boxes[cls][image] = N x 5 array of detections in # (x1, y1, x2, y2, score) all_boxes = [[[] for _ in xrange(num_images)] for _ in xrange(imdb.num_classes)] # output_dir = get_output_dir(imdb, name) # timers _t = {'im_detect': Timer(), 'misc': Timer()} # det_file = os.path.join(output_dir, 'detections.pkl') old_dets=None for i in range(num_images): im = cv2.imread(imdb.image_path_at(i)) _t['im_detect'].tic() scores, boxes = im_detect(net, im) detect_time = _t['im_detect'].toc(average=False) _t['misc'].tic() if vis or sav: # im2show = np.copy(im[:, :, (2, 1, 0)]) im2show = np.copy(im) # skip j = 0, because it's the background class for j in xrange(1, imdb.num_classes): inds = np.where(scores[:, j] > thresh)[0] cls_scores = scores[inds, j] cls_boxes = boxes[inds, j * 4:(j + 1) * 4] cls_dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])) \ .astype(np.float32, copy=False) cls_dets = filterThresh(cls_dets,im,10) cls_dets = filterShape(cls_dets) keep = nms(cls_dets, cfg.TEST.NMS) cls_dets = cls_dets[keep, :] #check if new dets were there before, # if not then : the former detection was a misdetection if vis: im2show = vis_detections(im2show, imdb.classes[j], cls_dets,thresh) all_boxes[j][i] = cls_dets if i>0: if len(all_boxes[j][i-1])>0: # print all_boxes[j][i-1] old_cls_dets=all_boxes[j][i-1] keep=np.zeros((old_cls_dets.shape[0])) for l in range(old_cls_dets.shape[0]): #compute IoU old_x1 = old_cls_dets[l, 0] old_y1 = old_cls_dets[l, 1] old_x2 = old_cls_dets[l, 2] old_y2 = old_cls_dets[l, 3] area = (old_x2 - old_x1 + 1.) * (old_y2 - old_y1 + 1.) if len(all_boxes[j][i])>0: for k in range(cls_dets.shape[0]): new_x1 = cls_dets[k,0] new_y1 = cls_dets[k,1] new_x2 = cls_dets[k,2] new_y2 = cls_dets[k,3] w = max(0.,min(old_x2, new_x2) - max(old_x1, new_x1) + 1.) h = max(0.,min(old_y2, new_y2) - max(old_y1, new_y1) + 1.) inter = w * h iou=inter/area if iou>0.5: keep[l]=1 for l in range(old_cls_dets.shape[0]): if not keep[l]: print "image[",i-1,"]", "false detection[",l,"]" # Limit to max_per_image detections *over all classes* if max_per_image > 0: image_scores = np.hstack([all_boxes[j][i][:, -1]for j in xrange(1, imdb.num_classes)]) if len(image_scores) > max_per_image: image_thresh = np.sort(image_scores)[-max_per_image] for j in xrange(1, imdb.num_classes): keep = np.where(all_boxes[j][i][:, -1] >= image_thresh)[0] all_boxes[j][i] = all_boxes[j][i][keep, :] nms_time = _t['misc'].toc(average=False) print 'im_detect: {:d}/{:d} {:.3f}s {:.3f}s' \ .format(i + 1, num_images, detect_time, nms_time) # if vis: # cv2.imshow('test', im2show) # cv2.waitKey(1) if sav: cv2.imwrite(output_dir_detections+str(i)+'.png', im2show) with open(det_file, 'wb') as f: cPickle.dump(all_boxes, f, cPickle.HIGHEST_PROTOCOL) print 'Evaluating detections' imdb.evaluate_detections(all_boxes, output_dir)
def test(): import os # im_file = 'demo/004545.jpg' # im_file = 'data/VOCdevkit2007/VOC2007/JPEGImages/009036.jpg' # im_file = '/media/longc/Data/data/2DMOT2015/test/ETH-Crossing/img1/000100.jpg' matName = 'exp5bC9.mat' model_file = '/home/dong/PycharmProjects/fasterRCNN/faster_rcnn_pytorch-master/model/CLASP_m_rotation_withNoRot_More/faster_rcnn_20000.h5' # model_file = '/media/longc/Data/models/faster_rcnn_pytorch3/faster_rcnn_100000.h5' # model_file = '/media/longc/Data/models/faster_rcnn_pytorch2/faster_rcnn_2000.h5' #CLASP_class = np.asarray(['__background__', # always index 0 # 'person', 'bin']) UCF_class = np.asarray(['__background__', 'person', 'bin']) label = UCF_class[1:] #CLASP_class[1:] detector = FasterRCNN(UCF_class) #CLASP_class network.load_net(model_file, detector) detector.cuda() detector.eval() print('load model successfully!') #filename = "/home/dong/PycharmProjects/fasterRCNN/faster_rcnn_pytorch-master/CLASP/video/07212017_EXPERIMENT_9A_Aug7/mp4s/Camera_9.mp4" #vid = imageio.get_reader(filename, 'ffmpeg') imgPath = "/home/dong/PycharmProjects/fasterRCNN/faster_rcnn_pytorch-master/CLASP/exp5bC9/exp5bC9/" #"/home/dong/PycharmProjects/fasterRCNN/faster_rcnn_pytorch-master/CLASP/C11_50_selected/" imgType = '*.jpg' image_list = [] for filename in glob.glob(imgPath + imgType): # assuming jpg #im = Image.open(filename) image_list.append(filename) #im.close() spliter = 'Frame' #'Frame' result = {x: np.zeros([1, 5]) for x in label} for i, name in enumerate(image_list): ele = Image.open(name) image = np.asarray(ele) str = ele.filename str = str.split(spliter)[1].split('.')[0] ind = int(str) t = Timer() t.tic() dets, scores, classes = detector.detect(image, 0.7) runtime = t.toc() for j, label in enumerate(classes): tmp = np.empty([1, 5]) tmp[0][0:4] = dets[j] tmp[0][4] = ind if result[label].max() == 0: result[label][0] = tmp else: result[label] = np.append(result[label], tmp, axis=0) print('Progress: {a:8.2f}%'.format(a=i * 100.0 / image_list.__len__())) print('total spend: {}s'.format(runtime)) ele.close() sio.savemat(matName, result) #result_9AC11_selected.mat #for im in enumerate(vid): #image = np.asarray(im) # network.save_net(r'/media/longc/Data/models/VGGnet_fast_rcnn_iter_70000.h5', detector) # print('save model succ') # image = np.zeros(shape=[600, 800, 3], dtype=np.uint8) + 255 '''
def test_net(name, net, imdb, max_per_image=300, thresh=1 / 7.0, vis=False, iters="0000", ext=[]): """Test a Fast R-CNN network on an image database.""" vis_www = True num_images = len(imdb.image_index) # all detections are collected into: # all_boxes[cls][image] = N x 5 array of detections in # (x1, y1, x2, y2, score) all_boxes = [[[] for _ in xrange(num_images)] for _ in xrange(imdb.num_classes)] output_dir = get_output_dir(imdb, name) # timers _t = {'im_detect': Timer(), 'misc': Timer()} det_file = os.path.join(output_dir, 'detections.pkl') images = [] print("in test process, plz wait, total %d items", num_images) for i in tqdm(range(num_images)): im = cv2.imread(imdb.image_path_at(i)) _t['im_detect'].tic() scores, boxes = im_detect(net, im) detect_time = _t['im_detect'].toc(average=False) _t['misc'].tic() if vis: # im2show = np.copy(im[:, :, (2, 1, 0)]) im2show = np.copy(im) if vis_www: im2show = np.copy(im) images.append(np.copy(im)) # skip j = 0, because it's the background class for j in range(1, imdb.num_classes): inds = np.where(scores[:, j] > thresh)[0] cls_scores = scores[inds, j] cls_boxes = boxes[inds, j * 4:(j + 1) * 4] cls_dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])) \ .astype(np.float32, copy=False) keep = nms(cls_dets, cfg.TEST.NMS) cls_dets = cls_dets[keep, :] if vis: im2show = vis_detections(im2show, imdb.classes[j], cls_dets, thresh) if vis_www: im2show = vis_detections(im2show, imdb.classes[j], cls_dets, thresh) all_boxes[j][i] = cls_dets # Limit to max_per_image detections *over all classes* if max_per_image > 0: image_scores = np.hstack( [all_boxes[j][i][:, -1] for j in xrange(1, imdb.num_classes)]) if len(image_scores) > max_per_image: image_thresh = np.sort(image_scores)[-max_per_image] for j in xrange(1, imdb.num_classes): keep = np.where(all_boxes[j][i][:, -1] >= image_thresh)[0] all_boxes[j][i] = all_boxes[j][i][keep, :] nms_time = _t['misc'].toc(average=False) # print 'im_detect: {:d}/{:d} {:.3f}s {:.3f}s' \ # .format(i + 1, num_images, detect_time, nms_time) if vis: cv2.imshow('test', im2show) cv2.waitKey(1) if vis_www: images.append(np.copy(im2show)) with open(det_file, 'wb') as f: cPickle.dump(all_boxes, f, cPickle.HIGHEST_PROTOCOL) vis_dir = os.path.join(output_dir, "..", "..", "..", "www", "_".join(ext)) print(vis_dir) if not os.path.exists(vis_dir): os.makedirs(vis_dir) visImg.writeImgHTML(images, iters, "test", 2, vis_dir) print 'Evaluating detections' imdb.evaluate_detections(all_boxes, output_dir)
def get_preds(name, net, imdb, max_per_image=300, thresh=0.05, test_bbox_reg=True, vis=False): """Extract predictions and visual features (of the GT boxes).""" num_images = len(imdb.image_index) output_dir = get_output_dir(imdb, name) # timers _t = {'im_detect': Timer(), 'misc': Timer()} det_file = os.path.join(output_dir, 'saved_feats.pkl') dets = [] for i in range(num_images): all_boxes = [[] for j in range(imdb.num_classes)] gt_boxes = imdb.all_boxes[ imdb.im_to_first_box[i]:imdb.im_to_last_box[i] + 1, :] gt_classes = imdb.labels[imdb. im_to_first_box[i]:imdb.im_to_last_box[i] + 1] im = imdb.im_getter(i) _t['im_detect'].tic() scores, boxes = im_detect(net, im) detect_time = _t['im_detect'].toc(average=False) _t['misc'].tic() # skip j = 0, because it's the background class for j in range(1, imdb.num_classes): inds = np.where(scores[:, j] > thresh)[0] cls_scores = scores[inds, j] cls_boxes = boxes[inds, j * 4:(j + 1) * 4] cls_dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32, copy=False) keep = nms(cls_dets, cfg.TEST.NMS) cls_dets = cls_dets[keep, :] all_boxes[j] = cls_dets # Limit to max_per_image detections *over all classes* if max_per_image > 0: image_scores = np.hstack( [all_boxes[j][:, -1] for j in range(1, imdb.num_classes)]) if len(image_scores) > max_per_image: image_thresh = np.sort(image_scores)[-max_per_image] for j in range(1, imdb.num_classes): keep = np.where(all_boxes[j][:, -1] >= image_thresh)[0] all_boxes[j] = all_boxes[j][keep, :] nms_time = _t['misc'].toc(average=False) dets.append(make_results_tuple(all_boxes, gt_classes, gt_boxes)) if i % 100 == 0: print('im_detect: {:d}/{:d} {:.3f}s {:.3f}s'.format( i + 1, num_images, detect_time, nms_time), flush=True) with open(det_file, 'wb') as f: pickle.dump(dets, f, pickle.HIGHEST_PROTOCOL)
def test(): import os imdb_name = 'CaltechPedestrians_test' imdb = get_imdb(imdb_name) cfg_file = 'experiments/cfgs/faster_rcnn_end2end.yml' model_dir = 'data/pretrained_model/' pre_model_name = 'CaltechPedestrians_train_10_vgg16_0.7_b3.h5' pretrained_model = model_dir + pre_model_name cfg_from_file(cfg_file) if 'vgg16' in pre_model_name.split('_'): detector = FasterRCNN_VGG(classes=imdb.classes, debug=False) elif 'res' in pre_model_name.split('_'): detector = FasterRCNN_RES(classes=imdb.classes, debug=False) else: detector = FasterRCNN_VGG(classes=imdb.classes, debug=False) network.load_net(pretrained_model, detector) detector.cuda() detector.eval() print('load model successfully!') blob = init_data(is_cuda=True) t = Timer() t.tic() cap = cv2.VideoCapture(video_file) init = True while (cap.isOpened()): ret, frame = cap.read() if ret: p = Timer() p.tic() if init: cnt = 1 fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter(output_file, fourcc, fps, (frame.shape[1], frame.shape[0])) init = False try: dets, scores, classes = detector.detect(frame, blob, thr=0.7, nms_thresh=0.3) frame = np.copy(frame) for i, det in enumerate(dets): det = tuple(int(x) for x in det) cv2.rectangle(frame, det[0:2], det[2:4], (255, 205, 51), 2) # cv2.putText(frame, '%s: %.3f' % (classes[i], scores[i]), (det[0], det[1] + 15), \ # cv2.FONT_HERSHEY_PLAIN, 1.0, (0, 0, 255), thickness=1) cv2.imshow('demo', frame) cv2.waitKey(1000) cv2.destroyAllWindows() except IndexError as e: pass finally: print(cnt, '-frame : {:.3f}s'.format(p.toc())) cnt += 1 out.write(frame) else: break runtime = t.toc() print('{} frames / total spend: {}s / {:2.1f} fps'.format( cnt, int(runtime), cnt / runtime)) cap.release() out.release()
def test_net(name, net, imdb, max_per_image=300, thresh=0.05, test_bbox_reg=True, vis=False): """Test a Fast R-CNN network on an image database.""" num_images = len(imdb.image_index) # all detections are collected into: # all_boxes[cls][image] = N x 5 array of detections in # (x1, y1, x2, y2, score) all_boxes = [[[] for _ in range(num_images)] for _ in range(imdb.num_classes)] output_dir = get_output_dir(imdb, name) # timers _t = {'im_detect': Timer(), 'misc': Timer()} det_file = os.path.join(output_dir, 'detections.pkl') cmap = get_cmap(imdb.num_classes) for i in range(num_images): im = imdb.im_getter(i) _t['im_detect'].tic() scores, boxes = im_detect(net, im, test_bbox_reg) detect_time = _t['im_detect'].toc(average=False) _t['misc'].tic() if vis: # im2show = np.copy(im[:, :, (2, 1, 0)]) im2show = np.copy(im, 'C') im2show_gt = np.copy(im, 'C') # skip j = 0, because it's the background class for j in range(1, imdb.num_classes): inds = np.where(scores[:, j] > thresh)[0] cls_scores = scores[inds, j] cls_boxes = boxes[inds, j * 4:(j + 1) * 4] cls_dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])) \ .astype(np.float32, copy=False) keep = nms(cls_dets, cfg.TEST.NMS) cls_dets = cls_dets[keep, :] if vis: im2show = vis_detections(im2show, imdb.classes[j], cls_dets, thresh=thresh, color=cmap(j)) gt_boxes = imdb.all_boxes[ imdb.im_to_first_box[i]:imdb.im_to_last_box[i] + 1, :] gt_classes = imdb.labels[imdb.im_to_first_box[i]:imdb. im_to_last_box[i] + 1] gt_dets = np.hstack(( gt_boxes, np.ones((gt_boxes.shape[0], 1)), ))[gt_classes == j] im2show_gt = vis_detections(im2show_gt, imdb.classes[j], gt_dets, thresh=thresh, color=cmap(j)) all_boxes[j][i] = cls_dets # Limit to max_per_image detections *over all classes* if max_per_image > 0: image_scores = np.hstack( [all_boxes[j][i][:, -1] for j in range(1, imdb.num_classes)]) if len(image_scores) > max_per_image: image_thresh = np.sort(image_scores)[-max_per_image] for j in range(1, imdb.num_classes): keep = np.where(all_boxes[j][i][:, -1] >= image_thresh)[0] all_boxes[j][i] = all_boxes[j][i][keep, :] nms_time = _t['misc'].toc(average=False) print('im_detect: {:d}/{:d} {:.3f}s {:.3f}s' \ .format(i + 1, num_images, detect_time, nms_time)) if vis: path = os.path.join(output_dir, 'viz') if not os.path.exists(path): os.mkdir(path) cv2.imwrite(os.path.join(path, '{}.jpg'.format(i)), im2show) cv2.imwrite(os.path.join(path, '{}_gt.jpg'.format(i)), im2show_gt) with open(det_file, 'wb') as f: pickle.dump(all_boxes, f, pickle.HIGHEST_PROTOCOL) print('Evaluating detections') imdb.evaluate_detections(all_boxes, output_dir)
def test_net(net, imdb, max_per_image=300, thresh=0.05, vis=False): """Test a Fast R-CNN network on an image database.""" num_images = len(imdb.image_index) # all detections are collected into: # all_boxes[cls][image] = N x 5 array of detections in # (x1, y1, x2, y2, score) all_boxes = [[[] for _ in xrange(num_images)] for _ in xrange(imdb.num_classes)] # output_dir = get_output_dir(imdb, name) # timers _t = {'im_detect': Timer(), 'misc': Timer()} # det_file = os.path.join(output_dir, 'detections.pkl') for i in range(num_images): im = cv2.imread(imdb.image_path_at(i)) _t['im_detect'].tic() scores, boxes = im_detect(net, im) detect_time = _t['im_detect'].toc(average=False) _t['misc'].tic() if vis or sav: # im2show = np.copy(im[:, :, (2, 1, 0)]) im2show = np.copy(im) # skip j = 0, because it's the background class for j in xrange(1, imdb.num_classes): inds = np.where(scores[:, j] > thresh)[0] cls_scores = scores[inds, j] cls_boxes = boxes[inds, j * 4:(j + 1) * 4] cls_dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])) \ .astype(np.float32, copy=False) keep = nms(cls_dets, cfg.TEST.NMS) cls_dets = cls_dets[keep, :] if vis: im2show = vis_detections(im2show, imdb.classes[j], cls_dets) all_boxes[j][i] = cls_dets # Limit to max_per_image detections *over all classes* if max_per_image > 0: image_scores = np.hstack( [all_boxes[j][i][:, -1] for j in xrange(1, imdb.num_classes)]) if len(image_scores) > max_per_image: image_thresh = np.sort(image_scores)[-max_per_image] for j in xrange(1, imdb.num_classes): keep = np.where(all_boxes[j][i][:, -1] >= image_thresh)[0] all_boxes[j][i] = all_boxes[j][i][keep, :] nms_time = _t['misc'].toc(average=False) print 'im_detect: {:d}/{:d} {:.3f}s {:.3f}s' \ .format(i + 1, num_images, detect_time, nms_time) # if vis: # cv2.imshow('test', im2show) # cv2.waitKey(1) if sav: cv2.imwrite(output_dir_detections + str(i) + '.png', im2show) with open(det_file, 'wb') as f: cPickle.dump(all_boxes, f, cPickle.HIGHEST_PROTOCOL) print 'Evaluating detections' imdb.evaluate_detections(all_boxes, output_dir)
def make_dir(output_dir): if not os.path.exists(output_dir): os.makedirs(output_dir) make_dir(output_dir) if __name__ == '__main__': # load data imdb, roidb, ratio_list, ratio_index = extract_roidb(test_name) imdb.competition_mode(on=True) print('{:d} roidb entries'.format(len(roidb))) t = Timer() file_name = pre_model_name.split('.h5')[0] + '_det.pkl' det_file = os.path.join(output_dir, file_name) try: if os.path.getsize(det_file) > 0: with open(det_file, 'rb') as fid: all_boxes = pickle.load(fid) start = t.tic() print('Evaluating detections') imdb.evaluate_detections(all_boxes, output_dir) end = t.tic() print("test time: %0.4fs" % (end - start)) except FileNotFoundError as e: print(str(e)) # start from making det file
params = list(net.parameters()) optimizer = torch.optim.SGD(params[8:], lr=lr, momentum=momentum, weight_decay=weight_decay) if not os.path.exists(output_dir): os.makedirs(output_dir) # training train_loss = 0 tp, tf, fg, bg = 0., 0., 0, 0 step_cnt = 0 re_cnt = False t = Timer() t.tic() train_data = json.load(open('../data/train_data.json')) dataset = Dataset('../data', train_data, transform=Augmentation()).even() for step in range(start_step, end_step + 1): iter = 0 for i in np.random.permutation(len(dataset)): im_data, gt_boxes, im_info = dataset[i] im_info = np.expand_dims(im_info, 0) im_data = np.expand_dims(im_data, 0) gt_ishard = np.zeros(len(gt_boxes)).astype('int32') dontcare_areas = np.zeros(0, dtype='float64') net(im_data, im_info, gt_boxes, gt_ishard, dontcare_areas) loss = net.loss + net.rpn.loss
params.append(p) # optimizer = torch.optim.Adam(params[-8:], lr=lr) optimizer = torch.optim.SGD(params, lr=lr, momentum=momentum, weight_decay=weight_decay) if not os.path.exists(output_dir): os.makedirs(output_dir) # training train_loss = 0 tp, tf, fg, bg = 0., 0., 0, 0 step_cnt = 0 re_cnt = False time = Timer() time.tic() for step in range(start_step, end_step + 1): # get one batch blobs = data_layer.forward() im_data = blobs['data'] im_info = blobs['im_info'] #---[heiht, width, scale] gt_boxes = blobs['gt_boxes'] #---[[x1, y1, x2, y2, label]] gt_ishard = blobs['gt_ishard'] dontcare_areas = blobs['dontcare_areas'] net(im_data, im_info, gt_boxes, gt_ishard, dontcare_areas) loss = net.loss + net.rpn.loss if _DEBUG: tp += float(net.tp) tf += float(net.tf)
if use_tensorboard: cc = CrayonClient(hostname='127.0.0.1') if remove_all_log: cc.remove_all_experiments() if exp_name is None: exp_name = datetime.now().strftime('vgg16_%m-%d_%H-%M') exp = cc.create_experiment(exp_name) else: exp = cc.open_experiment(exp_name) # training train_loss = 0 tp, tf, fg, bg = 0., 0., 0, 0 step_cnt = 0 re_cnt = False t = Timer() t.tic() for step in range(start_step, end_step+1): # get one batch blobs = data_layer.forward() ###### im_data = blobs['data'] im_info = blobs['im_info'] gt_boxes = blobs['gt_boxes'] gt_ishard = blobs['gt_ishard'] dontcare_areas = blobs['dontcare_areas'] # forward net(im_data, im_info, gt_boxes, gt_ishard, dontcare_areas) loss = net.loss + net.rpn.loss
def track(): def id_track(dataset, features): from collections import Counter def dist(f1, f2): score = (torch.sqrt((f1 - f2) ** 2)).sum(0).data.cpu().numpy()[0] return score id_list = [] id_count = {'f' + str(i): [] for i in range(len(features))} for dataframe in dataset: for i, f in enumerate(features): init_val = 1e15 for data in dataframe: score = dist(f, data['feature']) if score < init_val: init_val = score id = data['id'] id_count['f' + str(i)].append(id) for list in id_count.values(): c1 = Counter(list) most_id = c1.most_common(1)[0][0] id_list.append(most_id) return id_list import os imdb_name = 'CaltechPedestrians_test' imdb = get_imdb(imdb_name) cfg_file = 'experiments/cfgs/faster_rcnn_end2end.yml' model_dir = 'data/pretrained_model/' pre_model_name = 'CaltechPedestrians_train_2_vgg16_0.7_b3.h5' pretrained_model = model_dir + pre_model_name cfg_from_file(cfg_file) name_blocks = pre_model_name.split('_') if 'vgg16' in name_blocks: detector = FasterRCNN_VGG(classes=imdb.classes, debug=False) elif 'resnet50' in name_blocks: detector = FasterRCNN_RES(classes=imdb.classes, debug=False) else: detector = FasterRCNN_VGG(classes=imdb.classes, debug=False) relu = True if 'relu' in name_blocks else False network.load_net(pretrained_model, detector) detector.cuda() detector.eval() print('load model successfully!') blob = init_data(is_cuda=True) t = Timer() t.tic() cap = cv2.VideoCapture(video_file) init = True while (cap.isOpened()): ret, frame = cap.read() if ret: p = Timer() p.tic() if init: cnt = 1 fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter(output_file, fourcc, fps, (frame.shape[1], frame.shape[0])) init = False try: # detect tid = (cnt-1) % tps dets, scores, classes = detector.detect(frame, blob, thr=0.7, nms_thresh=0.3) frame = np.copy(frame) # feature extraction features = [] for i, det in enumerate(dets): gt_box = det[np.newaxis,:] features.append(detector.extract_feature_vector(frame, blob, gt_box, relu=relu)) det = tuple(int(x) for x in det) cv2.rectangle(frame, det[0:2], det[2:4], (255, 205, 51), 2) dataframe = [] if tid == 0: dataset = [] for i, f in enumerate(features): data = {} data['id'] = i data['feature'] = f dataframe.append(data) dataset.append(dataframe) anchors = dets elif tid > 0 and tid < tps-1: overlaps = bbox_overlaps(np.ascontiguousarray(anchors, dtype=np.float) \ , np.ascontiguousarray(dets, dtype=np.float)) # max : K max overlaps score about N dets overlaps = np.multiply(overlaps, overlaps > 0.7) max_arg = overlaps.argmax(axis=0) for i, arg in enumerate(max_arg): if arg >= len(features): continue data = {} data['id'] = arg data['feature'] = features[arg] dataframe.append(data) dataset.append(dataframe) anchors = dets else: id_list = id_track(dataset, features) for i, id in enumerate(id_list): det = tuple(int(x)-2 for x in dets[i]) cv2.putText(frame, 'id: ' + str(id), det[0:2], cv2.FONT_HERSHEY_PLAIN, 2.0, (0, 0, 255)) # cv2.imshow('demo', frame) # cv2.waitKey(1000) # cv2.destroyAllWindows() except: pass finally: if cnt % 10 == 0: print(cnt,'-frame : {:.3f}s'.format(p.toc())) cnt += 1 out.write(frame) else: break runtime = t.toc() print('{} frames / total spend: {}s / {:2.1f} fps'.format(cnt, int(runtime), cnt/runtime)) cap.release() out.release()