| import gradio as gr |
| import cv2 |
| import numpy as np |
| from ultralytics import YOLO |
| import easyocr |
|
|
| |
| model = YOLO("best (1).pt") |
|
|
| |
| reader = easyocr.Reader(['en']) |
|
|
| def detect_and_recognize_license_plate(image): |
| """ |
| Detects and recognizes license plates in an image. |
| """ |
| |
| image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR) |
|
|
| |
| results = model(image) |
| license_plates = [] |
|
|
| |
| for result in results: |
| for box in result.boxes: |
| |
| x1, y1, x2, y2 = map(int, box.xyxy[0].tolist()) |
|
|
| |
| cropped_plate = image[y1:y2, x1:x2] |
|
|
| |
| ocr_results = reader.readtext(cropped_plate) |
| plate_text = " ".join([res[1] for res in ocr_results]) |
|
|
| |
| cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2) |
| cv2.putText(image, plate_text, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2) |
|
|
| |
| license_plates.append(plate_text) |
|
|
| |
| output_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) |
|
|
| |
| return output_image, ", ".join(license_plates) |
|
|
| |
| interface = gr.Interface( |
| fn=detect_and_recognize_license_plate, |
| inputs=gr.Image(label="Upload Image"), |
| outputs=[gr.Image(label="Detected License Plate"), gr.Textbox(label="Extracted Text")], |
| title="License Plate Detection and Recognition", |
| description="Upload an image to detect and recognize license plates." |
| ) |
|
|
| |
| interface.launch() |