|
|
|
import streamlit as st |
|
from huggingface_hub import hf_hub_download |
|
from ultralytics import YOLO |
|
import cv2 |
|
import numpy as np |
|
from PIL import Image |
|
|
|
|
|
weights_path = hf_hub_download(repo_id="krishnamishra8848/Nepal-Vehicle-License-Plate-Detection", filename="best.pt") |
|
|
|
|
|
model = YOLO(weights_path) |
|
|
|
|
|
def detect_license_plate(image): |
|
|
|
img = np.array(image) |
|
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) |
|
|
|
|
|
results = model(img) |
|
|
|
|
|
for result in results: |
|
if hasattr(result, 'boxes') and result.boxes is not None: |
|
for box, conf in zip(result.boxes.xyxy, result.boxes.conf): |
|
x1, y1, x2, y2 = map(int, box) |
|
cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2) |
|
label = f"{conf:.2f}" |
|
cv2.putText(img, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) |
|
|
|
|
|
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) |
|
return Image.fromarray(img) |
|
|
|
|
|
st.title("Nepal Vehicle License Plate Detection") |
|
st.write("Upload an image to detect license plates.") |
|
|
|
|
|
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"]) |
|
|
|
if uploaded_file is not None: |
|
|
|
image = Image.open(uploaded_file) |
|
st.image(image, caption="Uploaded Image", use_column_width=True) |
|
|
|
|
|
st.write("Processing...") |
|
result_image = detect_license_plate(image) |
|
|
|
|
|
st.image(result_image, caption="Detection Results", use_column_width=True) |
|
|