Update app.py
Browse files
app.py
CHANGED
|
@@ -7,9 +7,9 @@ from PIL import Image, ImageDraw
|
|
| 7 |
from transformers import YolosImageProcessor, YolosForObjectDetection
|
| 8 |
from datetime import datetime
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
# Load
|
| 12 |
-
#
|
| 13 |
processor = YolosImageProcessor.from_pretrained(
|
| 14 |
"nickmuchi/yolos-small-finetuned-license-plate-detection"
|
| 15 |
)
|
|
@@ -18,23 +18,24 @@ model = YolosForObjectDetection.from_pretrained(
|
|
| 18 |
)
|
| 19 |
model.eval()
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
#
|
| 23 |
-
#
|
| 24 |
conn = sqlite3.connect("vehicles.db", check_same_thread=False)
|
| 25 |
cursor = conn.cursor()
|
| 26 |
cursor.execute("""
|
| 27 |
CREATE TABLE IF NOT EXISTS vehicles (
|
| 28 |
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 29 |
time TEXT,
|
|
|
|
| 30 |
vehicle_type TEXT
|
| 31 |
)
|
| 32 |
""")
|
| 33 |
conn.commit()
|
| 34 |
|
| 35 |
-
#
|
| 36 |
# Plate Color Classifier
|
| 37 |
-
#
|
| 38 |
def classify_plate_color(plate_img):
|
| 39 |
img = np.array(plate_img)
|
| 40 |
hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
|
|
@@ -48,77 +49,23 @@ def classify_plate_color(plate_img):
|
|
| 48 |
return "Commercial"
|
| 49 |
return "Personal"
|
| 50 |
|
| 51 |
-
#
|
| 52 |
-
#
|
| 53 |
-
#
|
| 54 |
-
def
|
| 55 |
-
cursor.execute("
|
|
|
|
|
|
|
|
|
|
| 56 |
data = cursor.fetchall()
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
cv2.putText(
|
| 62 |
-
canvas,
|
| 63 |
-
f"{vtype}: {count}",
|
| 64 |
-
(40, y),
|
| 65 |
-
cv2.FONT_HERSHEY_SIMPLEX,
|
| 66 |
-
0.9,
|
| 67 |
-
(0,0,0),
|
| 68 |
-
2
|
| 69 |
-
)
|
| 70 |
-
y += 50
|
| 71 |
-
return canvas
|
| 72 |
-
|
| 73 |
-
# -------------------------------
|
| 74 |
-
# Main Pipeline
|
| 75 |
-
# -------------------------------
|
| 76 |
-
def process_image(img):
|
| 77 |
-
image = Image.fromarray(img)
|
| 78 |
-
draw = ImageDraw.Draw(image)
|
| 79 |
-
|
| 80 |
-
inputs = processor(images=image, return_tensors="pt")
|
| 81 |
-
with torch.no_grad():
|
| 82 |
-
outputs = model(**inputs)
|
| 83 |
-
|
| 84 |
-
target_sizes = torch.tensor([[image.size[1], image.size[0]]])
|
| 85 |
-
results = processor.post_process_object_detection(
|
| 86 |
-
outputs, threshold=0.4, target_sizes=target_sizes
|
| 87 |
-
)[0]
|
| 88 |
-
|
| 89 |
-
if len(results["boxes"]) == 0:
|
| 90 |
-
return image, "No Plate Detected", generate_dashboard()
|
| 91 |
-
|
| 92 |
-
x1, y1, x2, y2 = map(int, results["boxes"][0].tolist())
|
| 93 |
-
plate = image.crop((x1, y1, x2, y2))
|
| 94 |
-
|
| 95 |
-
vehicle_type = classify_plate_color(plate)
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
)
|
| 104 |
-
conn.commit()
|
| 105 |
-
|
| 106 |
-
return image, vehicle_type, generate_dashboard()
|
| 107 |
-
|
| 108 |
-
# -------------------------------
|
| 109 |
-
# Gradio UI
|
| 110 |
-
# -------------------------------
|
| 111 |
-
with gr.Blocks() as demo:
|
| 112 |
-
gr.Markdown("## 🚗 Vehicle Classification & Live Dashboard")
|
| 113 |
-
|
| 114 |
-
with gr.Row():
|
| 115 |
-
inp = gr.Image(type="numpy", sources=["upload", "webcam"])
|
| 116 |
-
out = gr.Image()
|
| 117 |
-
|
| 118 |
-
result = gr.Textbox(label="Vehicle Type")
|
| 119 |
-
dashboard = gr.Image(label="Live Dashboard")
|
| 120 |
-
|
| 121 |
-
btn = gr.Button("Detect Vehicle")
|
| 122 |
-
btn.click(process_image, inp, [out, result, dashboard])
|
| 123 |
-
|
| 124 |
-
demo.launch()
|
|
|
|
| 7 |
from transformers import YolosImageProcessor, YolosForObjectDetection
|
| 8 |
from datetime import datetime
|
| 9 |
|
| 10 |
+
# -----------------------------
|
| 11 |
+
# Load Model
|
| 12 |
+
# -----------------------------
|
| 13 |
processor = YolosImageProcessor.from_pretrained(
|
| 14 |
"nickmuchi/yolos-small-finetuned-license-plate-detection"
|
| 15 |
)
|
|
|
|
| 18 |
)
|
| 19 |
model.eval()
|
| 20 |
|
| 21 |
+
# -----------------------------
|
| 22 |
+
# Database
|
| 23 |
+
# -----------------------------
|
| 24 |
conn = sqlite3.connect("vehicles.db", check_same_thread=False)
|
| 25 |
cursor = conn.cursor()
|
| 26 |
cursor.execute("""
|
| 27 |
CREATE TABLE IF NOT EXISTS vehicles (
|
| 28 |
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 29 |
time TEXT,
|
| 30 |
+
plate_id TEXT,
|
| 31 |
vehicle_type TEXT
|
| 32 |
)
|
| 33 |
""")
|
| 34 |
conn.commit()
|
| 35 |
|
| 36 |
+
# -----------------------------
|
| 37 |
# Plate Color Classifier
|
| 38 |
+
# -----------------------------
|
| 39 |
def classify_plate_color(plate_img):
|
| 40 |
img = np.array(plate_img)
|
| 41 |
hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
|
|
|
|
| 49 |
return "Commercial"
|
| 50 |
return "Personal"
|
| 51 |
|
| 52 |
+
# -----------------------------
|
| 53 |
+
# Bar Chart (OpenCV)
|
| 54 |
+
# -----------------------------
|
| 55 |
+
def generate_bar_chart():
|
| 56 |
+
cursor.execute("""
|
| 57 |
+
SELECT vehicle_type, COUNT(*) FROM vehicles
|
| 58 |
+
GROUP BY vehicle_type
|
| 59 |
+
""")
|
| 60 |
data = cursor.fetchall()
|
| 61 |
|
| 62 |
+
chart = np.ones((300, 400, 3), dtype=np.uint8) * 255
|
| 63 |
+
cv2.putText(chart, "Vehicle Count Report", (60,30),
|
| 64 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0,0,0), 2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
+
max_count = max([c for _, c in data], default=1)
|
| 67 |
+
x = 70
|
| 68 |
|
| 69 |
+
for vtype, count in data:
|
| 70 |
+
bar_height = int((count / max_count) * 180)
|
| 71 |
+
cv2.rectangle(chart, (x,2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|