Update app.py
Browse files
app.py
CHANGED
@@ -9,13 +9,14 @@ from PIL import Image
|
|
9 |
model_path = "best.pt"
|
10 |
model = YOLO(model_path)
|
11 |
|
12 |
-
# Class names
|
13 |
CLASS_NAMES = [
|
14 |
"card_title", "card_art", "card_type",
|
15 |
"card_set_symbol", "card_mana_cost",
|
16 |
"card_oracle_text", "card_power_toughness"
|
17 |
]
|
18 |
|
|
|
19 |
def segment_card(image):
|
20 |
image = np.array(image) # Convert PIL image to NumPy array
|
21 |
results = model(image) # Run YOLO inference
|
@@ -23,57 +24,49 @@ def segment_card(image):
|
|
23 |
# Convert to OpenCV format
|
24 |
annotated_image = image.copy()
|
25 |
|
26 |
-
# Dictionary to store the highest confidence detection for each class
|
27 |
-
best_detections = {}
|
28 |
-
|
29 |
# Extract bounding boxes and labels
|
30 |
for result in results:
|
31 |
for box in result.boxes:
|
32 |
x1, y1, x2, y2 = map(int, box.xyxy[0]) # Bounding box coordinates
|
33 |
class_id = int(box.cls[0]) # Class index
|
34 |
-
|
35 |
-
|
36 |
-
# Check if we have seen this class before
|
37 |
-
if class_id not in best_detections or conf > best_detections[class_id]["conf"]:
|
38 |
-
best_detections[class_id] = {"bbox": (x1, y1, x2, y2), "conf": conf}
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
x1, y1, x2, y2 = data["bbox"]
|
43 |
-
label = CLASS_NAMES[class_id]
|
44 |
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
47 |
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
font_thickness = 2
|
52 |
-
text_size = cv2.getTextSize(label, font, font_scale, font_thickness)[0]
|
53 |
-
text_x, text_y = x1, y1 - 10
|
54 |
|
55 |
-
|
56 |
-
|
57 |
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
|
78 |
return Image.fromarray(annotated_image) # Convert back to PIL Image
|
79 |
|
|
|
9 |
model_path = "best.pt"
|
10 |
model = YOLO(model_path)
|
11 |
|
12 |
+
# Class names
|
13 |
CLASS_NAMES = [
|
14 |
"card_title", "card_art", "card_type",
|
15 |
"card_set_symbol", "card_mana_cost",
|
16 |
"card_oracle_text", "card_power_toughness"
|
17 |
]
|
18 |
|
19 |
+
# Define inference function
|
20 |
def segment_card(image):
|
21 |
image = np.array(image) # Convert PIL image to NumPy array
|
22 |
results = model(image) # Run YOLO inference
|
|
|
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 |
+
# Draw bounding box **BELOW** text elements
|
36 |
+
cv2.rectangle(annotated_image, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
|
|
|
|
37 |
|
38 |
+
# Set text properties
|
39 |
+
font = cv2.FONT_HERSHEY_SIMPLEX
|
40 |
+
font_scale = 0.7 # Increased font size and thickness for better readability
|
41 |
+
font_thickness = 1.3
|
42 |
+
label_text = f"{label} ({confidence:.2f})"
|
43 |
|
44 |
+
# Get text size for proper background padding
|
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 |
|