Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
import cv2
|
4 |
+
import numpy as np
|
5 |
+
import pytesseract
|
6 |
+
from PIL import Image
|
7 |
+
|
8 |
+
# YOLOv5 modelini yükle (örnek: yolov5s)
|
9 |
+
model = torch.hub.load('ultralytics/yolov5', 'custom', path='yolov5s.pt', force_reload=True)
|
10 |
+
|
11 |
+
def detect_and_ocr(image):
|
12 |
+
# YOLOv5 ile plaka tespiti
|
13 |
+
results = model(image)
|
14 |
+
labels, cords = results.xyxyn[0][:, -1], results.xyxyn[0][:, :-1]
|
15 |
+
img = np.array(image)
|
16 |
+
h, w, _ = img.shape
|
17 |
+
plates = []
|
18 |
+
for i, (label, cord) in enumerate(zip(labels, cords)):
|
19 |
+
if int(label) == 0: # 0: 'license-plate' olarak eğitilmişse
|
20 |
+
x1, y1, x2, y2, conf = cord
|
21 |
+
x1, y1, x2, y2 = int(x1*w), int(y1*h), int(x2*w), int(y2*h)
|
22 |
+
plate_img = img[y1:y2, x1:x2]
|
23 |
+
plates.append(plate_img)
|
24 |
+
if not plates:
|
25 |
+
return "Plaka bulunamadı."
|
26 |
+
# OCR
|
27 |
+
ocr_results = []
|
28 |
+
for plate in plates:
|
29 |
+
text = pytesseract.image_to_string(plate, config='--psm 7')
|
30 |
+
ocr_results.append(text.strip())
|
31 |
+
return "\n".join(ocr_results)
|
32 |
+
|
33 |
+
iface = gr.Interface(
|
34 |
+
fn=detect_and_ocr,
|
35 |
+
inputs=gr.Image(source="upload", tool="editor", type="pil", label="Fotoğraf yükle veya çek"),
|
36 |
+
outputs="text",
|
37 |
+
title="Araç Plaka Tanıma ve OCR",
|
38 |
+
description="Kamera ile fotoğraf çek veya yükle, plaka ve yazı otomatik tespit edilsin."
|
39 |
+
)
|
40 |
+
|
41 |
+
iface.launch()
|