Ejemplo n.º 1
0
def demo(sess, net, image_name,imagedir, mode,pedestrian,cyclist):
    """Detect object classes in an image using pre-computed object proposals."""

    # Load the demo image
    im_file = os.path.join(imagedir, image_name)
    im = cv2.imread(im_file)
    save_path='result_image_new'
    # Detect all object classes and regress object bounds
    if mode == 'fast':
        scores, boxes = im_detect_fast(sess, net, im)
    else:
        multi_scales=[600,800,1000]    
        scores, boxes = im_detect(sess, net, im,multi_scales)
    # Visualize detections for each class
    CONF_THRESH = 0.4

    # Visualize people
    for cls_ind,cls in enumerate(CLASSES[1:]):
        cls_ind += 1 
   # cls = 'pedestrian'
        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=soft_nms(dets,sigma=0.6,Nt=0.3,method=2)
        dets=keep
    #    if cls=='pedestrian':
    #       map_file=pedestrian
    #    elif cls=='cyclist':
        map_file=pedestrian
        vis_detections(im, image_name, cls, dets,map_file,thresh=CONF_THRESH)
def demo(sess, net, mode, im):
    """Detect object classes in an image using pre-computed object proposals."""
    if mode == 'fast':
        scores, boxes = im_detect_fast(sess, net, im)
    else:
        multi_scales = size
        scores, boxes = im_detect(sess, net, im, multi_scales)
    # Visualize detections for each class
    CONF_THRESH = soft_thresh
    # Visualize people
    for cls_ind, cls in enumerate(CLASSES[1:]):
        cls_ind += 1
        # cls = 'pedestrian'
        #image_name=image_name.split('/')[-1].split('.')[0]
        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 = soft_nms(dets, sigma=0.6, Nt=0.3, method=2)
        dets = keep
        #print(dets)
        inds = np.where(dets[:, -1] >= CONF_THRESH)[0]
        bbox_list = []
        for i in inds:
            bbox = dets[i, :4]
            score = np.round(dets[i, -1] * 1000) / 1000
            xmin = round(bbox[0], 1)
            ymin = round(bbox[1], 1)
            xmax = round(bbox[2], 1)
            ymax = round(bbox[3], 1)
            bbox_list.append([xmin, ymin, xmax, ymax, score])
        return bbox_list
Ejemplo n.º 3
0
def demo(sess, net, image_name,xminarr,yminarr,xmaxarr,ymaxarr,results,score_file,index_file,num_boxes,imagedir, mode):
    """Detect object classes in an image using pre-computed object proposals."""

    # Load the demo image
    im_file = os.path.join(imagedir, image_name)
    im = cv2.imread(im_file)

    # Detect all object classes and regress object bounds
    if mode == 'fast':
        scores, boxes = im_detect_fast(sess, net, im)
    else:    
        scores, boxes = im_detect(sess, net, im)
    # Visualize detections for each class
    CONF_THRESH = 0.1

    # Visualize people
    cls_ind = 1 
    cls = 'person'
    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=soft_nms(dets,method=2)
    dets=keep
    if(dets.shape[0]!=0):
        index_file.write("{} {} ".format(image_name,num_boxes+1))
    num_boxes = vis_detections(im, image_name, cls, dets,xminarr,yminarr,xmaxarr,ymaxarr,results,score_file,index_file,num_boxes, thresh=CONF_THRESH)
    if(dets.shape[0]!=0):
        index_file.write("{}\n".format(num_boxes))
    return num_boxes
Ejemplo n.º 4
0
def demo(sess, net, image_name, imagedir, mode, map_file):
    """Detect object classes in an image using pre-computed object proposals."""
    #print(image_name)
    im = cv2.imread(image_name)
    #print(im)
    save_path = 'result_image_new'
    # Detect all object classes and regress object bounds
    if mode == 'fast':
        scores, boxes = im_detect_fast(sess, net, im)
    else:
        multi_scales = size
        scores, boxes = im_detect(sess, net, im, multi_scales)
    # Visualize detections for each class
    CONF_THRESH = soft_thresh
    # Visualize people
    for cls_ind, cls in enumerate(CLASSES[1:]):
        cls_ind += 1
        # cls = 'pedestrian'
        image_name = image_name.split('/')[-1].split('.')[0]
        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 = soft_nms(dets, sigma=0.6, Nt=0.3, method=2)
        dets = keep
        #print(dets)
        vis_detections(im, image_name, cls, dets, map_file, thresh=CONF_THRESH)
