Exemplo n.º 1
0
def save_results(image_path, anno):
    """Saves results of the prediction.

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

    Returns:
        Nothing.
    """

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

    # save
    fpath = os.path.join(os.path.dirname(image_path), 'result.png')
    new_img.save(fpath)
    subprocess.call(['chmod', '777', fpath])

    fpath = os.path.join(os.path.dirname(image_path), 'result.json')
    if type(anno) is dict:
        with open(fpath, 'w') as f:
            json.dump(anno, f)
    else:
        al.saveJSON(fpath, anno)
    subprocess.call(['chmod', '777', fpath])
Exemplo n.º 2
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])
Exemplo n.º 3
0
def save_results(image_path, anno):
    """Saves results of the prediction.

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

    Returns:
        Nothing.
    """

    # draw
    new_img = Image.open(image_path)
    d = ImageDraw.Draw(new_img)
    for r in anno.rects:
        d.rectangle([r.left(), r.top(), r.right(), r.bottom()], outline=(0, 255, 0))
    detections_count=len(anno.rects)
    if detections_count>0:
        print("Number of target detections:", detections_count)
    else:
        print("Target hasn't been detected.")

    # save
    output_path=os.path.dirname(image_path)+os.path.sep+'output'
    if not os.path.exists(output_path):
        os.makedirs(output_path)

    fpath = os.path.join(output_path, os.path.basename(image_path)+'_result.png')
    new_img.save(fpath)
    subprocess.call(['chmod', '777', fpath])

    fpath = os.path.join(output_path, os.path.basename(image_path)+'_result.json')
    al.saveJSON(fpath, anno)
    subprocess.call(['chmod', '777', fpath])
Exemplo n.º 4
0
def save_results_but_not_image(image_path, anno):
    """Saves results of the prediction.

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

    Returns:
        Nothing.
    """

    fpath = os.path.join(os.path.dirname(image_path), 'result.json')
    if type(anno) is dict:
        with open(fpath, 'w') as f:
            json.dump(anno, f)
    else:
        al.saveJSON(fpath, anno)
    subprocess.call(['chmod', '777', fpath])
Exemplo n.º 5
0
def save_results(image_path, anno):
    """Saves results of the prediction.

    Args:
        image_path (string): The path to source image to predict bounding boxes.
        anno (Annotation): The predicted annotations for source image.
    Returns:
        Nothing.
    """

    import cv2 as cv
    # draw - old pil
    # new_img = Image.open(image_path)
    # d = ImageDraw.Draw(new_img)

    # draw - new opencv
    new_img = imread(image_path)

    # open diameters file for writing
    diamfile = open(
        '/media/chris/data1/cda/cda/tensorbox/data/cda/ctx_output/' +
        'result-' + os.path.basename(image_path) + '.txt', 'w')

    rects = anno['rects'] if type(anno) is dict else anno.rects
    for r in rects:
        if r.score > 0.8:
            # draw circles
            centre = (int(round(r.left() + (r.right() - r.left()) / 2)),
                      int(round(r.bottom() + (r.top() - r.bottom()) / 2)))
            print centre
            print 'r.left ', r.left()
            print 'r.right ', r.right()
            print 'r.top ', r.top()
            print 'r.bottom ', r.bottom()
            # radius is the average of box height and width, halved
            radius = int(
                round(((r.right() - r.left()) / 2 +
                       (r.bottom() - r.top()) / 2) / 2))
            print 'radius = ', radius

            cv.circle(new_img, centre, radius, (0, 0, 255), -2, 8, 0)

            cv.line(new_img, (centre[0] - 3, centre[1]),
                    (centre[0] + 3, centre[1]), (0, 0, 255), 2, 8, 0)

            cv.line(new_img, (centre[0], centre[1] - 3),
                    (centre[0], centre[1] + 3), (0, 0, 255), 2, 8, 0)

            # find diameter in m
            diam = (((r.right() - r.left()) / 2 +
                     (r.bottom() - r.top()) / 2) / 2) * SCALE

            # write diameter to file
            diamfile.write(str(diam) + '\n')

            # draw thick rectangles
            # cor = (r.left(), r.top(), r.right(), r.bottom())
            # width = 3
            # for i in range(width):
            #    d.rectangle(cor, outline="red")
            #    cor = (cor[0]+1,cor[1]+1, cor[2]+1,cor[3]+1)

        #d.rectangle([r.left(), r.top(), r.right(), r.bottom()], outline=(255, 0, 0), 2, 8, 0)

    # save
    #fpath = os.path.join(os.path.dirname(image_path), 'result-' + os.path.basename(image_path) + '.png')
    fpath = os.path.join(
        '/media/chris/data1/cda/cda/tensorbox/data/cda/ctx_output/',
        'result-' + os.path.basename(image_path) + '.png')

    # new_img.save(fpath)
    cv.imwrite(fpath, new_img)
    subprocess.call(['chmod', '777', fpath])

    #fpath = os.path.join(os.path.dirname(image_path), 'result-' + os.path.basename(image_path) + '.json')
    fpath = os.path.join(
        '/media/chris/data1/cda/cda/tensorbox/data/cda/ctx_output/',
        'result-' + os.path.basename(image_path) + '.json')

    if type(anno) is dict:
        with open(fpath, 'w') as f:
            json.dump(anno, f)
    else:
        al.saveJSON(fpath, anno)
    subprocess.call(['chmod', '777', fpath])