Exemplo n.º 1
0
    def Faster_run(self, image, is_init=True):

        #print (' done.')

        # Warmup on a dummy image
        im = 128 * np.ones((300, 300, 3), dtype=np.uint8)
        for i in xrange(2):
            im_detect(self.sess, self.net, im)
        self.demo(image_name=image, is_init=is_init)
        return self.objects, self.feature_map, self.rpn_boxes, self.rpn_scores, self.im_scales
Exemplo n.º 2
0
    def process_frame(self, video_name, im_name, CLASSES, CONF_THRESH):
        # Output frame path
        im_path_ = os.path.join(api_config.upload_folder,
                                video_name.split(".")[0],
                                "annotated-frames", os.path.basename(im_name))
        im = np.array(Image.open(im_name))
        im = im[:, :, ::-1]
        timer = Timer()
        timer.tic()
        scores, boxes = im_detect(self.sess, self.net, im)
        timer.toc()
        print ('Detection took {:.3f}s for '
               '{:d} object proposals').format(timer.total_time,
                                               boxes.shape[0])

        NMS_THRESH = 0.3
        im = im[:, :, (2, 1, 0)]
        fig, ax = plt.subplots(figsize=(12, 12))
        ax.imshow(im, aspect='equal')
        self.annotation = xml_setup(im_name, im.shape)
        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, :]
            self.draw(im_path_, cls, dets, ax, thresh=CONF_THRESH)
        xml_write(video_name, os.path.basename(im_name), self.annotation)
        plt.savefig(im_path_, bbox_inches='tight')
        plt.close()
Exemplo n.º 3
0
def demo(sess, net, image_name):
    """Detect object classes in an image using pre-computed object proposals."""

    # Load the demo image
    im = cv2.imread(image_name)

    # 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.8
    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)
Exemplo n.º 4
0
def demo(sess, net, image_name):
    """Detect object classes in an image using pre-computed object proposals."""

    # Load the demo image
    im = cv2.imread(image_name)

    # 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.8
    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)
Exemplo n.º 5
0
    def __init__(self):
        cfg.TEST.HAS_RPN = True  # Use RPN for proposals

        prototxt = '/home/andrewsilva/faster_rcnn/py-faster-rcnn/models/coco_blocks/faster_rcnn_alt_opt/faster_rcnn_test.pt'
        caffemodel = '/home/andrewsilva/faster_rcnn/py-faster-rcnn/output/default/train/coco_blocks_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))
        # TODO set this to be my list of classes
        self.class_list = [
            '__background__', 'person', 'backpack', 'bottle', 'cup', 'bowl',
            'banana', 'apple', 'orange', 'pizza', 'donut', 'tv', 'laptop',
            'cell phone', 'book', 'screw', 'block', 'beam'
        ]

        caffe.set_mode_gpu()
        caffe.set_device(0)
        cfg.GPU_ID = 0
        self.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(1):
            _, _ = im_detect(self.net, im)
Exemplo n.º 6
0
def demo(net, matlab, image_filepath, classes, method, par1, par2):
    # Detect all object classes and regress object bounds
    timer = Timer()
    timer.tic()
    # Load pre-computed Selected Search object proposals
    obj_proposals = ROI_boxes(matlab, image_filepath, method, par1, par2)
    global OP_num
    OP_num = len(obj_proposals)
    if len(obj_proposals)==0:
        dets = []
        timer.toc()
        return dets, timer.total_time

    # Load the demo image
    im = cv2.imread(image_filepath)
    scores, boxes = im_detect(net, im, obj_proposals)
    timer.toc()

    # 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, :]
    return dets, timer.total_time
