Esempio n. 1
0
def detect(net, im):
    """Detect object classes in an image using pre-computed object proposals."""
    ########!!!!!!!!!!!!   cancel iamge load, use image pass in
    # Load the demo image
    #im_file = os.path.join(cfg.DATA_DIR, 'demo', image_name)
    #im = cv2.imread(im_file)

    # Detect all object classes and regress object bounds
    timer = Timer()
    timer.tic()
    scores, boxes = im_detect(net, im)

    timer.toc()
    print('Detection took {:.3f}s for '
          '{:d} object proposals').format(timer.total_time, boxes.shape[0])

    # Visualize detections for each class
    CONF_THRESH = 0.4
    NMS_THRESH = 0.3
    # For human detecting
    # pdb.set_trace()
    cls_ind = 15
    cls = CLASSES[cls_ind]
    # 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, :]
    #print dets
    return cls, dets
Esempio n. 2
0
def setupNet():
    #if __name__ == '__main__':
    cfg.TEST.HAS_RPN = True  # Use RPN for proposals

    #args = parse_args()

    demo_net = 'zf'
    gpu_id = 0
    cpu_mode = 0
    prototxt = os.path.join(cfg.MODELS_DIR, NETS[demo_net][0],
                            'faster_rcnn_alt_opt', 'faster_rcnn_test.pt')
    caffemodel = os.path.join(cfg.DATA_DIR, 'faster_rcnn_models',
                              NETS[demo_net][1])

    if not os.path.isfile(caffemodel):
        raise IOError(('{:s} not found.\nDid you run ./data/script/'
                       'fetch_faster_rcnn_models.sh?').format(caffemodel))

    if cpu_mode:
        caffe.set_mode_cpu()
    else:
        caffe.set_mode_gpu()
        caffe.set_device(gpu_id)
        cfg.GPU_ID = gpu_id
    net = caffe.Net(prototxt, caffemodel, caffe.TEST)

    print '\n\nLoaded network {:s}'.format(caffemodel)

    # Warmup on a dummy image
    im = 128 * np.ones((300, 500, 3), dtype=np.uint8)
    for i in xrange(2):
        _, _ = im_detect(net, im)

    print 'Network is loaded'
    return net
Esempio n. 3
0
    def get_query_local_feat(self, query, box=None):
        '''
        Extract local query feature using bbx
        '''
        if box is None:

            # For paris and oxford
            query, bbx = self.query_info(query)

        else:

            # locations are provided
            xmin = box[0]
            ymin = box[1]
            xmax = box[2]
            ymax = box[3]

        im = cv2.imread(query)

        height = np.shape(im)[0]
        width = np.shape(im)[1]

        # Forward pass
        scores, boxes = test_ops.im_detect(self.net, im, boxes=None)

        # Get conv5 layer
        feat = self.net.blobs[self.layer].data.squeeze()

        # Get the image/feature ratio
        mult_h = float(np.shape(feat)[1]) / height
        mult_w = float(np.shape(feat)[2]) / width

        # Resize the bounding box to feature size
        if box is None:

            # Adjust query bounding box to feature space
            bbx[0] *= mult_w
            bbx[2] *= mult_w
            bbx[1] *= mult_h
            bbx[3] *= mult_h

        else:

            bbx = [
                int(math.floor(xmin * mult_w)),
                int(math.floor(ymin * mult_h)),
                int(math.ceil(xmax * mult_w)),
                int(math.ceil(ymax * mult_h))
            ]

        # Crop local features with bounding box
        local_feat = feat[:, bbx[1]:bbx[3], bbx[0]:bbx[2]]

        # sum pool
        if self.pooling is 'sum':
            local_feat = np.sum(np.sum(local_feat, axis=1), axis=1)
        else:
            local_feat = np.max(np.max(local_feat, axis=1), axis=1)

        return local_feat
  def extract_feat_image(self,image):

    im            = cv2.imread(image)
    scores, boxes = test_ops.im_detect(self.net, im, boxes=None)
    feat          = self.net.blobs[self.layer].data

    return feat
