mawady-uni commited on
Commit
16dffb4
·
verified ·
1 Parent(s): ebb5646

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -0
app.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from PIL import Image
3
+ from ultralytics import YOLO
4
+ import pandas as pd
5
+ import numpy as np
6
+
7
+ # COCO class names
8
+ COCO_CLASSES = [
9
+ 'person','bicycle','car','motorcycle','airplane','bus','train','truck','boat',
10
+ 'traffic light','fire hydrant','stop sign','parking meter','bench','bird','cat',
11
+ 'dog','horse','sheep','cow','elephant','bear','zebra','giraffe','backpack',
12
+ 'umbrella','handbag','tie','suitcase','frisbee','skis','snowboard','sports ball',
13
+ 'kite','baseball bat','baseball glove','skateboard','surfboard','tennis racket',
14
+ 'bottle','wine glass','cup','fork','knife','spoon','bowl','banana','apple',
15
+ 'sandwich','orange','broccoli','carrot','hot dog','pizza','donut','cake',
16
+ 'chair','couch','potted plant','bed','dining table','toilet','tv','laptop',
17
+ 'mouse','remote','keyboard','cell phone','microwave','oven','toaster','sink',
18
+ 'refrigerator','book','clock','vase','scissors','teddy bear','hair drier',
19
+ 'toothbrush'
20
+ ]
21
+
22
+ # Load YOLOv8 models
23
+ yolo_fast = YOLO("yolov8n.pt") # fast nano
24
+ yolo_acc = YOLO("yolo12n.pt") # accurate small
25
+
26
+ def detect_top3_with_image(image):
27
+ image_rgb = image.convert("RGB")
28
+
29
+ # YOLO fast detection
30
+ results_fast = yolo_fast(image_rgb)[0]
31
+ boxes_fast = results_fast.boxes
32
+ top_fast = sorted(zip(boxes_fast.cls.cpu().numpy(), boxes_fast.conf.cpu().numpy()),
33
+ key=lambda x: x[1], reverse=True)[:3]
34
+ fast_results = [f"{COCO_CLASSES[int(cls)]} ({conf*100:.1f}%)" for cls, conf in top_fast]
35
+
36
+ # Convert plot to RGB
37
+ fast_plot = results_fast.plot()
38
+ fast_img = Image.fromarray(np.array(fast_plot)[:,:,::-1]) # BGR to RGB
39
+
40
+ # YOLO accurate detection
41
+ results_acc = yolo_acc(image_rgb)[0]
42
+ boxes_acc = results_acc.boxes
43
+ top_acc = sorted(zip(boxes_acc.cls.cpu().numpy(), boxes_acc.conf.cpu().numpy()),
44
+ key=lambda x: x[1], reverse=True)[:3]
45
+ acc_results = [f"{COCO_CLASSES[int(cls)]} ({conf*100:.1f}%)" for cls, conf in top_acc]
46
+
47
+ # Convert plot to RGB
48
+ acc_plot = results_acc.plot()
49
+ acc_img = Image.fromarray(np.array(acc_plot)[:,:,::-1]) # BGR to RGB
50
+
51
+ # Top-3 table
52
+ df = pd.DataFrame({
53
+ "Rank": [1,2,3],
54
+ "YOLOv8n": fast_results + [""]*(3-len(fast_results)),
55
+ "YOLOv12n": acc_results + [""]*(3-len(acc_results))
56
+ })
57
+
58
+ return df, fast_img, acc_img
59
+
60
+ iface = gr.Interface(
61
+ fn=detect_top3_with_image,
62
+ inputs=gr.Image(type="pil"),
63
+ outputs=[
64
+ gr.Dataframe(headers=["Rank","YOLOv8n","YOLOv12n"], type="pandas", label="Top-3 Detections"),
65
+ gr.Image(label="YOLOv8n Detection Output"),
66
+ gr.Image(label="YOLOv12n Detection Output")
67
+ ],
68
+ title="Image Object Detection Validator",
69
+ description="Upload an AI-generated image to see the top-3 detected objects and the visual detection output for nano version of both YOLOv8 and YOLOv12 models."
70
+ )
71
+
72
+ if __name__ == "__main__":
73
+ iface.launch()