Exemplo n.º 7
0
def demo(sess, net, image_name, thresh=0.05):
    """Detect object classes in an image using pre-computed object proposals."""

    # Load the demo image
    image = PIL.Image.open(image_name)
    im = cv2.imread(image_name)

    # Detect all object classes and regress object bounds
    timer = Timer()
    timer.tic()

    im_num = os.path.split(image_name)[1].split('.')[0]
    scores, boxes = im_detect(sess,
                              net,
                              im,
                              save_feature=True,
                              feature_path='./data/conv.npy')
    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.7
    NMS_THRESH = 0.3
    results = []
    name = image_name.split('/')[-1]
    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]
        cls_lables = np.full_like(cls_scores, cls_ind)
        dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis],
                          cls_lables[:, np.newaxis])).astype(np.float32)
        keep = nms(dets, NMS_THRESH)
        dets = dets[keep, :]
        inds = np.where(dets[:, -2] > thresh)[0]
        dets = dets[inds]
        for i in range(dets.shape[0]):
            name = str(name)
            category = int(dets[i, -1])
            bbox = list(map(float, dets[i, :4]))
            bbox = [round(b, 2) for b in bbox]
            score = float(dets[i, -2])
            dic = collections.OrderedDict()
            dic['name'] = str(name)
            dic['category'] = int(category)
            dic['bbox'] = bbox
            dic['score'] = float(score)
            results.append(dic)
        im = vis_detections(image, cls, dets, ax=None, thresh=CONF_THRESH)

    out_path = './data/detection_result'
    if not os.path.exists(out_path):
        os.makedirs(out_path)
    out_path = os.path.join(out_path, os.path.split(image_name)[-1])
    image.save(out_path)
Exemplo n.º 8
0
def demo(sess, net, image_name):
# def parse_args():
#     """Parse input arguments."""
#     parser = argparse.ArgumentParser(description='Faster R-CNN demo')
#     parser.add_argument('--gpu', dest='gpu_id', help='GPU device id to use [0]',
#                         default=0, type=int)
#     parser.add_argument('--cpu', dest='cpu_mode',
#                         help='Use CPU mode (overrides --gpu)',
#                         action='store_true')
#     parser.add_argument('--net', dest='demo_net', help='Network to use [vgg16]',
#                         default='VGGnet_test')
#     # parser.add_argument('--model', dest='model', help='Model path',
#     #                     default='model/VGGnet_fast_rcnn_iter_60000.ckpt')
#
#     args = parser.parse_args()
#
#     return args

if __name__ == '__main__':
    cfg.TEST.HAS_RPN = True  # Use RPN for proposals

    # args = parse_args()

    # if args.model == ' ' or not os.path.exists(args.model):
    #     print ('current path is ' + os.path.abspath(__file__))
    #     raise IOError(('Error: Model not found.\n'))

    # model = os.path.join(this_dir,'output','default','voc_2007_trainval','VGGnet_fast_rcnn_iter_100000.ckpt')

    model= os.path.join(this_dir,'model-6.16.ckpt')

    # init session
    sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True))
    # load network
    net = get_network('VGGnet_test')
    # load model
    # print ('Loading network {:s}... '.format(args.demo_net)),
    saver = tf.train.Saver()
    saver.restore(sess, model)
    # print (' done.')
    # model = os.path.join(this_dir, 'model.ckpt')
    # saver.restore(sess, model)

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

    im_names = glob.glob(os.path.join(cfg.DATA_DIR, 'demo42', '*.*'))

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

    plt.show()
Exemplo n.º 9
0
    def detect(self,
               img,
               ret=dict(),
               net='VGGnet_test',
               model=os.path.join(local_dir,
                                  'models/VGGnet_fast_rcnn_iter_150000.ckpt')):
        """Detect object classes in an image using pre-computed object proposals."""
        self.sess = tf.Session(config=tf.ConfigProto(
            allow_soft_placement=True))
        # load network
        self.net = get_network(net)
        #load model
        print('Loading network {:s}... '.format(net)),
        self.saver = tf.train.Saver()
        self.saver.restore(self.sess, model)
        print(' done.')

        starttime = time.time()
        # Load the demo image
        im = cv2.imread(img)
        print im.shape
        im = cv2.resize(im, (int((400.0 / im.shape[0]) * im.shape[1]), 400))
        print im.shape
        # Detect all object classes and regress object bounds
        timer = Timer()
        timer.tic()
        scores, boxes = im_detect(self.sess, self.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.8
        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, :]
            im = self.vis_detections(im, cls, dets, CONF_THRESH)
        endtime = time.time()
        ret['result'] = (True, "%.3f" % (endtime - starttime))
        ret['drawImg'] = im
        print "finish detecting"
        return im
