Exemplo n.º 1
0
def postprocess(image_info, np_pred_boxes, np_pred_confidences, H, options):
    pred_anno = al.Annotation()
    #pred_anno.imageName = image_info['path']
    #pred_anno.imagePath = os.path.abspath(image_info['path'])
    _, rects = add_rectangles(H, [image_info['transformed']],
                              np_pred_confidences,
                              np_pred_boxes,
                              use_stitching=True,
                              rnn_len=H['rnn_len'],
                              min_conf=options['min_conf'],
                              tau=options['tau'],
                              show_suppressed=False)

    rects = [
        r for r in rects
        if r.x1 < r.x2 and r.y1 < r.y2 and r.score > options['min_conf']
    ]
    h, w = image_info['original'].shape[:2]
    if 'rotate90' in H['data'] and H['data']['rotate90']:
        # original image height is a width for roatated one
        rects = Rotate90.invert(h, rects)
    pred_anno.rects = rects
    pred_anno = rescale_boxes((H['image_height'], H['image_width']), pred_anno,
                              h, w)
    return pred_anno
Exemplo n.º 2
0
def hot_predict_img(image, parameters, to_json=True):
    """Makes predictions when all long running preparation operations are made.

    Args:
        image_path (string): The path to the source image.
        parameters (dict): The parameters produced by :func:`initialize`.

    Returns (Annotation):
        The annotation for the source image.
    """

    H = parameters['hypes']
    # The default options for prediction of bounding boxes.
    options = H['evaluate']
    if 'pred_options' in parameters:
        # The new options for prediction of bounding boxes
        for key, val in parameters['pred_options'].items():
            options[key] = val

    # predict
    orig_img = (image)[:, :, :3]
    img = Rotate90.do(
        orig_img
    ) if 'rotate90' in H['data'] and H['data']['rotate90'] else orig_img
    img = imresize(img, (512, 512), interp='cubic')
    np_pred_boxes, np_pred_confidences = parameters['sess']. \
        run([parameters['pred_boxes'], parameters['pred_confidences']],
            feed_dict={parameters['x_in']: img})

    image_info = {'original': orig_img, 'transformed': img}
    pred_anno = postprocess(image_info, np_pred_boxes, np_pred_confidences, H,
                            options)
    result = [r.writeJSON() for r in pred_anno] if to_json else pred_anno
    return result
Exemplo n.º 3
0
def preprocess_image(anno, H):
    image = imread(anno.imageName)
    # skip greyscale images
    if len(image.shape) < 3:
        return []

    # 4 channels images check
    if image.shape[2] == 4:
        image = image[:, :, :3]

    if 'rotate90' in H['data'] and H['data']['rotate90']:
        image, anno = Rotate90.do(image, anno)

    if image.shape[0] != H["image_height"] or image.shape[1] != H["image_width"]:
        anno = rescale_boxes(image.shape, anno, H["image_height"], H["image_width"])
        image = imresize(image, (H["image_height"], H["image_width"]), interp='cubic')

    result = [(image, anno)]
    if 'augmentations' in H['data']:
        new_res = []
        for i, a in result:
            augmented = Augmentations(H['data']).process(i, a)
            new_res.append(augmented)
        result = new_res
    return result
Exemplo n.º 4
0
def sliding_predict(image_path, parameters, to_json, H, options):
    orig_img = imread(image_path)[:, :, :3]
    height, width, _ = orig_img.shape
    if options.get('verbose', False):
        print(width, height)

    sl_win_options = H['sliding_predict']
    assert (sl_win_options['window_height'] > sl_win_options['overlap'])
    slides = propose_slides(height, sl_win_options['window_height'], sl_win_options['overlap'])

    result = []
    for top, bottom in slides:
        bottom = min(height, top + sl_win_options['window_height'])
        if options.get('verbose', False):
            print('Slide: ', 0, top, width, bottom)

        img = orig_img[top:bottom, 0:width]
        img = Rotate90.do(img)[0] if 'rotate90' in H['data'] and H['data']['rotate90'] else img
        img = imresize(img, (H['image_height'], H['image_width']), interp='cubic')

        np_pred_boxes, np_pred_confidences = parameters['sess']. \
            run([parameters['pred_boxes'], parameters['pred_confidences']], feed_dict={parameters['x_in']: img})
        image_info = {'path': image_path, 'original_shape': (bottom-top, width), 'transformed': img, 'a': orig_img[top:bottom, 0:width]}

        pred_boxes = postprocess_regular(image_info, np_pred_boxes, np_pred_confidences, H, options)
        shift_boxes(pred_boxes, top)
        result.extend(pred_boxes)

    result = combine_boxes(result, sl_win_options['iou_min'], sl_win_options['nms'])
    result = [r.writeJSON() for r in result] if to_json else result
    return result
Exemplo n.º 5
0
def regular_predict(image_path, parameters, to_json, H, options):
    orig_img = imread(image_path)[:, :, :3]
    img = Rotate90.do(orig_img)[0] if 'rotate90' in H['data'] and H['data']['rotate90'] else orig_img
    img = imresize(img, (H['image_height'], H['image_width']), interp='cubic')
    np_pred_boxes, np_pred_confidences = parameters['sess']. \
        run([parameters['pred_boxes'], parameters['pred_confidences']], feed_dict={parameters['x_in']: img})

    image_info = {'path': image_path, 'original_shape': img.shape[:2], 'transformed': img}
    pred_anno = postprocess_regular(image_info, np_pred_boxes, np_pred_confidences, H, options)
    result = [r.writeJSON() for r in pred_anno] if to_json else pred_anno
    return result
Exemplo n.º 6
0
def postprocess_regular(image_info, np_pred_boxes, np_pred_confidences, H, options):
    pred_anno = al.Annotation()
    pred_anno.imageName = image_info['path']
    pred_anno.imagePath = os.path.abspath(image_info['path'])
    _, rects = add_rectangles(H, [image_info['transformed']], np_pred_confidences, np_pred_boxes, use_stitching=True,
                              rnn_len=H['rnn_len'], min_conf=options['min_conf'], tau=options['tau'],
                              show_suppressed=False)

    h, w = image_info['original_shape']
    if 'rotate90' in H['data'] and H['data']['rotate90']:
        # original image height is a width for rotated one
        rects = Rotate90.invert(h, rects)

    rects = [r for r in rects if r.x1 < r.x2 and r.y1 < r.y2 and r.score > options['min_conf']]
    pred_anno.rects = rects
    pred_anno = rescale_boxes((H['image_height'], H['image_width']), pred_anno, h, w)
    return pred_anno