Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -11,12 +11,79 @@ model = torch.jit.load(TORCHSCRIPT_PATH)
|
|
11 |
|
12 |
with open(LABELS_PATH, "r") as f:
|
13 |
idx2Label = json.load(f)["idx2Label"]
|
14 |
-
|
15 |
img_transforms = transforms.ToTensor()
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
def predict(img, conf_thresh=0.4):
|
18 |
img_input = [img_transforms(img)]
|
19 |
_, pred = model(img_input)
|
|
|
20 |
out_img = img.copy()
|
21 |
draw = ImageDraw.Draw(out_img)
|
22 |
font = ImageFont.truetype("res/Tuffy_Bold.ttf", 25)
|
@@ -37,7 +104,7 @@ def predict(img, conf_thresh=0.4):
|
|
37 |
draw.text((x1, y1), text, font=font, fill="black")
|
38 |
|
39 |
return out_img
|
40 |
-
|
41 |
example_imgs = [
|
42 |
["res/example.jpg", 0.4],
|
43 |
["res/screenlane-snapchat-profile.jpg", 0.4],
|
|
|
11 |
|
12 |
with open(LABELS_PATH, "r") as f:
|
13 |
idx2Label = json.load(f)["idx2Label"]
|
14 |
+
|
15 |
img_transforms = transforms.ToTensor()
|
16 |
+
|
17 |
+
# inter_class_nms and iou functions implemented by GPT
|
18 |
+
def inter_class_nms(boxes, scores, iou_threshold=0.5):
|
19 |
+
# Convert boxes and scores to torch tensors if they are not already
|
20 |
+
boxes = torch.as_tensor(boxes)
|
21 |
+
scores, class_indices = scores.max(dim=1)
|
22 |
+
|
23 |
+
# Keep track of final boxes and scores
|
24 |
+
final_boxes = []
|
25 |
+
final_scores = []
|
26 |
+
final_class_indices = []
|
27 |
+
|
28 |
+
for class_index in range(scores.shape[1]):
|
29 |
+
# Filter boxes and scores for the current class
|
30 |
+
class_scores = scores[:, class_index]
|
31 |
+
class_boxes = boxes
|
32 |
+
|
33 |
+
# Indices of boxes sorted by score (highest first)
|
34 |
+
sorted_indices = torch.argsort(class_scores, descending=True)
|
35 |
+
|
36 |
+
while len(sorted_indices) > 0:
|
37 |
+
# Take the box with the highest score
|
38 |
+
highest_index = sorted_indices[0]
|
39 |
+
highest_box = class_boxes[highest_index]
|
40 |
+
|
41 |
+
# Add the highest box and score to the final list
|
42 |
+
final_boxes.append(highest_box)
|
43 |
+
final_scores.append(class_scores[highest_index])
|
44 |
+
final_class_indices.append(class_index)
|
45 |
+
|
46 |
+
# Remove the highest box from the list
|
47 |
+
sorted_indices = sorted_indices[1:]
|
48 |
+
|
49 |
+
# Compute IoU of the highest box with the rest
|
50 |
+
ious = iou(class_boxes[sorted_indices], highest_box)
|
51 |
+
|
52 |
+
# Keep only boxes with IoU less than the threshold
|
53 |
+
sorted_indices = sorted_indices[ious < iou_threshold]
|
54 |
+
|
55 |
+
return {'boxes': final_boxes, 'scores': final_scores}
|
56 |
+
|
57 |
+
|
58 |
+
def iou(boxes1, boxes2):
|
59 |
+
"""
|
60 |
+
Compute the Intersection over Union (IoU) of two sets of boxes.
|
61 |
+
|
62 |
+
Args:
|
63 |
+
- boxes1 (Tensor[N, 4]): ground truth boxes
|
64 |
+
- boxes2 (Tensor[M, 4]): predicted boxes
|
65 |
+
|
66 |
+
Returns:
|
67 |
+
- iou (Tensor[N, M]): the NxM matrix containing the pairwise IoU values for every element in boxes1 and boxes2
|
68 |
+
"""
|
69 |
+
|
70 |
+
area1 = (boxes1[:, 2] - boxes1[:, 0]) * (boxes1[:, 3] - boxes1[:, 1])
|
71 |
+
area2 = (boxes2[:, 2] - boxes2[:, 0]) * (boxes2[:, 3] - boxes2[:, 1])
|
72 |
+
|
73 |
+
lt = torch.max(boxes1[:, None, :2], boxes2[:, :2]) # [N,M,2]
|
74 |
+
rb = torch.min(boxes1[:, None, 2:], boxes2[:, 2:]) # [N,M,2]
|
75 |
+
|
76 |
+
wh = (rb - lt).clamp(min=0) # [N,M,2]
|
77 |
+
inter = wh[:, :, 0] * wh[:, :, 1] # [N,M]
|
78 |
+
|
79 |
+
iou = inter / (area1[:, None] + area2 - inter)
|
80 |
+
|
81 |
+
return iou
|
82 |
+
|
83 |
def predict(img, conf_thresh=0.4):
|
84 |
img_input = [img_transforms(img)]
|
85 |
_, pred = model(img_input)
|
86 |
+
pred = inter_class_nms(pred['boxes'], pred['scores'])
|
87 |
out_img = img.copy()
|
88 |
draw = ImageDraw.Draw(out_img)
|
89 |
font = ImageFont.truetype("res/Tuffy_Bold.ttf", 25)
|
|
|
104 |
draw.text((x1, y1), text, font=font, fill="black")
|
105 |
|
106 |
return out_img
|
107 |
+
|
108 |
example_imgs = [
|
109 |
["res/example.jpg", 0.4],
|
110 |
["res/screenlane-snapchat-profile.jpg", 0.4],
|