danielritchie commited on
Commit
9ba69a1
·
verified ·
1 Parent(s): 180137f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -21
app.py CHANGED
@@ -14,13 +14,11 @@ def process_image(image, model):
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),
@@ -29,40 +27,47 @@ def process_image(image, model):
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 (standard first, then duck)
36
  standard_image = process_image(image, standard_model)
37
  duck_image = process_image(image, duck_model)
38
 
39
- # Create side-by-side comparison
40
  height, width = image.shape[:2]
41
- canvas = np.zeros((height, width * 2, 3), dtype=np.uint8)
42
 
43
- # Place images side by side (standard left, duck right)
 
 
 
44
  canvas[:, :width] = standard_image
45
- canvas[:, width:] = duck_image
 
 
 
46
 
47
  # Add model labels
48
- cv2.putText(canvas, "Standard YOLOv8", (10, 30),
49
  cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
50
- cv2.putText(canvas, "Duck Detector", (width + 10, 30),
51
  cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
52
 
53
  return canvas
54
 
55
- # Create Gradio interface
56
- iface = gr.Interface(
57
- fn=compare_models,
58
- inputs=gr.Image(type="pil", height=200), # Smaller input image
59
- outputs=gr.Image(type="numpy", height=400), # Larger output for side-by-side comparison
60
- title="YOLO Model Comparison",
61
- description="Compare standard YOLOv8 model (left) with Duck Detector (right)",
62
- examples=[["test_image.jpg"]],
63
- cache_examples=True,
64
- css="img.output {width: 100% !important; height: auto !important;}" # Force output to span full width
65
- )
 
 
 
 
66
 
67
  # Launch the interface
68
  if __name__ == "__main__":
 
14
  for r in results:
15
  boxes = r.boxes
16
  for box in boxes:
 
17
  x1, y1, x2, y2 = map(int, box.xyxy[0].cpu().numpy())
18
  conf = float(box.conf[0])
19
  cls = int(box.cls[0])
20
  class_name = model.names[cls]
21
 
 
22
  cv2.rectangle(processed_image, (x1, y1), (x2, y2), (0, 255, 0), 2)
23
  label = f"{class_name} ({conf:.2f})"
24
  cv2.putText(processed_image, label, (x1, y1-10),
 
27
  return processed_image
28
 
29
  def compare_models(input_image):
 
30
  image = np.array(input_image)
31
 
 
32
  standard_image = process_image(image, standard_model)
33
  duck_image = process_image(image, duck_model)
34
 
 
35
  height, width = image.shape[:2]
36
+ gap = 20 # Add a 20-pixel gap between images
37
 
38
+ # Create canvas with extra width for the gap
39
+ canvas = np.zeros((height, width * 2 + gap, 3), dtype=np.uint8)
40
+
41
+ # Place images with gap in between
42
  canvas[:, :width] = standard_image
43
+ canvas[:, width + gap:] = duck_image
44
+
45
+ # Fill gap with white or gray color (optional)
46
+ canvas[:, width:width + gap] = [128, 128, 128] # Gray gap
47
 
48
  # Add model labels
49
+ cv2.putText(canvas, "YOLOv8", (10, 30),
50
  cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
51
+ cv2.putText(canvas, "YOLOv8 Fine Tune", (width + gap + 10, 30),
52
  cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
53
 
54
  return canvas
55
 
56
+ # Create Gradio interface with stacked layout
57
+ with gr.Blocks() as iface:
58
+ gr.Markdown("# YOLO Model Comparison")
59
+ gr.Markdown("Compare standard YOLOv8 model (left) with Fine-tuned model (right)")
60
+
61
+ with gr.Column():
62
+ input_image = gr.Image(type="pil", label="Input Image", height=200)
63
+ submit_btn = gr.Button("Compare Models")
64
+ output_image = gr.Image(type="numpy", label="Comparison Result", height=400)
65
+
66
+ submit_btn.click(
67
+ fn=compare_models,
68
+ inputs=input_image,
69
+ outputs=output_image,
70
+ )
71
 
72
  # Launch the interface
73
  if __name__ == "__main__":