danielritchie commited on
Commit
3a603bd
·
verified ·
1 Parent(s): 421253b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -60
app.py CHANGED
@@ -2,69 +2,39 @@ import gradio as gr
2
  import cv2
3
  import numpy as np
4
  from ultralytics import YOLO
5
- import torch
6
 
7
  # Initialize models
8
- duck_model = YOLO('https://huggingface.co/brainwavecollective/yolo8n-rubber-duck-detector/resolve/main/yolov8n_rubberducks4.pt')
9
  standard_model = YOLO('yolov8n.pt')
10
 
11
- def calculate_iou(box1, box2):
12
- x1 = max(box1[0], box2[0])
13
- y1 = max(box1[1], box2[1])
14
- x2 = min(box1[2], box2[2])
15
- y2 = min(box1[3], box2[3])
16
- intersection = max(0, x2 - x1) * max(0, y2 - y1)
17
-
18
- box1_area = (box1[2] - box1[0]) * (box1[3] - box1[1])
19
- box2_area = (box2[2] - box2[0]) * (box2[3] - box2[1])
20
- union = box1_area + box2_area - intersection
21
-
22
- return intersection / union if union > 0 else 0
23
-
24
- def process_image(image, model, is_duck_model=True):
25
- results = model(image, conf=0.4)
26
 
27
- valid_boxes = []
28
  for r in results:
29
- for box in r.boxes:
30
- class_name = model.names[int(box.cls[0])]
31
- # For both models, only show teddy bear class
32
- if class_name == "teddy bear":
33
- valid_boxes.append({
34
- 'coords': box.xyxy[0].cpu().numpy().tolist(),
35
- 'confidence': float(box.conf[0])
36
- })
37
-
38
- # Filter overlapping boxes
39
- filtered_boxes = []
40
- for i, box in enumerate(valid_boxes):
41
- should_add = True
42
- for existing_box in filtered_boxes:
43
- if calculate_iou(box['coords'], existing_box['coords']) > 0.5:
44
- if box['confidence'] <= existing_box['confidence']:
45
- should_add = False
46
- break
47
- if should_add:
48
- filtered_boxes.append(box)
49
 
50
- # Draw boxes and labels
51
- processed_image = image.copy()
52
- for box in filtered_boxes:
53
- x1, y1, x2, y2 = map(int, box['coords'])
54
- cv2.rectangle(processed_image, (x1, y1), (x2, y2), (0, 255, 0), 2)
55
- label = f"{'Duck' if is_duck_model else 'Standard'} ({box['confidence']:.2f})"
56
- cv2.putText(processed_image, label, (x1, y1-10),
57
- cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
58
-
59
- return processed_image, len(filtered_boxes)
60
 
61
  def compare_models(input_image):
62
  # Convert from Gradio's PIL image to OpenCV format
63
  image = np.array(input_image)
64
 
65
  # Process with both models
66
- duck_image, duck_detections = process_image(image, duck_model, True)
67
- standard_image, std_detections = process_image(image, standard_model, False)
68
 
69
  # Create side-by-side comparison
70
  height, width = image.shape[:2]
@@ -74,16 +44,11 @@ def compare_models(input_image):
74
  canvas[:, :width] = duck_image
75
  canvas[:, width:] = standard_image
76
 
77
- # Add labels for each model
78
- cv2.putText(canvas, "Rubber Duck YOLO", (10, 30),
79
- cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
80
- cv2.putText(canvas, f"Detections: {duck_detections}", (10, 60),
81
  cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
82
-
83
  cv2.putText(canvas, "Standard YOLOv8", (width + 10, 30),
84
  cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
85
- cv2.putText(canvas, f"Detections: {std_detections}", (width + 10, 60),
86
- cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
87
 
88
  return canvas
89
 
@@ -93,11 +58,11 @@ iface = gr.Interface(
93
  inputs=gr.Image(type="pil"),
94
  outputs=gr.Image(type="numpy"),
95
  title="YOLO Model Comparison",
96
- description="Compare Rubber Duck YOLO detector with standard YOLOv8 model",
97
- examples=[["example1.jpg"], ["example2.jpg"]], # Add your example images here
98
  cache_examples=True
99
  )
100
 
101
  # Launch the interface
102
  if __name__ == "__main__":
103
- iface.launch(share=True)
 
2
  import cv2
3
  import numpy as np
4
  from ultralytics import YOLO
 
5
 
6
  # Initialize models
7
+ duck_model = YOLO('https://huggingface.co/brainwavecollective/yolo8n-rubber-duck-detector/resolve/main/yolov8n_rubberducks.pt')
8
  standard_model = YOLO('yolov8n.pt')
9
 
10
+ def process_image(image, model):
11
+ results = model(image)
12
+ processed_image = image.copy()
 
 
 
 
 
 
 
 
 
 
 
 
13
 
 
14
  for r in results:
15
+ boxes = r.boxes
16
+ for box in boxes:
17
+ # Get box coordinates and confidence
18
+ x1, y1, x2, y2 = map(int, box.xyxy[0].cpu().numpy())
19
+ conf = float(box.conf[0])
20
+ cls = int(box.cls[0])
21
+ class_name = model.names[cls]
22
+
23
+ # Draw box and label
24
+ cv2.rectangle(processed_image, (x1, y1), (x2, y2), (0, 255, 0), 2)
25
+ label = f"{class_name} ({conf:.2f})"
26
+ cv2.putText(processed_image, label, (x1, y1-10),
27
+ cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
 
 
 
 
 
 
 
28
 
29
+ return processed_image
 
 
 
 
 
 
 
 
 
30
 
31
  def compare_models(input_image):
32
  # Convert from Gradio's PIL image to OpenCV format
33
  image = np.array(input_image)
34
 
35
  # Process with both models
36
+ duck_image = process_image(image, duck_model)
37
+ standard_image = process_image(image, standard_model)
38
 
39
  # Create side-by-side comparison
40
  height, width = image.shape[:2]
 
44
  canvas[:, :width] = duck_image
45
  canvas[:, width:] = standard_image
46
 
47
+ # Add model labels
48
+ cv2.putText(canvas, "Duck Detector", (10, 30),
 
 
49
  cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
 
50
  cv2.putText(canvas, "Standard YOLOv8", (width + 10, 30),
51
  cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
 
 
52
 
53
  return canvas
54
 
 
58
  inputs=gr.Image(type="pil"),
59
  outputs=gr.Image(type="numpy"),
60
  title="YOLO Model Comparison",
61
+ description="Compare Duck Detector with standard YOLOv8 model",
62
+ examples=[["test_image.jpg"]],
63
  cache_examples=True
64
  )
65
 
66
  # Launch the interface
67
  if __name__ == "__main__":
68
+ iface.launch()