Update app.py
Browse files
app.py
CHANGED
@@ -1,54 +1,69 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import cv2
|
3 |
-
import numpy as np
|
4 |
-
from PIL import Image
|
5 |
-
from ultralytics import YOLO
|
6 |
-
|
7 |
-
# Load the YOLO
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
for
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
)
|
52 |
-
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
from PIL import Image
|
5 |
+
from ultralytics import YOLO
|
6 |
+
|
7 |
+
# Load the YOLO models
|
8 |
+
model_all = YOLO('best.pt') # Model for helmet, license plate, and motorcyclist
|
9 |
+
model_np = YOLO('best_1.pt') # Model for number plate detection
|
10 |
+
|
11 |
+
def predict(image):
|
12 |
+
# Convert PIL Image to numpy array
|
13 |
+
img_array = np.array(image)
|
14 |
+
|
15 |
+
# Perform inference with both models
|
16 |
+
results_all = model_all(img_array)
|
17 |
+
results_np = model_np(img_array)
|
18 |
+
|
19 |
+
# Plot results from the first model (all detections)
|
20 |
+
for r in results_all:
|
21 |
+
im_array = r.plot()
|
22 |
+
im = Image.fromarray(im_array[..., ::-1]) # RGB PIL image
|
23 |
+
|
24 |
+
# Initialize counters and confidence lists
|
25 |
+
class_counts_all = {i: 0 for i in range(len(model_all.names))}
|
26 |
+
class_confidences_all = {i: [] for i in range(len(model_all.names))}
|
27 |
+
np_count = 0
|
28 |
+
np_confidences = []
|
29 |
+
|
30 |
+
# Process results from the first model (all detections)
|
31 |
+
for box in results_all[0].boxes:
|
32 |
+
cls = int(box.cls[0])
|
33 |
+
conf = float(box.conf[0])
|
34 |
+
class_counts_all[cls] += 1
|
35 |
+
class_confidences_all[cls].append(conf)
|
36 |
+
|
37 |
+
# Process results from the second model (number plate detection)
|
38 |
+
for box in results_np[0].boxes:
|
39 |
+
np_count += 1
|
40 |
+
np_confidences.append(float(box.conf[0]))
|
41 |
+
|
42 |
+
# Create output string
|
43 |
+
output = "Detection Results:\n"
|
44 |
+
output += "Model 1 (Helmet, License Plate, Motorcyclist):\n"
|
45 |
+
for i in range(len(model_all.names)):
|
46 |
+
count = class_counts_all[i]
|
47 |
+
avg_conf = np.mean(class_confidences_all[i]) if class_confidences_all[i] else 0
|
48 |
+
output += f"{model_all.names[i]}: {count} detections (Avg. Confidence: {avg_conf:.2f})\n"
|
49 |
+
|
50 |
+
output += "\nModel 2 (Number Plate Detection):\n"
|
51 |
+
avg_np_conf = np.mean(np_confidences) if np_confidences else 0
|
52 |
+
output += f"Number Plates: {np_count} detections (Avg. Confidence: {avg_np_conf:.2f})\n"
|
53 |
+
|
54 |
+
return im, output
|
55 |
+
|
56 |
+
# Create Gradio interface
|
57 |
+
iface = gr.Interface(
|
58 |
+
fn=predict,
|
59 |
+
inputs=gr.Image(type="pil"),
|
60 |
+
outputs=[
|
61 |
+
gr.Image(type="pil", label="Detected Image"),
|
62 |
+
gr.Textbox(label="Detection Results")
|
63 |
+
],
|
64 |
+
title="Helmet, License Plate, and Motorcyclist Detection",
|
65 |
+
description="Upload an image to detect helmets, license plates, and motorcyclists using two specialized models."
|
66 |
+
)
|
67 |
+
|
68 |
+
# Launch the interface
|
69 |
+
iface.launch(share='True')
|