jainvrushab commited on
Commit
f35bee1
·
verified ·
1 Parent(s): 7d9837c

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +54 -0
  2. best.pt +3 -0
  3. requirements.txt +5 -0
app.py ADDED
@@ -0,0 +1,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 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()
best.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e339c22b8c597e5702dd82873b68147fc7e5cf94f0c3ee865901f62686b3e9c8
3
+ size 5591279
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ streamlit
2
+ ultralytics
3
+ Pillow
4
+ numpy
5
+ opencv-python-headless