Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,39 +1,29 @@
|
|
| 1 |
-
# app.py
|
| 2 |
-
|
| 3 |
import numpy as np
|
| 4 |
-
np.int = int #
|
| 5 |
|
| 6 |
-
import cv2
|
| 7 |
-
import json
|
| 8 |
-
import tempfile
|
| 9 |
-
import re
|
| 10 |
-
import gradio as gr
|
| 11 |
from ultralytics import YOLO
|
| 12 |
from paddleocr import PaddleOCR
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
# βββ 1) Load models βββββββββββββββββββββββββββββββββββββββββββββββ
|
| 17 |
yolo = YOLO("models/best.pt")
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
rec=True, # recognition only
|
| 22 |
rec_model_dir="models/ocr_model",
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
| 26 |
)
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
def format_turkish_plate(s: str) -> str:
|
| 31 |
s = re.sub(r'[^A-Z0-9]', '', s.upper())
|
| 32 |
m = re.match(r'^(\d{2})([A-Z]{1,3})(\d{2,4})$', s)
|
| 33 |
return f"{m.group(1)} {m.group(2)} {m.group(3)}" if m else "Unknown"
|
| 34 |
|
| 35 |
-
|
| 36 |
-
# βββ 3) Singleβimage inference βββββββββββββββββββββββββββββββββββββ
|
| 37 |
def run_image(img, conf=0.25):
|
| 38 |
bgr = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
| 39 |
res = yolo(bgr, conf=conf)[0]
|
|
@@ -41,102 +31,81 @@ def run_image(img, conf=0.25):
|
|
| 41 |
|
| 42 |
for box, score in zip(res.boxes.xyxy.cpu().numpy(),
|
| 43 |
res.boxes.conf.cpu().numpy()):
|
| 44 |
-
x1,
|
| 45 |
crop = out[y1:y2, x1:x2]
|
| 46 |
-
if crop.size == 0:
|
| 47 |
-
continue
|
| 48 |
|
| 49 |
plate_img = cv2.resize(crop, (128, 32))
|
| 50 |
-
|
| 51 |
-
# <-- ALWAYS pass det=False here
|
| 52 |
try:
|
| 53 |
recs = ocr.ocr(plate_img, det=False, cls=True)
|
| 54 |
if recs and recs[0]:
|
| 55 |
-
|
| 56 |
else:
|
| 57 |
-
|
| 58 |
-
except
|
| 59 |
-
|
| 60 |
|
| 61 |
-
plate = format_turkish_plate(
|
| 62 |
label = f"{plate} ({ocr_score:.2f})"
|
| 63 |
|
| 64 |
-
cv2.rectangle(out, (x1,
|
| 65 |
-
cv2.putText(out, label, (x1,
|
| 66 |
-
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,
|
| 67 |
|
| 68 |
return cv2.cvtColor(out, cv2.COLOR_BGR2RGB), f"{len(res.boxes)} plate(s) detected"
|
| 69 |
|
| 70 |
-
|
| 71 |
-
# βββ 4) Video inference βββββββββββββββββββββββββββββββββββββββββββ
|
| 72 |
def run_video(video_file, conf=0.25):
|
| 73 |
cap = cv2.VideoCapture(video_file)
|
| 74 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 75 |
-
w
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
writer = cv2.VideoWriter(tmp_out, cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h))
|
| 80 |
-
records = []
|
| 81 |
-
idx = 0
|
| 82 |
|
| 83 |
while True:
|
| 84 |
ret, frame = cap.read()
|
| 85 |
-
if not ret:
|
| 86 |
-
|
| 87 |
-
idx += 1
|
| 88 |
-
t = idx / fps
|
| 89 |
|
| 90 |
res = yolo(frame, conf=conf)[0]
|
| 91 |
-
for (x1,
|
| 92 |
crop = frame[y1:y2, x1:x2]
|
| 93 |
-
if crop.size
|
| 94 |
-
continue
|
| 95 |
|
| 96 |
-
plate_img = cv2.resize(crop, (128,
|
| 97 |
-
|
| 98 |
-
# <-- ALWAYS pass det=False here
|
| 99 |
try:
|
| 100 |
recs = ocr.ocr(plate_img, det=False, cls=True)
|
| 101 |
if recs and recs[0]:
|
| 102 |
-
|
| 103 |
else:
|
| 104 |
-
|
| 105 |
-
except
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
plate = format_turkish_plate(raw_text)
|
| 109 |
-
if plate != "Unknown":
|
| 110 |
-
records.append({
|
| 111 |
-
"time_s": round(t, 2),
|
| 112 |
-
"plate" : plate,
|
| 113 |
-
"conf" : round(ocr_score, 3)
|
| 114 |
-
})
|
| 115 |
-
|
| 116 |
-
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
| 117 |
-
cv2.putText(frame, plate, (x1, y1 - 5),
|
| 118 |
-
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
|
| 119 |
|
| 120 |
-
|
|
|
|
|
|
|
| 121 |
|
| 122 |
-
|
| 123 |
-
|
|
|
|
| 124 |
|
| 125 |
-
|
| 126 |
-
json.dump(records, f, indent=2)
|
| 127 |
-
|
| 128 |
-
return tmp_out, "Done"
|
| 129 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
-
#
|
| 132 |
with gr.Blocks() as demo:
|
| 133 |
gr.Markdown("## π License Plate Detection & Recognition")
|
| 134 |
-
|
| 135 |
with gr.Row():
|
| 136 |
with gr.Column():
|
| 137 |
img_in = gr.Image(type="numpy", label="Upload Image")
|
| 138 |
vid_in = gr.File(label="Upload Video (.mp4)")
|
| 139 |
-
conf = gr.Slider(0,
|
| 140 |
btn_i = gr.Button("Run Image")
|
| 141 |
btn_v = gr.Button("Run Video")
|
| 142 |
with gr.Column():
|
|
@@ -144,8 +113,8 @@ with gr.Blocks() as demo:
|
|
| 144 |
vid_out = gr.Video(label="Annotated Video")
|
| 145 |
status = gr.Textbox(label="Status / JSON Path")
|
| 146 |
|
| 147 |
-
btn_i.click(run_image, [img_in,
|
| 148 |
-
btn_v.click(run_video, [vid_in,
|
| 149 |
|
| 150 |
-
if __name__
|
| 151 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
| 1 |
import numpy as np
|
| 2 |
+
np.int = int # PaddleOCRβnin eski np.int kullanΔ±m yamasΔ±
|
| 3 |
|
| 4 |
+
import cv2, json, tempfile, re, gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
from ultralytics import YOLO
|
| 6 |
from paddleocr import PaddleOCR
|
| 7 |
|
| 8 |
+
# 1) Modelleri yΓΌkle
|
|
|
|
|
|
|
| 9 |
yolo = YOLO("models/best.pt")
|
| 10 |
+
ocr = PaddleOCR(
|
| 11 |
+
det=False, # tespit yok
|
| 12 |
+
rec=True, # sadece okuma
|
|
|
|
| 13 |
rec_model_dir="models/ocr_model",
|
| 14 |
+
rec_image_shape="3,32,128", # inference.yml ile uyumlu
|
| 15 |
+
cls=True, # aΓ§Δ± dΓΌzeltmesi
|
| 16 |
+
use_angle_cls=True,
|
| 17 |
+
use_space_char=True
|
| 18 |
)
|
| 19 |
|
| 20 |
+
# 2) Plaka formatlayΔ±cΔ±
|
| 21 |
+
def format_turkish_plate(s):
|
|
|
|
| 22 |
s = re.sub(r'[^A-Z0-9]', '', s.upper())
|
| 23 |
m = re.match(r'^(\d{2})([A-Z]{1,3})(\d{2,4})$', s)
|
| 24 |
return f"{m.group(1)} {m.group(2)} {m.group(3)}" if m else "Unknown"
|
| 25 |
|
| 26 |
+
# 3) GΓΆrΓΌntΓΌ iΓ§in iΕlem
|
|
|
|
| 27 |
def run_image(img, conf=0.25):
|
| 28 |
bgr = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
| 29 |
res = yolo(bgr, conf=conf)[0]
|
|
|
|
| 31 |
|
| 32 |
for box, score in zip(res.boxes.xyxy.cpu().numpy(),
|
| 33 |
res.boxes.conf.cpu().numpy()):
|
| 34 |
+
x1,y1,x2,y2 = box.astype(int)
|
| 35 |
crop = out[y1:y2, x1:x2]
|
| 36 |
+
if crop.size == 0: continue
|
|
|
|
| 37 |
|
| 38 |
plate_img = cv2.resize(crop, (128, 32))
|
|
|
|
|
|
|
| 39 |
try:
|
| 40 |
recs = ocr.ocr(plate_img, det=False, cls=True)
|
| 41 |
if recs and recs[0]:
|
| 42 |
+
raw,ocr_score = recs[0][1][0], recs[0][1][1]
|
| 43 |
else:
|
| 44 |
+
raw,ocr_score = "", 0.0
|
| 45 |
+
except:
|
| 46 |
+
raw,ocr_score = "", 0.0
|
| 47 |
|
| 48 |
+
plate = format_turkish_plate(raw)
|
| 49 |
label = f"{plate} ({ocr_score:.2f})"
|
| 50 |
|
| 51 |
+
cv2.rectangle(out, (x1,y1),(x2,y2), (0,255,0), 2)
|
| 52 |
+
cv2.putText(out, label, (x1,y1-5),
|
| 53 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,255,0), 2)
|
| 54 |
|
| 55 |
return cv2.cvtColor(out, cv2.COLOR_BGR2RGB), f"{len(res.boxes)} plate(s) detected"
|
| 56 |
|
| 57 |
+
# 4) Video iΓ§in iΕlem
|
|
|
|
| 58 |
def run_video(video_file, conf=0.25):
|
| 59 |
cap = cv2.VideoCapture(video_file)
|
| 60 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 61 |
+
w,h = int(cap.get(3)), int(cap.get(4))
|
| 62 |
+
outfp = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False).name
|
| 63 |
+
writer = cv2.VideoWriter(outfp, cv2.VideoWriter_fourcc(*"mp4v"), fps, (w,h))
|
| 64 |
+
records = []; idx = 0
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
while True:
|
| 67 |
ret, frame = cap.read()
|
| 68 |
+
if not ret: break
|
| 69 |
+
idx += 1; t = idx/fps
|
|
|
|
|
|
|
| 70 |
|
| 71 |
res = yolo(frame, conf=conf)[0]
|
| 72 |
+
for (x1,y1,x2,y2) in res.boxes.xyxy.cpu().numpy().astype(int):
|
| 73 |
crop = frame[y1:y2, x1:x2]
|
| 74 |
+
if crop.size==0: continue
|
|
|
|
| 75 |
|
| 76 |
+
plate_img = cv2.resize(crop, (128,32))
|
|
|
|
|
|
|
| 77 |
try:
|
| 78 |
recs = ocr.ocr(plate_img, det=False, cls=True)
|
| 79 |
if recs and recs[0]:
|
| 80 |
+
raw,ocr_score = recs[0][1][0], recs[0][1][1]
|
| 81 |
else:
|
| 82 |
+
raw,ocr_score = "", 0.0
|
| 83 |
+
except:
|
| 84 |
+
raw,ocr_score = "", 0.0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
+
plate = format_turkish_plate(raw)
|
| 87 |
+
if plate!="Unknown":
|
| 88 |
+
records.append({"time_s":round(t,2),"plate":plate,"conf":round(ocr_score,3)})
|
| 89 |
|
| 90 |
+
cv2.rectangle(frame,(x1,y1),(x2,y2),(0,255,0),2)
|
| 91 |
+
cv2.putText(frame,plate,(x1,y1-5),
|
| 92 |
+
cv2.FONT_HERSHEY_SIMPLEX,0.6,(0,255,0),2)
|
| 93 |
|
| 94 |
+
writer.write(frame)
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
+
cap.release(); writer.release()
|
| 97 |
+
with open("output.json","w") as f:
|
| 98 |
+
json.dump(records,f,indent=2)
|
| 99 |
+
return outfp, "Done"
|
| 100 |
|
| 101 |
+
# 5) Gradio ArayΓΌzΓΌ
|
| 102 |
with gr.Blocks() as demo:
|
| 103 |
gr.Markdown("## π License Plate Detection & Recognition")
|
|
|
|
| 104 |
with gr.Row():
|
| 105 |
with gr.Column():
|
| 106 |
img_in = gr.Image(type="numpy", label="Upload Image")
|
| 107 |
vid_in = gr.File(label="Upload Video (.mp4)")
|
| 108 |
+
conf = gr.Slider(0,1,0.25,0.01, label="YOLO Confidence")
|
| 109 |
btn_i = gr.Button("Run Image")
|
| 110 |
btn_v = gr.Button("Run Video")
|
| 111 |
with gr.Column():
|
|
|
|
| 113 |
vid_out = gr.Video(label="Annotated Video")
|
| 114 |
status = gr.Textbox(label="Status / JSON Path")
|
| 115 |
|
| 116 |
+
btn_i.click(run_image, [img_in,conf],[img_out,status])
|
| 117 |
+
btn_v.click(run_video, [vid_in,conf],[vid_out,status])
|
| 118 |
|
| 119 |
+
if __name__=="__main__":
|
| 120 |
demo.launch()
|