|
import gradio as gr |
|
import torch |
|
import cv2 |
|
import numpy as np |
|
import pytesseract |
|
from PIL import Image |
|
|
|
|
|
model = torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt', force_reload=True) |
|
|
|
def detect_and_ocr(image): |
|
|
|
results = model(image) |
|
labels, cords = results.xyxyn[0][:, -1], results.xyxyn[0][:, :-1] |
|
img = np.array(image) |
|
h, w, _ = img.shape |
|
plates = [] |
|
for i, (label, cord) in enumerate(zip(labels, cords)): |
|
if int(label) == 0: |
|
x1, y1, x2, y2, conf = cord |
|
x1, y1, x2, y2 = int(x1*w), int(y1*h), int(x2*w), int(y2*h) |
|
plate_img = img[y1:y2, x1:x2] |
|
plates.append(plate_img) |
|
if not plates: |
|
return "Plaka bulunamadı." |
|
|
|
ocr_results = [] |
|
for plate in plates: |
|
text = pytesseract.image_to_string(plate, config='--psm 7') |
|
ocr_results.append(text.strip()) |
|
return "\n".join(ocr_results) |
|
|
|
iface = gr.Interface( |
|
fn=detect_and_ocr, |
|
inputs=gr.Image(source="upload", tool="editor", type="pil", label="Fotoğraf yükle veya çek"), |
|
outputs="text", |
|
title="Araç Plaka Tanıma ve OCR", |
|
description="Kamera ile fotoğraf çek veya yükle, plaka ve yazı otomatik tespit edilsin." |
|
) |
|
|
|
iface.launch() |