Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import numpy as np
|
| 4 |
+
import cv2
|
| 5 |
+
import sqlite3
|
| 6 |
+
import pandas as pd
|
| 7 |
+
from PIL import Image, ImageDraw
|
| 8 |
+
from transformers import YolosImageProcessor, YolosForObjectDetection
|
| 9 |
+
import easyocr
|
| 10 |
+
from datetime import datetime
|
| 11 |
+
|
| 12 |
+
# -------- Database --------
|
| 13 |
+
conn = sqlite3.connect("vehicles.db", check_same_thread=False)
|
| 14 |
+
cursor = conn.cursor()
|
| 15 |
+
cursor.execute("""
|
| 16 |
+
CREATE TABLE IF NOT EXISTS vehicles(
|
| 17 |
+
plate TEXT,
|
| 18 |
+
type TEXT,
|
| 19 |
+
time TEXT
|
| 20 |
+
)
|
| 21 |
+
""")
|
| 22 |
+
conn.commit()
|
| 23 |
+
|
| 24 |
+
# -------- Models --------
|
| 25 |
+
processor = YolosImageProcessor.from_pretrained("nickmuchi/yolos-small-finetuned-license-plate-detection")
|
| 26 |
+
model = YolosForObjectDetection.from_pretrained("nickmuchi/yolos-small-finetuned-license-plate-detection")
|
| 27 |
+
model.eval()
|
| 28 |
+
reader = easyocr.Reader(['en'])
|
| 29 |
+
|
| 30 |
+
# -------- Plate Color --------
|
| 31 |
+
def classify_plate_color(plate):
|
| 32 |
+
img = np.array(plate)
|
| 33 |
+
hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
|
| 34 |
+
|
| 35 |
+
green = np.sum(cv2.inRange(hsv, (35,40,40),(85,255,255)))
|
| 36 |
+
yellow = np.sum(cv2.inRange(hsv,(15,50,50),(35,255,255)))
|
| 37 |
+
white = np.sum(cv2.inRange(hsv,(0,0,200),(180,30,255)))
|
| 38 |
+
|
| 39 |
+
if green > yellow and green > white:
|
| 40 |
+
return "EV"
|
| 41 |
+
elif yellow > green and yellow > white:
|
| 42 |
+
return "Commercial"
|
| 43 |
+
else:
|
| 44 |
+
return "Personal"
|
| 45 |
+
|
| 46 |
+
# -------- OCR --------
|
| 47 |
+
def read_plate(img):
|
| 48 |
+
results = reader.readtext(np.array(img))
|
| 49 |
+
if results:
|
| 50 |
+
return results[0][1]
|
| 51 |
+
return "UNKNOWN"
|
| 52 |
+
|
| 53 |
+
# -------- Dashboard --------
|
| 54 |
+
def get_stats():
|
| 55 |
+
df = pd.read_sql("SELECT * FROM vehicles", conn)
|
| 56 |
+
if len(df)==0:
|
| 57 |
+
return "No vehicles yet"
|
| 58 |
+
return df['type'].value_counts().to_string()
|
| 59 |
+
|
| 60 |
+
# -------- Pipeline --------
|
| 61 |
+
def process_image(img):
|
| 62 |
+
image = Image.fromarray(img)
|
| 63 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 64 |
+
with torch.no_grad():
|
| 65 |
+
outputs = model(**inputs)
|
| 66 |
+
|
| 67 |
+
results = processor.post_process_object_detection(
|
| 68 |
+
outputs, threshold=0.3,
|
| 69 |
+
target_sizes=torch.tensor([[image.size[1], image.size[0]]])
|
| 70 |
+
)[0]
|
| 71 |
+
|
| 72 |
+
draw = ImageDraw.Draw(image)
|
| 73 |
+
|
| 74 |
+
if len(results["boxes"])==0:
|
| 75 |
+
return image,"No Plate","",get_stats()
|
| 76 |
+
|
| 77 |
+
box = results["boxes"][0].tolist()
|
| 78 |
+
x1,y1,x2,y2 = map(int,box)
|
| 79 |
+
|
| 80 |
+
plate = image.crop((x1,y1,x2,y2))
|
| 81 |
+
plate_text = read_plate(plate)
|
| 82 |
+
vtype = classify_plate_color(plate)
|
| 83 |
+
|
| 84 |
+
cursor.execute("INSERT INTO vehicles VALUES (?,?,?)",
|
| 85 |
+
(plate_text,vtype,datetime.now().strftime("%H:%M:%S")))
|
| 86 |
+
conn.commit()
|
| 87 |
+
|
| 88 |
+
draw.rectangle([x1,y1,x2,y2],outline="red",width=3)
|
| 89 |
+
draw.text((x1,y1-10),f"{plate_text} | {vtype}",fill="red")
|
| 90 |
+
|
| 91 |
+
return image, plate_text, vtype, get_stats()
|
| 92 |
+
|
| 93 |
+
# -------- UI --------
|
| 94 |
+
with gr.Blocks() as demo:
|
| 95 |
+
gr.Markdown("# 🚗 Smart Traffic & EV Detection System")
|
| 96 |
+
|
| 97 |
+
img = gr.Image(type="numpy", sources=["upload","webcam"])
|
| 98 |
+
out_img = gr.Image()
|
| 99 |
+
plate = gr.Textbox(label="Number Plate")
|
| 100 |
+
vtype = gr.Textbox(label="Vehicle Type")
|
| 101 |
+
stats = gr.Textbox(label="Dashboard")
|
| 102 |
+
|
| 103 |
+
btn = gr.Button("Scan Vehicle")
|
| 104 |
+
btn.click(process_image, img, [out_img,plate,vtype,stats])
|
| 105 |
+
|
| 106 |
+
demo.launch()
|