Ejemplo n.º 5
0
def demo(sess, net, image_name, imagedir, mode):
    """Detect object classes in an image using pre-computed object proposals."""

    # Load the demo image
    im_file = os.path.join(imagedir, image_name)
    im = cv2.imread(im_file)
    save_path = 'wider_result_1class_image'
    if not os.path.exists(save_path):
        os.mkdir(save_path)
    # Detect all object classes and regress object bounds
    if mode == 'fast':
        scores, boxes = im_detect_fast(sess, net, im)
    else:
        multi_scales = [700, 800, 900]
        scores, boxes = im_detect(sess, net, im, multi_scales)
    # Visualize detections for each class
    CONF_THRESH = 0.4

    # Visualize people
    for cls_ind, cls in enumerate(CLASSES[1:]):
        cls_ind += 1
        # cls = 'pedestrian'
        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 = soft_nms(dets, sigma=0.6, Nt=0.3, method=2)
        dets = keep
        vis_detections(im, image_name, cls, dets, thresh=CONF_THRESH)
    image_name = image_name.split('/')[-1].split('.')[0] + '.jpg'
    cv2.imwrite(os.path.join(save_path, image_name), im)
Ejemplo n.º 6
0
def demo(sess, net, image_name, xminarr, yminarr, xmaxarr, ymaxarr, results,
         score_file, index_file, num_boxes):
    """Detect object classes in an image using pre-computed object proposals."""

    # Load the demo image
    #   im_file = os.path.join(cfg.DATA_DIR, 'coco-minival_images', image_name)
    # im_file = os.path.join(cfg.DATA_DIR, 'coco-test-dev', image_name)
    im_file = os.path.join('/SSD2/jeff/coco', 'trainval2017', 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.035
    NMS_THRESH = 0.7
    # Visualize people
    cls_ind = 1
    cls = 'person'
    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)
    keep = soft_nms(dets, method=2)

    #dets = dets[keep, :]
    dets = keep
    if (dets.shape[0] != 0):
        index_file.write("{} {} ".format(image_name, num_boxes + 1))
    num_boxes = vis_detections(im,
                               image_name,
                               cls,
                               dets,
                               xminarr,
                               yminarr,
                               xmaxarr,
                               ymaxarr,
                               results,
                               score_file,
                               index_file,
                               num_boxes,
                               thresh=CONF_THRESH)
    if (dets.shape[0] != 0):
        index_file.write("{}\n".format(num_boxes))
    # 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, thresh=CONF_THRESH)
    return num_boxes
def demo(sess, net, image_name, xminarr, yminarr, xmaxarr, ymaxarr, results,
         score_file, index_file, num_boxes, imagedir, mode):
    """Detect object classes in an image using pre-computed object proposals."""

    # Load the demo image
    im_file = os.path.join(imagedir, image_name)
    im = cv2.imread(im_file)
    ##################
    name_parts = image_name.split('/')
    file_name = '/disks/data4/zyli/Faster-RCNN-AlphaPose/heatmap/%s/%s.npy' % (
        name_parts[-2], name_parts[-1].replace('.jpg', ''))
    assert (os.path.exists(file_name))
    hm = np.array(np.load(file_name), np.float32)
    hm = cv2.resize(hm, (im.shape[1], im.shape[0]),
                    interpolation=cv2.INTER_LINEAR)
    im = np.concatenate([im, hm], axis=2)
    ##################

    # Detect all object classes and regress object bounds
    if mode == 'fast':
        scores, boxes = im_detect_fast(sess, net, im)
    else:
        scores, boxes = im_detect(sess, net, im)
    # Visualize detections for each class
    CONF_THRESH = 0.1

    # Visualize people
    cls_ind = 1
    cls = 'person'
    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 = soft_nms(dets, method=2)
    dets = keep
    if (dets.shape[0] != 0):
        index_file.write("{} {} ".format(image_name, num_boxes + 1))
    num_boxes = vis_detections(im,
                               image_name,
                               cls,
                               dets,
                               xminarr,
                               yminarr,
                               xmaxarr,
                               ymaxarr,
                               results,
                               score_file,
                               index_file,
                               num_boxes,
                               thresh=CONF_THRESH)
    if (dets.shape[0] != 0):
        index_file.write("{}\n".format(num_boxes))
    return num_boxes
