|
from typing import get_args |
|
|
|
import cv2 |
|
import numpy as np |
|
import streamlit as st |
|
from PIL import Image |
|
|
|
from fast_alpr import ALPR |
|
from fast_alpr.default_detector import PlateDetectorModel |
|
from fast_alpr.default_ocr import OcrModel |
|
|
|
|
|
DETECTOR_MODELS = list(get_args(PlateDetectorModel)) |
|
OCR_MODELS = list(get_args(OcrModel)) |
|
|
|
OCR_MODELS.remove("global-plates-mobile-vit-v2-model") |
|
OCR_MODELS.insert(0, "global-plates-mobile-vit-v2-model") |
|
|
|
st.title("FastALPR Demo") |
|
st.write("An automatic license plate recognition (ALPR) system with customizable detector and OCR models.") |
|
st.markdown(""" |
|
[FastALPR](https://github.com/ankandrew/fast-alpr) library uses [open-image-models](https://github.com/ankandrew/open-image-models) |
|
for plate detection and [fast-plate-ocr](https://github.com/ankandrew/fast-plate-ocr) for optical character recognition (**OCR**). |
|
""") |
|
|
|
|
|
|
|
detector_model = st.sidebar.selectbox("Choose Detector Model", DETECTOR_MODELS) |
|
ocr_model = st.sidebar.selectbox("Choose OCR Model", OCR_MODELS) |
|
|
|
|
|
uploaded_file = st.file_uploader("Upload an image of a vehicle with a license plate", type=["jpg", "jpeg", "png"]) |
|
|
|
if uploaded_file is not None: |
|
|
|
img = Image.open(uploaded_file) |
|
img_array = np.array(img.convert("RGB")) |
|
st.image(img, caption="Uploaded Image", use_column_width=True) |
|
|
|
|
|
alpr = ALPR(detector_model=detector_model, ocr_model=ocr_model) |
|
|
|
|
|
st.write("Processing...") |
|
results = alpr.predict(img_array) |
|
|
|
|
|
annotated_img_array = alpr.draw_predictions(img_array) |
|
|
|
|
|
annotated_img = Image.fromarray(annotated_img_array) |
|
st.image(annotated_img, caption="Annotated Image with OCR Results", use_column_width=True) |
|
|
|
|
|
if results: |
|
st.write("**OCR Results:**") |
|
for result in results: |
|
|
|
plate_text = result.ocr.text if result.ocr else "N/A" |
|
plate_confidence = result.ocr.confidence if result.ocr else 0.0 |
|
st.write(f"- Detected Plate: `{plate_text}` with confidence `{plate_confidence:.2f}`") |
|
else: |
|
st.write("No license plate detected 😔.") |
|
else: |
|
st.write("Please upload an image to continue.") |