ankandrew commited on
Commit
eac4457
·
verified ·
1 Parent(s): fdb844e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -0
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import get_args
2
+
3
+ import cv2
4
+ import numpy as np
5
+ import streamlit as st
6
+ from PIL import Image
7
+
8
+ from fast_alpr import ALPR
9
+ from fast_alpr.default_detector import PlateDetectorModel
10
+ from fast_alpr.default_ocr import OcrModel
11
+
12
+ # Default models
13
+ DETECTOR_MODELS = list(get_args(PlateDetectorModel))
14
+ OCR_MODELS = list(get_args(OcrModel))
15
+ # Put european OCR first
16
+ OCR_MODELS.remove("european-plates-mobile-vit-v2-model")
17
+ OCR_MODELS.insert(0, "european-plates-mobile-vit-v2-model")
18
+
19
+ st.title("FastALPR Demo")
20
+ st.write("An automatic license plate recognition (ALPR) system with customizable detector and OCR models.")
21
+
22
+ # Sidebar for selecting models
23
+ detector_model = st.sidebar.selectbox("Choose Detector Model", DETECTOR_MODELS)
24
+ ocr_model = st.sidebar.selectbox("Choose OCR Model", OCR_MODELS)
25
+
26
+ # Load image
27
+ uploaded_file = st.file_uploader("Upload an image of a vehicle with a license plate", type=["jpg", "jpeg", "png"])
28
+
29
+ if uploaded_file is not None:
30
+ # Convert uploaded file to a format compatible with OpenCV
31
+ img = Image.open(uploaded_file)
32
+ img_array = np.array(img.convert("RGB")) # Convert to RGB if needed
33
+ st.image(img, caption="Uploaded Image", use_column_width=True)
34
+
35
+ # Initialize ALPR with selected models
36
+ alpr = ALPR(detector_model=detector_model, ocr_model=ocr_model)
37
+
38
+ # Run ALPR on the uploaded image
39
+ st.write("Processing...")
40
+ results = alpr.predict(img_array)
41
+
42
+ # Draw predictions on the image
43
+ annotated_img_array = alpr.draw_predictions(img_array)
44
+
45
+ # Convert the annotated image back to display in Streamlit
46
+ annotated_img = Image.fromarray(cv2.cvtColor(annotated_img_array, cv2.COLOR_BGR2RGB))
47
+ st.image(annotated_img, caption="Annotated Image with OCR Results", use_column_width=True)
48
+
49
+ # Display OCR results in text format for more detail
50
+ if results:
51
+ st.write("**OCR Results:**")
52
+ for result in results:
53
+ st.write(f"- Detected Plate: `{result['text']}` with confidence `{result['confidence']:.2f}`")
54
+ else:
55
+ st.write("No license plate detected.")
56
+ else:
57
+ st.write("Please upload an image to continue.")