Esempio n. 5
0
def demo(net, image_name):
    """Detect object classes in an image using pre-computed object proposals."""

    # Load the demo image
    # im_file = os.path.join('/home/shelly/superstar/star/SuperNova/static/upload',image_name)
    # change filepath
    im_file = os.path.join(
        '/home/shelly/flask/star/star/SuperNova/static/upload', image_name)
    im = cv2.imread(im_file)

    # Detect all object classes and regress object bounds
    timer = Timer()
    timer.tic()
    scores, boxes = im_detect(net, im)
    timer.toc()
    print('Detection took {:.3f}s for '
          '{:d} object proposals').format(timer.total_time, boxes.shape[0])

    # Visualize detections for each class
    CONF_THRESH = 0.1
    NMS_THRESH = 0.3
    for cls_ind, cls in enumerate(CLASSES[1:]):
        cls_ind += 1  # because we skipped background
        cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)]
        cls_scores = scores[:, cls_ind]
        dets = np.hstack(
            (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32)
        keep = nms(dets, NMS_THRESH)
        dets = dets[keep, :]
        vis_detections(im, cls, dets, image_name, thresh=CONF_THRESH)
Esempio n. 6
0
def demo(sess, net, image_name):
    """Detect object classes in an image using pre-computed object proposals."""

    # Load the demo image
    im_file = os.path.join(cfg.DATA_DIR, 'demo', image_name)
    #im_file = os.path.join('/home/corgi/Lab/label/pos_frame/ACCV/training/000001/',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
    im = im[:, :, (2, 1, 0)]
    fig, ax = plt.subplots(figsize=(12, 12))
    ax.imshow(im, aspect='equal')

    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)
        keep = nms(dets, NMS_THRESH)
        dets = dets[keep, :]
        vis_detections(im, cls, dets, ax, thresh=CONF_THRESH)
Esempio n. 7
0
def test(a):
    cfg.TEST.HAS_RPN = True  # Use RPN for proposals

    # args = parse_args()

    prototxt = 'static/model/faster_rcnn_test.pt'
    caffemodel = 'static/model/VGG16_faster_rcnn_final.caffemodel'

    if not os.path.isfile(caffemodel):
        raise IOError(('{:s} not found.\nDid you run ./data/script/'
                       'fetch_faster_rcnn_models.sh?').format(caffemodel))

    caffe.set_mode_gpu()
    caffe.set_device(0)
    cfg.GPU_ID = 0
    net = caffe.Net(prototxt, caffemodel, caffe.TEST)

    print '\n\nLoaded network {:s}'.format(caffemodel)

    # Warmup on a dummy image
    im = 128 * np.ones((300, 500, 3), dtype=np.uint8)
    for i in xrange(2):
        _, _ = im_detect(net, im)

    print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
    print 'Demo for data/demo/{}'.format(a)
    demo(net, a)

    # plt.savefig("/home/shelly/superstar/star/SuperNova/index/static/test3.jpg",dpi=300)
    # change filepath
    plt.savefig(
        "/home/shelly/flask/star/star/SuperNova/index/static/test3.jpg",
        dpi=300)
	def extract_feat_image(self, image, layer_roi='pool5'):
		im 						= cv2.imread(image)
		scores, boxes = test_ops.im_detect(self.net, im, boxes = None, REG_BOXES=self.REG_BOXES)
		# extract feature
		feat          = self.net.blobs[layer_roi].data

		return feat, boxes, scores
Esempio n. 9
0
    def get_query_local_feat(self, query, box=None):
        im = cv2.imread(query)
        _ = test_ops.im_detect(self.net, im, boxes=None)
        feat = self.net.blobs[self.layer].data.squeeze()

        height, width = im.shape
        mult_h = float(np.shape(feat)[1]) / height
        mult_w = float(np.shape(feat)[2]) / width

        if box is None:
            query, bbx = self.query_info(query)
            bbx[0] *= mult_w
            bbx[2] *= mult_w
            bbx[1] *= mult_h
            bbx[3] *= mult_h
        else:
            xmin, ymin, xmax, ymax = box[0:4]
            bbx = [
                int(math.floor(xmin * mult_w)),
                int(math.floor(ymin * mult_h)),
                int(math.ceil(xmax * mult_w)),
                int(math.ceil(ymax * mult_h))
            ]

        local_feat = feat[:, bbx[1]:bbx[3], bbx[0]:bbx[2]]
        return self.pool_feats(local_feat)
Esempio n. 10
0
 def image2features(self, im):
     scores, boxes = test_ops.im_detect(self.net,
                                        im,
                                        boxes=None,
                                        REG_BOXES=self.use_regressed_boxes)
     feat = self.net.blobs[self.layer_roi].data
     return feat, boxes, scores
    def extract_feat_image(self, image):

        im = cv2.imread(image)
        scores, boxes = test_ops.im_detect(self.net, im, boxes=None)
        feat = self.net.blobs[self.layer].data

        return feat
Esempio n. 12
0
    def extract_feat_image(self, sess, image):

        im = cv2.imread(image)

        layer_roi = 'pool_5'
        scores, boxes, feat = test_ops.im_detect(sess, self.net, layer_roi, im, False, boxes = None)

        return feat,boxes,scores
