def detect_im(net, im, thresh=0.05):
    im_scale = _compute_scaling_factor(im.shape,cfg.TEST.SCALES[0],cfg.TEST.MAX_SIZE)
    im_blob = _get_image_blob(im,[im_scale])
    probs, boxes = forward_net(net,im_blob[0],im_scale,False)
    boxes = boxes[:, 0:4]

    inds = np.where(probs[:, 0] > thresh)[0]
    probs = probs[inds, 0]
    boxes = boxes[inds, :]
    dets = np.hstack((boxes, probs[:, np.newaxis])) \
            .astype(np.float32, copy=False)
    keep = nms(dets, cfg.TEST.NMS_THRESH)
    cls_dets = dets[keep, :]
    return cls_dets
def detect_im(net, im, thresh=0.05):
    im_scale = _compute_scaling_factor(im.shape, cfg.TEST.SCALES[0],
                                       cfg.TEST.MAX_SIZE)
    im_blob = _get_image_blob(im, [im_scale])
    probs, boxes = forward_net(net, im_blob[0], im_scale, False)
    boxes = boxes[:, 0:4]

    inds = np.where(probs[:, 0] > thresh)[0]
    probs = probs[inds, 0]
    boxes = boxes[inds, :]
    dets = np.hstack((boxes, probs[:, np.newaxis])) \
            .astype(np.float32, copy=False)
    keep = nms(dets, cfg.TEST.NMS_THRESH)
    cls_dets = dets[keep, :]
    return cls_dets
Example #3
0
def detect(net,
           im_path,
           thresh=0.05,
           visualize=False,
           timers=None,
           pyramid=False,
           dect_visualization_folder=None):
    """
    Main module to detect faces
    :param net: The trained network
    :param im_path: The path to the image
    :param thresh: Detection with a less score than thresh are ignored
    :param visualize: Whether to visualize the detections
    :param timers: Timers for calculating detect time (if None new timers would be created)
    :param pyramid: Whether to use pyramid during inference
    :param visualization_folder: If set the visualizations would be saved in this folder (if visualize=True)
    :return: cls_dets (bounding boxes concatenated with scores) and the timers
    """
    if not timers:
        timers = {'detect': Timer(), 'misc': Timer()}

    im = cv2.imread(im_path)
    im_class__file = im_path.split('/')[-2]
    imfname = os.path.basename(im_path)
    sys.stdout.flush()
    timers['detect'].tic()

    if not pyramid:
        im_scale = _compute_scaling_factor(im.shape, cfg.TEST.SCALES[0],
                                           cfg.TEST.MAX_SIZE)
        im_blob = _get_image_blob(im, [im_scale])
        probs, boxes = forward_net(net, im_blob[0], im_scale, False)
        boxes = boxes[:, 0:4]
    else:
        all_probs = []
        all_boxes = []
        # Compute the scaling coefficients for the pyramid
        base_scale = _compute_scaling_factor(im.shape,
                                             cfg.TEST.PYRAMID_BASE_SIZE[0],
                                             cfg.TEST.PYRAMID_BASE_SIZE[1])
        pyramid_scales = [
            float(scale) / cfg.TEST.PYRAMID_BASE_SIZE[0] * base_scale
            for scale in cfg.TEST.SCALES
        ]

        im_blobs = _get_image_blob(im, pyramid_scales)

        for i in range(len(pyramid_scales)):
            probs, boxes = forward_net(net, im_blobs[i], pyramid_scales[i],
                                       True)
            for j in xrange(len(probs)):
                # Do not apply M3 to the largest scale
                if i < len(pyramid_scales) - 1 or j < len(probs) - 1:
                    all_boxes.append(boxes[j][:, 0:4])
                    all_probs.append(probs[j].copy())

        probs = np.concatenate(all_probs)
        boxes = np.concatenate(all_boxes)

    timers['detect'].toc()
    timers['misc'].tic()

    inds = np.where(probs[:, 0] > thresh)[0]
    probs = probs[inds, 0]
    boxes = boxes[inds, :]
    dets = np.hstack((boxes, probs[:, np.newaxis])) \
            .astype(np.float32, copy=False)
    keep = nms(dets, cfg.TEST.NMS_THRESH)
    cls_dets = dets[keep, :]
    if visualize:
        plt_name = os.path.splitext(imfname)[0] + '_detections_{}'.format(
            net.name)
        dect_visualization_folder = os.path.join(dect_visualization_folder,
                                                 im_class__file)
        visusalize_detections(im,
                              cls_dets,
                              plt_name=plt_name,
                              visualization_folder=dect_visualization_folder)
    timers['misc'].toc()
    return cls_dets, timers
