Sarvamangalak commited on
Commit
5ee84b0
·
verified ·
1 Parent(s): 412d849

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -106
app.py CHANGED
@@ -1,21 +1,15 @@
1
- # -----------------------------
2
- # Full Vehicle Detection & Dashboard
3
- # -----------------------------
4
  import torch
5
- from transformers import YolosImageProcessor, YolosForObjectDetection
6
- from PIL import Image, ImageDraw
7
- import easyocr
8
  import numpy as np
9
  import cv2
10
  import sqlite3
11
- import pandas as pd
12
- import matplotlib.pyplot as plt
13
- import gradio as gr
14
  from datetime import datetime
15
 
16
- # -----------------------------
17
- # Load Models
18
- # -----------------------------
19
  processor = YolosImageProcessor.from_pretrained(
20
  "nickmuchi/yolos-small-finetuned-license-plate-detection"
21
  )
@@ -24,128 +18,107 @@ model = YolosForObjectDetection.from_pretrained(
24
  )
25
  model.eval()
26
 
27
- reader = easyocr.Reader(['en'])
28
-
29
- # -----------------------------
30
- # Database Setup
31
- # -----------------------------
32
- conn = sqlite3.connect("vehicle_data.db", check_same_thread=False)
33
  cursor = conn.cursor()
34
- cursor.execute('''
35
  CREATE TABLE IF NOT EXISTS vehicles (
36
  id INTEGER PRIMARY KEY AUTOINCREMENT,
37
- timestamp TEXT,
38
- license_plate TEXT,
39
  vehicle_type TEXT
40
  )
41
- ''')
42
  conn.commit()
43
 
44
- # -----------------------------
45
  # Plate Color Classifier
46
- # -----------------------------
47
  def classify_plate_color(plate_img):
48
  img = np.array(plate_img)
49
  hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
50
- green = cv2.inRange(hsv, (35, 40, 40), (85, 255, 255))
51
- yellow = cv2.inRange(hsv, (15, 50, 50), (35, 255, 255))
52
- white = cv2.inRange(hsv, (0, 0, 200), (180, 30, 255))
53
- g = np.sum(green)
54
- y = np.sum(yellow)
55
- w = np.sum(white)
56
- if g > y and g > w:
57
  return "EV"
58
- elif y > g and y > w:
59
  return "Commercial"
60
- else:
61
- return "Personal"
62
-
63
- # -----------------------------
64
- # Process Image
65
- # -----------------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
  def process_image(img):
67
  image = Image.fromarray(img)
68
  draw = ImageDraw.Draw(image)
69
-
70
  inputs = processor(images=image, return_tensors="pt")
71
  with torch.no_grad():
72
  outputs = model(**inputs)
73
 
74
  target_sizes = torch.tensor([[image.size[1], image.size[0]]])
75
  results = processor.post_process_object_detection(
76
- outputs, threshold=0.3, target_sizes=target_sizes
77
  )[0]
78
 
79
  if len(results["boxes"]) == 0:
80
- return image, "No license plate detected", update_dashboard()
81
-
82
- # Assume first detected plate
83
- box = results["boxes"][0].tolist()
84
- x1, y1, x2, y2 = map(int, box)
85
- plate_crop = image.crop((x1, y1, x2, y2))
86
-
87
- # OCR
88
- ocr_result = reader.readtext(np.array(plate_crop))
89
- license_plate = ocr_result[0][1].replace(" ", "") if ocr_result else "UNKNOWN"
90
-
91
- # Vehicle Type
92
- vehicle_type = classify_plate_color(plate_crop)
93
-
94
- # Draw rectangle + label
95
- draw.rectangle([x1, y1, x2, y2], outline="yellow", width=3)
96
- draw.text((x1, y1 - 20), f"{vehicle_type} | {license_plate}", fill="black")
97
-
98
- # -----------------------------
99
- # Insert into Database
100
- # -----------------------------
101
- cursor.execute('''
102
- INSERT INTO vehicles (timestamp, license_plate, vehicle_type)
103
- VALUES (?, ?, ?)
104
- ''', (datetime.now().strftime("%Y-%m-%d %H:%M:%S"), license_plate, vehicle_type))
105
  conn.commit()
106
 
107
- # -----------------------------
108
- # Update Dashboard
109
- # -----------------------------
110
- return image, f"{vehicle_type} | {license_plate}", update_dashboard()
111
-
112
- # -----------------------------
113
- # Update Dashboard
114
- # -----------------------------
115
- def update_dashboard():
116
- df = pd.read_sql_query("SELECT * FROM vehicles", conn)
117
- if df.empty:
118
- return "No vehicles detected yet"
119
-
120
- counts = df['vehicle_type'].value_counts()
121
- plt.figure(figsize=(5,3))
122
- counts.plot(kind='bar', color=['green','yellow','blue'])
123
- plt.title("Vehicle Type Counts")
124
- plt.ylabel("Number of Vehicles")
125
- plt.xlabel("Type")
126
- plt.tight_layout()
127
-
128
- buf = np.zeros((1,1)) # placeholder
129
- plt.savefig("dashboard.png")
130
- plt.close()
131
-
132
- return "dashboard.png"
133
-
134
- # -----------------------------
135
- # Gradio Interface
136
- # -----------------------------
137
  with gr.Blocks() as demo:
138
- gr.Markdown("# 🚗 Smart Vehicle Classification & EV Dashboard")
139
- gr.Markdown("Upload or use webcam to detect vehicles. System logs each vehicle, reads license plate, classifies type, and updates dashboard.")
140
-
141
  with gr.Row():
142
- input_img = gr.Image(type="numpy", source="upload")
143
- output_img = gr.Image()
144
-
145
- result_text = gr.Textbox(label="Detected Vehicle")
146
- dashboard_img = gr.Image(label="Dashboard")
147
-
148
- detect_btn = gr.Button("Detect Vehicle")
149
- detect_btn.click(process_image, inputs=input_img, outputs=[output_img, result_text, dashboard_img])
150
 
151
  demo.launch()
 
1
+ import gradio as gr
 
 
2
  import torch
 
 
 
3
  import numpy as np
4
  import cv2
5
  import sqlite3
6
+ from PIL import Image, ImageDraw
7
+ from transformers import YolosImageProcessor, YolosForObjectDetection
 
8
  from datetime import datetime
9
 
10
+ # -------------------------------
11
+ # Load YOLOS (lightweight)
12
+ # -------------------------------
13
  processor = YolosImageProcessor.from_pretrained(
14
  "nickmuchi/yolos-small-finetuned-license-plate-detection"
15
  )
 
18
  )
19
  model.eval()
20
 
21
+ # -------------------------------
22
+ # SQLite (SAFE)
23
+ # -------------------------------
24
+ conn = sqlite3.connect("vehicles.db", check_same_thread=False)
 
 
25
  cursor = conn.cursor()
26
+ cursor.execute("""
27
  CREATE TABLE IF NOT EXISTS vehicles (
28
  id INTEGER PRIMARY KEY AUTOINCREMENT,
29
+ time TEXT,
 
30
  vehicle_type TEXT
31
  )
32
+ """)
33
  conn.commit()
34
 
35
+ # -------------------------------
36
  # Plate Color Classifier
37
+ # -------------------------------
38
  def classify_plate_color(plate_img):
39
  img = np.array(plate_img)
40
  hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
41
+
42
+ green = cv2.inRange(hsv, (35,40,40), (85,255,255))
43
+ yellow = cv2.inRange(hsv, (15,50,50), (35,255,255))
44
+
45
+ if np.sum(green) > np.sum(yellow):
 
 
46
  return "EV"
47
+ elif np.sum(yellow) > 0:
48
  return "Commercial"
49
+ return "Personal"
50
+
51
+ # -------------------------------
52
+ # Dashboard (NO matplotlib)
53
+ # -------------------------------
54
+ def generate_dashboard():
55
+ cursor.execute("SELECT vehicle_type, COUNT(*) FROM vehicles GROUP BY vehicle_type")
56
+ data = cursor.fetchall()
57
+
58
+ canvas = np.ones((250, 400, 3), dtype=np.uint8) * 255
59
+ y = 40
60
+ for vtype, count in data:
61
+ cv2.putText(
62
+ canvas,
63
+ f"{vtype}: {count}",
64
+ (40, y),
65
+ cv2.FONT_HERSHEY_SIMPLEX,
66
+ 0.9,
67
+ (0,0,0),
68
+ 2
69
+ )
70
+ y += 50
71
+ return canvas
72
+
73
+ # -------------------------------
74
+ # Main Pipeline
75
+ # -------------------------------
76
  def process_image(img):
77
  image = Image.fromarray(img)
78
  draw = ImageDraw.Draw(image)
79
+
80
  inputs = processor(images=image, return_tensors="pt")
81
  with torch.no_grad():
82
  outputs = model(**inputs)
83
 
84
  target_sizes = torch.tensor([[image.size[1], image.size[0]]])
85
  results = processor.post_process_object_detection(
86
+ outputs, threshold=0.4, target_sizes=target_sizes
87
  )[0]
88
 
89
  if len(results["boxes"]) == 0:
90
+ return image, "No Plate Detected", generate_dashboard()
91
+
92
+ x1, y1, x2, y2 = map(int, results["boxes"][0].tolist())
93
+ plate = image.crop((x1, y1, x2, y2))
94
+
95
+ vehicle_type = classify_plate_color(plate)
96
+
97
+ draw.rectangle([x1,y1,x2,y2], outline="green", width=3)
98
+ draw.text((x1,y1-10), vehicle_type, fill="black")
99
+
100
+ cursor.execute(
101
+ "INSERT INTO vehicles (time, vehicle_type) VALUES (?,?)",
102
+ (datetime.now().isoformat(), vehicle_type)
103
+ )
 
 
 
 
 
 
 
 
 
 
 
104
  conn.commit()
105
 
106
+ return image, vehicle_type, generate_dashboard()
107
+
108
+ # -------------------------------
109
+ # Gradio UI
110
+ # -------------------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
  with gr.Blocks() as demo:
112
+ gr.Markdown("## 🚗 Vehicle Classification & Live Dashboard")
113
+
 
114
  with gr.Row():
115
+ inp = gr.Image(type="numpy", sources=["upload", "webcam"])
116
+ out = gr.Image()
117
+
118
+ result = gr.Textbox(label="Vehicle Type")
119
+ dashboard = gr.Image(label="Live Dashboard")
120
+
121
+ btn = gr.Button("Detect Vehicle")
122
+ btn.click(process_image, inp, [out, result, dashboard])
123
 
124
  demo.launch()