Esempio n. 13
0
 def get_query_local_feat(self,query,box=None):
     
     '''
     Extract local query feature using bbx
     '''
     if box is None:
         
         # For paris and oxford
         query,bbx = self.query_info(query)
         
     else:
         
         # locations are provided
         xmin = box[0]
         ymin = box[1]
         xmax = box[2]
         ymax = box[3]
         
     im = cv2.imread(query)
     
     height = np.shape(im)[0]
     width = np.shape(im)[1]
 
     
     # Forward pass
     scores, boxes = test_ops.im_detect(self.net, im, boxes = None)
     
     # Get conv5 layer
     feat = self.net.blobs[self.layer].data.squeeze()
             
     # Get the image/feature ratio
     mult_h = float(np.shape(feat)[1])/height
     mult_w = float(np.shape(feat)[2])/width
 
     # Resize the bounding box to feature size
     if box is None:
         
         # Adjust query bounding box to feature space
         bbx[0] *= mult_w
         bbx[2] *= mult_w
         bbx[1] *= mult_h
         bbx[3] *= mult_h
         
     else:
         
         bbx = [int(math.floor(xmin*mult_w)),int(math.floor(ymin*mult_h)),int(math.ceil(xmax*mult_w)),int(math.ceil(ymax*mult_h))]
         
     # Crop local features with bounding box
     local_feat = feat[:,bbx[1]:bbx[3],bbx[0]:bbx[2]]
     
     # sum pool
     if self.pooling is 'sum':
         local_feat =  np.sum(np.sum(local_feat,axis=1),axis=1)
     else:
         local_feat =  np.max(np.max(local_feat,axis=1),axis=1) 
         
     return local_feat
Esempio n. 14
0
def demo(net, image_name):
    im_file = os.path.join(cfg.DATA_DIR, 'demo', image_name)

    im = cv2.imread(im_file)

    det_boxes, det_probs, det_class = im_detect(net, im)

    print('det_boxes: ', det_boxes)
    print('det_probs: ', det_probs)
    print('det_class: ', det_class)
    def extract_feat_image(self, image, layer_roi='pool5'):
        im = cv2.imread(image)
        scores, boxes = test_ops.im_detect(self.net,
                                           im,
                                           boxes=None,
                                           REG_BOXES=self.REG_BOXES)
        # extract feature
        feat = self.net.blobs[layer_roi].data

        return feat, boxes, scores
Esempio n. 16
0
    def extract_feat_image(self, sess, image):

        im = cv2.imread(image)

        scores, boxes, feat = test_ops.im_detect(sess,
                                                 self.net,
                                                 self.layer,
                                                 im,
                                                 True,
                                                 boxes=None)
        # feat = self.net.get_output(self.layer)

        return feat
Esempio n. 17
0
 def get_query_local_feat(self, query, box=None):
     im   = cv2.imread(query)
     _    = test_ops.im_detect(self.net, im, boxes = None)
     feat = self.net.blobs[self.layer].data.squeeze()
     
     height, width = im.shape
     mult_h = float(np.shape(feat)[1]) / height
     mult_w = float(np.shape(feat)[2]) / width
     
     if box is None:
         query, bbx = self.query_info(query)
         bbx[0] *= mult_w
         bbx[2] *= mult_w
         bbx[1] *= mult_h
         bbx[3] *= mult_h
     else:
         xmin, ymin, xmax, ymax = box[0:4]
         bbx = [int(math.floor(xmin*mult_w)),int(math.floor(ymin*mult_h)),int(math.ceil(xmax*mult_w)),int(math.ceil(ymax*mult_h))]
     
     local_feat = feat[:,bbx[1]:bbx[3],bbx[0]:bbx[2]]
     return self.pool_feats(local_feat)