Exemplo n.º 10
0
def demo(sess, net, image_name):
    """Detect object classes in an image using pre-computed object proposals."""

    # Load the demo image
    im = cv2.imread(image_name)

    # 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.2
    NMS_THRESH = 0.2

    cls_name = dict(zip(np.arange(len(CLASSES)), CLASSES))
    cls_matrix = np.arange(len(CLASSES)).reshape([1, -1]) + np.zeros(
        [boxes.shape[0], 1])
    cls_scores_fg = scores[:, 1:len(CLASSES)]
    cls_boxes_fg = boxes[:, 4:4 * (len(CLASSES))]
    cls_matrix_fg = cls_matrix[:, 1:len(CLASSES)]

    cls_scores_fg = cls_scores_fg.reshape([-1, 1])
    cls_boxes_fg = cls_boxes_fg.reshape([-1, 4])
    cls_matrix_fg = cls_matrix_fg.reshape([-1, 1])

    keeps = np.where(cls_scores_fg >= CONF_THRESH)[0]

    cls_scores_fg = cls_scores_fg[keeps]
    cls_boxes_fg = cls_boxes_fg[keeps]
    cls_matrix_fg = cls_matrix_fg[keeps]

    dets = np.hstack((cls_boxes_fg, cls_scores_fg)).astype(np.float32)
    keep = nms(dets, NMS_THRESH)
    print len(keep)
    dets = dets[keep, :]
    cls_matrix_fg = cls_matrix_fg[keep, :]
    for cls_ind, cls in enumerate(CLASSES[1:]):
        cls_ind += 1
        cls_fg_ind = np.where(cls_matrix_fg == cls_ind)[0]
        detses = dets[cls_fg_ind, :]
        vis_detections(im, cls, detses, ax, thresh=CONF_THRESH)
Exemplo n.º 11
0
def demo(net, matlab, image_filepath, classes, args):
    """Detect object classes in an image using pre-computed object proposals."""
    timer = Timer()
    timer.tic()
    # Load pre-computed Selected Search object proposals
    obj_proposals = ROI_boxes(matlab, image_filepath, args.OP_method)
    if len(obj_proposals)==0:
        return

    # Load the demo image
    im = cv2.imread(image_filepath)

    # Detect all object classes and regress object bounds

    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, :]
        if (len(dets) == 0):
            global count
            count += 1
            print('{} No Ear detected').format(count)
        # print 'All {} detections with p({} | box) >= {:.1f}'.format(cls, cls,
        #                                                             CONF_THRESH)
        if args.video_mode:
            visualise(im, cls, dets, thresh=CONF_THRESH)
        elif args.image_path is not None:
            vis_detections(im, cls, dets, thresh=CONF_THRESH)
Exemplo n.º 12
0
def demo(net, matlab, image_filepath, classes, args):
    """Detect object classes in an image using pre-computed object proposals."""
    timer = Timer()
    timer.tic()
    # Load pre-computed Selected Search object proposals
    obj_proposals = ROI_boxes(matlab, image_filepath, args.OP_method)
    if len(obj_proposals) == 0:
        return

    # Load the demo image
    im = cv2.imread(image_filepath)

    # Detect all object classes and regress object bounds

    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, :]
        if (len(dets) == 0):
            global count
            count += 1
            print('{} No Ear detected').format(count)
        # print 'All {} detections with p({} | box) >= {:.1f}'.format(cls, cls,
        #                                                             CONF_THRESH)
        if args.video_mode:
            visualise(im, cls, dets, thresh=CONF_THRESH)
        elif args.image_path is not None:
            vis_detections(im, cls, dets, thresh=CONF_THRESH)
Exemplo n.º 13
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')
    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]
        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)
Exemplo n.º 14
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')
    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]
        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)
