Spaces:
Sleeping
Sleeping
File size: 9,500 Bytes
a4acadb | 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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 | import os
os.environ['OMP_NUM_THREADS'] = '1'
import re
from datetime import datetime
import asyncio
import cv2
import gradio as gr
import numpy as np
import torch
from PIL import Image
from paddleocr import PaddleOCR
from ultralytics import YOLO
from transformers import AutoImageProcessor, AutoModelForImageClassification
from sqlalchemy import create_engine, text
from dotenv import load_dotenv
from huggingface_hub import InferenceClient
# Fix async warning
asyncio.set_event_loop_policy(asyncio.DefaultEventLoopPolicy())
# Load environment variables
load_dotenv()
DATABASE_URL = os.getenv("DATABASE_URL")
engine = create_engine(DATABASE_URL)
HF_TOKEN = os.getenv("HF_TOKEN")
client = InferenceClient(
model="defog/sqlcoder-7b-2",
token=HF_TOKEN
)
# ---------------- LOAD MODELS ---------------- #
# YOLO (plate detection)
try:
yolo_model = YOLO("license-plate-finetune-v1s.pt")
except Exception as e:
print(f"Warning: Failed to load YOLO model: {e}")
yolo_model = None
# OCR
ocr = PaddleOCR(use_angle_cls=True, lang="en", show_log=False)
# Vehicle classification
try:
processor = AutoImageProcessor.from_pretrained("dima806/vehicle_10_types_image_detection")
vehicle_model = AutoModelForImageClassification.from_pretrained("dima806/vehicle_10_types_image_detection")
device = "cuda" if torch.cuda.is_available() else "cpu"
vehicle_model.to(device)
vehicle_model.eval()
except Exception as e:
print(f"Warning: Failed to load vehicle classification model: {e}")
processor = None
vehicle_model = None
device = "cpu"
# Regex
plate_regex = re.compile(r"[A-Z]{2}\d{1,2}[A-Z]{1,3}\d{3,4}")
# Database initialization
def init_db():
with engine.connect() as conn:
conn.execute(text("""
CREATE TABLE IF NOT EXISTS vehicle_logs (
id SERIAL PRIMARY KEY,
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
plate TEXT,
vehicle_type TEXT,
vehicle_conf FLOAT,
date TEXT,
time TEXT
)
"""))
conn.commit()
init_db()
# ---------------- PREPROCESS ---------------- #
def preprocess_plate(crop):
gray = cv2.cvtColor(crop, cv2.COLOR_RGB2GRAY)
resized = cv2.resize(gray, (320, 96))
clahe = cv2.createCLAHE(2.0, (8, 8))
enhanced = clahe.apply(resized)
filtered = cv2.bilateralFilter(enhanced, 7, 50, 50)
kernel = np.array([[0,-1,0],[-1,5,-1],[0,-1,0]])
sharp = cv2.filter2D(filtered, -1, kernel)
return cv2.cvtColor(sharp, cv2.COLOR_GRAY2BGR)
# ---------------- AUGMENT ---------------- #
def build_crops(crop):
return [
crop,
cv2.resize(crop, None, fx=1.2, fy=1.2),
cv2.GaussianBlur(crop, (3,3), 0)
]
# ---------------- OCR ---------------- #
def run_ocr(image):
return ocr.ocr(image, cls=True)
def parse_ocr(ocr_out):
texts, confs = [], []
if not ocr_out:
return texts, confs
items = ocr_out[0] if isinstance(ocr_out[0], list) else ocr_out
for item in items:
try:
text, conf = item[1]
texts.append(text)
confs.append(float(conf))
except:
continue
return texts, confs
# ---------------- CLEAN ---------------- #
def clean_text(text):
return re.sub(r"[^A-Z0-9]", "", text.upper())
def fix_common(text):
return (
text.replace("O", "0")
.replace("I", "1")
.replace("B", "8")
.replace("Z", "2")
.replace("S", "5")
)
# ---------------- VEHICLE CLASSIFICATION ---------------- #
def classify_vehicle(image_np):
try:
image_pil = Image.fromarray(image_np)
inputs = processor(images=image_pil, return_tensors="pt")
inputs = {k: v.to(device) for k, v in inputs.items()}
with torch.no_grad():
outputs = vehicle_model(**inputs)
logits = outputs.logits
probs = torch.nn.functional.softmax(logits, dim=-1)
pred = probs.argmax(-1).item()
confidence = float(probs.max().item())
label = vehicle_model.config.id2label[pred]
return label, confidence
except Exception as e:
print("Vehicle classification error:", e)
return "unknown", 0.0
# ---------------- MAIN PIPELINE ---------------- #
def detect(image):
now = datetime.now()
date = now.strftime("%Y-%m-%d")
time = now.strftime("%H:%M:%S")
# ๐น Vehicle classification
vehicle_type, vehicle_conf = classify_vehicle(image)
# ๐น Plate detection
results = yolo_model(image)
boxes = results[0].boxes
if boxes is None or len(boxes) == 0:
return f"{date} {time} | {vehicle_type} |", {
"date": date,
"time": time,
"vehicle_type": vehicle_type,
"vehicle_confidence": round(vehicle_conf, 3),
"plate": ""
}
h, w = image.shape[:2]
xyxy = boxes.xyxy.cpu().numpy()
confs = boxes.conf.cpu().numpy()
collected = []
for i, (x1, y1, x2, y2) in enumerate(xyxy):
if confs[i] < 0.5:
continue
pad = int(0.12 * max(x2 - x1, y2 - y1))
l = max(int(x1 - pad), 0)
t = max(int(y1 - pad), 0)
r = min(int(x2 + pad), w - 1)
b = min(int(y2 + pad), h - 1)
crop = image[t:b, l:r]
for variant in build_crops(crop):
pre = preprocess_plate(variant)
ocr_out = run_ocr(pre)
texts, confs_ocr = parse_ocr(ocr_out)
for txt, cf in zip(texts, confs_ocr):
if cf < 0.3:
continue
norm = fix_common(clean_text(txt))
if len(norm) < 4:
continue
collected.append(norm)
if not collected:
plate = ""
else:
combined = "".join(collected)
match = plate_regex.search(combined)
plate = match.group(0) if match else combined
# SAVE TO DATABASE
try:
with engine.connect() as conn:
conn.execute(text("""
INSERT INTO vehicle_logs
(plate, vehicle_type, vehicle_conf, date, time)
VALUES (:plate, :vehicle_type, :vehicle_conf, :date, :time)
"""), {
"plate": plate,
"vehicle_type": vehicle_type,
"vehicle_conf": float(vehicle_conf),
"date": date,
"time": time
})
conn.commit()
except Exception as e:
print("Database insert error:", e)
# ๐น Final Output
return f"{date} {time} | {vehicle_type} | {plate}", {
"date": date,
"time": time,
"vehicle_type": vehicle_type,
"vehicle_confidence": round(vehicle_conf, 3),
"plate": plate
}
# ---------------- LLM SQL LAYER (SQLCoder) ---------------- #
def ask_llm(user_query):
schema = """
Table: vehicle_logs
Columns:
- id (SERIAL PRIMARY KEY)
- timestamp (TIMESTAMP)
- plate (TEXT) - License plate number
- vehicle_type (TEXT) - Type of vehicle
- vehicle_conf (FLOAT) - Detection confidence
- date (TEXT) - Date in YYYY-MM-DD format
- time (TEXT) - Time in HH:MM:SS format
"""
prompt = f"""### Task
Generate PostgreSQL SQL query for the following question.
### Rules
- Only SELECT queries allowed
- Use vehicle_logs table
- No markdown, no explanation
- Output SQL only
- Use appropriate WHERE clauses for filtering
- Use COUNT(*), SUM(), AVG() for aggregations if needed
- Order by timestamp DESC for chronological queries
### Schema
{schema}
### User Question
{user_query}
### SQL Query
"""
try:
response = client.text_generation(
prompt,
max_new_tokens=150,
temperature=0.1
)
sql_query = response.strip()
# Clean up markdown formatting if present
sql_query = sql_query.replace("```sql", "")
sql_query = sql_query.replace("```", "")
sql_query = sql_query.strip()
return sql_query
except Exception as e:
print(f"LLM error: {e}")
return f"SELECT * FROM vehicle_logs LIMIT 10; -- Error: {e}"
def run_query(user_query):
sql_query = ask_llm(user_query)
try:
with engine.connect() as conn:
result = conn.execute(text(sql_query))
rows = [dict(row._mapping) for row in result]
return {
"query": user_query,
"sql": sql_query,
"result": rows
}
except Exception as e:
return {
"error": str(e),
"sql": sql_query
}
# ---------------- UI ---------------- #
with gr.Blocks() as demo:
gr.Markdown("# ๐ Vehicle Intelligence System")
with gr.Tab("Detection"):
img = gr.Image(type="numpy")
out1 = gr.Textbox(label="Result")
out2 = gr.JSON(label="Structured Output")
btn = gr.Button("Detect")
btn.click(
fn=detect,
inputs=img,
outputs=[out1, out2]
)
with gr.Tab("Ask Database"):
query_input = gr.Textbox(
label="Ask Anything",
placeholder="How many cars today?"
)
query_output = gr.JSON()
ask_btn = gr.Button("Ask")
ask_btn.click(
fn=run_query,
inputs=query_input,
outputs=query_output
)
demo.launch(server_name="0.0.0.0", server_port=7860) |