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()