Exemplo n.º 15
0
 def find_objects(self, input_image):
     # input_image = input_image.astype(float)
     # start_time = time.time()
     caffe.set_mode_gpu()
     caffe.set_device(0)
     scores, boxes = im_detect(self.net, input_image)
     # print 'CNN took: ', time.time() - start_time
     # Visualize detections for each class
     objects_detected = []
     CONF_THRESH = 0.7
     NMS_THRESH = 0.2
     class_index = 11
     class_name = 'tv'
     class_boxes = boxes[:, 4 * class_index:4 * (class_index + 1)]
     class_scores = scores[:, class_index]
     detections = np.hstack(
         (class_boxes, class_scores[:, np.newaxis])).astype(np.float32)
     keepers = nms(detections, NMS_THRESH)
     detections = detections[keepers, :]
     detections = detections[detections[:, -1] >= CONF_THRESH]
     detections[:,
                -1] = 3  # TODO: Hack so that TV returns as 3 which is tablet in the filter...
     objects_detected.append(detections)
     for cls_ind, cls in enumerate(self.class_list[15:]):
         cls_ind += 15  # 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, :]
         dets = dets[dets[:, -1] >= CONF_THRESH]
         dets[:, -1] = 2  # TODO: Hack so that all blocks have object ID 2
         # append data structure with format [[x1, y1, x2, y2, obj_id], [x1, y1, x2, y2, obj_id], ...] for all boxes
         objects_detected.append(dets)
     return objects_detected
Exemplo n.º 16
0
    if args.model == ' ' or not os.path.exists(args.model):
        print ('current path is ' + os.path.abspath(__file__))
        raise IOError(('Error: Model not found.\n'))

    # init session
    sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True))
    # load network
    net = get_network(args.demo_net)
    # load model
    print ('Loading network {:s}... '.format(args.demo_net)),
    saver = tf.train.Saver()
    saver.restore(sess, args.model)
    print (' done.')

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

    im_names = glob.glob(os.path.join(cfg.DATA_DIR, 'demo', '*.png')) + \
               glob.glob(os.path.join(cfg.DATA_DIR, 'demo', '*.jpg'))

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

    plt.show()

Exemplo n.º 17
0
def extract_fc7_features(net, img_box_label, img_root, list_path, feature_root,
                         label_list_path, label2index, raw2path, sample_ratio,
                         dataset):
    # check output file existence
    if os.path.exists(label_list_path):
        os.remove(label_list_path)
    label_list = []

    # load image list of current dataset
    with open(list_path, 'r') as list_file:
        image_list = list_file.read().splitlines()

    # for each image
    for i in range(0, len(image_list)):
        image_id = image_list[i]
        print('fc7 processing[%d/%d] %s' % (len(image_list),
                                            (i + 1), image_id))
        if image_id not in img_box_label:
            continue

        # get boxes
        curr_img_boxes = np.array(img_box_label[image_id])
        box_num = curr_img_boxes.shape[0]
        if box_num == 0:
            continue

        # fc7 feature saving path
        feature_id = image_id + '.bin'
        feature_path = os.path.join(feature_root, feature_id)

        if not os.path.exists(feature_path):
            # extract fc7
            img = cv2.imread(os.path.join(img_root, image_id + '.jpg'))

            # pre fc7
            im_detect(net, img, curr_img_boxes[:, :4])
            pre_fc7s = np.array(net.blobs['fc7'].data)

            # sbj fc7
            im_detect(net, img, curr_img_boxes[:, 5:9])
            sbj_fc7s = np.array(net.blobs['fc7'].data)

            # obj fc7
            im_detect(net, img, curr_img_boxes[:, 10:14])
            obj_fc7s = np.array(net.blobs['fc7'].data)

            fc7s = np.concatenate((sbj_fc7s, pre_fc7s, obj_fc7s), axis=1)

            # dump feature file
            with open(feature_path, 'w') as feature_file:
                pickle.dump(fc7s, feature_file)

        # prepare roidb
        # format:
        # img_id.bin offset label_ind vrd_label_ind
        for box_id in range(0, len(curr_img_boxes)):
            raw_label_ind = curr_img_boxes[box_id, 4]
            label_list.append(feature_id + ' ' + str(box_id) + ' ' +
                              str(raw_label_ind) + ' ' + str(raw_label_ind) +
                              '\n')

            if dataset == 'test':
                continue

            label_inds = raw2path[raw_label_ind]
            # last one on path is raw label
            for i in range(len(label_inds) - 1):
                label_ind = label_inds[i]
                sample_prob = sample_ratio[label_ind]
                p = np.array([sample_prob, 1 - sample_prob])
                sample = np.random.choice([True, False], p=p.ravel())
                if sample:
                    label_list.append(feature_id + ' ' + str(box_id) + ' ' +
                                      str(label_ind) + ' ' +
                                      str(raw_label_ind) + '\n')

        if (i + 1) % 10000 == 0 or (i + 1) == len(image_list):
            with open(label_list_path, 'a') as label_file:
                label_file.writelines(label_list)
            del label_list
            label_list = []

    if len(label_list) > 0:
        with open(label_list_path, 'a') as label_file:
            label_file.writelines(label_list)
