RashiAgarwal
commited on
Commit
·
2a4462d
1
Parent(s):
5f47025
Upload display.py
Browse files- display.py +94 -0
display.py
ADDED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import cv2
|
3 |
+
import albumentations as A
|
4 |
+
from utils import *
|
5 |
+
import random
|
6 |
+
from albumentations.pytorch import ToTensorV2
|
7 |
+
|
8 |
+
def inference(image: np.ndarray, iou_thresh: float = 0.5, thresh: float = 0.4, show_cam: bool = False, transparency: float = 0.5):
|
9 |
+
|
10 |
+
transforms = A.Compose(
|
11 |
+
[
|
12 |
+
A.LongestMaxSize(max_size=config.IMAGE_SIZE),
|
13 |
+
A.PadIfNeeded(
|
14 |
+
min_height=config.IMAGE_SIZE, min_width=config.IMAGE_SIZE, border_mode=cv2.BORDER_CONSTANT
|
15 |
+
),
|
16 |
+
A.Normalize(mean=[0, 0, 0], std=[1, 1, 1], max_pixel_value=255,),
|
17 |
+
ToTensorV2(),
|
18 |
+
],
|
19 |
+
)
|
20 |
+
with torch.no_grad():
|
21 |
+
transformed_image = transforms(image=image)["image"].unsqueeze(0)
|
22 |
+
output = model(transformed_image)
|
23 |
+
|
24 |
+
bboxes = [[] for _ in range(1)]
|
25 |
+
for i in range(3):
|
26 |
+
batch_size, A1, S, _, _ = output[i].shape
|
27 |
+
anchor = scaled_anchors[i].to('cpu')
|
28 |
+
boxes_scale_i = cells_to_bboxes(
|
29 |
+
output[i].to('cpu'), anchor, S=S, is_preds=True
|
30 |
+
)
|
31 |
+
for idx, (box) in enumerate(boxes_scale_i):
|
32 |
+
bboxes[idx] += box
|
33 |
+
|
34 |
+
nms_boxes = non_max_suppression(
|
35 |
+
bboxes[0], iou_threshold=iou_thresh, threshold=thresh, box_format="midpoint",
|
36 |
+
)
|
37 |
+
plot_img = draw_predictions(image, nms_boxes, class_labels=config.PASCAL_CLASSES)
|
38 |
+
if not show_cam:
|
39 |
+
return [plot_img]
|
40 |
+
|
41 |
+
grayscale_cam = cam(transformed_image, scaled_anchors)[0, :, :]
|
42 |
+
img = cv2.resize(image, (416, 416))
|
43 |
+
img = np.float32(img) / 255
|
44 |
+
cam_image = show_cam_on_image(img, grayscale_cam, use_rgb=True, image_weight=transparency)
|
45 |
+
return [plot_img, cam_image]
|
46 |
+
|
47 |
+
|
48 |
+
def draw_predictions(image: np.ndarray, boxes: list[list], class_labels: list[str]) -> np.ndarray:
|
49 |
+
"""Plots predicted bounding boxes on the image"""
|
50 |
+
|
51 |
+
colors = [[random.randint(0, 255) for _ in range(3)] for name in class_labels]
|
52 |
+
|
53 |
+
im = np.array(image)
|
54 |
+
height, width, _ = im.shape
|
55 |
+
bbox_thick = int((height + width) /1000)
|
56 |
+
|
57 |
+
# Create a Rectangle patch
|
58 |
+
for box in boxes:
|
59 |
+
assert len(box) == 6, "box should contain class pred, confidence, x, y, width, height"
|
60 |
+
class_pred = box[0]
|
61 |
+
conf = box[1]
|
62 |
+
box = box[2:]
|
63 |
+
upper_left_x = box[0] - box[2] / 2
|
64 |
+
upper_left_y = box[1] - box[3] / 2
|
65 |
+
|
66 |
+
x1 = int(upper_left_x * width)
|
67 |
+
y1 = int(upper_left_y * height)
|
68 |
+
|
69 |
+
x2 = x1 + int(box[2] * width)
|
70 |
+
y2 = y1 + int(box[3] * height)
|
71 |
+
|
72 |
+
cv2.rectangle(
|
73 |
+
image,
|
74 |
+
(x1, y1), (x2, y2),
|
75 |
+
color=colors[int(class_pred)],
|
76 |
+
thickness=bbox_thick
|
77 |
+
)
|
78 |
+
text = f"{class_labels[int(class_pred)]}: {conf:.2f}"
|
79 |
+
t_size = cv2.getTextSize(text, 0, 0.7, thickness=bbox_thick // 2)[0]
|
80 |
+
c3 = (x1 + t_size[0], y1 - t_size[1] - 3)
|
81 |
+
|
82 |
+
cv2.rectangle(image, (x1, y1), c3, colors[int(class_pred)], -1)
|
83 |
+
cv2.putText(
|
84 |
+
image,
|
85 |
+
text,
|
86 |
+
(x1, y1 - 2),
|
87 |
+
cv2.FONT_ITALIC,
|
88 |
+
0.7,
|
89 |
+
(0, 0, 0),
|
90 |
+
bbox_thick // 2,
|
91 |
+
lineType=cv2.LINE_8,
|
92 |
+
)
|
93 |
+
|
94 |
+
return image
|