Esempio n. 18
0
def demo(sess, net, image_name):
    """Detect object classes in an image using pre-computed object proposals."""
    # im_names = ['000456.jpg', '000542.jpg', '001150.jpg',
    #             '001763.jpg', '004545.jpg']
    #一张张图片输入
    # net = vgg16()
    # demo(sess, net, im_name)

    im_file = os.path.join(cfg.DATA_DIR, 'demo', image_name)
    im = cv2.imread(im_file)

    # Detect all object classes and regress object bounds
    timer = Timer()
    timer.tic()
    #输入 net = vgg16()   im一张图片
    scores, boxes = im_detect(sess, net, im)

    # post_nms_topN = cfg[cfg_key].RPN_POST_NMS_TOP_N#__C.TEST.RPN_POST_NMS_TOP_N = 300    非极大值抑制输出的 最大个数
    # nms_thresh = cfg[cfg_key].RPN_NMS_THRESH#__C.TEST.RPN_NMS_THRESH = 0.7

    # scores 是rpn scores = self._predictions['cls_prob']  =  每个类别的概率cls_score 讲过soft_max得到
    # pred_boxes
    # pred_boxes = bbox_transform_inv(boxes, box_deltas)
    # 做回归预测   两条路劲rpn得到的的 box_deltas 作为 dx dy dw dh 与 筛选出来的框做回归预测
    # pred_boxes  anchors回归预测后的值
    # return scores, pred_boxes



    timer.toc()
    print('Detection took {:.3f}s for {:d} object proposals'.format(timer.total_time, boxes.shape[0]))

    # Visualize detections for each class
    CONF_THRESH = 0.8
    NMS_THRESH = 0.3
    for cls_ind, cls in enumerate(CLASSES[1:]):#CLASSES就是英文标签
        cls_ind += 1 # because we skipped background
        cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)]
        #得到抽取出来的 一个框  一个 boxes shape (?, 21*4)  得到 [?*21,4]
        cls_scores = scores[:, cls_ind]#scores    shape  (?,21)  #  得到[?*21]
        dets = np.hstack((cls_boxes,# [?*21,4]
                          cls_scores[:, np.newaxis])).astype(np.float32)#[?*21,1]


        #得到的是[?*21,x,y,w,h,scores]  0.3
        #所有的框预测 21个类别  输入的是  所有的框预测一个类别的框与得分值
        # 是一个一个类别输入NMS  假如类别1    则输入 [?,x,y,w,h,scores]
        keep = nms(dets, NMS_THRESH)#为什么要用   (cpu)gpu_nms.pys
        #nms 纯python语言实现:简介方便、速度慢
        #Cython是一个快速生成Python扩展模块的工具,从语法层面上来讲是Python语法和C语言语法的混血,
        # 当Python性能遇到瓶颈时,Cython直接将C的原生速度植入Python程序,这样使Python程序无需使用C重写
        # ,能快速整合原有的Python程序,这样使得开发效率和执行效率都有很大的提高,而这些中间的部分,
        # 都是Cython帮我们做了。

        #https://www.cnblogs.com/king-lps/p/9031568.html  解释
        #  之前的 非极大值抑制  作用实在rpn路径上的   rpn路径就是为了做推荐而已 推荐 VGG16特征出来的框,但是太多 所以需要筛选
        #这里 是筛选出来之后  再做  nms


        #CONF_THRESH = 0.8
        #NMS_THRESH = 0.3
        dets = dets[keep, :]
        #im输入的图像经过限制在600,1000
        #cls  是实际的英文标签
        # dets是 [?*21,x,y,w,h,scores]  经过nms 得到 的    0.3
        #for      cls_ind, cls         in enumerate(CLASSES[1:]):#CLASSES就是英文标签


        #所有的框预测  21个类别  输入的是  所有的框预测一个类别的框与得分值
        # 进行NMS后 得到的框就是需要输出的框   他对应的英文标签是  cls    #CONF_THRESH = 0.8
        vis_detections(im, cls, dets, thresh=CONF_THRESH)
Esempio n. 19
0
 def image2features(self, im):
     _ = test_ops.im_detect(self.net, im, boxes=None)
     feats = self.net.blobs[self.layer].data.squeeze()
     return self.pool_feats(feats)
Esempio n. 20
0
 def image2features(self, im):
     scores, boxes = test_ops.im_detect(self.net, im, boxes=None, REG_BOXES=self.use_regressed_boxes)
     feat          = self.net.blobs[self.layer_roi].data
     return feat, boxes, scores
Esempio n. 21
0
    if not os.path.isfile(args.model):
        raise IOError(('{:s} not found.\nDid you run ./data/script/'
                       'fetch_faster_rcnn_models.sh?').format(caffemodel))
    # init session
    sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True))
    # load network
    net = get_network(args.demo_net)
    # load model
    saver = tf.train.Saver()
    saver.restore(sess, args.model)
    #sess.run(tf.initialize_all_variables())

    print '\n\nLoaded network {:s}'.format(args.model)

    # Warmup on a dummy image
    im = 128 * np.ones((720, 1280, 3), dtype=np.uint8)
    for i in xrange(2):
        _, _ = im_detect(sess, net, im)

    #im_names = ['000001.jpg','000002.jpg','000005.jpg','000050.jpg','000100.jpg']
    im_names = [
        '000456.jpg', '000542.jpg', '001150.jpg', '001763.jpg', '004545.jpg'
    ]

    for im_name in im_names:
        print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
        print 'Demo for data/demo/{}'.format(im_name)
        demo(sess, net, im_name)

    plt.show()