<jupyter_start><jupyter_text>Import packages<jupyter_code>import matplotlib.pyplot as plt
from PIL import Image, ImageDraw
import numpy as np
import pandas as pd
import os.path as osp
import os
import argparse<jupyter_output><empty_output><jupyter_text>Get home_pathWe get `home_path` on different machines<jupyter_code>home_path = osp.expanduser('~')
home_path<jupyter_output><empty_output><jupyter_text>Compute the IoUWe define the function to compute the IoU of a predicted box with a list of ground truth boxes.A box is represented by a list `[xmin, ymin, xmax, ymax]`.A list ground truth boxes is a list of boxes `[box1, box2, box3]`.All are `np.array`<jupyter_code>def get_iou(pred_img_box, gt_img_boxes):
    # Compute the area of all boxes including the predicted box
    pred_box_area  = (pred_img_box[2] - pred_img_box[0] + 1) # area = w
    pred_box_area *= (pred_img_box[3] - pred_img_box[1] + 1) # area = w * h
    gt_boxes_area  = (gt_img_boxes[:, 2] - gt_img_boxes[:, 0] + 1)  # area = w
    gt_boxes_area *= (gt_img_boxes[:, 3] - gt_img_boxes[:, 1] + 1) # area = w * h

    xx1 = np.maximum(pred_img_box[0], gt_img_boxes[:, 0])
    yy1 = np.maximum(pred_img_box[1], gt_img_boxes[:, 1])
    xx2 = np.minimum(pred_img_box[2], gt_img_boxes[:, 2])
    yy2 = np.minimum(pred_img_box[3], gt_img_boxes[:, 3])

    # Compute the area of intersection
    w = np.maximum(0.0, xx2 - xx1 + 1)
    h = np.maximum(0.0, yy2 - yy1 + 1)

    inter = w * h
    # get IoU all boxes with the box of highest conf
    iou = inter / (pred_box_area + gt_boxes_area[:] - inter)
    return iou<jupyter_output><empty_output><jupyter_text>Check whether a predict box is a ground truth<jupyter_code>def pred_box_is_gt_box(pred_img_box, gt_img_boxes, threshold=0.5):
    iou = get_iou(pred_img_box, gt_img_boxes)
    return np.any(iou > threshold)<jupyter_output><empty_output><jupyter_text>Get the prediction from a file<jupyter_code># args = argparse.ArgumentParser().parse_args()

result_path = 'Dropbox/DeepFPNResnet/Resnet101/afw_Deepresnet101_val.txt'
data_root = 'Datasets/AFW'

def get_pred_data():
    # file_name score 
    boxes = {}
    scores = {}
    img_names = []
    img_paths = []

    with open(osp.join(home_path, result_path), "r") as f:
        lines = f.readlines()

    for line in lines:
        line = line.strip()
        elems = line.split(" ")
        img_name = elems[0] + '.jpg'
        score = float(elems[1])
        box = elems[2:]
        box = [float(value) for value in box]

        if len(img_names) == 0 or img_name != img_names[-1]:
            img_names.append(img_name)
            img_paths.append(osp.join(home_path, data_root, img_name))

        if boxes.get(img_name) is None:
            boxes[img_name] = []
            scores[img_name] = []
        boxes[img_name].append(box)
        scores[img_name].append(score)

    final_boxes, final_scores = [], []
    for img_name in img_names:
        final_boxes.append(np.array(boxes[img_name]))
        final_scores.append(np.array(scores[img_name]))

    return img_names, img_paths, final_boxes, final_scores

img_names, img_paths, final_boxes, final_scores = get_pred_data()
img_names[:5]
img_paths[:5]
final_boxes[:5]
final_scores[:5]<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup