jainvrushab commited on
Commit
3cc129a
·
verified ·
1 Parent(s): dd30dd6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -54
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 model
8
- model = YOLO('best.pt')
9
-
10
- def predict(image):
11
- # Convert PIL Image to numpy array
12
- img_array = np.array(image)
13
-
14
- # Perform inference
15
- results = model(img_array)
16
-
17
- # Plot results
18
- for r in results:
19
- im_array = r.plot()
20
- im = Image.fromarray(im_array[..., ::-1]) # RGB PIL image
21
-
22
- # Get detection counts and confidences
23
- class_counts = {i: 0 for i in range(len(model.names))}
24
- class_confidences = {i: [] for i in range(len(model.names))}
25
-
26
- for box in results[0].boxes:
27
- cls = int(box.cls[0])
28
- conf = float(box.conf[0])
29
- class_counts[cls] += 1
30
- class_confidences[cls].append(conf)
31
-
32
- # Create output string
33
- output = "Detection Results:\n"
34
- for i in range(len(model.names)):
35
- count = class_counts[i]
36
- avg_conf = np.mean(class_confidences[i]) if class_confidences[i] else 0
37
- output += f"{model.names[i]}: {count} detections (Avg. Confidence: {avg_conf:.2f})\n"
38
-
39
- return im, output
40
-
41
- # Create Gradio interface
42
- iface = gr.Interface(
43
- fn=predict,
44
- inputs=gr.Image(type="pil"),
45
- outputs=[
46
- gr.Image(type="pil", label="Detected Image"),
47
- gr.Textbox(label="Detection Results")
48
- ],
49
- title="Helmet and License Plate Detection",
50
- description="Upload an image to detect helmets, license plates, and motorcyclists."
51
- )
52
-
53
- # Launch the interface
54
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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')