csprojectworkspace commited on
Commit
9a0b521
·
verified ·
1 Parent(s): 0261967

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -18
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import gradio as gr
2
  from ultralytics import YOLO
3
- import numpy as np
4
  from PIL import Image
 
5
 
6
  # load model
7
  model = YOLO("best.pt")
@@ -10,31 +10,40 @@ def predict(image):
10
  results = model(image)
11
  r = results[0]
12
 
13
- # Get detection results
14
- detections = []
 
15
  if len(r.boxes) > 0:
16
  for box in r.boxes:
17
  class_id = int(box.cls[0])
18
  class_name = model.names[class_id]
19
- confidence = float(box.conf[0])
20
- detections.append({
21
- "class": class_name,
22
- "confidence": round(confidence * 100, 2)
23
- })
24
 
25
- # Sort by confidence (highest first)
26
- if detections:
27
- detections.sort(key=lambda x: x["confidence"], reverse=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
- # Build result text with ALL detections
30
- lines = [f"Detected {len(detections)} object(s):\n"]
31
- for i, d in enumerate(detections, 1):
32
- lines.append(f"{i}. {d['class']}: {d['confidence']}%")
33
- result_text = "\n".join(lines)
34
  else:
35
  result_text = "No detection"
36
 
37
- # Return annotated image AND the detection text
38
  output_image = r.plot()
39
  return Image.fromarray(output_image), result_text
40
 
@@ -43,7 +52,7 @@ demo = gr.Interface(
43
  inputs=gr.Image(type="pil"),
44
  outputs=[
45
  gr.Image(type="pil", label="Detection"),
46
- gr.Textbox(label="Result", lines=10) # taller box to show all results
47
  ],
48
  title="YOLOv8 Detection",
49
  description="Upload an image and detect objects"
 
1
  import gradio as gr
2
  from ultralytics import YOLO
 
3
  from PIL import Image
4
+ from collections import defaultdict
5
 
6
  # load model
7
  model = YOLO("best.pt")
 
10
  results = model(image)
11
  r = results[0]
12
 
13
+ # Group detections by class
14
+ class_confidences = defaultdict(list)
15
+
16
  if len(r.boxes) > 0:
17
  for box in r.boxes:
18
  class_id = int(box.cls[0])
19
  class_name = model.names[class_id]
20
+ confidence = float(box.conf[0]) * 100
21
+ class_confidences[class_name].append(confidence)
 
 
 
22
 
23
+ if class_confidences:
24
+ lines = []
25
+ # Sort classes by their max confidence (highest first)
26
+ sorted_classes = sorted(
27
+ class_confidences.items(),
28
+ key=lambda x: max(x[1]),
29
+ reverse=True
30
+ )
31
+
32
+ for class_name, confidences in sorted_classes:
33
+ count = len(confidences)
34
+ avg_conf = round(sum(confidences) / count, 2)
35
+ max_conf = round(max(confidences), 2)
36
+
37
+ if count > 1:
38
+ lines.append(f"• {class_name} ({count}x) — avg: {avg_conf}% | best: {max_conf}%")
39
+ else:
40
+ lines.append(f"• {class_name} — {max_conf}%")
41
 
42
+ total = sum(len(v) for v in class_confidences.values())
43
+ result_text = f"Detected {total} object(s) in {len(class_confidences)} class(es):\n\n" + "\n".join(lines)
 
 
 
44
  else:
45
  result_text = "No detection"
46
 
 
47
  output_image = r.plot()
48
  return Image.fromarray(output_image), result_text
49
 
 
52
  inputs=gr.Image(type="pil"),
53
  outputs=[
54
  gr.Image(type="pil", label="Detection"),
55
+ gr.Textbox(label="Result", lines=10)
56
  ],
57
  title="YOLOv8 Detection",
58
  description="Upload an image and detect objects"