Beispiel #1
0
def nms(dets, thresh, force_cpu=False):
    """Dispatch to either CPU or GPU NMS implementations."""

    if dets.shape[0] == 0:
        return []
    if cfg.USE_GPU_NMS and not force_cpu:
        return py_cpu_nms(dets, thresh)
    else:
        return py_cpu_nms(dets, thresh)
Beispiel #2
0
def process_image(net, im_file, outfolder, prob_threshold=0.5, nms_threshold=0.3, imgprefix=""):
    """Detect object classes in an image using pre-computed object proposals."""

    # 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)
    timer.toc()
    print("Detection took {:.3f}s for {:d} object proposals".format(timer.total_time, boxes.shape[0]))

    (dir, name) = os.path.split(im_file)
    imgname = os.path.join(dir, imgprefix + name)

    # Visualize detections for each class
    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)
        # TODO: for some reason native nms is not working properly
        # I have replaced it by the python version by now, but
        # idea in the future would it be to solve it and uncomment the
        # the following line and comment the python nms version.
        # keep = nms(dets, nms_threshold)
        keep = py_cpu_nms(dets, nms_threshold)
        dets = dets[keep, :]
        save_detections(im, cls, dets, imgname, outfolder, thresh=prob_threshold)
Beispiel #3
0
def apply_nms(dets, nms_thresh):
    from nms.py_cpu_nms import py_cpu_nms

    # Group detections into matrix form:
    #  [[top, left, bottom, right, score], ...]
    per_class_dets = dict()
    for det_i, det in enumerate(dets):
        if det.cls not in per_class_dets:
            per_class_dets[det.cls] = [[
                det.top, det.left, det.bottom, det.right, det.score, det_i
            ]]
        else:
            per_class_dets[det.cls].append(
                [det.top, det.left, det.bottom, det.right, det.score, det_i])

    # Apply NMS
    class_removals = dict()
    idx_det_keep = []
    for k in per_class_dets.keys():
        per_class_dets[k] = np.array(per_class_dets[k])
        idx_nms_keep = py_cpu_nms(per_class_dets[k], nms_thresh)
        if len(idx_nms_keep) < len(per_class_dets[k]):
            class_removals[k] = len(per_class_dets[k]) - len(idx_nms_keep)
        idx_det_keep += list(per_class_dets[k][idx_nms_keep, -1])

    filtered_dets = []
    for idx in idx_det_keep:
        filtered_dets.append(dets[int(idx)])

    return (filtered_dets, class_removals)
Beispiel #4
0
def demo(net, image_name):
    """Detect object classes in an image using pre-computed object proposals."""

    global ax
    # 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.6
    NMS_THRESH = 0.3

    im = im[:, :, (2, 1, 0)]

    fig, ax = plt.subplots(figsize=(6.4, 4.8))
    ax.imshow(im, aspect='equal')
    animated = False
    all_dets = []

    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_inds = np.ones((len(cls_scores), 1)) * cls_ind
        # col 4 and col -1 are both cls_scores, to satisfy nms()
        dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis], cls_inds,
                          cls_scores[:, np.newaxis])).astype(np.float32)
        all_dets.append(dets)

    all_dets = np.concatenate(all_dets)
    conf_inds = all_dets[:, 4] >= CONF_THRESH
    conf_dets = all_dets[conf_inds]
    keep = py_cpu_nms(conf_dets, NMS_THRESH)  # nms(conf_dets, NMS_THRESH) #
    nms_dets = conf_dets[keep, :]
    vis_detections(im, ax, nms_dets, thresh=CONF_THRESH, animated=animated)

    plt.axis('off')
    plt.tight_layout()
    plt.title(image_name)

    if animated:
        ani = FuncAnimation(fig,
                            vis_detections_animate,
                            frames=xrange(len(ax.patches)),
                            interval=500,
                            blit=True)
        ani.save('/tmp/animation.gif', writer='imagemagick', fps=1)
    else:
        plt.draw()
def nms(dets, thresh, force_cpu=False):
    """Dispatch to either CPU or GPU NMS implementations."""

    if dets.shape[0] == 0:
        return []
    # if cfg.USE_GPU_NMS and not force_cpu:
    #  return gpu_nms(dets, thresh, device_id=cfg.GPU_ID)
    # else:
    # return cpu_nms(dets, thresh)
    return py_cpu_nms(dets=dets, thresh=thresh)