File size: 2,577 Bytes
eac4457
 
 
 
 
 
 
 
 
 
 
 
 
 
68e6d39
 
 
eac4457
 
 
e02f820
e86fb97
 
e02f820
eac4457
e86fb97
eac4457
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b80d21a
eac4457
 
 
 
 
 
b80d21a
 
 
 
eac4457
685dca3
eac4457
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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

# Default models
DETECTOR_MODELS = list(get_args(PlateDetectorModel))
OCR_MODELS = list(get_args(OcrModel))
# Put global OCR first
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**).
""")


# Sidebar for selecting models
detector_model = st.sidebar.selectbox("Choose Detector Model", DETECTOR_MODELS)
ocr_model = st.sidebar.selectbox("Choose OCR Model", OCR_MODELS)

# Load image
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:
    # Convert uploaded file to a format compatible with OpenCV
    img = Image.open(uploaded_file)
    img_array = np.array(img.convert("RGB"))  # Convert to RGB if needed
    st.image(img, caption="Uploaded Image", use_column_width=True)

    # Initialize ALPR with selected models
    alpr = ALPR(detector_model=detector_model, ocr_model=ocr_model)

    # Run ALPR on the uploaded image
    st.write("Processing...")
    results = alpr.predict(img_array)

    # Draw predictions on the image
    annotated_img_array = alpr.draw_predictions(img_array)

    # Convert the annotated image back to display in Streamlit
    annotated_img = Image.fromarray(annotated_img_array)
    st.image(annotated_img, caption="Annotated Image with OCR Results", use_column_width=True)

    # Display OCR results in text format for more detail
    if results:
        st.write("**OCR Results:**")
        for result in results:
            # Access the detection and OCR attributes from ALPRResult
            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.")