Exemple #1
0
def save_results(image_path, anno, fname='result'):
    """Saves results of the prediction.

    Args:
        image_path (string): The path to source image to predict bounding boxes.
        anno (Annotation, list): The predicted annotations for source image or the list of bounding boxes.

    Returns:
        Nothing.
    """

    # draw
    new_img = Image.open(image_path)
    d = ImageDraw.Draw(new_img)
    is_list = type(anno) is list
    rects = anno if is_list else anno.rects
    for r in rects:
        if is_list:
            d.rectangle([r['x1'], r['y1'], r['x2'], r['y2']],
                        outline=(255, 0, 0))
        else:
            d.rectangle([r.left(), r.top(),
                         r.right(), r.bottom()],
                        outline=(255, 0, 0))

    # save
    prediction_image_path = os.path.join(os.path.dirname(image_path),
                                         fname + '.png')
    new_img.save(prediction_image_path)
    subprocess.call(['chmod', '644', prediction_image_path])

    fpath = os.path.join(os.path.dirname(image_path), fname + '.json')
    if is_list:
        json.dump({
            'image_path': prediction_image_path,
            'rects': anno
        }, open(fpath, 'w'))
    else:
        al.saveJSON(fpath, anno)
    subprocess.call(['chmod', '644', fpath])