def process_image(filename): print 'Process image: ', filename #image = PIL.Image.open(filename) if not os.path.isfile(filename): print 'Image file %s not exist' % filename return image = D.cv2.imread(filename) #data.set_data(image) show_img = image[:, :, (2, 1, 0)] ax.cla() ax.imshow(show_img, aspect='equal') print 'Start detection' timer = D.Timer() timer.tic() scores, boxes = D.im_detect(net, image) timer.toc() print('Detection took {:.3f}s for ' '{:d} object proposals').format(timer.total_time, boxes.shape[0]) print 'Plot result.' CONF_THRESH = 0.8 NMS_THRESH = 0.3 for cls_ind, cls in enumerate(D.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 = D.nms(dets, NMS_THRESH) dets = dets[keep, :] plot_rect(image, cls, dets, thresh=CONF_THRESH) fig.canvas.draw()
def faster_rcnn_detect(ax, image): if not net: init_caffe() # Caffe needs to be started in this thread, otherwise GIL will make it very slow r,g,b,a = cv2.split(image) bgr_img = cv2.merge([b,g,r]) timer = D.Timer() timer.tic() scores, boxes = D.im_detect(net, bgr_img) timer.toc() print ('Detection took {:.3f}s for ' '{:d} object proposals').format(timer.total_time, boxes.shape[0]) plot_image(ax, image, boxes, scores) return image
def faster_rcnn_detect(ax, image): if not net: init_caffe( ) # Caffe needs to be started in this thread, otherwise GIL will make it very slow r, g, b, a = cv2.split(image) bgr_img = cv2.merge([b, g, r]) timer = D.Timer() timer.tic() scores, boxes = D.im_detect(net, bgr_img) timer.toc() print(('Detection took {:.3f}s for ' '{:d} object proposals').format(timer.total_time, boxes.shape[0])) plot_image(ax, image, boxes, scores) return image
def init_caffe(): # TODO: parse args into here global net prototxt = os.path.join(D.cfg.MODELS_DIR, D.NETS['vgg16'][0], 'faster_rcnn_alt_opt', 'faster_rcnn_test.pt') caffemodel = os.path.join(D.cfg.DATA_DIR, 'faster_rcnn_models' , D.NETS['vgg16'][1]) gpu_id = 0 D.caffe.set_mode_gpu() D.caffe.set_device(gpu_id) D.cfg.GPU_ID = gpu_id D.cfg.TEST.HAS_RPN = True net = D.caffe.Net(prototxt, caffemodel, D.caffe.TEST) # Warmup on a dummy image im = 128 * np.ones((300, 500, 3), dtype = np.uint8) for _ in xrange(2): _, _ = D.im_detect(net, im)
def init_caffe(): # TODO: parse args into here global net prototxt = os.path.join(D.cfg.MODELS_DIR, D.NETS['vgg16'][0], 'faster_rcnn_alt_opt', 'faster_rcnn_test.pt') caffemodel = os.path.join(D.cfg.DATA_DIR, 'faster_rcnn_models', D.NETS['vgg16'][1]) gpu_id = 0 D.caffe.set_mode_gpu() D.caffe.set_device(gpu_id) D.cfg.GPU_ID = gpu_id D.cfg.TEST.HAS_RPN = True net = D.caffe.Net(prototxt, caffemodel, D.caffe.TEST) # Warmup on a dummy image im = 128 * np.ones((300, 500, 3), dtype=np.uint8) for _ in range(2): _, _ = D.im_detect(net, im)
def process_image(filename): if not net: init_caffe( ) # Caffe needs to be started in this thread, otherwise GIL will make it very slow print 'Process image: %s' % filename if not os.path.isfile(filename): print 'Image file %s not exist' % filename return image = D.cv2.imread(filename) timer = D.Timer() timer.tic() scores, boxes = D.im_detect(net, image) timer.toc() print('Detection took {:.3f}s for ' '{:d} object proposals').format(timer.total_time, boxes.shape[0]) show_img = image[:, :, (2, 1, 0)] # Reorder to RGB plot_image(show_img, boxes, scores)