Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import cv2, os, pandas as pd
|
| 4 |
+
from inference_utils import load_models, process_frame
|
| 5 |
+
|
| 6 |
+
vehicle_detector, vehicle_classifier, plate_detector, ocr_reader = load_models()
|
| 7 |
+
log_path = "vehicle_log.csv"
|
| 8 |
+
if not os.path.exists(log_path):
|
| 9 |
+
pd.DataFrame(columns=["timestamp", "vehicle_type", "plate_text"]).to_csv(log_path, index=False)
|
| 10 |
+
|
| 11 |
+
def run_pipeline(video):
|
| 12 |
+
cap = cv2.VideoCapture(video)
|
| 13 |
+
logs = []
|
| 14 |
+
|
| 15 |
+
while cap.isOpened():
|
| 16 |
+
ret, frame = cap.read()
|
| 17 |
+
if not ret: break
|
| 18 |
+
frame = cv2.resize(frame, (640, 480))
|
| 19 |
+
res = process_frame(frame, vehicle_detector, vehicle_classifier, plate_detector, ocr_reader)
|
| 20 |
+
logs.extend(res)
|
| 21 |
+
|
| 22 |
+
df = pd.DataFrame(logs, columns=["timestamp", "vehicle_type", "plate_text"])
|
| 23 |
+
df.to_csv(log_path, mode='a', header=False, index=False)
|
| 24 |
+
return f"Processed {len(logs)} vehicles. Log saved to {log_path}."
|
| 25 |
+
|
| 26 |
+
iface = gr.Interface(fn=run_pipeline,
|
| 27 |
+
inputs=gr.Video(label="Upload Video"),
|
| 28 |
+
outputs="text",
|
| 29 |
+
title="Vehicle ANPR Pipeline",
|
| 30 |
+
description="Uploads a video, detects vehicles, classifies type, reads plate and logs to CSV.")
|
| 31 |
+
|
| 32 |
+
iface.launch()
|