Update app.py
Browse files
app.py
CHANGED
@@ -24,49 +24,64 @@ def segment_card(image):
|
|
24 |
# Convert to OpenCV format
|
25 |
annotated_image = image.copy()
|
26 |
|
|
|
|
|
|
|
27 |
# Extract bounding boxes and labels
|
28 |
for result in results:
|
29 |
for box in result.boxes:
|
30 |
x1, y1, x2, y2 = map(int, box.xyxy[0]) # Bounding box coordinates
|
31 |
class_id = int(box.cls[0]) # Class index
|
32 |
-
label = CLASS_NAMES[class_id] # Get class label
|
33 |
confidence = box.conf[0].item() # Confidence score
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
text_size = cv2.getTextSize(label_text, font, font_scale, font_thickness)[0]
|
46 |
-
text_x, text_y = x1, y1 - 10
|
47 |
-
|
48 |
-
# Ensure text doesn't go out of bounds
|
49 |
-
text_y = max(text_y, text_size[1] + 10)
|
50 |
-
|
51 |
-
# Draw **filled rectangle background** for the text (above bounding box)
|
52 |
-
cv2.rectangle(
|
53 |
-
annotated_image,
|
54 |
-
(text_x, text_y - text_size[1] - 5),
|
55 |
-
(text_x + text_size[0] + 5, text_y + 5),
|
56 |
-
(0, 255, 0), # Background color (Green)
|
57 |
-
-1
|
58 |
-
)
|
59 |
-
|
60 |
-
# Draw the **text label above the rectangle**
|
61 |
-
cv2.putText(
|
62 |
-
annotated_image,
|
63 |
-
label_text,
|
64 |
-
(text_x, text_y),
|
65 |
-
font,
|
66 |
-
font_scale,
|
67 |
-
(0, 0, 0), # Text color (Black for contrast)
|
68 |
-
font_thickness
|
69 |
-
)
|
70 |
|
71 |
return Image.fromarray(annotated_image) # Convert back to PIL Image
|
72 |
|
|
|
24 |
# Convert to OpenCV format
|
25 |
annotated_image = image.copy()
|
26 |
|
27 |
+
# Dictionary to track the highest confidence detection per class
|
28 |
+
best_detections = {}
|
29 |
+
|
30 |
# Extract bounding boxes and labels
|
31 |
for result in results:
|
32 |
for box in result.boxes:
|
33 |
x1, y1, x2, y2 = map(int, box.xyxy[0]) # Bounding box coordinates
|
34 |
class_id = int(box.cls[0]) # Class index
|
|
|
35 |
confidence = box.conf[0].item() # Confidence score
|
36 |
+
|
37 |
+
# Check if this is the highest confidence detection for the class
|
38 |
+
if class_id not in best_detections or confidence > best_detections[class_id]["confidence"]:
|
39 |
+
best_detections[class_id] = {
|
40 |
+
"bbox": (x1, y1, x2, y2),
|
41 |
+
"confidence": confidence
|
42 |
+
}
|
43 |
+
|
44 |
+
# Draw the highest confidence detections
|
45 |
+
for class_id, detection in best_detections.items():
|
46 |
+
x1, y1, x2, y2 = detection["bbox"]
|
47 |
+
label = CLASS_NAMES[class_id]
|
48 |
+
confidence = detection["confidence"]
|
49 |
+
|
50 |
+
# Draw bounding box **BELOW** text elements
|
51 |
+
cv2.rectangle(annotated_image, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
52 |
+
|
53 |
+
# Set text properties
|
54 |
+
font = cv2.FONT_HERSHEY_SIMPLEX
|
55 |
+
font_scale = 0.8 # Increased font size for better readability
|
56 |
+
font_thickness = 2
|
57 |
+
label_text = f"{label} ({confidence:.2f})"
|
58 |
+
|
59 |
+
# Get text size for proper background padding
|
60 |
+
text_size = cv2.getTextSize(label_text, font, font_scale, font_thickness)[0]
|
61 |
+
text_x, text_y = x1, y1 - 10
|
62 |
+
|
63 |
+
# Ensure text doesn't go out of bounds
|
64 |
+
text_y = max(text_y, text_size[1] + 10)
|
65 |
+
|
66 |
+
# Draw **filled rectangle background** for the text (above bounding box)
|
67 |
+
cv2.rectangle(
|
68 |
+
annotated_image,
|
69 |
+
(text_x, text_y - text_size[1] - 5),
|
70 |
+
(text_x + text_size[0] + 5, text_y + 5),
|
71 |
+
(0, 255, 0), # Background color (Green)
|
72 |
+
-1
|
73 |
+
)
|
74 |
|
75 |
+
# Draw the **text label above the rectangle**
|
76 |
+
cv2.putText(
|
77 |
+
annotated_image,
|
78 |
+
label_text,
|
79 |
+
(text_x, text_y),
|
80 |
+
font,
|
81 |
+
font_scale,
|
82 |
+
(0, 0, 0), # Text color (Black for contrast)
|
83 |
+
font_thickness
|
84 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
|
86 |
return Image.fromarray(annotated_image) # Convert back to PIL Image
|
87 |
|