Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,49 +3,128 @@ import degirum as dg
|
|
| 3 |
from PIL import Image
|
| 4 |
import degirum_tools
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
#
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
model_zoo_url = "https://cs.degirum.com/degirum/degirum"
|
| 17 |
|
| 18 |
-
# lp_det_model_name: Name of the model for license plate detection.
|
| 19 |
lp_det_model_name = "yolov8n_relu6_global_lp_det--640x640_quant_n2x_orca1_1"
|
| 20 |
-
|
| 21 |
-
# lp_ocr_model_name: Name of the model for license plate OCR.
|
| 22 |
lp_ocr_model_name = "yolov8s_relu6_lp_ocr_7ch--256x128_quant_n2x_orca1_1"
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
import degirum_tools
|
| 5 |
|
| 6 |
+
# -----------------------------
|
| 7 |
+
# Page config
|
| 8 |
+
# -----------------------------
|
| 9 |
+
st.set_page_config(
|
| 10 |
+
page_title="DeGirum License Plate Demo",
|
| 11 |
+
page_icon="🚗",
|
| 12 |
+
layout="centered",
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
# -----------------------------
|
| 16 |
+
# App title & intro
|
| 17 |
+
# -----------------------------
|
| 18 |
+
st.title("License Plate Detection & Recognition (DeGirum Cloud)")
|
| 19 |
+
|
| 20 |
+
st.markdown(
|
| 21 |
+
"""
|
| 22 |
+
This demo shows how to build a simple **Automatic License Plate Recognition (ALPR)**
|
| 23 |
+
pipeline using models hosted on **DeGirum Cloud**.
|
| 24 |
+
|
| 25 |
+
**What this app does:**
|
| 26 |
+
1. Detects license plates in an uploaded image.
|
| 27 |
+
2. Crops each plate region.
|
| 28 |
+
3. Runs an OCR model to read the characters on the plate.
|
| 29 |
+
4. Displays the original and annotated images **side by side**.
|
| 30 |
+
"""
|
| 31 |
+
)
|
| 32 |
|
| 33 |
+
st.sidebar.header("About this demo")
|
| 34 |
+
st.sidebar.markdown(
|
| 35 |
+
"""
|
| 36 |
+
- **Inference location:** DeGirum Cloud
|
| 37 |
+
- **Models used:**
|
| 38 |
+
- LP detection: `yolov8n_relu6_global_lp_det--640x640_quant_n2x_orca1_1`
|
| 39 |
+
- LP OCR: `yolov8s_relu6_lp_ocr_7ch--256x128_quant_n2x_orca1_1`
|
| 40 |
+
- **Libraries:**
|
| 41 |
+
- `degirum`
|
| 42 |
+
- `degirum_tools`
|
| 43 |
+
- `streamlit`
|
| 44 |
+
"""
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
# -----------------------------
|
| 48 |
+
# Configuration
|
| 49 |
+
# -----------------------------
|
| 50 |
+
hw_location = "@cloud"
|
| 51 |
model_zoo_url = "https://cs.degirum.com/degirum/degirum"
|
| 52 |
|
|
|
|
| 53 |
lp_det_model_name = "yolov8n_relu6_global_lp_det--640x640_quant_n2x_orca1_1"
|
|
|
|
|
|
|
| 54 |
lp_ocr_model_name = "yolov8s_relu6_lp_ocr_7ch--256x128_quant_n2x_orca1_1"
|
| 55 |
|
| 56 |
+
|
| 57 |
+
# -----------------------------
|
| 58 |
+
# Model loading (cached)
|
| 59 |
+
# -----------------------------
|
| 60 |
+
@st.cache_resource(show_spinner=True)
|
| 61 |
+
def load_compound_model():
|
| 62 |
+
model_zoo = dg.connect(hw_location, model_zoo_url, token=st.secrets["DG_TOKEN"])
|
| 63 |
+
|
| 64 |
+
lp_det_model = model_zoo.load_model(
|
| 65 |
+
lp_det_model_name,
|
| 66 |
+
image_backend="pil",
|
| 67 |
+
overlay_color=(255, 0, 0),
|
| 68 |
+
overlay_line_width=2,
|
| 69 |
+
overlay_font_scale=2,
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
lp_ocr_model = model_zoo.load_model(
|
| 73 |
+
lp_ocr_model_name,
|
| 74 |
+
image_backend="pil",
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
# Create a compound cropping model with 5% crop extent
|
| 78 |
+
crop_model = degirum_tools.CroppingAndClassifyingCompoundModel(
|
| 79 |
+
lp_det_model, lp_ocr_model, 5.0
|
| 80 |
)
|
| 81 |
|
| 82 |
+
return crop_model
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
crop_model = load_compound_model()
|
| 86 |
+
|
| 87 |
+
# -----------------------------
|
| 88 |
+
# File upload UI
|
| 89 |
+
# -----------------------------
|
| 90 |
+
st.subheader("Upload an image and run the models")
|
| 91 |
+
|
| 92 |
+
uploaded_file = st.file_uploader(
|
| 93 |
+
"Choose an image containing a vehicle / license plate",
|
| 94 |
+
type=["jpg", "jpeg", "png"],
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
run_button = st.button("Run Inference", type="primary", disabled=uploaded_file is None)
|
| 98 |
+
|
| 99 |
+
# -----------------------------
|
| 100 |
+
# Inference
|
| 101 |
+
# -----------------------------
|
| 102 |
+
if run_button and uploaded_file is not None:
|
| 103 |
+
with st.spinner("Running license plate detection and recognition..."):
|
| 104 |
+
# Load full-res image and create a display copy
|
| 105 |
+
orig_image = Image.open(uploaded_file).convert("RGB")
|
| 106 |
+
display_image = orig_image.copy()
|
| 107 |
+
display_image.thumbnail((640, 640), Image.Resampling.LANCZOS)
|
| 108 |
+
|
| 109 |
+
# Run model on the resized display image
|
| 110 |
+
inference_results = crop_model(display_image)
|
| 111 |
+
|
| 112 |
+
st.subheader("Results")
|
| 113 |
+
|
| 114 |
+
col1, col2 = st.columns(2, gap="medium")
|
| 115 |
+
|
| 116 |
+
with col1:
|
| 117 |
+
st.markdown("**Original image**")
|
| 118 |
+
st.image(display_image, use_container_width=True)
|
| 119 |
+
|
| 120 |
+
with col2:
|
| 121 |
+
st.markdown("**Detection & recognition**")
|
| 122 |
+
st.image(
|
| 123 |
+
inference_results.image_overlay,
|
| 124 |
+
caption="License plates with bounding boxes and labels",
|
| 125 |
+
use_container_width=True,
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
st.caption("Inference complete. Detected plates and OCR results are shown on the right.")
|
| 129 |
+
elif uploaded_file is None:
|
| 130 |
+
st.info("👈 Upload an image to get started.")
|