Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,10 @@ from open_image_models import LicensePlateDetector
|
|
| 3 |
from PIL import Image
|
| 4 |
import cv2
|
| 5 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# Define the available models
|
| 8 |
PlateDetectorModel = ['yolo-v9-t-640-license-plate-end2end',
|
|
@@ -11,23 +15,25 @@ PlateDetectorModel = ['yolo-v9-t-640-license-plate-end2end',
|
|
| 11 |
'yolo-v9-t-256-license-plate-end2end']
|
| 12 |
|
| 13 |
# Streamlit interface
|
| 14 |
-
st.title("License Plate Detection with Open Image Models")
|
| 15 |
st.write("Select a model and upload an image to perform license plate detection.")
|
|
|
|
| 16 |
|
| 17 |
# Model selection dropdown
|
| 18 |
-
selected_model = st.selectbox("Select a License Plate Detection Model", PlateDetectorModel)
|
| 19 |
|
| 20 |
# File uploader for images
|
| 21 |
-
uploaded_file = st.file_uploader("
|
| 22 |
|
| 23 |
if uploaded_file is not None:
|
| 24 |
-
# Load the image
|
| 25 |
image = Image.open(uploaded_file)
|
| 26 |
st.image(image, caption='Uploaded Image', use_column_width=True)
|
|
|
|
| 27 |
st.write("")
|
| 28 |
-
st.write("Detecting license plates
|
| 29 |
|
| 30 |
-
# Convert the image to an OpenCV format
|
| 31 |
image_np = np.array(image)
|
| 32 |
image_cv2 = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR)
|
| 33 |
|
|
@@ -37,9 +43,32 @@ if uploaded_file is not None:
|
|
| 37 |
# Perform license plate detection
|
| 38 |
detections = lp_detector.predict(image_cv2)
|
| 39 |
|
| 40 |
-
# Display the detected plates
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
# Annotate and display the image with detected plates
|
| 44 |
annotated_image = lp_detector.display_predictions(image_cv2)
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
import cv2
|
| 5 |
import numpy as np
|
| 6 |
+
from rich.console import Console
|
| 7 |
+
|
| 8 |
+
# Set up the rich console for better terminal output
|
| 9 |
+
console = Console()
|
| 10 |
|
| 11 |
# Define the available models
|
| 12 |
PlateDetectorModel = ['yolo-v9-t-640-license-plate-end2end',
|
|
|
|
| 15 |
'yolo-v9-t-256-license-plate-end2end']
|
| 16 |
|
| 17 |
# Streamlit interface
|
| 18 |
+
st.title("🚗 License Plate Detection with Open Image Models 🚓")
|
| 19 |
st.write("Select a model and upload an image to perform license plate detection.")
|
| 20 |
+
st.markdown("---")
|
| 21 |
|
| 22 |
# Model selection dropdown
|
| 23 |
+
selected_model = st.selectbox("🔍 Select a License Plate Detection Model", PlateDetectorModel)
|
| 24 |
|
| 25 |
# File uploader for images
|
| 26 |
+
uploaded_file = st.file_uploader("📂 Upload an image...", type=["jpg", "png", "jpeg", "webp"])
|
| 27 |
|
| 28 |
if uploaded_file is not None:
|
| 29 |
+
# Load the image using PIL
|
| 30 |
image = Image.open(uploaded_file)
|
| 31 |
st.image(image, caption='Uploaded Image', use_column_width=True)
|
| 32 |
+
|
| 33 |
st.write("")
|
| 34 |
+
st.write("🔍 **Detecting license plates...**")
|
| 35 |
|
| 36 |
+
# Convert the PIL image to an OpenCV format (NumPy array)
|
| 37 |
image_np = np.array(image)
|
| 38 |
image_cv2 = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR)
|
| 39 |
|
|
|
|
| 43 |
# Perform license plate detection
|
| 44 |
detections = lp_detector.predict(image_cv2)
|
| 45 |
|
| 46 |
+
# Display the detected plates using `rich` for colorful output in the console
|
| 47 |
+
console.print(f"[bold green]Detections: [/bold green] {detections}")
|
| 48 |
+
|
| 49 |
+
# Streamlit display for detections
|
| 50 |
+
if detections:
|
| 51 |
+
st.success(f"✅ {len(detections)} License Plates Detected!")
|
| 52 |
+
for i, detection in enumerate(detections):
|
| 53 |
+
st.write(f"**Plate {i+1}:** {detection}")
|
| 54 |
+
else:
|
| 55 |
+
st.warning("⚠️ No license plates detected!")
|
| 56 |
|
| 57 |
# Annotate and display the image with detected plates
|
| 58 |
annotated_image = lp_detector.display_predictions(image_cv2)
|
| 59 |
+
|
| 60 |
+
# Convert the annotated image from BGR to RGB for Streamlit display
|
| 61 |
+
annotated_image_rgb = cv2.cvtColor(annotated_image, cv2.COLOR_BGR2RGB)
|
| 62 |
+
st.image(annotated_image_rgb, caption='Annotated Image with Detections', use_column_width=True)
|
| 63 |
+
|
| 64 |
+
# Add some additional style or layout to make the app more attractive
|
| 65 |
+
st.markdown("""
|
| 66 |
+
<style>
|
| 67 |
+
.stButton>button {
|
| 68 |
+
font-size: 16px;
|
| 69 |
+
background-color: #4CAF50;
|
| 70 |
+
color: white;
|
| 71 |
+
border-radius: 8px;
|
| 72 |
+
}
|
| 73 |
+
</style>
|
| 74 |
+
""", unsafe_allow_html=True)
|