def apply_nms(all_boxes, thresh): """Apply non-maximum suppression to all predicted boxes output by the test_net method. """ num_classes = len(all_boxes) num_images = len(all_boxes[0]) nms_boxes = [[[] for _ in xrange(num_images)] for _ in xrange(num_classes)] for cls_ind in xrange(num_classes): for im_ind in xrange(num_images): dets = all_boxes[cls_ind][im_ind] if dets == []: continue x1 = dets[:, 0] y1 = dets[:, 1] x2 = dets[:, 2] y2 = dets[:, 3] scores = dets[:, 4] inds = np.where((x2 > x1) & (y2 > y1) & (scores > cfg.TEST.DET_THRESHOLD))[0] dets = dets[inds, :] if dets == []: continue keep = nms(dets, thresh) if len(keep) == 0: continue nms_boxes[cls_ind][im_ind] = dets[keep, :].copy() return nms_boxes
def demo(sess, net, image_name, bbox): """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) if os.path.isfile(os.path.join(data_dir, image_name)): im_file = os.path.join(data_dir, image_name) else: im_file = os.path.join(data_dir_2, image_name) revise = 40 im = cv2.imread(im_file) #cv2.imwrite('b.jpg',im,[cv2.IMWRITE_JPEG_QUALITY, 70]) #pdb.set_trace() #im=cv2.imread('b.jpg') pixel_means = np.array([[[102, 115, 122]]]) #pdb.set_trace() #im[bbox[1]:bbox[1]+revise,bbox[0]:bbox[2],:]=pixel_means #im[bbox[1]:bbox[3],bbox[0]:bbox[0]+revise,:]=pixel_means #im[bbox[3]-revise:bbox[3],bbox[0]:bbox[2],:]=pixel_means #im[bbox[1]:bbox[3],bbox[2]-revise:bbox[2],:]=pixel_means #im[bbox[1]:bbox[3],bbox[0]:bbox[2],:]=pixel_means #im = io.imread('http://mscoco.org/images/%d'%(int(image_name))) # Detect all object classes and regress object bounds timer = Timer() timer.tic() try: scores, boxes, _, _ = im_detect(sess, net, im) except Exception as e: print(e) return 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.01 NMS_THRESH = 0.2 for cls_ind, cls in enumerate(CLASSES[1:]): if cls == 'authentic': continue cls_ind += 1 # because we skipped background #cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)] #cls_scores = scores[:, cls_ind] 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,False) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] #im_score=vis_detections(cv2.resize(im,None,None,fx=0.5,fy=0.5,interpolation=cv2.INTER_LINEAR), cls, dets,image_name, thresh=CONF_THRESH) im_score = vis_detections(im, cls, dets, image_name, thresh=CONF_THRESH) return im_score
def test_net(sess, net, imdb, weights_filename, max_per_image=100, thresh=0.05): np.random.seed(cfg.RNG_SEED) """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, weights_filename) # timers _t = {'im_detect': Timer(), 'misc': Timer()} for i in xrange(num_images): im = cv2.imread(imdb.image_path_at(i)) _t['im_detect'].tic() scores, boxes = im_detect(sess, net, im) _t['im_detect'].toc() _t['misc'].tic() # 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, :] 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, :] _t['misc'].toc() print ('im_detect: {:d}/{:d} {:.3f}s {:.3f}s') \ .format(i + 1, num_images, _t['im_detect'].average_time, _t['misc'].average_time) det_file = os.path.join(output_dir, 'detections.pkl') 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 demoRest(net, image_name, classes, box_file, obj_proposals, im_file, im): # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(net, im, obj_proposals) 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 in classes: cls_ind = CLASSES.index(cls) cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)] cls_scores = scores[:, cls_ind] keep = np.where(cls_scores >= CONF_THRESH)[0] cls_boxes = cls_boxes[keep, :] cls_scores = cls_scores[keep] dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] print 'All {} detections with p({} | box) >= {:.1f}'.format(cls, cls, CONF_THRESH) vis_detections(im, cls, dets, thresh=CONF_THRESH)
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 demo(net, image_name, classes): """Detect object classes in an image using pre-computed object proposals.""" # Load pre-computed object proposals (try Selected Search, later). obj_proposals = np.loadtxt('/data/object_proposals/oirds.csv', dtype=int) # Load the demo image (contains four cars and one unknown). im_file = '/data/OIRDS/train/crop/16546686_257_4353_513_4609.png' im = cv2.imread(im_file) # Detect all object classes and regress object bounds. timer = Timer() timer.tic() scores, boxes = im_detect(net, im, obj_proposals) 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 in classes: cls_ind = CLASSES.index(cls) cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] keep = np.where(cls_scores >= CONF_THRESH)[0] cls_boxes = cls_boxes[keep, :] cls_scores = cls_scores[keep] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] print 'All {} detections with p({} | box) >= {:.1f}'.format( cls, cls, CONF_THRESH) 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 apply_nms(all_boxes, thresh): """Apply non-maximum suppression to all predicted boxes output by the test_net method. """ num_classes = len(all_boxes) num_images = len(all_boxes[0]) nms_boxes = [[[] for _ in xrange(num_images)] for _ in xrange(num_classes)] for cls_ind in xrange(num_classes): for im_ind in xrange(num_images): dets = all_boxes[cls_ind][im_ind] if dets == []: continue x1 = dets[:, 0] y1 = dets[:, 1] x2 = dets[:, 2] y2 = dets[:, 3] scores = dets[:, 4] inds = np.where((x2 > x1) & (y2 > y1) & (scores > cfg.TEST.DET_THRESHOLD))[0] dets = dets[inds,:] if dets == []: continue keep = nms(dets, thresh) if len(keep) == 0: continue nms_boxes[cls_ind][im_ind] = dets[keep, :].copy() return nms_boxes
def apply_nms(all_boxes, thresh,intra_class_nms=False): """Apply non-maximum suppression to all predicted boxes output.""" num_classes = len(all_boxes) num_images = len(all_boxes[0]) nms_boxes = [[[] for _ in xrange(num_images)] for _ in xrange(num_classes)] for im_ind in xrange(num_images): for cls_ind in xrange(num_classes): dets = all_boxes[cls_ind][im_ind] if dets == []: continue if not 'keep_box_all_class' in vars(): dets_aug = dets else: dets_aug = np.row_stack((keep_box_all_class,dets)) keep = nms(dets_aug, thresh) if len(keep) == 0:continue if intra_class_nms: keep_box_all_class = dets_aug[keep, :].copy() else: nms_boxes[cls_ind][im_ind] = dets_aug[keep, :].copy() if intra_class_nms: #run over all classes to match image with class keep_set = set([tuple(x) for x in keep_box_all_class]) for cls_ind in xrange(num_classes): class_set = set([tuple(x) for x in all_boxes[cls_ind][im_ind]]) nms_boxes[cls_ind][im_ind] = np.array([x for x in class_set & keep_set]).copy() del keep_box_all_class return nms_boxes
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 recognize_img(net, image_name, box_file, classes): obj_proposals = sio.loadmat(box_file)['boxes'] # Load the demo image im = cv2.imread(image_name) # Detect all object classes and regress object bounds scores, boxes = im_detect(net, im, obj_proposals) #print type(boxes) #dims = boxes.shape #rows = dims[0] #cols = dims[1] # Visualize detections for each class CONF_THRESH = 0.85 NMS_THRESH = 0.3 data_list = [] for cls in classes: cls_ind = CLASSES.index(cls) cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)] cls_scores = scores[:, cls_ind] keep = np.where(cls_scores >= CONF_THRESH)[0] cls_boxes = cls_boxes[keep, :] cls_scores = cls_scores[keep] dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] tmplist = get_detection_box(cls, dets, thresh=CONF_THRESH) if len(tmplist) == 0: continue data_list.extend(tmplist) data_list.sort(key=lambda obj:obj.get('xoffset'), reverse=False) #data_list = char_roi_filter(data_list) str = '' for elem in data_list: str = str + elem.get('char') return str
def demo(net, image_name, classes): """Detect object classes in an image using pre-computed object proposals.""" # Load pre-computed Selected Search object proposals box_file = os.path.join(cfg.ROOT_DIR, 'data', 'demo', image_name + '_boxes.mat') obj_proposals = sio.loadmat(box_file)['boxes'] # Load the demo image im_file = os.path.join(cfg.ROOT_DIR, 'data', 'demo', image_name + '.jpg') im = cv2.imread(im_file) # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(net, im, obj_proposals) 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 in classes: cls_ind = CLASSES.index(cls) 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, :] print 'All {} detections with p({} | box) >= {:.1f}'.format(cls, cls, CONF_THRESH) vis_detections(im, cls, dets, thresh=CONF_THRESH)
def demo(net, im, scale_factor, classes): """Detect object classes in an image using pre-computed object proposals.""" im2 = cv2.resize(im, (0,0), fx=1.0/scale_factor, fy=1.0/scale_factor) obj_proposals_in = [] dlib.find_candidate_object_locations(im2, obj_proposals_in, min_size=70) obj_proposals = np.empty((len(obj_proposals_in),4)) for idx in range(len(obj_proposals_in)): obj_proposals[idx] = [obj_proposals_in[idx].left(), obj_proposals_in[idx].top(), obj_proposals_in[idx].right(), obj_proposals_in[idx].bottom()] # Detect all object classes and regress object bounds scores, boxes = im_detect(net, im2, obj_proposals) # Visualize detections for each class CONF_THRESH = 0.8 NMS_THRESH = 0.3 for cls in classes: cls_ind = CLASSES.index(cls) 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, :] return [im2, cls, dets, CONF_THRESH]
def boxes_filter(dets, PRE_NMS_TOPN, NMS_THRESH, POST_NMS_TOPN, CONF_THRESH, USE_GPU=False): """ filter the proposal boxes """ # speed up nms if PRE_NMS_TOPN > 0: dets = dets[:min(len(dets), PRE_NMS_TOPN), :] # apply nms if NMS_THRESH > 0 and NMS_THRESH < 1: if USE_GPU: keep = nms_gpu(dets, NMS_THRESH) else: keep = nms(dets, NMS_THRESH) dets = dets[keep, :] if POST_NMS_TOPN > 0: dets = dets[:min(len(dets), POST_NMS_TOPN), :] inds = np.where(dets[:, -1] >= CONF_THRESH)[0] dets = dets[inds, :] return dets
def detect(self, img): bbox = self.bbox(img) scores, boxes = im_detect(self.net, img, bbox) result = [] CONF_THRESH = 0.8 NMS_THRESH = 0.3 for cls in self.CLASSES[1:]: cls_ind = self.CLASSES.index(cls) 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, :] inds = np.where(dets[:, -1] >= CONF_THRESH)[0] if len(inds) == 0: continue for i in inds: bbox = dets[i, :4] x1, y1, x2, y2 = map(int, bbox) result.append({ "label": cls, "bbox": [x1, y1, x2, y2] }) return result
def on_get(self, req, resp, name): t1 = time() names = name.split("&") prototxt = os.path.join(cfg.ROOT_DIR, 'models/CaffeNet/test.prototxt') caffemodel = os.path.join( cfg.ROOT_DIR, 'data/fast_rcnn_models/' 'caffenet_fast_rcnn_iter_40000.caffemodel') if not os.path.isfile(caffemodel): raise falcon.HTTPPreconditionFailed("Error", "Caffe model not found") caffe.set_mode_cpu() net = caffe.Net(prototxt, caffemodel, caffe.TEST) classes = read_synset(self.storage_path) ext = os.path.splitext(names[0])[1][1:] image_path = os.path.join(self.storage_path, names[0]) im = cv2.imread(image_path) rects = [] dlib.find_candidate_object_locations(im, rects, min_size=np.size(im, 1)) obj_proposals = np.empty((len(rects), 4), dtype=int) for k, d in enumerate(rects): obj_proposals[k] = [d.left(), d.top(), d.right(), d.bottom()] scores, boxes = im_detect(net, im, obj_proposals) CONF_THRESH = 0.9 NMS_THRESH = 0.3 for cls in classes: if str(cls).startswith('__background__'): continue cls_ind = CLASSES.index(cls) cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] keep = np.where(cls_scores >= CONF_THRESH)[0] cls_boxes = cls_boxes[keep, :] cls_scores = cls_scores[keep] 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) result_image_path = os.path.join(self.storage_path, "{0}.{1}".format(_generate_id(), ext)) if 'image/jpeg' == mimetypes.guess_type(image_path, strict=False): cv2.imwrite(result_image_path, im, [int(cv2.IMWRITE_JPEG_QUALITY), 100]) else: cv2.imwrite(result_image_path, im) t2 = time() - t1 data_size = os.path.getsize(image_path) data_size += os.path.getsize(result_image_path) data_size /= 1024.0 * 1024.0 cost = estimate_cost.estimateCost(data_size, t2, data_size) resp.status = falcon.HTTP_200 # OK resp.body = os.path.split(result_image_path)[1] + '&' + str( os.path.getsize(result_image_path) / 1024) + '&' + str( t2 * 1000.0) + '&' + "{:2.5f}".format(cost)
def runDetection(net, basePath, testFileName, classes): ftest = open(testFileName, 'r') imageFileName = basePath + '/' + ftest.readline().strip() num = 1 outputFile = open('CarDetectionResult_window_30000.txt', 'w') while imageFileName: print imageFileName print 'now is ', num num += 1 imageFileBaseName = os.path.basename(imageFileName) imageFileDir = os.path.dirname(imageFileName) boxFileName = imageFileDir + '/' + imageFileBaseName.replace( '.jpg', '_boxes.mat') print boxFileName obj_proposals = sio.loadmat(boxFileName)['boxes'] #obj_proposals[:,2] = obj_proposals[:, 2] + obj_proposals[:, 0] #obj_proposals[:,3] = obj_proposals[:, 3] + obj_proposals[:, 1] im = cv2.imread(imageFileName) timer = Timer() timer.tic() scores, boxes = im_detect(net, im, obj_proposals) timer.toc() print('Detection took {:.3f} for ' '{:d} object proposals').format(timer.total_time, boxes.shape[0]) CONF_THRESH = 0.8 NMS_THRESH = 0.3 for cls in classes: cls_ind = CLASSES.index(cls) 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, :] print 'All {} detections with p({} | box) >= {:.1f}'.format( cls, cls, CONF_THRESH) inds = np.where(dets[:, -1] >= CONF_THRESH)[0] print 'Detected car number ', inds.size if len(inds) != 0: outputFile.write(imageFileName + ' ') outputFile.write(str(inds.size) + ' ') for i in inds: bbox = dets[i, :4] outputFile.write( str(int(bbox[0])) + ' ' + str(int(bbox[1])) + ' ' + str(int(bbox[2])) + ' ' + str(int(bbox[3])) + ' ') outputFile.write('\n') else: outputFile.write(imageFileName + ' 0' '\n') temp = ftest.readline().strip() if temp: imageFileName = basePath + '/' + temp else: break
def demo(net, image_name, classes, ssdir, imgdir, normdir, savefile): """Detect object classes in an image using pre-computed object proposals.""" box_file = os.path.join(ssdir, image_name + '.mat') obj_proposals = sio.loadmat(box_file)['boxes'] # Load the demo image im_file = os.path.join(imgdir, image_name + '.jpg') im = cv2.imread(im_file) #print(np.shape(im)) # Load the demo image norm_file = os.path.join(normdir, image_name + '.jpg') norm_im = cv2.imread(norm_file) norm_im = cv2.resize(norm_im, (im.shape[0], im.shape[1]) ) im = (im, norm_im) # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(net, im, obj_proposals) 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 thresh = 0.3 fid = open(savefile,'w') cnt = 0 for cls in classes: cnt = cnt + 1 cls_ind = CLASSES.index(cls) 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, :] inds = np.where(dets[:, -1] >= thresh)[0] for i in inds: bbox = dets[i, :4] score = dets[i, -1] fid.write('{0:d}'.format(cnt)) fid.write(' ') fid.write('{0:.3f}'.format(score)) for j in range(4): fid.write(' ') fid.write('{0:.3f}'.format(bbox[j])) fid.write('\n') fid.close()
def demo(net, image_name, box_file, out_img, classes): obj_proposals = sio.loadmat(box_file)['boxes'] # Load the demo image im_file = image_name#os.path.join(cfg.ROOT_DIR, 'data', 'demo', image_name + '.jpg') im = cv2.imread(im_file) # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(net, im, obj_proposals) #print type(boxes) dims = boxes.shape print dims rows = dims[0] cols = dims[1] #for elem in boxes.flat: # print elem print '-===-=-==-==-=-====================--------' 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.85 NMS_THRESH = 0.3 img = im[:, :, (2, 1, 0)] fig, ax = plt.subplots(figsize=(12, 12)) ax.imshow(im, aspect='equal') data_list = []; for cls in classes: cls_ind = CLASSES.index(cls) cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)] #print cls_boxes #print '=================' cls_scores = scores[:, cls_ind] keep = np.where(cls_scores >= CONF_THRESH)[0] #print cls cls_boxes = cls_boxes[keep, :] cls_scores = cls_scores[keep] dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] tmplist = vis_detections(ax, cls, dets, thresh=CONF_THRESH) if len(tmplist) == 0: continue data_list.extend(tmplist) #print data_list #print '=====================' plt.savefig(out_img) data_list.sort(key=lambda obj:obj.get('xoffset'), reverse=False) str = '' for elem in data_list: str = str + elem.get('char') return str
def test_net(sess, net, imdb, weights_filename, max_per_image=100, thresh=0.05): np.random.seed(cfg.RNG_SEED) """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, weights_filename) # timers _t = {'im_detect' : Timer(), 'misc' : Timer()} for i in range(num_images): im = cv2.imread(imdb.image_path_at(i)) _t['im_detect'].tic() scores, boxes = im_detect(sess, net, im) _t['im_detect'].toc() _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][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, :] _t['misc'].toc() print('im_detect: {:d}/{:d} {:.3f}s {:.3f}s' \ .format(i + 1, num_images, _t['im_detect'].average_time, _t['misc'].average_time)) det_file = os.path.join(output_dir, 'detections.pkl') 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 callback2(self, bbox): global cv_image # obj_proposals bbox_num = len(bbox.data) / 4 print "bbox_num:", bbox_num obj_proposals = np.array([[0 for i in range(0, 4)] for j in range(0, bbox_num)]) for i in range(0, bbox_num): obj_proposals[i][0] = bbox.data[4 * i] obj_proposals[i][1] = bbox.data[4 * i + 2] obj_proposals[i][2] = bbox.data[4 * i + 1] obj_proposals[i][3] = bbox.data[4 * i + 3] ############################################## # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(self.net, cv_image, obj_proposals) timer.toc() print('Detection took {:.3f}s for ' '{:d} object proposals').format(timer.total_time, boxes.shape[0]) # insert predicted class label to each box labels = np.array([0 for i in range(0, boxes.shape[0])]) for i in range(0, boxes.shape[0]): tmpscores = scores[i, :] labels[i] = np.argmax(tmpscores) # Visualize detections for each class output_image = cv_image for cls_ind in range(1, len(CLASSES)): if cls_ind not in labels: continue inds = np.where(labels == cls_ind) cls_boxes = boxes[inds, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[inds, cls_ind] cls_boxes = cls_boxes[0] cls_scores = cls_scores[0] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, self.NMS_THRESH) dets = dets[keep, :] for i in range(0, dets.shape[0]): if dets[i, -1] > self.CONF_THRESH: bbox_ = dets[i, :4] print " DETECTED! ", CLASSES[cls_ind], dets[i, :] cv2.rectangle(output_image, (bbox_[0], bbox_[1]), (bbox_[2], bbox_[3]), (0, 0, 255), 2) cv2.putText(output_image, CLASSES[cls_ind], (bbox_[0], bbox_[1]), cv2.FONT_HERSHEY_COMPLEX, 1., (0, 0, 255), 2) cv2.imshow("Image window", output_image) cv2.waitKey(3)
def test_eval(sess, net, imdb, weights_filename, max_per_image=100, thresh=0.05): np.random.seed(cfg.RNG_SEED) """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, weights_filename) # timers _t = {'im_detect': Timer(), 'misc': Timer()} for i in range(num_images): im = cv2.imread(imdb.image_path_at(i)) _t['im_detect'].tic() scores, boxes = im_detect(sess, net, im) _t['im_detect'].toc() _t['misc'].tic() imc = im.copy() # 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][i] = cls_dets high_score_index = np.argmax(cls_scores) cls_box = cls_boxes[high_score_index] cv2.rectangle(imc, (cls_box[0], cls_box[1]), (cls_box[2], cls_box[3]), color=(0, 255, 0)) cv2.putText(imc, imdb._classes[j], (int(cls_box[0]), int((cls_box[1] + cls_box[3]) / 2)), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 255, 0), 1, cv2.LINE_AA) cv2.imshow('boxs', cv2.resize(imc, (0, 0), fx=2, fy=2)) cv2.waitKey(0)
def demo(net, image_name, classes): """Detect object classes in an image using pre-computed object proposals.""" # Load pre-computed Selected Search object proposals # box_file = os.path.join(cfg.ROOT_DIR, 'data', 'demo',image_name + '_boxes.mat') test_mats_path = '/home/tanshen/fast-rcnn/data/kaggle/test_bbox' box_file = os.path.join(test_mats_path ,image_name + '_boxes.mat') obj_proposals = sio.loadmat(box_file)['boxes'] # Load the demo image test_images_path = '/home/tanshen/fast-rcnn/data/kaggle/ImagesTest' # im_file = os.path.join(cfg.ROOT_DIR, 'data', 'demo', image_name + '.jpg') im_file = os.path.join(test_images_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(net, im, obj_proposals) 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 NMS_THRESH = 0.3 max_inds = 0 max_score = 0.0 for cls in classes: cls_ind = CLASSES.index(cls) cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)] cls_scores = scores[:, cls_ind] keep = np.where(cls_scores >= CONF_THRESH)[0] cls_boxes = cls_boxes[keep, :] cls_scores = cls_scores[keep] dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] # print 'All {} detections with p({} | box) >= {:.1f} in {}'.format(cls, cls, # CONF_THRESH, image_name) #if get_max!=[]: [ind,tmp]=get_max(im, cls, dets, thresh=CONF_THRESH) #print image_name,cls,tmp #vis_detections(im, cls, dets, image_name, thresh=CONF_THRESH) #print dets[:,-1] #print image_name,max_score file.writelines([image_name,'\t',cls,'\t',str(tmp),'\n']) if(max_score<tmp): max_score=tmp cls_max=cls print image_name,cls_max,max_score
def runDetection (net, basePath, testFileName,classes): ftest = open(testFileName,'r') imageFileName = basePath+'/' + ftest.readline().strip() num = 1 outputFile = open('CarDetectionResult_window_30000.txt','w') while imageFileName: print imageFileName print 'now is ',num num += 1 imageFileBaseName = os.path.basename(imageFileName) imageFileDir = os.path.dirname(imageFileName) boxFileName = imageFileDir +'/'+imageFileBaseName.replace('.jpg','_boxes.mat') print boxFileName obj_proposals = sio.loadmat(boxFileName)['boxes'] #obj_proposals[:,2] = obj_proposals[:, 2] + obj_proposals[:, 0] #obj_proposals[:,3] = obj_proposals[:, 3] + obj_proposals[:, 1] im = cv2.imread(imageFileName) timer = Timer() timer.tic() scores, boxes = im_detect(net, im, obj_proposals) timer.toc() print ('Detection took {:.3f} for ' '{:d} object proposals').format(timer.total_time, boxes.shape[0]) CONF_THRESH = 0.8 NMS_THRESH = 0.3 for cls in classes: cls_ind = CLASSES.index(cls) 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, :] print 'All {} detections with p({} | box) >= {:.1f}'.format(cls, cls, CONF_THRESH) inds = np.where(dets[:, -1] >= CONF_THRESH)[0] print 'Detected car number ', inds.size if len(inds) != 0: outputFile.write(imageFileName+' ') outputFile.write(str(inds.size)+' ') for i in inds: bbox = dets[i, :4] outputFile.write(str(int(bbox[0]))+' '+ str(int(bbox[1]))+' '+ str(int(bbox[2]))+' '+ str(int(bbox[3]))+' ') outputFile.write('\n') else: outputFile.write(imageFileName +' 0' '\n') temp = ftest.readline().strip() if temp: imageFileName = basePath+'/' + temp else: break
def detect(self, image_name, mode, mixed=True): # DJDJ # Load the demo image #im_file = os.path.join(cfg.ROOT_DIR, 'data', 'demo', image_name) #im = cv2.imread(im_file) im = cv2.imread(image_name) # Detect all object classes and regress object bounds for i in range(1): timer = Timer() timer.tic() if mixed: scores, boxes = im_detect_mixed(self.net, im) else: scores, boxes = im_detect(self.net, im, obj_proposals) 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 timer = Timer() result = {} if mode == '3': # Car mode classes = CLASSES_CAR else: classes = CLASSES for cls in CLASSES: if mode == '3' and (cls in CLASSES_CAR) == False: # Car mode continue cls_ind = CLASSES.index(cls) 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) timer.tic() keep = nms(dets, NMS_THRESH) timer.toc() dets = dets[keep, :] result[cls_ind] = dets #print 'All {} detections with p({} | box) >= {:.1f}'.format(cls, cls, CONF_THRESH) #vis_detections(im, cls, dets, thresh=CONF_THRESH) #print ('nms took {:.3f}s').format(timer.total_time) return result
def recognize_checkcode_img(net, image_name, classes): boxes = get_selective_search_boxes(image_name) if boxes is None: dict = {} dict['ccvalue'] = '' dict['rects'] = [] dict['code'] = 1 return dict #im = cv2.imread(image_name) im = load_image(image_name) #print im #print type(im) #print im.shape #cv2.imwrite('asasdf.jpg', im) scores, boxes = im_detect(net, im, boxes) CONF_THRESH = 0.5 NMS_THRESH = 0.1 data_list = [] for cls in classes: cls_ind = CLASSES.index(cls) cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)] cls_scores = scores[:, cls_ind] keep = np.where(cls_scores >= CONF_THRESH)[0] cls_boxes = cls_boxes[keep, :] cls_scores = cls_scores[keep] dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] tmplist = get_detection_box(cls, dets, thresh=CONF_THRESH) if len(tmplist) == 0: continue data_list.extend(tmplist) data_list.sort(key=lambda obj:obj.get('xoffset'), reverse=False) # #print data_list #print len(data_list) #print '-=-=-=-=-=-=-=-=' data_list = rect_filter(data_list, 0.85) #print len(data_list) #print '-=-=-=-=-=-=-=-=' data_list = char_roi_filter(data_list) #print len(res_list) #print '-=-=-=-=-=-=-=-=' str = '' for elem in data_list: str = str + elem.get('char') #print res_list dict = {} dict['ccvalue'] = str dict['rects'] = data_list dict['code'] = 0 #print dict return dict
def detect(self, image_name, mode, mixed=True): # DJDJ # Load the demo image #im_file = os.path.join(cfg.ROOT_DIR, 'data', 'demo', image_name) #im = cv2.imread(im_file) im = cv2.imread(image_name) # Detect all object classes and regress object bounds for i in range(1): timer = Timer() timer.tic() if mixed: scores, boxes = im_detect_mixed(self.net, im) else: scores, boxes = im_detect(self.net, im, obj_proposals) 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 timer = Timer() result = {} if mode == '3': # Car mode classes = CLASSES_CAR else: classes = CLASSES for cls in CLASSES: if mode == '3' and (cls in CLASSES_CAR) == False: # Car mode continue cls_ind = CLASSES.index(cls) 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) timer.tic() keep = nms(dets, NMS_THRESH) timer.toc() dets = dets[keep, :] result[cls_ind] = dets #print 'All {} detections with p({} | box) >= {:.1f}'.format(cls, cls, CONF_THRESH) #vis_detections(im, cls, dets, thresh=CONF_THRESH) #print ('nms took {:.3f}s').format(timer.total_time) return result
def Detect_proposals(net, image_path, proposal_path): """Detect object classes in an image assuming the whole image is an object.""" # Load the image im = cv2.imread(image_path) h, w, c = im.shape img_blob = {} img_blob['img_path'] = image_path img_blob['pred'] = [] # Load proposals proposals = load_proposal(proposal_path) if len(proposals) == 0: img_blob['rcnn_time'] = 0 return img_blob # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(net, im, proposals) timer.toc() # CONF_THRESH = 0.005 NMS_THRESH = 0.3 for cls in CLASSES: if cls == '__background__': continue cls_ind = CLASSES.index(cls) 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, :] inds = np.where(dets[:, -1] >= CONF_THRESH)[0] # for each matching bbx for i in inds: bbox = dets[i, :4] score = dets[i, -1] bbx_d = {} bbx_d['bbox'] = [int(bbox[0]), int(bbox[1]), int(bbox[2]), int(bbox[3])] bbx_d['score'] = float(score) bbx_d['class'] = cls img_blob['pred'] += [bbx_d] print timer.total_time img_blob['rcnn_time'] = timer.total_time return img_blob
def demo(net, image_name): # get the proposals by using the shell to use c++ codes os.system( '/media/DataDisk/twwang/fast-rcnn/rcnn_test/proposals_for_python.sh' \ + ' ' + image_name) # Load computed Selected Search object proposals data = open('/home/twwang/temp_proposal', "rb").read() number_proposals = struct.unpack("i", data[0:4])[0] number_edge = struct.unpack("i", data[4:8])[0] assert number_edge == 4, 'The size is not matched!\n' + \ 'Note that the first two variables are the number of proposals\n' + \ ' and number of coordinates in a box, which is 4 by default\n' #cfg.NUM_PPS = 10 number_proposals = min(cfg.NUM_PPS, number_proposals) obj_proposals = np.asarray( struct.unpack( str(number_proposals * 4) + 'f', data[8:8 + 16 * number_proposals])).reshape(number_proposals, 4) im = cv2.imread(image_name) #print im.shape #im = cv2.flip(im, 0) #im = cv2.transpose(im) # Detect all object classes and regress object bounds timer = Timer() timer.tic() if cfg.MULTI_LABEL: scores, boxes, multi_labels = im_detect(net, im, obj_proposals) else: scores, boxes = im_detect(net, im, obj_proposals) timer.toc() print('Detection took {:.3f}s for ' '{:d} object proposals').format(timer.total_time, boxes.shape[0]) # Visualize detections for each class for cls in ['Upper', 'Lower', 'Whole']: cls_ind = CLASSES.index(cls) 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, image_name, thresh=CONF_THRESH) print ('The demo image is save as {}').format("/home/twwang/demo_results/" + \ os.path.split(image_name)[1])
def mytest(net, imageName): '''it is a simple test for one image''' obj_proposals = getProposal(imageName) im = cv2.imread(imageName) scores, boxes = im_detect(net, im, obj_proposals) # visualizing CONF_THRESH = 0.8 NMS_THRESH = 0.3 # change the order ? im = im[:, :, (2, 1, 0)] for cls in np.arange(len(CLASSES)): '''test the score on all the classes''' cls_boxes = boxes[:, 4 * cls: 4 * (cls + 1)] # get boxes cls_scores = scores[:, cls] # compute the nms results dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis]) ).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] # plot if necessary indexs = np.where(dets[:, -1] >= CONF_THRESH)[0] if indexs == 0: # not necessary continue fig, ax = plt.subplot(figsize=(12, 12)) ax.imshow(im, aspect='equal') for i in indexs: 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='red', linewidth=3.5 ) ) ax.text(bbox[0], bbox[1] - 2, '{:s} {:.3f}'.format(CLASSES[cls], score), bbox=dict(facecolor='blue', alpha=0.5), fontsize=14, color='white') ax.set_title(('{} detections with ' 'p({} | box) >= {:.1f}').format(CLASSES[cls], CLASSES[cls], CONF_THRESH), fontsize=14) plt.axis('off') plt.tight_layout() plt.draw()
def demo(net, image_name): # get the proposals by using the shell to use c++ codes os.system( '/media/DataDisk/twwang/fast-rcnn/rcnn_test/proposals_for_python.sh' \ + ' ' + image_name) # Load computed Selected Search object proposals data = open('/home/twwang/temp_proposal', "rb").read() number_proposals = struct.unpack("i", data[0:4])[0] number_edge = struct.unpack("i", data[4:8])[0] assert number_edge == 4, 'The size is not matched!\n' + \ 'Note that the first two variables are the number of proposals\n' + \ ' and number of coordinates in a box, which is 4 by default\n' #cfg.NUM_PPS = 10 number_proposals = min(cfg.NUM_PPS, number_proposals) obj_proposals = np.asarray(struct.unpack( str(number_proposals * 4) + 'f', data[8: 8 + 16 * number_proposals])).reshape(number_proposals, 4) im = cv2.imread(image_name) #print im.shape #im = cv2.flip(im, 0) #im = cv2.transpose(im) # Detect all object classes and regress object bounds timer = Timer() timer.tic() if cfg.MULTI_LABEL: scores, boxes, multi_labels = im_detect(net, im, obj_proposals) else: scores, boxes = im_detect(net, im, obj_proposals) timer.toc() print ('Detection took {:.3f}s for ' '{:d} object proposals').format(timer.total_time, boxes.shape[0]) # Visualize detections for each class for cls in ['Upper', 'Lower', 'Whole']: cls_ind = CLASSES.index(cls) 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, image_name, thresh=CONF_THRESH) print ('The demo image is save as {}').format("/home/twwang/demo_results/" + \ os.path.split(image_name)[1])
def demo(net, image_name,obj_proposals,fs): """Detect object classes in an image using pre-computed object proposals.""" # Load pre-computed Selected Search object proposals # box_file = os.path.join(cfg.ROOT_DIR, 'data', 'demo', # image_name + '_boxes.mat') # obj_proposals = sio.loadmat(box_file)['boxes'] # # Load the demo image image_folder="/home/yarley/yanxp/dataset/VOCdevkit2007/VOC2007/JPEGImages/" im = cv2.imread(image_folder+image_name) height=im.shape[0] width=im.shape[1] # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(net, im,obj_proposals) timer.toc() print ('Detection took {:.3f}s for ' '{:d} object proposals').format(timer.total_time, boxes.shape[0]) Label=[] BBox=[] Score=[] # Visualize detections for each class CONF_THRESH = 0.5 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) print dets.shape keep = nms(dets, NMS_THRESH) dets = dets[keep, :] print dets.shape raw_input() inds = np.where(dets[:, -1] >= CONF_THRESH)[0] if len(inds) == 0 : continue # vis_detections(image_name, im, cls, dets, thresh=CONF_THRESH) for i in inds: bbox = dets[i, :4] score = dets[i, -1] Label.append(cls_ind) Score.append(score) BBox.append(bbox[0]) BBox.append(bbox[1]) BBox.append(bbox[2]) BBox.append(bbox[3])
def callback2(self,bbox): global cv_image # obj_proposals bbox_num = len(bbox.data) / 4; print "bbox_num:", bbox_num obj_proposals = np.array( [ [0 for i in range(0,4)] for j in range(0,bbox_num) ] ) for i in range( 0, bbox_num ): obj_proposals[ i ][ 0 ] = bbox.data[ 4 * i ] obj_proposals[ i ][ 1 ] = bbox.data[ 4 * i + 2 ] obj_proposals[ i ][ 2 ] = bbox.data[ 4 * i + 1 ] obj_proposals[ i ][ 3 ] = bbox.data[ 4 * i + 3 ] ############################################## # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(self.net, cv_image, obj_proposals) timer.toc() print ('Detection took {:.3f}s for ' '{:d} object proposals').format(timer.total_time, boxes.shape[0]) # insert predicted class label to each box labels = np.array([0 for i in range(0,boxes.shape[0])]) for i in range(0,boxes.shape[0]): tmpscores = scores[i, :] labels[ i ] = np.argmax(tmpscores) # Visualize detections for each class output_image = cv_image for cls_ind in range(1,len(CLASSES)): if cls_ind not in labels: continue inds = np.where( labels == cls_ind ) cls_boxes = boxes[inds, 4*cls_ind:4*(cls_ind + 1)] cls_scores = scores[inds, cls_ind] cls_boxes = cls_boxes[ 0 ] cls_scores = cls_scores[ 0 ] dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, self.NMS_THRESH) dets = dets[keep, :] for i in range(0,dets.shape[0]): if dets[i, -1] > self.CONF_THRESH: bbox_ = dets[i, :4] print " DETECTED! ", CLASSES[ cls_ind ], dets[i, :] cv2.rectangle(output_image, (bbox_[ 0 ], bbox_[ 1 ]),(bbox_[ 2 ], bbox_[ 3 ]),(0,0,255),2) cv2.putText(output_image,CLASSES[ cls_ind ],(bbox_[ 0 ], bbox_[ 1 ]),cv2.FONT_HERSHEY_COMPLEX, 1.,(0,0,255),2) cv2.imshow("Image window", output_image) cv2.waitKey(3)
def test_net(net, imdb): """Test a Fast R-CNN network on an image database.""" num_images = len(imdb.image_index) # heuristic: keep an average of 40 detections per class per images prior # to NMS max_per_set = 40 * num_images # heuristic: keep at most 100 detection per class per image prior to NMS max_per_image = 100 # detection thresold for each class (this is adaptively set based on the # max_per_set constraint) thresh = -np.inf * np.ones(imdb.num_classes) # top_scores will hold one minheap of scores per class (used to enforce # the max_per_set constraint) top_scores = [[] for _ in xrange(imdb.num_classes)] # 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, net) if not os.path.exists(output_dir): os.makedirs(output_dir) # timers _t = {'im_detect': Timer(), 'misc': Timer()} roidb = imdb.roidb for i in xrange(num_images): im = cv2.imread(imdb.image_path_at(i)) _t['im_detect'].tic() scores, boxes = im_detect(net, im, roidb[i]['boxes']) _t['im_detect'].toc() _t['misc'].tic() inds = np.where(roidb[i]['gt_classes'] == 0)[0] cls_scores = scores[inds, 1] cls_boxes = boxes[inds, 4:8] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32, copy=False) sio.savemat('%s/%d.mat' % (output_dir, i), {'dets': dets}) if 0: keep = nms(dets, 0.3) vis_detections(im, imdb.classes[1], dets[keep, :]) _t['misc'].toc() print 'im_detect: {:d}/{:d} {:.3f}s {:.3f}s' \ .format(i + 1, num_images, _t['im_detect'].average_time, _t['misc'].average_time) """
def extract_regions_and_feats(sess, net, im, max_per_image=10, max_per_class=3, thresh=0.1): """Extract regions and features with respect to each region""" # all detections are collected into: # all_boxes[cls][image] = N x 5 array of detections in # (x1, y1, x2, y2, score) # timecrs _t = {'im_detect' : Timer(), 'misc' : Timer()} if type(im) == str: im = cv2.imread(im) _t['im_detect'].tic() scores, boxes, feats = im_detect(sess, net, im) _t['im_detect'].toc() _t['misc'].tic() all_boxes = [[] for _ in xrange(81)] all_feats = [[] for _ in xrange(81)] # skip j = 0, because it's the background class for j in xrange(1, 81): image_thresh = np.sort(scores[:,j])[-max_per_image] th = thresh if thresh > image_thresh else image_thresh inds = np.where(scores[:, j] > th)[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, :] feats_part = feats[keep, :] all_boxes[j] = cls_dets all_feats[j] = feats_part # 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 xrange(1, 81)]) if len(image_scores) > max_per_image: image_thresh = np.sort(image_scores)[-max_per_image] for j in xrange(1, 81): keep = np.where(all_boxes[j][:, -1] >= image_thresh)[0] all_boxes[j] = all_boxes[j][keep, :] all_feats[j] = all_feats[j][keep, :] _t['misc'].toc() # print 'im_detect in {:.3f}s {:.3f}s' \ # .format(_t['im_detect'].average_time, # _t['misc'].average_time) boxes = get_boxes(sess, net, im, all_feats, all_boxes) return boxes
def test_net(net, imdb): """Test a Fast R-CNN network on an image database.""" num_images = len(imdb.image_index) # heuristic: keep an average of 40 detections per class per images prior # to NMS max_per_set = 40 * num_images # heuristic: keep at most 100 detection per class per image prior to NMS max_per_image = 100 # detection thresold for each class (this is adaptively set based on the # max_per_set constraint) thresh = -np.inf * np.ones(imdb.num_classes) # top_scores will hold one minheap of scores per class (used to enforce # the max_per_set constraint) top_scores = [[] for _ in xrange(imdb.num_classes)] # 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, net) if not os.path.exists(output_dir): os.makedirs(output_dir) # timers _t = {'im_detect' : Timer(), 'misc' : Timer()} roidb = imdb.roidb for i in xrange(num_images): im = cv2.imread(imdb.image_path_at(i)) _t['im_detect'].tic() scores, boxes = im_detect(net, im, roidb[i]['boxes']) _t['im_detect'].toc() _t['misc'].tic() inds = np.where(roidb[i]['gt_classes'] == 0)[0] cls_scores = scores[inds, 1] cls_boxes = boxes[inds, 4:8] dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32, copy=False) sio.savemat('%s/%d.mat' % (output_dir, i), {'dets': dets}) if 0: keep = nms(dets, 0.3) vis_detections(im, imdb.classes[1], dets[keep, :]) _t['misc'].toc() print 'im_detect: {:d}/{:d} {:.3f}s {:.3f}s' \ .format(i + 1, num_images, _t['im_detect'].average_time, _t['misc'].average_time) """
def demo(net, im_file, classes): """Detect object classes in an image using pre-computed object proposals.""" # Load pre-computed Selected Search object proposals #box_file = os.path.join(cfg.ROOT_DIR, 'data', 'demo',image_name + '_boxes.mat') #obj_proposals = sio.loadmat(box_file)['boxes'] # im_file = os.path.join(cfg.ROOT_DIR, 'data', 'demo', image_name + '.jpg') # im_file = os.path.join('/media/wxie/UNTITLED/vision_log/rgb', image_name + '.jpg') # Dummy bounding box list with only 1 bounding box the size of the image im = cv2.imread(im_file) img_size_box = np.array([[0,0,im.shape[1]-1,im.shape[0]-1]]) timer2 = Timer() timer2.tic() obj_proposals = run_dlib_selective_search(im_file) timer2.toc() print ('Proposal selective search took {:.3f}s').format(timer2.total_time) # 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(net, im, obj_proposals) # scores, boxes = im_detect(net, im, img_size_box) 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 in classes: cls_ind = CLASSES.index(cls) cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)] cls_scores = scores[:, cls_ind] keep = np.where(cls_scores >= CONF_THRESH)[0] cls_boxes = cls_boxes[keep, :] cls_scores = cls_scores[keep] dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] print 'All {} detections with p({} | box) >= {:.1f}'.format(cls, cls, CONF_THRESH) vis_detections(im, cls, dets, thresh=CONF_THRESH)
def apply_nms(all_boxes, thresh): """Apply non-maximum suppression to all predicted boxes output by the test_net method. """ num_classes = len(all_boxes) num_images = len(all_boxes[0]) nms_boxes = [[[] for _ in xrange(num_images)] for _ in xrange(num_classes)] for cls_ind in xrange(num_classes): for im_ind in xrange(num_images): dets = all_boxes[cls_ind][im_ind] if dets == []: continue keep = nms(dets, thresh) if len(keep) == 0: continue nms_boxes[cls_ind][im_ind] = dets[keep, :].copy() return nms_boxes
def demo(net, image_name, classes, NUM): """Detect object classes in an image using pre-computed object proposals.""" # Load pre-computed Selected Search object proposals box_file = os.path.join(cfg.ROOT_DIR, 'data', 'demo', 'lp', image_name + '_boxes') if NUM == 2000: box_file += '.mat' else: box_file += '1.mat' obj_proposals = sio.loadmat(box_file)['boxes'] # Load the demo image im_file = os.path.join(cfg.ROOT_DIR, 'data', 'demo', 'lp', image_name) if int(image_name) < 6: im_file += '.bmp' else: im_file += '.jpg' im = cv2.imread(im_file) # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(net, im, obj_proposals) timer.toc() print('Detection on image {:s} took {:.3f}s for ' '{:d} object proposals').format(image_name, timer.total_time, boxes.shape[0]) # Visualize detections for each class CONF_THRESH = 0.99 NMS_THRESH = 0.3 savefile = '' for cls in classes: cls_ind = CLASSES.index(cls) cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] keep = np.where(cls_scores >= CONF_THRESH)[0] cls_boxes = cls_boxes[keep, :] cls_scores = cls_scores[keep] 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, im_file, thresh=CONF_THRESH)
def apply_nms(dets, cls_ind=0, conf_threshold=0.5, nms_threshold=0.3): assert(len(dets)==2) scores, boxes = dets assert(scores.shape[0]==boxes.shape[0]) assert(scores.shape[1]*4==boxes.shape[1]) assert(cls_ind<scores.shape[1] and cls_ind>=0) cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)] cls_scores = scores[:, cls_ind] ids = cls_scores>conf_threshold cls_scores = cls_scores[ids] cls_boxes = cls_boxes[ids, :] dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, nms_threshold) dets = dets[keep, :] return dets[:, :-1], dets[:, -1]
def demo(net, image_name, classes): """Detect object classes in an image using pre-computed object proposals.""" # Load pre-computed Selected Search object proposals box_file = os.path.join(cfg.ROOT_DIR, 'data', 'demo', image_name + '_boxes.mat') obj_proposals = sio.loadmat(box_file)['boxes'] # Load the demo image im_file = os.path.join(cfg.ROOT_DIR, 'data', 'demo', image_name + '.jpg') im = cv2.imread(im_file) # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(net, im, obj_proposals) timer.toc() print ('Detection took {:.3f}s for ' '{:d} object proposals').format(timer.total_time, boxes.shape[0]) layer = 'fc7' count = 0 feat = net.blobs[layer].data sio.savemat('featmat.mat',{'feat':feat}) total_norm = 0.0 total_norm += np.sqrt((feat ** 2).sum(axis=1)).sum() count += feat.shape[0] print('avg feature norm: {:.3f}'.format(total_norm / count)) # Visualize detections for each class CONF_THRESH = 0.8 NMS_THRESH = 0.3 for cls in classes: cls_ind = CLASSES.index(cls) cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)] cls_scores = scores[:, cls_ind] keep = np.where(cls_scores >= CONF_THRESH)[0] cls_boxes = cls_boxes[keep, :] cls_scores = cls_scores[keep] dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] print 'All {} detections with p({} | box) >= {:.1f}'.format(cls, cls, CONF_THRESH) vis_detections(im, cls, dets, thresh=CONF_THRESH)
def boxes_filter(dets, PRE_NMS_TOPN, NMS_THRESH, POST_NMS_TOPN, USE_GPU=False): """ filter the proposal boxes """ # speed up nms if PRE_NMS_TOPN > 0: dets = dets[:min(len(dets), PRE_NMS_TOPN)] # apply nms if NMS_THRESH > 0 and NMS_THRESH < 1: if USE_GPU: keep = nms_gpu(dets, NMS_THRESH) else: keep = nms(dets, NMS_THRESH) dets = dets[keep, :] if POST_NMS_TOPN > 0: dets = dets[:min(len(dets), POST_NMS_TOPN)] return dets
def boxes_filter(dets, PRE_NMS_TOPN, NMS_THRESH, POST_NMS_TOPN, USE_GPU=False): """ filter the proposal boxes """ # speed up nms if PRE_NMS_TOPN > 0: dets = dets[: min(len(dets), PRE_NMS_TOPN)] # apply nms if NMS_THRESH > 0 and NMS_THRESH < 1: if USE_GPU: keep = nms_gpu(dets, NMS_THRESH) else: keep = nms(dets, NMS_THRESH) dets = dets[keep, :] if POST_NMS_TOPN > 0: dets = dets[: min(len(dets), POST_NMS_TOPN)] return dets
def demo(net, image_name, classes, ssdir, imgdir, savefile): """Detect object classes in an image using pre-computed object proposals.""" box_file = os.path.join(ssdir, image_name + '.mat') obj_proposals = sio.loadmat(box_file)['boxes'] # Load the demo image im_file = os.path.join(imgdir, image_name + '.jpg') im = cv2.imread(im_file) im2 = Image.open(im_file) draw = ImageDraw.Draw(im2) # print(np.shape(im2)) im = np.reshape(im, (1, im.shape[0], im.shape[1], 3)) #print(np.shape(im)) # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(net, im, obj_proposals) 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.3 NMS_THRESH = 0.3 thresh = 0.3 for cls in classes: cls_ind = CLASSES.index(cls) 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, :] # print 'All {} detections with p({} | box) >= {:.1f}'.format(cls, cls, CONF_THRESH) vis_detections_print(draw, cls, dets, thresh=CONF_THRESH) im2.save(savefile, "PNG") del draw
def demo(net, image_name, classes): """Detect object classes in an image using pre-computed object proposals.""" # Load pre-computed Selected Search object proposals (use pre-computed proposal(selective search or ...) box_file = os.path.join(cfg.ROOT_DIR, 'data', 'demo', image_name + '_boxes.mat') obj_proposals = sio.loadmat(box_file)['boxes'] # Load the demo image im_file = os.path.join(cfg.ROOT_DIR, 'data', 'demo', image_name + '.jpg') im = cv2.imread(im_file) # Detect all object classes and regress object bounds timer = Timer() timer.tic() # detect boxes with its scores, using the image(im) and the proposal(obj_proposals), with their model(net) scores, boxes = im_detect(net, im, obj_proposals) 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 print '###score, box', scores, boxes, # for each class, get all correct indexes, and the boxes, and scores for cls in classes: cls_ind = CLASSES.index(cls) # each loop represent one class, and then there are many detected boxes(or none), get All of them cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] keep = np.where(cls_scores >= CONF_THRESH)[0] cls_boxes = cls_boxes[keep, :] cls_scores = cls_scores[keep] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) # non-maximum NMS????(why NMS and det?) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] print 'All {} detections with p({} | box) >= {:.1f}'.format( cls, cls, CONF_THRESH) # draw the bounding-box of the detected objects(for each class) vis_detections(im, cls, dets, thresh=CONF_THRESH)
def demo(net, image_name, classes, video=False, frame=1): """Detect object classes in an image using pre-computed object proposals.""" # Load pre-computed Selected Search object proposals box_file = os.path.join(cfg.ROOT_DIR, 'data', 'demo', '000004_boxes.mat') obj_proposals = sio.loadmat(box_file)['boxes'] # Load the demo image print image_name if video: cap = cv2.VideoCapture(image_name) cap.set(cv2.CAP_PROP_POS_FRAMES, frame) ret, im = cap.read() cap.release() else: 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, obj_proposals) 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 for cls in classes: cls_ind = CLASSES.index(cls) cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] keep = np.where(cls_scores >= CONF_THRESH)[0] cls_boxes = cls_boxes[keep, :] cls_scores = cls_scores[keep] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] print 'All {} detections with p({} | box) >= {:.1f}'.format( cls, cls, CONF_THRESH) vis_detections(im, cls, dets, thresh=CONF_THRESH, index=frame)
def demo(net, image_name, classes, ssdir, imgdir, savefile): """Detect object classes in an image using pre-computed object proposals.""" box_file = os.path.join(ssdir, image_name + '.mat') obj_proposals = sio.loadmat(box_file)['boxes'] # Load the demo image im_file = os.path.join(imgdir, image_name + '.jpg') im = cv2.imread(im_file) im2 = Image.open(im_file) draw = ImageDraw.Draw(im2) # print(np.shape(im2)) im = np.reshape(im, (1, im.shape[0], im.shape[1], 3)) #print(np.shape(im)) # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(net, im, obj_proposals) 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.3 NMS_THRESH = 0.3 thresh = 0.3 for cls in classes: cls_ind = CLASSES.index(cls) 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, :] # print 'All {} detections with p({} | box) >= {:.1f}'.format(cls, cls, CONF_THRESH) vis_detections_print(draw, cls, dets, thresh=CONF_THRESH) im2.save(savefile, "PNG") del draw
def test_net_vertical(sess, net, imdb, weights_filename, max_per_image=100, thresh=0.05): count = 0 np.random.seed(cfg.RNG_SEED) """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, weights_filename) # timers _t = {'im_detect' : Timer(), 'misc' : Timer()} im = None for i in range(num_images): im = cv2.imread(imdb.image_path_at(i)) print (imdb.image_path_at(i)) _t['im_detect'].tic() scores, boxes = im_detect(sess, net, im) _t['im_detect'].toc() _t['misc'].tic() # skip j = 0, because it's the background class for j, cls in enumerate(CLASSES[1:]): j += 1 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][i] = cls_dets vertical_points = vis_detections(im, cls, cls_dets, thresh=0.9) #res_image, flag = vc.spine_contour(im, vertical_points, imdb.image_path_at(i)) #res_image = Draw_bone(res_image,vertical_points) #cv2.imwrite("test-" + str(count) + ".jpg", im) cv2.imshow('result', im) cv2.waitKey(1000) count = count + 1 '''
def draw (net, image_set, image_name, output_file, ssmode): """Detect object classes in an image using pre-computed object proposals.""" # Load pre-computed Selected Search object proposals if ssmode == 'full': box_file = os.path.join(coco_root, 'fast_rcnn_boxes_full', image_set, image_name + '.npz') else: box_file = os.path.join(coco_root, 'fast_rcnn_boxes', image_set, image_name + '.npz') if not os.path.exists(box_file): print 'File does not exist', box_file return arr = np.load(box_file) scores = arr['scores'] boxes = arr['boxes'] # Visualize detections for each class fig = plt.figure( figsize=(12, 12), dpi=80) ax = fig.add_subplot(111) im_file = os.path.join(coco_root, 'images', image_set, image_name + '.jpg') im = cv2.imread(im_file) CONF_THRESH = 0.1 NMS_THRESH = 0.1 for cls in CLASSES: if cls == '__background__': continue try: cls_ind = CLASSES.index(cls) 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, :] print 'All {} detections with p({} | box) >= {:.1f}'.format(cls, cls, CONF_THRESH) vis_detections(im, cls, dets, ax, thresh=CONF_THRESH) except: pass plt.savefig(output_file)
def demo(sess, net, image_name,bbox): """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) if os.path.isfile(os.path.join(data_dir, image_name)): im_file = os.path.join(data_dir, image_name) else: im_file = os.path.join(data_dir_2, image_name) revise=40 im = cv2.imread(im_file) pixel_means=np.array([[[102, 115, 122]]]) # Detect all object classes and regress object bounds timer = Timer() timer.tic() try: scores, boxes,_,_ = im_detect(sess, net, im) except Exception as e: print(e) return 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.0 NMS_THRESH = 1.0 for cls_ind, cls in enumerate(CLASSES[1:]): if cls=='authentic': continue 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, :] im_score=vis_detections(im, cls, dets,image_name, thresh=CONF_THRESH) return im_score
def internal_detect_object(self, image, classes): # Load Object proposals #timer = Timer() #timer.tic() obj_proposals = self._obj_proposal_module.get_object_proposals(image) #timer.toc() # Show Object proposals information #print 'it took {:.3f}s for image ceate {:d} obj_proposals'.format(timer.total_time, obj_proposals.shape[0]) # Detect all object classes and regress object bounds #timer = Timer() #timer.tic() scores, boxes = im_detect(self._net, image, obj_proposals) #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 #Dictionary Result for Detection class_detections = {} #Extract detect result for each Class for cls in classes: cls_ind = CLASSES.index(cls) cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)] cls_scores = scores[:, cls_ind] keep = np.where(cls_scores >= CONF_THRESH)[0] cls_boxes = cls_boxes[keep, :] cls_scores = cls_scores[keep] dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] #print 'All {} detections with p({} | box) >= {:.1f}'.format(cls, cls, CONF_THRESH) #get current class detect result detections = self.validate_detections(image, cls, dets, thresh=CONF_THRESH) if len(detections) is not 0: class_detections[cls] = detections #Return Detection Result return class_detections
def _detect_obj(self, im, rects): rospy.loginfo('{} object proposals'.format(len(rects))) scores, boxes = im_detect(self.net, im, rects) # Visualize detections for each class CONF_THRESH = 0.8 NMS_THRESH = 0.3 for cls in CLASSES[1:]: cls_ind = CLASSES.index(cls) cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)] cls_scores = scores[:, cls_ind] keep = np.where(cls_scores >= CONF_THRESH)[0] cls_boxes = cls_boxes[keep, :] cls_scores = cls_scores[keep] dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] im = vis_detections(im, cls, dets, thresh=CONF_THRESH) return im
def demo(net, image_name, classes): """Detect object classes in an image using pre-computed object proposals.""" # Load pre-computed Selected Search object proposals (use pre-computed proposal(selective search or ...) box_file = os.path.join(cfg.ROOT_DIR, "data", "demo", image_name + "_boxes.mat") obj_proposals = sio.loadmat(box_file)["boxes"] # Load the demo image im_file = os.path.join(cfg.ROOT_DIR, "data", "demo", image_name + ".jpg") im = cv2.imread(im_file) # Detect all object classes and regress object bounds timer = Timer() timer.tic() # detect boxes with its scores, using the image(im) and the proposal(obj_proposals), with their model(net) scores, boxes = im_detect(net, im, obj_proposals) 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 print "###score, box", scores, boxes, # for each class, get all correct indexes, and the boxes, and scores for cls in classes: cls_ind = CLASSES.index(cls) # each loop represent one class, and then there are many detected boxes(or none), get All of them cls_boxes = boxes[:, 4 * cls_ind : 4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] keep = np.where(cls_scores >= CONF_THRESH)[0] cls_boxes = cls_boxes[keep, :] cls_scores = cls_scores[keep] dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) # non-maximum NMS????(why NMS and det?) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] print "All {} detections with p({} | box) >= {:.1f}".format(cls, cls, CONF_THRESH) # draw the bounding-box of the detected objects(for each class) vis_detections(im, cls, dets, thresh=CONF_THRESH)
def demo(net, image_set, image_name, ax): """Detect object classes in an image using pre-computed object proposals.""" # Load pre-computed Selected Search object proposals box_file = os.path.join(coco_root, 'boxes', image_set, image_name + '.mat') obj_proposals = sio.loadmat(box_file)['boxes'] # Load the demo image im_file = os.path.join(coco_root, 'images', image_set, image_name + '.jpg') im = cv2.imread(im_file) # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(net, im, obj_proposals) 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.1 NMS_THRESH = 0.1 for cls in CLASSES: if cls == '__background__': continue try: cls_ind = CLASSES.index(cls) 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, :] print 'All {} detections with p({} | box) >= {:.1f}'.format(cls, cls, CONF_THRESH) vis_detections(im, cls, dets, ax, thresh=CONF_THRESH) except: pass output_file = os.path.join(coco_root, 'fast-rcnn', image_set, image_name + '.jpg') plt.savefig(output_file)
def realtime_detection(sess, net, imdb, image_folder, weights_filename, max_per_image=100, thresh=0.05, visualization='True'): np.random.seed(cfg.RNG_SEED) #cap = cv2.VideoCapture("/home/vca_ann/CNN/rcnn2/tf-faster-rcnn/data/demo/768x576.avi") cap = cv2.VideoCapture("/home/vca_ann/dataset/parking_1920_part_2.mp4") fig, ax = plt.subplots() while (True): ret, im = cap.read() #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(imdb._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.cla()