import gradio as gr
from ultralytics import YOLO
from PIL import Image
import numpy as np
import os
import logging
import cv2
import easyocr
from plate_utils import extract_plate, standardize_plate
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
helmet_model = YOLO("Helmet-Detect-model/best.pt")
reader = easyocr.Reader(['en'])
def detect_helmet(image):
results = helmet_model(image)
results_plotted = results[0].plot()
return Image.fromarray(results_plotted)
def process_plate(image):
if isinstance(image, Image.Image):
image = np.array(image)
plate_img = extract_plate(image)
if plate_img is None:
return "No plate detected", None
results = reader.readtext(plate_img)
text = ""
for (bbox, text_result, prob) in results:
text += text_result + " "
text = text.strip()
standardized = standardize_plate(text)
if standardized:
text = f"Raw: {text}\nStandardized: {standardized}"
return text, Image.fromarray(plate_img)
def get_example_images(folder_path):
examples = []
if os.path.exists(folder_path):
for filename in os.listdir(folder_path):
if filename.lower().endswith(('.png', '.jpg', '.jpeg')) and not filename.startswith('.'):
examples.append(os.path.join(folder_path, filename))
return examples
with gr.Blocks(
title="Helmet Detection and Number Plate OCR",
theme=gr.themes.Soft(primary_hue="blue", secondary_hue="indigo")
) as demo:
gr.Markdown(
"""
Helmet Detection & Number Plate Recognition
Created by Abhay Gupta | Advanced Computer Vision Project
"""
)
with gr.Tabs() as tabs:
with gr.Tab("Helmet Detection", id=1):
with gr.Row():
with gr.Column(scale=1):
helmet_input = gr.Image(
type="pil",
label="Upload Image",
tool="select",
height=300
)
helmet_button = gr.Button(
"Detect Helmets",
variant="primary",
scale=1
)
with gr.Column(scale=1):
helmet_output = gr.Image(
type="pil",
label="Detection Result",
height=300
)
gr.Markdown("### Example Images")
helmet_examples = get_example_images("Helmet-Detect-model/test_images")
if helmet_examples:
gr.Examples(
examples=helmet_examples,
inputs=helmet_input,
outputs=helmet_output,
fn=detect_helmet,
cache_examples=True,
label="Click to try an example"
)
with gr.Tab("Number Plate OCR", id=2):
with gr.Row():
with gr.Column(scale=1):
plate_input = gr.Image(
type="pil",
label="Upload Image",
tool="select",
height=300
)
plate_button = gr.Button(
"Read Number Plate",
variant="primary",
scale=1
)
with gr.Column(scale=1):
with gr.Group():
plate_text = gr.Textbox(
label="Extracted Text",
lines=3,
show_copy_button=True
)
plate_output = gr.Image(
type="pil",
label="Processed Plate",
height=200
)
gr.Markdown("### Example Images")
plate_examples = get_example_images("Number_plate_OCR/test_images")
if plate_examples:
gr.Examples(
examples=plate_examples,
inputs=plate_input,
outputs=[plate_text, plate_output],
fn=process_plate,
cache_examples=True,
label="Click to try an example"
)
gr.Markdown(
"""
Computer Vision Project by Abhay Gupta
"""
)
helmet_button.click(
fn=detect_helmet,
inputs=helmet_input,
outputs=helmet_output
)
plate_button.click(
fn=process_plate,
inputs=plate_input,
outputs=[plate_text, plate_output]
)
demo.launch()