Exemplo n.º 18
0
    args = parse_args()

    if args.model == ' ' or not os.path.exists(args.model):
        print('current path is ' + os.path.abspath(__file__))
        raise IOError(('Error: Model not found.\n'))

    # init session
    sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True))
    # load network
    net = get_network(args.demo_net)
    # load model
    print('Loading network {:s}... '.format(args.demo_net)),
    saver = tf.train.Saver()
    saver.restore(sess, args.model)
    print(' done.')

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

    im_names = glob.glob(os.path.join(cfg.DATA_DIR, 'demo', '*.png')) + \
               glob.glob(os.path.join(cfg.DATA_DIR, 'demo', '*.jpg'))

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

    plt.show()
Exemplo n.º 19
0
    def demo(self, image_name, is_init=True):
        """Detect object classes in an image using pre-computed object proposals."""

        # Detect all object classes and regress object bounds
        timer = Timer()
        timer.tic()
        if is_init:
            raw_scores, raw_boxes, self.feature_map, self.rpn_boxes, self.rpn_scores, self.im_scales = im_detect(
                self.sess, self.net, image_name, is_part=False)
            CONF_THRESH = self.score_thresh
            NMS_THRESH = self.nms_thresh
            self.objects = []
            for cls_ind, cls in enumerate(CLASSES[1:]):
                cls_ind += 1  # because we skipped background
                cls_boxes = raw_boxes[:, 4 * cls_ind:4 * (cls_ind + 1)]
                cls_scores = raw_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:
                    for i in inds:
                        bbox = dets[i, :4]
                        score = dets[i, -1]
                        box_height = bbox[3] - bbox[1]
                        box_width = bbox[2] - bbox[0]
                        c_x = np.round(bbox[0] + box_width / 2.0)
                        c_y = np.round(bbox[1] + box_height / 2.0)
                        if cls == 'stawberry':
                            cls = 'strawberry'
                        object_coordinates = {
                            'name': cls,
                            'score': score,
                            'boxes': list([c_x, c_y, box_width, box_height])
                        }
                        self.objects.append(object_coordinates)
        else:
            _, _, self.feature_map, self.rpn_boxes, self.rpn_scores, self.im_scales = im_detect(
                self.sess, self.net, image_name, is_part=True)
        timer.toc()
Exemplo n.º 20
0
    #    f=open(this_dir+'/v26_v51/detect_v26_v51.txt','w')
    f = open(this_dir + '/v52_v77/detect_v52_v77.txt', 'w')
    #    f=open(this_dir+'/v78_v107/detect_v78_v107.txt','w')

    for cate in range(52, 78):
        im_names = glob.glob(
            os.path.join(this_dir + '/v52_v77/videos/video_' + str(cate) +
                         '/images/*.jpg'))
        for im_name in im_names:
            k = 0
            im = cv2.imread(im_name)

            # 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])

            CONF_THRESH = 0.7
            NMS_THRESH = 0.1
            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, :]
