File size: 1,356 Bytes
27d49f5
 
 
 
 
 
 
 
81c06d7
27d49f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
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
import gradio as gr
import torch
import cv2
import numpy as np
import pytesseract
from PIL import Image

# YOLOv5 modelini yükle (örnek: yolov5s)
model = torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt', force_reload=True)

def detect_and_ocr(image):
    # YOLOv5 ile plaka tespiti
    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:  # 0: 'license-plate' olarak eğitilmişse
            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
    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()