krishnamishra8848's picture
Update app.py
41356b7 verified
raw
history blame
1.93 kB
# Install necessary libraries
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
# Step 1: Download the YOLO model weights from your Hugging Face repository
weights_path = hf_hub_download(repo_id="krishnamishra8848/Nepal-Vehicle-License-Plate-Detection", filename="best.pt")
# Step 2: Load the YOLO model
model = YOLO(weights_path)
# Step 3: Function to process and display results
def detect_license_plate(image):
# Convert the PIL image to a numpy array
img = np.array(image)
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
# Perform inference
results = model(img)
# Draw bounding boxes and confidence scores
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) # Convert to integers
cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2) # Green rectangle
label = f"{conf:.2f}"
cv2.putText(img, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
# Convert back to RGB for Streamlit display
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
return Image.fromarray(img)
# Streamlit Interface
st.title("Nepal Vehicle License Plate Detection")
st.write("Upload an image to detect license plates.")
# File uploader
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
# Display the uploaded image
image = Image.open(uploaded_file)
st.image(image, caption="Uploaded Image", use_column_width=True)
# Run detection
st.write("Processing...")
result_image = detect_license_plate(image)
# Display the results
st.image(result_image, caption="Detection Results", use_column_width=True)