| |
|
|
| import cv2 |
| import numpy as np |
| from PIL import Image |
|
|
| def load_img(img_path, input_shape): |
| |
| image = cv2.imread(img_path) |
| image_height, image_width = image.shape[:2] |
| Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) |
|
|
| input_height, input_width = input_shape[2:] |
| image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) |
| resized = cv2.resize(image_rgb, (input_width, input_height)) |
|
|
| input_image = resized / 255.0 |
| input_image = input_image.transpose(2,0,1) |
| input_tensor = input_image[np.newaxis, :, :, :].astype(np.float32) |
|
|
| return image, image_height, image_width, input_height, input_width, input_tensor |
|
|
| def nms(boxes, scores, iou_threshold): |
| |
| sorted_indices = np.argsort(scores)[::-1] |
|
|
| keep_boxes = [] |
| while sorted_indices.size > 0: |
| |
| box_id = sorted_indices[0] |
| keep_boxes.append(box_id) |
|
|
| |
| ious = compute_iou(boxes[box_id, :], boxes[sorted_indices[1:], :]) |
|
|
| |
| keep_indices = np.where(ious < iou_threshold)[0] |
|
|
| |
| sorted_indices = sorted_indices[keep_indices + 1] |
|
|
| return keep_boxes |
|
|
| def compute_iou(box, boxes): |
| |
| xmin = np.maximum(box[0], boxes[:, 0]) |
| ymin = np.maximum(box[1], boxes[:, 1]) |
| xmax = np.minimum(box[2], boxes[:, 2]) |
| ymax = np.minimum(box[3], boxes[:, 3]) |
|
|
| |
| intersection_area = np.maximum(0, xmax - xmin) * np.maximum(0, ymax - ymin) |
|
|
| |
| box_area = (box[2] - box[0]) * (box[3] - box[1]) |
| boxes_area = (boxes[:, 2] - boxes[:, 0]) * (boxes[:, 3] - boxes[:, 1]) |
| union_area = box_area + boxes_area - intersection_area |
|
|
| |
| iou = intersection_area / union_area |
|
|
| return iou |
|
|
|
|
| def xywh2xyxy(x): |
| |
| y = np.copy(x) |
| y[..., 0] = x[..., 0] - x[..., 2] / 2 |
| y[..., 1] = x[..., 1] - x[..., 3] / 2 |
| y[..., 2] = x[..., 0] + x[..., 2] / 2 |
| y[..., 3] = x[..., 1] + x[..., 3] / 2 |
| return y |