Ejemplo n.º 8
0
def detect(sess, net, imagedir, mode):
    """Detects object classes in an image using pre-computed object proposals."""
    # Load the demo image
    im = cv2.imread(imagedir)

    # Detect all object classes and regress object bounds
    if mode == 'fast':
        scores, boxes = im_detect_fast(sess, net, im)
    else:
        scores, boxes = im_detect(sess, net, im)

    # boxes for people
    cls_ind = 1
    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)
    dets = soft_nms(dets, method=2)
    xminarr, yminarr, xmaxarr, ymaxarr = extract_bbox(dets)
    return xminarr, yminarr, xmaxarr, ymaxarr
Ejemplo n.º 9
0
def demo(sess, net, image_name, xminarr, yminarr, xmaxarr, ymaxarr, results,
         score_file, index_file, num_boxes, imagedir, mode, map_file):
    """Detect object classes in an image using pre-computed object proposals."""

    # Load the demo image
    im_file = os.path.join(imagedir, image_name)
    im = cv2.imread(im_file)

    # Detect all object classes and regress object bounds
    if mode == 'fast':
        scores, boxes = im_detect_fast(sess, net, im)
    else:
        scores, boxes = im_detect(sess, net, im)
    # Visualize detections for each class
    CONF_THRESH = 0.5

    # Visualize people
    cls_ind = 1
    cls = 'person'
    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 = soft_nms(dets, method=2)
    dets = keep
    if (dets.shape[0] != 0):
        index_file.write("{} {} ".format(image_name, num_boxes + 1))
    num_boxes = vis_detections(im,
                               image_name,
                               cls,
                               dets,
                               xminarr,
                               yminarr,
                               xmaxarr,
                               ymaxarr,
                               results,
                               score_file,
                               index_file,
                               num_boxes,
                               map_file,
                               thresh=CONF_THRESH)
    if (dets.shape[0] != 0):
        index_file.write("{}\n".format(num_boxes))
    return num_boxes
        filename = os.path.join(image_dir, img_name)
        if os.path.exists(filename):
            image = skimage.io.imread(filename)
            [img_height, img_width, _] = image.shape
            boxes, scores = detect(sess, net, image)
            dets_ = np.hstack((boxes, scores[:,
                                             np.newaxis])).astype(np.float32)
            # prvs = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
            # next = cv2.cvtColor(image_mid,cv2.COLOR_BGR2GRAY)
            # opflow = cv2.calcOpticalFlowFarneback(prvs,next, None, 0.5, 3, 15, 3, 5, 1.2, 0)

            # dets = np.vstack((dets, op_argument(dets_, opflow, img_height, img_width)))
            dets = np.vstack((dets, dets_))

    keep = soft_nms(dets, method=2)
    detections = keep
    # print(detections.shape)

    #     Parse the outputs.
    det_conf = detections[:, 4]
    det_xmin = detections[:, 1]
    det_ymin = detections[:, 0]
    det_xmax = detections[:, 3]
    det_ymax = detections[:, 2]

    top_indices1 = [m for m, conf in enumerate(det_conf) if conf > 0.1]
    top_indices3 = [
        m for m, height in enumerate(det_ymax - det_ymin)
        if height > 0.1 * img_height
    ]
