Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,121 +1,107 @@
|
|
| 1 |
# app.py
|
| 2 |
|
| 3 |
import numpy as np
|
| 4 |
-
np.int = int # PaddleOCRβnin eski np.int
|
| 5 |
|
| 6 |
-
import cv2, json, tempfile, re
|
|
|
|
| 7 |
from ultralytics import YOLO
|
| 8 |
from paddleocr import PaddleOCR
|
| 9 |
|
| 10 |
# βββ 1) Load models βββββββββββββββββββββββββββββββββββββββββββββββ
|
| 11 |
yolo = YOLO("models/best.pt")
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
rec=True, # sadece okuma
|
| 16 |
rec_model_dir="models/ocr_model",
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
use_angle_cls=True, # v2.x flag
|
| 20 |
-
use_space_char=True # boΕluk okumasΔ±na izin
|
| 21 |
)
|
| 22 |
|
| 23 |
-
# βββ 2)
|
| 24 |
def format_turkish_plate(s: str) -> str:
|
| 25 |
s = re.sub(r'[^A-Z0-9]', '', s.upper())
|
| 26 |
m = re.match(r'^(\d{2})([A-Z]{1,3})(\d{2,4})$', s)
|
| 27 |
return f"{m.group(1)} {m.group(2)} {m.group(3)}" if m else "Unknown"
|
| 28 |
|
| 29 |
-
# βββ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
def run_image(img, conf=0.25):
|
| 31 |
bgr = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
| 32 |
res = yolo(bgr, conf=conf)[0]
|
| 33 |
out = bgr.copy()
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
res.boxes.conf.cpu().numpy()):
|
| 37 |
-
x1,y1,x2,y2 = box.astype(int)
|
| 38 |
crop = out[y1:y2, x1:x2]
|
| 39 |
-
if crop.size == 0:
|
| 40 |
continue
|
| 41 |
-
|
| 42 |
-
plate_img = cv2.resize(crop, (128, 32))
|
| 43 |
-
|
| 44 |
-
# dahili tespit kapalΔ±, sadece tanΔ±ma+angle-cls
|
| 45 |
try:
|
| 46 |
-
recs = ocr.ocr(plate_img, det=False, cls=
|
| 47 |
except Exception:
|
| 48 |
recs = []
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
else:
|
| 53 |
-
raw_text, ocr_score = "", 0.0
|
| 54 |
-
|
| 55 |
-
plate = format_turkish_plate(raw_text)
|
| 56 |
-
label = f"{plate} ({ocr_score:.2f})"
|
| 57 |
-
|
| 58 |
cv2.rectangle(out, (x1,y1),(x2,y2), (0,255,0), 2)
|
| 59 |
cv2.putText(out, label, (x1, y1-5),
|
| 60 |
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,255,0), 2)
|
| 61 |
-
|
| 62 |
return cv2.cvtColor(out, cv2.COLOR_BGR2RGB), f"{len(res.boxes)} plate(s) detected"
|
| 63 |
|
| 64 |
-
# βββ 4) Video
|
| 65 |
def run_video(video_file, conf=0.25):
|
| 66 |
cap = cv2.VideoCapture(video_file)
|
| 67 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 68 |
w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 69 |
h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 70 |
-
|
| 71 |
-
writer = cv2.VideoWriter(
|
| 72 |
-
records
|
| 73 |
-
|
| 74 |
while True:
|
| 75 |
ret, frame = cap.read()
|
| 76 |
if not ret: break
|
| 77 |
-
idx += 1; t = idx
|
| 78 |
-
|
| 79 |
res = yolo(frame, conf=conf)[0]
|
| 80 |
-
for
|
|
|
|
| 81 |
crop = frame[y1:y2, x1:x2]
|
| 82 |
-
if crop.size == 0:
|
| 83 |
continue
|
| 84 |
-
|
| 85 |
-
plate_img = cv2.resize(crop, (128, 32))
|
| 86 |
try:
|
| 87 |
-
recs = ocr.ocr(plate_img, det=False, cls=
|
| 88 |
except Exception:
|
| 89 |
recs = []
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
raw_text, ocr_score = recs[0][0], recs[0][1]
|
| 93 |
-
else:
|
| 94 |
-
raw_text, ocr_score = "", 0.0
|
| 95 |
-
|
| 96 |
-
plate = format_turkish_plate(raw_text)
|
| 97 |
if plate != "Unknown":
|
| 98 |
-
records.append({
|
| 99 |
-
"time_s": round(t,2),
|
| 100 |
-
"plate": plate,
|
| 101 |
-
"conf": round(ocr_score,3)
|
| 102 |
-
})
|
| 103 |
-
|
| 104 |
cv2.rectangle(frame, (x1,y1),(x2,y2), (0,255,0), 2)
|
| 105 |
cv2.putText(frame, plate, (x1, y1-5),
|
| 106 |
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,255,0), 2)
|
| 107 |
-
|
| 108 |
writer.write(frame)
|
| 109 |
-
|
| 110 |
cap.release(); writer.release()
|
| 111 |
with open("output.json","w") as f:
|
| 112 |
json.dump(records, f, indent=2)
|
| 113 |
-
return
|
| 114 |
|
| 115 |
# βββ 5) Gradio UI ββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 116 |
with gr.Blocks() as demo:
|
| 117 |
gr.Markdown("## π License Plate Detection + Recognition")
|
| 118 |
-
|
| 119 |
with gr.Row():
|
| 120 |
with gr.Column():
|
| 121 |
img_in = gr.Image(type="numpy", label="Upload Image")
|
|
@@ -127,9 +113,8 @@ with gr.Blocks() as demo:
|
|
| 127 |
img_out = gr.Image(type="numpy", label="Annotated Image")
|
| 128 |
vid_out = gr.Video(label="Annotated Video")
|
| 129 |
status = gr.Textbox(label="Status / JSON Path")
|
| 130 |
-
|
| 131 |
b1.click(run_image, [img_in,conf], [img_out,status])
|
| 132 |
b2.click(run_video, [vid_in,conf], [vid_out,status])
|
| 133 |
|
| 134 |
-
if __name__
|
| 135 |
demo.launch()
|
|
|
|
| 1 |
# app.py
|
| 2 |
|
| 3 |
import numpy as np
|
| 4 |
+
np.int = int # patch for PaddleOCRβnin eski np.int Γ§aΔrΔ±larΔ±na
|
| 5 |
|
| 6 |
+
import cv2, json, tempfile, re
|
| 7 |
+
import gradio as gr
|
| 8 |
from ultralytics import YOLO
|
| 9 |
from paddleocr import PaddleOCR
|
| 10 |
|
| 11 |
# βββ 1) Load models βββββββββββββββββββββββββββββββββββββββββββββββ
|
| 12 |
yolo = YOLO("models/best.pt")
|
| 13 |
+
ocr = PaddleOCR(
|
| 14 |
+
det=False, # GΓΆrΓΌntΓΌ genel tespiti burada kapalΔ±
|
| 15 |
+
rec=True,
|
|
|
|
| 16 |
rec_model_dir="models/ocr_model",
|
| 17 |
+
use_textline_orientation=False,
|
| 18 |
+
lang="en"
|
|
|
|
|
|
|
| 19 |
)
|
| 20 |
|
| 21 |
+
# βββ 2) Turkish plate formatter ββββββββββββββββββββββββββββββββββββ
|
| 22 |
def format_turkish_plate(s: str) -> str:
|
| 23 |
s = re.sub(r'[^A-Z0-9]', '', s.upper())
|
| 24 |
m = re.match(r'^(\d{2})([A-Z]{1,3})(\d{2,4})$', s)
|
| 25 |
return f"{m.group(1)} {m.group(2)} {m.group(3)}" if m else "Unknown"
|
| 26 |
|
| 27 |
+
# βββ yardΔ±mcΔ±: OCR Γ§Δ±ktΔ±sΔ±nΔ± normalize et βββββββββββββββββββββββββ
|
| 28 |
+
def extract_text_score(recs):
|
| 29 |
+
if not recs:
|
| 30 |
+
return "", 0.0
|
| 31 |
+
first = recs[0]
|
| 32 |
+
# det=False ile dΓΆnen format: ["TEXT", score]
|
| 33 |
+
if isinstance(first, (list,tuple)) and len(first)==2 and isinstance(first[0], str):
|
| 34 |
+
return first[0], float(first[1])
|
| 35 |
+
# det=True formatΔ± (eskiden): [["TEXT", score], β¦]
|
| 36 |
+
if isinstance(first, list) and first and isinstance(first[0], (list,tuple)):
|
| 37 |
+
return first[0][0], float(first[0][1])
|
| 38 |
+
# tanΔ±madΔ±
|
| 39 |
+
return "", 0.0
|
| 40 |
+
|
| 41 |
+
# βββ 3) Singleβimage inference βββββββββββββββββββββββββββββββββββββ
|
| 42 |
def run_image(img, conf=0.25):
|
| 43 |
bgr = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
| 44 |
res = yolo(bgr, conf=conf)[0]
|
| 45 |
out = bgr.copy()
|
| 46 |
+
for box in res.boxes.xyxy.cpu().numpy().astype(int):
|
| 47 |
+
x1,y1,x2,y2 = box
|
|
|
|
|
|
|
| 48 |
crop = out[y1:y2, x1:x2]
|
| 49 |
+
if crop.size == 0:
|
| 50 |
continue
|
| 51 |
+
plate_img = cv2.resize(crop, (128,32))
|
|
|
|
|
|
|
|
|
|
| 52 |
try:
|
| 53 |
+
recs = ocr.ocr(plate_img, det=False, cls=False)
|
| 54 |
except Exception:
|
| 55 |
recs = []
|
| 56 |
+
raw, score = extract_text_score(recs)
|
| 57 |
+
plate = format_turkish_plate(raw)
|
| 58 |
+
label = f"{plate} ({score:.2f})"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
cv2.rectangle(out, (x1,y1),(x2,y2), (0,255,0), 2)
|
| 60 |
cv2.putText(out, label, (x1, y1-5),
|
| 61 |
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,255,0), 2)
|
|
|
|
| 62 |
return cv2.cvtColor(out, cv2.COLOR_BGR2RGB), f"{len(res.boxes)} plate(s) detected"
|
| 63 |
|
| 64 |
+
# βββ 4) Video inference βββββββββββββββββββββββββββββββββββββββββββ
|
| 65 |
def run_video(video_file, conf=0.25):
|
| 66 |
cap = cv2.VideoCapture(video_file)
|
| 67 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 68 |
w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 69 |
h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 70 |
+
tmp_out = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False).name
|
| 71 |
+
writer = cv2.VideoWriter(tmp_out, cv2.VideoWriter_fourcc(*"mp4v"), fps, (w,h))
|
| 72 |
+
records = []
|
| 73 |
+
idx = 0
|
| 74 |
while True:
|
| 75 |
ret, frame = cap.read()
|
| 76 |
if not ret: break
|
| 77 |
+
idx += 1; t = idx/fps
|
|
|
|
| 78 |
res = yolo(frame, conf=conf)[0]
|
| 79 |
+
for box in res.boxes.xyxy.cpu().numpy().astype(int):
|
| 80 |
+
x1,y1,x2,y2 = box
|
| 81 |
crop = frame[y1:y2, x1:x2]
|
| 82 |
+
if crop.size == 0:
|
| 83 |
continue
|
| 84 |
+
plate_img = cv2.resize(crop, (128,32))
|
|
|
|
| 85 |
try:
|
| 86 |
+
recs = ocr.ocr(plate_img, det=False, cls=False)
|
| 87 |
except Exception:
|
| 88 |
recs = []
|
| 89 |
+
raw, score = extract_text_score(recs)
|
| 90 |
+
plate = format_turkish_plate(raw)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
if plate != "Unknown":
|
| 92 |
+
records.append({"time_s":round(t,2),"plate":plate,"conf":round(score,3)})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
cv2.rectangle(frame, (x1,y1),(x2,y2), (0,255,0), 2)
|
| 94 |
cv2.putText(frame, plate, (x1, y1-5),
|
| 95 |
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,255,0), 2)
|
|
|
|
| 96 |
writer.write(frame)
|
|
|
|
| 97 |
cap.release(); writer.release()
|
| 98 |
with open("output.json","w") as f:
|
| 99 |
json.dump(records, f, indent=2)
|
| 100 |
+
return tmp_out, "Done"
|
| 101 |
|
| 102 |
# βββ 5) Gradio UI ββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 103 |
with gr.Blocks() as demo:
|
| 104 |
gr.Markdown("## π License Plate Detection + Recognition")
|
|
|
|
| 105 |
with gr.Row():
|
| 106 |
with gr.Column():
|
| 107 |
img_in = gr.Image(type="numpy", label="Upload Image")
|
|
|
|
| 113 |
img_out = gr.Image(type="numpy", label="Annotated Image")
|
| 114 |
vid_out = gr.Video(label="Annotated Video")
|
| 115 |
status = gr.Textbox(label="Status / JSON Path")
|
|
|
|
| 116 |
b1.click(run_image, [img_in,conf], [img_out,status])
|
| 117 |
b2.click(run_video, [vid_in,conf], [vid_out,status])
|
| 118 |
|
| 119 |
+
if __name__=="__main__":
|
| 120 |
demo.launch()
|