Update app.py
Browse files
app.py
CHANGED
@@ -1,51 +1,88 @@
|
|
1 |
-
# Install necessary libraries
|
2 |
import streamlit as st
|
3 |
from huggingface_hub import hf_hub_download
|
4 |
from ultralytics import YOLO
|
5 |
import cv2
|
6 |
import numpy as np
|
7 |
from PIL import Image
|
|
|
|
|
|
|
8 |
|
9 |
-
#
|
10 |
-
|
11 |
|
12 |
-
#
|
13 |
-
|
14 |
|
15 |
-
#
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
20 |
|
21 |
-
#
|
22 |
-
|
|
|
|
|
|
|
23 |
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
for result in results:
|
26 |
if hasattr(result, 'boxes') and result.boxes is not None:
|
27 |
-
for box
|
28 |
-
x1, y1, x2, y2 = map(int, box)
|
29 |
-
|
30 |
-
|
31 |
-
cv2.putText(img, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
|
32 |
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
-
|
38 |
-
st.title("Nepal Vehicle License Plate Detection")
|
39 |
-
st.write("Upload an image to detect license plates.")
|
40 |
|
41 |
-
#
|
42 |
-
uploaded_file = st.file_uploader("Choose an image
|
43 |
|
44 |
if uploaded_file is not None:
|
45 |
-
#
|
46 |
-
st.write("Processing...")
|
47 |
image = Image.open(uploaded_file)
|
48 |
-
result_image = detect_license_plate(image)
|
49 |
|
50 |
-
# Display
|
51 |
-
st.image(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
from huggingface_hub import hf_hub_download
|
3 |
from ultralytics import YOLO
|
4 |
import cv2
|
5 |
import numpy as np
|
6 |
from PIL import Image
|
7 |
+
import matplotlib.pyplot as plt
|
8 |
+
import tempfile
|
9 |
+
import os
|
10 |
|
11 |
+
# Title for the Streamlit App
|
12 |
+
st.title("Nepal Vehicle License Plate and Character Detection")
|
13 |
|
14 |
+
# Description
|
15 |
+
st.write("Upload an image to detect license plates and their characters using two advanced YOLO models.")
|
16 |
|
17 |
+
# Download YOLO model weights from Hugging Face
|
18 |
+
@st.cache_resource
|
19 |
+
def load_models():
|
20 |
+
# Full license plate detection model
|
21 |
+
full_plate_model_path = hf_hub_download(
|
22 |
+
repo_id="krishnamishra8848/Nepal-Vehicle-License-Plate-Detection", filename="last.pt"
|
23 |
+
)
|
24 |
+
full_plate_model = YOLO(full_plate_model_path)
|
25 |
|
26 |
+
# Character detection model
|
27 |
+
character_model_path = hf_hub_download(
|
28 |
+
repo_id="krishnamishra8848/Nepal_Vehicle_License_Plates_Detection_Version3", filename="best.pt"
|
29 |
+
)
|
30 |
+
character_model = YOLO(character_model_path)
|
31 |
|
32 |
+
return full_plate_model, character_model
|
33 |
+
|
34 |
+
# Load models
|
35 |
+
full_plate_model, character_model = load_models()
|
36 |
+
|
37 |
+
# Function to detect and crop license plates
|
38 |
+
def detect_and_crop_license_plate(image):
|
39 |
+
img_bgr = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
40 |
+
results = full_plate_model(img_bgr)
|
41 |
+
|
42 |
+
cropped_images = []
|
43 |
for result in results:
|
44 |
if hasattr(result, 'boxes') and result.boxes is not None:
|
45 |
+
for box in result.boxes.xyxy:
|
46 |
+
x1, y1, x2, y2 = map(int, box)
|
47 |
+
cropped_image = img_bgr[y1:y2, x1:x2]
|
48 |
+
cropped_images.append(cropped_image)
|
|
|
49 |
|
50 |
+
return cropped_images, img_bgr
|
51 |
+
|
52 |
+
# Function to detect characters
|
53 |
+
def detect_characters(image):
|
54 |
+
results = character_model(image)
|
55 |
+
for result in results:
|
56 |
+
if hasattr(result, 'boxes') and result.boxes is not None:
|
57 |
+
for box in result.boxes.xyxy:
|
58 |
+
x1, y1, x2, y2 = map(int, box)
|
59 |
+
cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
60 |
|
61 |
+
return image
|
|
|
|
|
62 |
|
63 |
+
# Upload an image file
|
64 |
+
uploaded_file = st.file_uploader("Choose an image file", type=["jpg", "jpeg", "png"])
|
65 |
|
66 |
if uploaded_file is not None:
|
67 |
+
# Load image
|
|
|
68 |
image = Image.open(uploaded_file)
|
|
|
69 |
|
70 |
+
# Display uploaded image
|
71 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
72 |
+
|
73 |
+
# Detect license plates and characters
|
74 |
+
with st.spinner("Processing image..."):
|
75 |
+
cropped_plates, original_image = detect_and_crop_license_plate(image)
|
76 |
+
|
77 |
+
if cropped_plates:
|
78 |
+
st.write(f"Detected {len(cropped_plates)} license plate(s). Showing results:")
|
79 |
+
for idx, cropped_image in enumerate(cropped_plates, 1):
|
80 |
+
st.write(f"License Plate {idx}:")
|
81 |
+
annotated_plate = detect_characters(cropped_image.copy())
|
82 |
+
st.image(cv2.cvtColor(annotated_plate, cv2.COLOR_BGR2RGB), caption=f"License Plate {idx} with Characters", use_column_width=True)
|
83 |
+
else:
|
84 |
+
st.write("No license plates detected. Running character detection on the full image.")
|
85 |
+
annotated_image = detect_characters(original_image.copy())
|
86 |
+
st.image(cv2.cvtColor(annotated_image, cv2.COLOR_BGR2RGB), caption="Full Image with Characters", use_column_width=True)
|
87 |
+
|
88 |
+
st.success("Processing complete!")
|