Update app.py
Browse files
app.py
CHANGED
|
@@ -1,137 +1,146 @@
|
|
| 1 |
-
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
-
import
|
| 6 |
-
import
|
| 7 |
-
from ultralytics import YOLO
|
| 8 |
-
from
|
| 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 |
-
with gr.Column():
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import os
|
| 3 |
+
import cv2
|
| 4 |
+
import json
|
| 5 |
+
import tempfile
|
| 6 |
+
import numpy as np
|
| 7 |
+
from ultralytics import YOLO
|
| 8 |
+
from paddleocr import PaddleOCR
|
| 9 |
+
import gradio as gr
|
| 10 |
+
from pathlib import Path
|
| 11 |
+
from datetime import datetime
|
| 12 |
+
|
| 13 |
+
# βββ 1) Load models βββββββββββββββββββββββββββββββββββββββββββββββ
|
| 14 |
+
yolo_model = YOLO("models/best.pt")
|
| 15 |
+
ocr_model = PaddleOCR(
|
| 16 |
+
det_model_dir=None, # we only use recognition
|
| 17 |
+
rec_model_dir="models/best_plate_model.pdparams", # your fine-tuned OCR weights
|
| 18 |
+
use_angle_cls=True,
|
| 19 |
+
use_space_char=True
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
# βββ 2) Plate formatting helper ββββββββββββββββββββββββββββββββββββ
|
| 23 |
+
import re
|
| 24 |
+
def format_turkish_plate(plate: str) -> str:
|
| 25 |
+
m = re.match(r"^(\d{2})([A-Z]{1,3})(\d{2,4})$", plate.replace(" ",""))
|
| 26 |
+
if m:
|
| 27 |
+
return f"{m.group(1)} {m.group(2)} {m.group(3)}"
|
| 28 |
+
return "Unknown"
|
| 29 |
+
|
| 30 |
+
# βββ 3) Singleβimage inference βββββββββββββββββββββββββββββββββββββ
|
| 31 |
+
def process_image(img_np, conf_thresh=0.25):
|
| 32 |
+
"""
|
| 33 |
+
Detect plates, OCR them, draw boxes & return annotated image + text list.
|
| 34 |
+
"""
|
| 35 |
+
# BGR β RGB
|
| 36 |
+
img_bgr = cv2.cvtColor(img_np, cv2.COLOR_RGB2BGR)
|
| 37 |
+
# YOLO detection
|
| 38 |
+
res = yolo_model(img_bgr, iou=0.3, conf=conf_thresh)[0]
|
| 39 |
+
boxes = res.boxes.xyxy.cpu().numpy()
|
| 40 |
+
scores = res.boxes.conf.cpu().numpy()
|
| 41 |
+
|
| 42 |
+
annotated = img_bgr.copy()
|
| 43 |
+
texts = []
|
| 44 |
+
for (x1,y1,x2,y2), conf in zip(boxes, scores):
|
| 45 |
+
x1,y1,x2,y2 = map(int,(x1,y1,x2,y2))
|
| 46 |
+
crop = annotated[y1:y2, x1:x2]
|
| 47 |
+
if crop.size==0: continue
|
| 48 |
+
|
| 49 |
+
# OCR
|
| 50 |
+
plate = cv2.resize(crop, (128,32))
|
| 51 |
+
rec = ocr_model.ocr(plate, cls=True)[0]
|
| 52 |
+
txt = "".join(seg[1][0] for seg in rec)
|
| 53 |
+
formatted = format_turkish_plate(txt)
|
| 54 |
+
texts.append((formatted, float(min(seg[1][1] for seg in rec))))
|
| 55 |
+
|
| 56 |
+
# draw
|
| 57 |
+
label = f"{formatted} ({conf:.2f})"
|
| 58 |
+
cv2.rectangle(annotated,(x1,y1),(x2,y2),(0,255,0),2)
|
| 59 |
+
cv2.putText(annotated,label,(x1,y1-5),
|
| 60 |
+
cv2.FONT_HERSHEY_SIMPLEX,0.7,(0,255,0),2)
|
| 61 |
+
|
| 62 |
+
# back to RGB
|
| 63 |
+
annotated = cv2.cvtColor(annotated, cv2.COLOR_BGR2RGB)
|
| 64 |
+
status = f"{len(texts)} plate(s) detected"
|
| 65 |
+
return annotated, status
|
| 66 |
+
|
| 67 |
+
# βββ 4) Video inference βββββββββββββββββββββββββββββββββββββββββββ
|
| 68 |
+
def process_video(video_file, conf_thresh=0.25):
|
| 69 |
+
"""
|
| 70 |
+
Runs the same pipeline frame by frame, writes output.json, and
|
| 71 |
+
returns path to an annotated video file for playback.
|
| 72 |
+
"""
|
| 73 |
+
cap = cv2.VideoCapture(video_file)
|
| 74 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 75 |
+
w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 76 |
+
h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 77 |
+
|
| 78 |
+
tmp_out = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False).name
|
| 79 |
+
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
|
| 80 |
+
writer = cv2.VideoWriter(tmp_out, fourcc, fps, (w,h))
|
| 81 |
+
|
| 82 |
+
json_out = []
|
| 83 |
+
frame_i = 0
|
| 84 |
+
while True:
|
| 85 |
+
ret, frame = cap.read()
|
| 86 |
+
if not ret: break
|
| 87 |
+
frame_i += 1
|
| 88 |
+
t = frame_i / fps
|
| 89 |
+
|
| 90 |
+
# detect & OCR
|
| 91 |
+
res = yolo_model(frame, iou=0.3, conf=conf_thresh)[0]
|
| 92 |
+
boxes = res.boxes.xyxy.cpu().numpy()
|
| 93 |
+
for (x1,y1,x2,y2) in boxes:
|
| 94 |
+
x1,y1,x2,y2 = map(int,(x1,y1,x2,y2))
|
| 95 |
+
crop = frame[y1:y2, x1:x2]
|
| 96 |
+
if crop.size==0: continue
|
| 97 |
+
|
| 98 |
+
plate = cv2.resize(crop,(128,32))
|
| 99 |
+
rec = ocr_model.ocr(plate, cls=True)[0]
|
| 100 |
+
txt = "".join(seg[1][0] for seg in rec)
|
| 101 |
+
formatted = format_turkish_plate(txt)
|
| 102 |
+
conf_score = min(seg[1][1] for seg in rec)
|
| 103 |
+
|
| 104 |
+
if formatted!="Unknown":
|
| 105 |
+
json_out.append({
|
| 106 |
+
"time_s": round(t,2),
|
| 107 |
+
"plate": formatted,
|
| 108 |
+
"conf": round(conf_score,3)
|
| 109 |
+
})
|
| 110 |
+
|
| 111 |
+
cv2.rectangle(frame,(x1,y1),(x2,y2),(0,255,0),2)
|
| 112 |
+
cv2.putText(frame,formatted,(x1,y1-5),
|
| 113 |
+
cv2.FONT_HERSHEY_SIMPLEX,0.7,(0,255,0),2)
|
| 114 |
+
|
| 115 |
+
writer.write(frame)
|
| 116 |
+
|
| 117 |
+
cap.release()
|
| 118 |
+
writer.release()
|
| 119 |
+
|
| 120 |
+
# save JSON
|
| 121 |
+
with open("output.json","w") as jf:
|
| 122 |
+
json.dump(json_out, jf, indent=2)
|
| 123 |
+
|
| 124 |
+
return tmp_out
|
| 125 |
+
|
| 126 |
+
# βββ 5) Gradio UI ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 127 |
+
with gr.Blocks() as demo:
|
| 128 |
+
gr.Markdown("## π Plate Detection + Recognition")
|
| 129 |
+
with gr.Row():
|
| 130 |
+
with gr.Column():
|
| 131 |
+
inp_img = gr.Image(type="numpy", label="Upload Image")
|
| 132 |
+
inp_vid = gr.File(label="Upload Video (.mp4)")
|
| 133 |
+
conf = gr.Slider(0,1,0.25,0.01, label="YOLO Confidence")
|
| 134 |
+
btn_img = gr.Button("Run Image")
|
| 135 |
+
btn_vid = gr.Button("Run Video")
|
| 136 |
+
with gr.Column():
|
| 137 |
+
out_img = gr.Image(type="numpy", label="Annotated Image")
|
| 138 |
+
out_vid = gr.Video(label="Annotated Video")
|
| 139 |
+
out_txt = gr.Textbox(label="Status / JSON Path")
|
| 140 |
+
|
| 141 |
+
btn_img.click(process_image, [inp_img, conf], [out_img, out_txt])
|
| 142 |
+
btn_vid.click(process_video, [inp_vid, conf], [out_vid, out_txt])
|
| 143 |
+
|
| 144 |
+
if __name__ == "__main__":
|
| 145 |
+
demo.launch()
|
| 146 |
+
|