Spaces:
Running
Running
import streamlit as st | |
from open_image_models import LicensePlateDetector | |
from PIL import Image | |
import cv2 | |
import numpy as np | |
from rich.console import Console | |
# Set up the rich console for better terminal output | |
console = Console() | |
# Define the available models | |
PlateDetectorModel = ['yolo-v9-t-640-license-plate-end2end', | |
'yolo-v9-t-512-license-plate-end2end', | |
'yolo-v9-t-384-license-plate-end2end', | |
'yolo-v9-t-256-license-plate-end2end'] | |
# Streamlit interface | |
st.title("π License Plate Detection with Open Image Models π") | |
st.write("Select a model and upload an image to perform license plate detection.") | |
st.markdown("---") | |
# Model selection dropdown | |
selected_model = st.selectbox("π Select a License Plate Detection Model", PlateDetectorModel) | |
# File uploader for images | |
uploaded_file = st.file_uploader("π Upload an image...", type=["jpg", "png", "jpeg", "webp"]) | |
if uploaded_file is not None: | |
# Load the image using PIL | |
image = Image.open(uploaded_file) | |
st.image(image, caption='Uploaded Image', use_column_width=True) | |
st.write("") | |
st.write("π **Detecting license plates...**") | |
# Convert the PIL image to an OpenCV format (NumPy array) | |
image_np = np.array(image) | |
image_cv2 = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR) | |
# Initialize the License Plate Detector | |
lp_detector = LicensePlateDetector(detection_model=selected_model) | |
# Perform license plate detection | |
detections = lp_detector.predict(image_cv2) | |
# Display the detected plates using `rich` for colorful output in the console | |
console.print(f"[bold green]Detections: [/bold green] {detections}") | |
# Streamlit display for detections | |
if detections: | |
st.success(f"β {len(detections)} License Plates Detected!") | |
for i, detection in enumerate(detections): | |
st.write(f"**Plate {i+1}:** {detection}") | |
else: | |
st.warning("β οΈ No license plates detected!") | |
# Annotate and display the image with detected plates | |
annotated_image = lp_detector.display_predictions(image_cv2) | |
# Convert the annotated image from BGR to RGB for Streamlit display | |
annotated_image_rgb = cv2.cvtColor(annotated_image, cv2.COLOR_BGR2RGB) | |
st.image(annotated_image_rgb, caption='Annotated Image with Detections', use_column_width=True) | |
# Add some additional style or layout to make the app more attractive | |
st.markdown(""" | |
<style> | |
.stButton>button { | |
font-size: 16px; | |
background-color: #4CAF50; | |
color: white; | |
border-radius: 8px; | |
} | |
</style> | |
""", unsafe_allow_html=True) | |