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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 |
+
)
|
| 22 |
+
model = YolosForObjectDetection.from_pretrained(
|
| 23 |
+
"nickmuchi/yolos-small-finetuned-license-plate-detection"
|
| 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()
|