Exemplo n.º 21
0
def extract_fc7_features(net, img_box_label, img_root, list_path, feature_root,
                         label_list_path, vrd2path, sample_ratio, dataset):
    # check output file existence
    if os.path.exists(label_list_path):
        os.remove(label_list_path)
    label_list = []

    # loading image file list of current dataset
    with open(list_path, 'r') as list_file:
        image_list = list_file.read().splitlines()

    # for each image file
    for i in range(0, len(image_list)):
        image_id = image_list[i]
        print('fc7 processing[%d/%d] %s' % (len(image_list),
                                            (i + 1), image_id))
        if image_id not in img_box_label:
            continue

        # get boxes
        curr_img_boxes = np.array(img_box_label[image_id])
        box_num = curr_img_boxes.shape[0]
        if box_num == 0:
            continue

        # fc7 feature saving path
        feature_id = image_id + '.bin'
        feature_path = os.path.join(feature_root, feature_id)

        if not os.path.exists(feature_path):
            # extract fc7
            img_path = os.path.join(img_root, image_id + '.jpg')
            img = cv2.imread(img_path)
            im_detect(net, img, curr_img_boxes[:, :4])
            fc7s = np.array(net.blobs['fc7'].data)

            # dump feature file
            with open(feature_path, 'w') as feature_file:
                pickle.dump(fc7s, feature_file)

        # prepare roidb
        # format: img_id.bin offset label_ind vrd_label_ind
        for box_id in range(0, len(curr_img_boxes)):
            vrd_label_ind = curr_img_boxes[box_id, 4]
            label_list.append(feature_id + ' ' + str(box_id) + ' ' +
                              str(vrd_label_ind) + ' ' + str(vrd_label_ind) +
                              '\n')

            # if dataset == 'test':
            #     pass
            # else:
            #     label_inds = vrd2path[vrd_label_ind]
            #     for label_ind in label_inds:
            #         sample_prob = sample_ratio[label_ind]
            #         if sample_prob < 1:
            #             p = np.array([sample_prob, 1-sample_prob])
            #             sample = np.random.choice([True, False], p=p.ravel())
            #             if sample:
            #                 label_list.append(feature_id + ' ' + str(box_id) + ' ' + str(label_ind) + ' ' + str(vrd_label_ind) + '\n')
            #         else:
            #             for s in range(int(sample_prob)):
            #                 label_list.append(feature_id + ' ' + str(box_id) + ' ' + str(label_ind) + ' ' + str(vrd_label_ind) + '\n')

        if (i + 1) % 10000 == 0 or (i + 1) == len(image_list):
            with open(label_list_path, 'a') as label_file:
                label_file.writelines(label_list)
            del label_list
            label_list = []

    if len(label_list) > 0:
        with open(label_list_path, 'a') as label_file:
            label_file.writelines(label_list)
