Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- app.py +70 -0
- requirements.txt +6 -0
- yolo11n-custom.pt +3 -0
app.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from ultralytics import YOLO
|
| 3 |
+
import cv2
|
| 4 |
+
import easyocr
|
| 5 |
+
import numpy as np
|
| 6 |
+
from PIL import Image
|
| 7 |
+
|
| 8 |
+
# Load YOLO model
|
| 9 |
+
model = YOLO('yolo11n-custom.pt')
|
| 10 |
+
model.fuse()
|
| 11 |
+
|
| 12 |
+
# Load EasyOCR
|
| 13 |
+
reader = easyocr.Reader(['en'])
|
| 14 |
+
|
| 15 |
+
def detect_license_plate(image):
|
| 16 |
+
results = model.predict(image, conf=0.15, iou=0.3, classes=[0])
|
| 17 |
+
plate_texts = []
|
| 18 |
+
img_array = np.array(image)
|
| 19 |
+
img = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR)
|
| 20 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 21 |
+
|
| 22 |
+
img_height, img_width, _ = img.shape
|
| 23 |
+
|
| 24 |
+
for result in results:
|
| 25 |
+
for bbox in result.boxes.xyxy:
|
| 26 |
+
x1, y1, x2, y2 = map(int, bbox.tolist())
|
| 27 |
+
plate = img[int(y1):int(y2), int(x1):int(x2)]
|
| 28 |
+
scale = 2
|
| 29 |
+
height, width = plate.shape[:2]
|
| 30 |
+
plate = cv2.resize(plate, (width * scale, height * scale), interpolation=cv2.INTER_CUBIC)
|
| 31 |
+
lab = cv2.cvtColor(plate, cv2.COLOR_RGB2LAB)
|
| 32 |
+
l, a, b = cv2.split(lab)
|
| 33 |
+
clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8, 8))
|
| 34 |
+
l = clahe.apply(l)
|
| 35 |
+
plate = cv2.merge((l, a, b))
|
| 36 |
+
plate = cv2.cvtColor(plate, cv2.COLOR_LAB2RGB)
|
| 37 |
+
|
| 38 |
+
text = reader.readtext(plate, detail=0, allowlist="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-")
|
| 39 |
+
text = " ".join(text).upper()
|
| 40 |
+
|
| 41 |
+
text_scale = max(1, width / 250)
|
| 42 |
+
thickness = max(2, width // 200)
|
| 43 |
+
cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), thickness)
|
| 44 |
+
(text_width, text_height), _ = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, text_scale, thickness)
|
| 45 |
+
text_x = x1 + (width - text_width) // 2
|
| 46 |
+
text_y = y1 - 10 if y1 > 50 else y2 + text_height + 20
|
| 47 |
+
text_box_y1 = text_y - text_height - 5
|
| 48 |
+
text_box_y2 = text_y + 5
|
| 49 |
+
cv2.rectangle(img, (text_x - 8, text_box_y1 - 3), (text_x + text_width + 8, text_box_y2 + 3), (0, 0, 0), -1)
|
| 50 |
+
cv2.rectangle(img, (text_x - 5, text_box_y1), (text_x + text_width + 5, text_box_y2), (255, 255, 255), -1)
|
| 51 |
+
cv2.putText(img, text, (text_x, text_y), cv2.FONT_HERSHEY_SIMPLEX, text_scale, (0, 0, 0), thickness)
|
| 52 |
+
|
| 53 |
+
plate_texts.append(text)
|
| 54 |
+
|
| 55 |
+
result_img = Image.fromarray(img)
|
| 56 |
+
return result_img, "\n".join(plate_texts) if plate_texts else "No license plates detected."
|
| 57 |
+
|
| 58 |
+
# Gradio UI
|
| 59 |
+
iface = gr.Interface(
|
| 60 |
+
fn=detect_license_plate,
|
| 61 |
+
inputs=gr.Image(type="pil"),
|
| 62 |
+
outputs=[
|
| 63 |
+
gr.Image(type="pil", label="Detected Image"),
|
| 64 |
+
gr.Textbox(label="Detected License Plates")
|
| 65 |
+
],
|
| 66 |
+
title="🚘 License Plate Detector",
|
| 67 |
+
description="Upload an image with license plates to detect them using YOLO and EasyOCR."
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
ultralytics==8.1.25 # for YOLO (ensure this matches your model version)
|
| 2 |
+
opencv-python
|
| 3 |
+
easyocr
|
| 4 |
+
numpy
|
| 5 |
+
pillow
|
| 6 |
+
gradio
|
yolo11n-custom.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c5ebaab2bc7c2036d97706cd1992389998de338961d0485879d0d128755c52e2
|
| 3 |
+
size 5477537
|