Ejemplo n.º 11
0
def demo_seq(sess, net, imgname,xminarr,yminarr,xmaxarr,ymaxarr,results,score_file,index_file,num_boxes,imagedir, fast_mode):
    """Detect object classes in an image using pre-computed object proposals."""
    idx = int(imgname.split('.')[0])
    # Load the demo image
    #im_file = os.path.join(cfg.DATA_DIR, 'coco-minival_images', image_name)
    im_file = os.path.join(imagedir, '%05d.jpg'%(idx))
    im = cv2.imread(im_file)

    # Detect all object classes and regress object bounds
    timer = Timer()
    timer.tic()
    if fast_mode == True:
        scores, boxes = im_detect_fast(sess, net, im)
    else:    
        scores, boxes = im_detect(sess, net, im)

    # Visualize detections for each class
    CONF_THRESH = 0.1
    NMS_THRESH = 0.7
    # Visualize people
    cls_ind = 1 
    cls = 'person'
    cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)]
    cls_scores = scores[:, cls_ind]

    #Prev image
    im_file = os.path.join(imagedir, '%05d.jpg'%(idx-1))
    if os.path.isfile(im_file):
        im = cv2.imread(im_file)
        if fast_mode == True:
            scores, boxes = im_detect_fast(sess, net, im)
        else:    
            scores, boxes = im_detect(sess, net, im)

        # Visualize people
        cls_ind = 1 
        cls_boxes = np.append(cls_boxes,boxes[:, 4*cls_ind:4*(cls_ind + 1)], axis=0)
        cls_scores = np.append(cls_scores,scores[:, cls_ind], axis=0)

    #Prev Prev image
    im_file = os.path.join(imagedir, '%05d.jpg'%(idx-2))
    if os.path.isfile(im_file):
        im = cv2.imread(im_file)
        if fast_mode == True:
            scores, boxes = im_detect_fast(sess, net, im)
        else:    
            scores, boxes = im_detect(sess, net, im)

        # Visualize people
        cls_ind = 1 
        cls_boxes = np.append(cls_boxes,boxes[:, 4*cls_ind:4*(cls_ind + 1)], axis=0)
        cls_scores = np.append(cls_scores,scores[:, cls_ind], axis=0)

    #Next image
    im_file = os.path.join(imagedir, '%05d.jpg'%(idx+1))
    if os.path.isfile(im_file):
        im = cv2.imread(im_file)
        if fast_mode == True:
            scores, boxes = im_detect_fast(sess, net, im)
        else:    
            scores, boxes = im_detect(sess, net, im)

        # Visualize people
        cls_ind = 1 
        cls_boxes = np.append(cls_boxes,boxes[:, 4*cls_ind:4*(cls_ind + 1)], axis=0)
        cls_scores = np.append(cls_scores,scores[:, cls_ind], axis=0)

    #Next Next image
    im_file = os.path.join(imagedir, '%05d.jpg'%(idx+2))
    if os.path.isfile(im_file):
        im = cv2.imread(im_file)
        if fast_mode == True:
            scores, boxes = im_detect_fast(sess, net, im)
        else:    
            scores, boxes = im_detect(sess, net, im)

        # Visualize people
        cls_ind = 1 
        cls_boxes = np.append(cls_boxes,boxes[:, 4*cls_ind:4*(cls_ind + 1)], axis=0)
        cls_scores = np.append(cls_scores,scores[:, cls_ind], axis=0)

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

    dets = np.hstack((cls_boxes,
                      cls_scores[:, np.newaxis])).astype(np.float32)
    #keep = nms(dets, NMS_THRESH)
    keep=soft_nms(dets,method=2)
    
    #dets = dets[keep, :]
    dets=keep
    if(dets.shape[0]!=0):
        index_file.write("{} {} ".format('%05d.jpg'%(idx),num_boxes+1))
    num_boxes = vis_detections(im, '%05d.jpg'%(idx), cls, dets,xminarr,yminarr,xmaxarr,ymaxarr,results,score_file,index_file,num_boxes, thresh=CONF_THRESH)
    if(dets.shape[0]!=0):
        index_file.write("{}\n".format(num_boxes))
    # 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, thresh=CONF_THRESH)
    return num_boxes