File size: 1,931 Bytes
5ee84b0
412d849
 
 
 
5ee84b0
 
412d849
 
5fce661
 
 
412d849
 
 
 
 
 
 
 
5fce661
 
 
5ee84b0
412d849
5ee84b0
412d849
 
5ee84b0
5fce661
412d849
 
5ee84b0
412d849
 
5fce661
412d849
5fce661
412d849
 
 
5ee84b0
 
 
 
 
412d849
5ee84b0
412d849
5ee84b0
 
5fce661
 
 
 
 
 
 
 
5ee84b0
 
5fce661
 
 
5ee84b0
5fce661
 
5ee84b0
5fce661
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import gradio as gr
import torch
import numpy as np
import cv2
import sqlite3
from PIL import Image, ImageDraw
from transformers import YolosImageProcessor, YolosForObjectDetection
from datetime import datetime

# -----------------------------
# Load Model
# -----------------------------
processor = YolosImageProcessor.from_pretrained(
    "nickmuchi/yolos-small-finetuned-license-plate-detection"
)
model = YolosForObjectDetection.from_pretrained(
    "nickmuchi/yolos-small-finetuned-license-plate-detection"
)
model.eval()

# -----------------------------
# Database
# -----------------------------
conn = sqlite3.connect("vehicles.db", check_same_thread=False)
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS vehicles (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    time TEXT,
    plate_id TEXT,
    vehicle_type TEXT
)
""")
conn.commit()

# -----------------------------
# Plate Color Classifier
# -----------------------------
def classify_plate_color(plate_img):
    img = np.array(plate_img)
    hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)

    green = cv2.inRange(hsv, (35,40,40), (85,255,255))
    yellow = cv2.inRange(hsv, (15,50,50), (35,255,255))

    if np.sum(green) > np.sum(yellow):
        return "EV"
    elif np.sum(yellow) > 0:
        return "Commercial"
    return "Personal"

# -----------------------------
# Bar Chart (OpenCV)
# -----------------------------
def generate_bar_chart():
    cursor.execute("""
        SELECT vehicle_type, COUNT(*) FROM vehicles
        GROUP BY vehicle_type
    """)
    data = cursor.fetchall()

    chart = np.ones((300, 400, 3), dtype=np.uint8) * 255
    cv2.putText(chart, "Vehicle Count Report", (60,30),
                cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0,0,0), 2)

    max_count = max([c for _, c in data], default=1)
    x = 70

    for vtype, count in data:
        bar_height = int((count / max_count) * 180)
        cv2.rectangle(chart, (x,2