JakeTurner616 commited on
Commit
e4bdce6
·
verified ·
1 Parent(s): e765d9a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -41
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 (Ensure these match your YOLO class labels)
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
- conf = float(box.conf[0]) # Confidence score
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
- # Draw only the best bounding box for each class
41
- for class_id, data in best_detections.items():
42
- x1, y1, x2, y2 = data["bbox"]
43
- label = CLASS_NAMES[class_id]
44
 
45
- # Draw bounding box
46
- cv2.rectangle(annotated_image, (x1, y1), (x2, y2), (0, 255, 0), 2)
 
 
 
47
 
48
- # Draw label text with background
49
- font = cv2.FONT_HERSHEY_SIMPLEX
50
- font_scale = 0.5
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
- # Ensure text doesn't go out of bounds
56
- text_y = max(text_y, text_size[1] + 10)
57
 
58
- # Draw filled rectangle for text background
59
- cv2.rectangle(
60
- annotated_image,
61
- (text_x, text_y - text_size[1] - 5),
62
- (text_x + text_size[0] + 5, text_y + 5),
63
- (0, 255, 0),
64
- -1
65
- )
66
 
67
- # Put text label on the image
68
- cv2.putText(
69
- annotated_image,
70
- label,
71
- (text_x, text_y),
72
- font,
73
- font_scale,
74
- (0, 0, 0), # Text color (black for contrast)
75
- font_thickness
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