def detect(net, im_path, thresh=0.05, visualize=False, timers=None, pyramid=False, visualization_folder=None):
    """
    Main module to detect faces
    :param net: The trained network
    :param im_path: The path to the image
    :param thresh: Detection with a less score than thresh are ignored
    :param visualize: Whether to visualize the detections
    :param timers: Timers for calculating detect time (if None new timers would be created)
    :param pyramid: Whether to use pyramid during inference
    :param visualization_folder: If set the visualizations would be saved in this folder (if visualize=True)
    :return: cls_dets (bounding boxes concatenated with scores) and the timers
    """
    if not timers:
        timers = {'detect': Timer(),
                  'misc': Timer()}

    im = cv2.imread(im_path)
    imfname = os.path.basename(im_path)
    sys.stdout.flush()
    timers['detect'].tic()

    if not pyramid:
        im_scale = _compute_scaling_factor(im.shape,cfg.TEST.SCALES[0],cfg.TEST.MAX_SIZE)
        im_blob = _get_image_blob(im,[im_scale])
        probs, boxes = forward_net(net,im_blob[0],im_scale,False)
        boxes = boxes[:, 0:4]
    else:
        all_probs = []
        all_boxes = []
        # Compute the scaling coefficients for the pyramid
        base_scale = _compute_scaling_factor(im.shape,cfg.TEST.PYRAMID_BASE_SIZE[0],cfg.TEST.PYRAMID_BASE_SIZE[1])
        pyramid_scales = [float(scale)/cfg.TEST.PYRAMID_BASE_SIZE[0]*base_scale
                          for scale in cfg.TEST.SCALES]

        im_blobs = _get_image_blob(im,pyramid_scales)

        for i in range(len(pyramid_scales)):
            probs,boxes = forward_net(net,im_blobs[i],pyramid_scales[i],True)
            for j in xrange(len(probs)):
                # Do not apply M3 to the largest scale
                if i<len(pyramid_scales)-1 or j<len(probs)-1:
                    all_boxes.append(boxes[j][:,0:4])
                    all_probs.append(probs[j].copy())

        probs = np.concatenate(all_probs)
        boxes = np.concatenate(all_boxes)

    timers['detect'].toc()
    timers['misc'].tic()

    inds = np.where(probs[:, 0] > thresh)[0]
    probs = probs[inds, 0]
    boxes = boxes[inds, :]
    dets = np.hstack((boxes, probs[:, np.newaxis])) \
            .astype(np.float32, copy=False)
    keep = nms(dets, cfg.TEST.NMS_THRESH)
    cls_dets = dets[keep, :]
    if visualize:
        plt_name = os.path.splitext(imfname)[0] + '_detections_{}'.format(net.name)
        visusalize_detections(im, cls_dets, plt_name=plt_name, visualization_folder=visualization_folder)
    timers['misc'].toc()
    return cls_dets,timers
Example #5
0
def detect(net, im_path, thresh=0.05, timers=None, pyramid=False):
    if not timers:
        timers = {'detect': Timer(), 'misc': Timer()}

    im = cv2.imread(im_path)
    imfname = os.path.basename(im_path)
    sys.stdout.flush()
    timers['detect'].tic()

    if not pyramid:
        im_scale = _compute_scaling_factor(im.shape, cfg.TEST.SCALES[0],
                                           cfg.TEST.MAX_SIZE)
        im_blob = _get_image_blob(im, [im_scale])
        probs, boxes = forward_net(net, im_blob[0], im_scale, pyramid=False)
        if isinstance(probs, list):
            probs = np.vstack(probs)
            boxes = np.vstack(boxes)
        boxes = boxes[:, 0:4]
    else:
        all_probs = []
        all_boxes = []
        # Compute the scaling coefficients for the pyramid
        base_scale = _compute_scaling_factor(im.shape,
                                             cfg.TEST.PYRAMID_BASE_SIZE[0],
                                             cfg.TEST.PYRAMID_BASE_SIZE[1])
        pyramid_scales = [
            float(scale) / cfg.TEST.PYRAMID_BASE_SIZE[0] * base_scale
            for scale in cfg.TEST.SCALES
        ]

        im_blobs = _get_image_blob(im, pyramid_scales)

        for i in range(len(pyramid_scales)):
            probs, boxes = forward_net(net,
                                       im_blobs[i],
                                       pyramid_scales[i],
                                       pyramid=True)
            for j in xrange(len(probs)):
                all_boxes.append(boxes[j][:, 0:4])
                all_probs.append(probs[j].copy())
            if cfg.TEST.FLIP:
                probs, boxes = forward_net(
                    net, {'data': im_blobs[i]['data'][..., ::-1]},
                    pyramid_scales[i],
                    pyramid=True,
                    flip=True)
                for j in xrange(len(probs)):
                    all_boxes.append(boxes[j][:, 0:4])
                    all_probs.append(probs[j].copy())

        probs = np.concatenate(all_probs)
        boxes = np.concatenate(all_boxes)
    timers['detect'].toc()
    timers['misc'].tic()
    cls_dets = [None] * (probs.shape[1] - 1)
    for class_i in range(1, probs.shape[1]):
        inds = np.where(probs[:, class_i] > thresh)[0]
        probs_i = probs[inds, class_i]
        boxes_i = boxes[inds, :]
        dets = np.hstack((boxes_i, probs_i[:, np.newaxis])) \
            .astype(np.float32, copy=False)
        if cfg.TEST.NMS_METHOD == "BBOX_VOTE":
            cls_dets[class_i - 1] = bbox_vote(dets)
        elif cfg.TEST.NMS_METHOD == "NMS":
            keep = nms(dets, cfg.TEST.NMS_THRESH)
            cls_dets[class_i - 1] = dets[keep, :]
        else:
            raise NotImplementedError("Unknown NMS method: {}".format(
                cfg.TEST.NMS_METHOD))
    assert all([_ is not None for _ in cls_dets]), 'None in detection results'
    timers['misc'].toc()
    return cls_dets, timers