Update app.py
Browse files
app.py
CHANGED
@@ -72,28 +72,41 @@ def process_video(video_path, target, progress=gr.Progress()):
|
|
72 |
|
73 |
boxes, scores, labels = result["boxes"], result["scores"], result["labels"]
|
74 |
|
|
|
75 |
for box, score, label in zip(boxes, scores, labels):
|
76 |
if score.item() >= 0.5:
|
77 |
box = [round(i, 2) for i in box.tolist()]
|
78 |
object_label = target
|
79 |
confidence = round(score.item(), 3)
|
80 |
annotation = f"{object_label}: {confidence}"
|
81 |
-
|
82 |
# Increase line width for the bounding box
|
83 |
draw.rectangle(box, outline="red", width=3)
|
84 |
-
|
85 |
-
#
|
86 |
-
|
|
|
87 |
try:
|
88 |
font = ImageFont.truetype("arial.ttf", font_size)
|
89 |
except IOError:
|
90 |
font = ImageFont.load_default()
|
91 |
-
|
92 |
-
|
93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
# Draw text in red
|
95 |
draw.text(text_position, annotation, fill="red", font=font)
|
96 |
-
|
97 |
max_score = max(max_score, confidence)
|
98 |
|
99 |
frame_path = os.path.join(temp_dir, f"frame_{batch_indices[idx]:04d}.png")
|
|
|
72 |
|
73 |
boxes, scores, labels = result["boxes"], result["scores"], result["labels"]
|
74 |
|
75 |
+
# Inside the loop where bounding boxes are drawn
|
76 |
for box, score, label in zip(boxes, scores, labels):
|
77 |
if score.item() >= 0.5:
|
78 |
box = [round(i, 2) for i in box.tolist()]
|
79 |
object_label = target
|
80 |
confidence = round(score.item(), 3)
|
81 |
annotation = f"{object_label}: {confidence}"
|
82 |
+
|
83 |
# Increase line width for the bounding box
|
84 |
draw.rectangle(box, outline="red", width=3)
|
85 |
+
|
86 |
+
# Calculate font size based on image dimensions
|
87 |
+
img_width, img_height = pil_img.size
|
88 |
+
font_size = int(min(img_width, img_height) * 0.03) # 3% of the smaller dimension
|
89 |
try:
|
90 |
font = ImageFont.truetype("arial.ttf", font_size)
|
91 |
except IOError:
|
92 |
font = ImageFont.load_default()
|
93 |
+
|
94 |
+
# Calculate text size
|
95 |
+
text_bbox = draw.textbbox((0, 0), annotation, font=font)
|
96 |
+
text_width = text_bbox[2] - text_bbox[0]
|
97 |
+
text_height = text_bbox[3] - text_bbox[1]
|
98 |
+
|
99 |
+
# Position text inside the top of the bounding box
|
100 |
+
text_position = (box[0], box[1])
|
101 |
+
|
102 |
+
# Draw semi-transparent background for text
|
103 |
+
draw.rectangle([text_position[0], text_position[1],
|
104 |
+
text_position[0] + text_width, text_position[1] + text_height],
|
105 |
+
fill=(0, 0, 0, 128))
|
106 |
+
|
107 |
# Draw text in red
|
108 |
draw.text(text_position, annotation, fill="red", font=font)
|
109 |
+
|
110 |
max_score = max(max_score, confidence)
|
111 |
|
112 |
frame_path = os.path.join(temp_dir, f"frame_{batch_indices[idx]:04d}.png")
|