Exemplo n.º 22
0
def getRes_Img(sess, net, image):
    """Detect object classes in an image using pre-computed object proposals."""

    imgCon = image.imgcontent
    imgString = base64.b64decode(imgCon)
    nparr = np.fromstring(imgString, np.uint8)
    im = cv2.imdecode(nparr, cv2.IMREAD_COLOR)

    # im=cv2.imread(image)
    timer = Timer()
    timer.tic()
    scores, boxes = im_detect(sess, net, im)
    timer.toc()
    im = im[:, :, (2, 1, 0)]

    CONF_THRESH = 0.8
    NMS_THRESH = 0.3
    res_img = Res_Image()  #初始化,图片中所有设备信息集合:设备信息+图片名称(编号)

    equiAllArr = []  #图片中所有设备的数组集合
    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, :]
        equiarr = EquiArr(im, cls, dets,
                          thresh=CONF_THRESH)  #只能给出某一类设备的所有候选框的集合

        if equiarr != None:
            for x in equiarr:
                equiAllArr.append(x)  #将一张图片中所有设备的信息整合到一个数组中

    equiAllArr.sort(key=lambda Reco_Equipment: Reco_Equipment.acreage,
                    reverse=False)
    equiAllArrNew = []  #嵌套数组
    ds = []  #数组索引

    #候选框是并列的,人为的创建嵌套数组,
    for i in range(len(equiAllArr)):
        if '_' not in equiAllArr[i].equiName:
            if (i in ds):  # 已经访问过的不再访问,直接跳过进行下一个
                continue
            ds.append(i)
            xmin = equiAllArr[i].area.xmin
            ymin = equiAllArr[i].area.ymin
            xmax = equiAllArr[i].area.xmax
            ymax = equiAllArr[i].area.ymax
            equChilds = []  # 子集,即被嵌套的设备集合
            for m in range(0, len(equiAllArr)):
                if (m in ds):
                    continue
                xx1 = np.maximum(xmin, equiAllArr[m].area.xmin)
                yy1 = np.maximum(ymin, equiAllArr[m].area.ymin)
                xx2 = np.minimum(xmax, equiAllArr[m].area.xmax)
                yy2 = np.minimum(ymax, equiAllArr[m].area.ymax)
                w = np.maximum(0, xx2 - xx1 + 1)
                h = np.maximum(0, yy2 - yy1 + 1)
                inter = float(w * h)  # 重叠部分面积
                if (inter / equiAllArr[m].acreage >=
                        0.8) and equiAllArr[i].equiName == 'DLQ' and endwith(
                            equiAllArr[m].equiName, '_CT'):
                    h1 = np.maximum(ymin,
                                    equiAllArr[m].area.ymin) - np.minimum(
                                        ymin, equiAllArr[m].area.ymin)
                    h2 = np.maximum(ymax,
                                    equiAllArr[m].area.ymax) - np.minimum(
                                        ymax, equiAllArr[m].area.ymax)
                    if h1 <= h2:
                        child = Reco_Equipment_child()
                        child.equiName = equiAllArr[i].equiName + '_MHCT'
                        child.area = equiAllArr[m].area
                        equChilds.append(child)
                        ds.append(m)
                    else:
                        child = Reco_Equipment_child()
                        child.equiName = equiAllArr[i].equiName + '_ZZCT'
                        child.area = equiAllArr[m].area
                        equChilds.append(child)
                        ds.append(m)
                elif (inter / equiAllArr[m].acreage >= 0.8) and endwith(
                        equiAllArr[m].equiName, '_CT'):
                    child = Reco_Equipment_child()
                    child.equiName = equiAllArr[i].equiName + '_CT'
                    child.area = equiAllArr[m].area
                    equChilds.append(child)
                    ds.append(m)
                elif (inter / equiAllArr[m].acreage >= 0.8) and endwith(
                        equiAllArr[m].equiName, '_JT'):
                    child = Reco_Equipment_child()
                    child.equiName = equiAllArr[i].equiName + '_JT'
                    child.area = equiAllArr[m].area
                    equChilds.append(child)
                    ds.append(m)
                elif (inter / equiAllArr[m].acreage >= 0.8):
                    child = Reco_Equipment_child()
                    child.equiName = equiAllArr[m].equiName
                    child.area = equiAllArr[m].area
                    equChilds.append(child)
                    ds.append(m)

            equiAllArr[i].children = equChilds
            equiAllArrNew.append(equiAllArr[i])

    for i in range(len(equiAllArr)):
        if (i in ds):  # 已经访问过的不再访问,直接跳过进行下一个
            continue
        equChilds = []
        equiAllArr[i].children = equChilds
        equiAllArrNew.append(equiAllArr[i])

    res_img.imgID = image.imgID
    # 按照条件选取需要的内容
    if image.equiptype == '':
        res_img.equipments = equiAllArrNew
    else:
        equiresArr = []
        res_equip = str.lower(image.equiptype)
        for n in range(len(equiAllArrNew)):
            if res_equip in equiAllArrNew[n].equiName:
                equiresArr.append(equiAllArrNew[n])
        res_img.equipments = equiresArr

    return res_img
Exemplo n.º 23
0
def ext_cnn_feat(im, boxes):
    im_detect(cnn, im, boxes)
    fc7s = np.array(cnn.blobs['fc7'].data)
    return fc7s