Update app.py
Browse files
app.py
CHANGED
@@ -4,17 +4,17 @@ from ultralytics import YOLO
|
|
4 |
import cv2
|
5 |
import numpy as np
|
6 |
from PIL import Image
|
7 |
-
|
8 |
import tempfile
|
9 |
import os
|
10 |
|
11 |
# Title for the Streamlit App
|
12 |
-
st.title("Nepal Vehicle License Plate and Character
|
13 |
|
14 |
# Description
|
15 |
-
st.write("Upload an image to detect license plates and
|
16 |
|
17 |
-
# Download YOLO model weights from Hugging Face
|
18 |
@st.cache_resource
|
19 |
def load_models():
|
20 |
# Full license plate detection model
|
@@ -29,10 +29,16 @@ def load_models():
|
|
29 |
)
|
30 |
character_model = YOLO(character_model_path)
|
31 |
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
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):
|
@@ -51,16 +57,36 @@ def detect_and_crop_license_plate(image):
|
|
51 |
|
52 |
return cropped_images, detected_image
|
53 |
|
54 |
-
# Function to detect characters
|
55 |
-
def
|
56 |
results = character_model(image)
|
|
|
57 |
for result in results:
|
58 |
if hasattr(result, 'boxes') and result.boxes is not None:
|
59 |
for box in result.boxes.xyxy:
|
60 |
x1, y1, x2, y2 = map(int, box)
|
61 |
-
|
62 |
-
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
# Upload an image file
|
66 |
uploaded_file = st.file_uploader("Choose an image file", type=["jpg", "jpeg", "png"])
|
@@ -77,15 +103,24 @@ if uploaded_file is not None:
|
|
77 |
st.image(cv2.cvtColor(detected_image, cv2.COLOR_BGR2RGB), caption="Detected License Plates", use_container_width=True)
|
78 |
|
79 |
if cropped_plates:
|
80 |
-
st.write(f"Detected {len(cropped_plates)} license plate(s).
|
81 |
for idx, cropped_image in enumerate(cropped_plates, 1):
|
82 |
-
st.write(f"License Plate {idx}:")
|
83 |
-
|
84 |
-
|
85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
else:
|
87 |
-
st.write("No license plates detected.
|
88 |
-
annotated_image = detect_characters(detected_image.copy())
|
89 |
-
st.image(cv2.cvtColor(annotated_image, cv2.COLOR_BGR2RGB), caption="Full Image with Characters", use_container_width=True)
|
90 |
|
91 |
st.success("Processing complete!")
|
|
|
4 |
import cv2
|
5 |
import numpy as np
|
6 |
from PIL import Image
|
7 |
+
from tensorflow.keras.models import load_model
|
8 |
import tempfile
|
9 |
import os
|
10 |
|
11 |
# Title for the Streamlit App
|
12 |
+
st.title("Nepal Vehicle License Plate and Character Recognition")
|
13 |
|
14 |
# Description
|
15 |
+
st.write("Upload an image to detect license plates, segment characters, and recognize each character using advanced YOLO and CNN models.")
|
16 |
|
17 |
+
# Download YOLO and CNN model weights from Hugging Face
|
18 |
@st.cache_resource
|
19 |
def load_models():
|
20 |
# Full license plate detection model
|
|
|
29 |
)
|
30 |
character_model = YOLO(character_model_path)
|
31 |
|
32 |
+
# Character recognition model
|
33 |
+
recognition_model_path = hf_hub_download(
|
34 |
+
repo_id="krishnamishra8848/Nepal_Vehicle_License_Plates_Character_Recognisation", filename="model.h5"
|
35 |
+
)
|
36 |
+
recognition_model = load_model(recognition_model_path)
|
37 |
+
|
38 |
+
return full_plate_model, character_model, recognition_model
|
39 |
|
40 |
# Load models
|
41 |
+
full_plate_model, character_model, recognition_model = load_models()
|
42 |
|
43 |
# Function to detect and crop license plates
|
44 |
def detect_and_crop_license_plate(image):
|
|
|
57 |
|
58 |
return cropped_images, detected_image
|
59 |
|
60 |
+
# Function to detect and crop characters
|
61 |
+
def detect_and_crop_characters(image):
|
62 |
results = character_model(image)
|
63 |
+
character_crops = []
|
64 |
for result in results:
|
65 |
if hasattr(result, 'boxes') and result.boxes is not None:
|
66 |
for box in result.boxes.xyxy:
|
67 |
x1, y1, x2, y2 = map(int, box)
|
68 |
+
character_crops.append(image[y1:y2, x1:x2])
|
69 |
+
return character_crops
|
70 |
+
|
71 |
+
# Function to recognize characters
|
72 |
+
def recognize_characters(character_crops):
|
73 |
+
class_labels = [
|
74 |
+
'क', 'को', 'ख', 'ग', 'च', 'ज', 'झ', 'ञ', 'डि', 'त', 'ना', 'प',
|
75 |
+
'प्र', 'ब', 'बा', 'भे', 'म', 'मे', 'य', 'लु', 'सी', 'सु', 'से', 'ह',
|
76 |
+
'०', '१', '२', '३', '४', '५', '६', '७', '८', '९'
|
77 |
+
]
|
78 |
+
recognized_characters = []
|
79 |
+
for char_crop in character_crops:
|
80 |
+
# Preprocess the cropped character for recognition model
|
81 |
+
resized = cv2.resize(char_crop, (64, 64))
|
82 |
+
normalized = resized / 255.0
|
83 |
+
reshaped = np.expand_dims(normalized, axis=0) # Add batch dimension
|
84 |
+
|
85 |
+
# Predict the character
|
86 |
+
prediction = recognition_model.predict(reshaped)
|
87 |
+
predicted_class = class_labels[np.argmax(prediction)]
|
88 |
+
recognized_characters.append(predicted_class)
|
89 |
+
return recognized_characters
|
90 |
|
91 |
# Upload an image file
|
92 |
uploaded_file = st.file_uploader("Choose an image file", type=["jpg", "jpeg", "png"])
|
|
|
103 |
st.image(cv2.cvtColor(detected_image, cv2.COLOR_BGR2RGB), caption="Detected License Plates", use_container_width=True)
|
104 |
|
105 |
if cropped_plates:
|
106 |
+
st.write(f"Detected {len(cropped_plates)} license plate(s).")
|
107 |
for idx, cropped_image in enumerate(cropped_plates, 1):
|
108 |
+
st.write(f"Processing License Plate {idx}:")
|
109 |
+
|
110 |
+
# Detect and crop characters
|
111 |
+
character_crops = detect_and_crop_characters(cropped_image)
|
112 |
+
|
113 |
+
if character_crops:
|
114 |
+
# Recognize characters
|
115 |
+
recognized_characters = recognize_characters(character_crops)
|
116 |
+
|
117 |
+
# Show each cropped character and prediction
|
118 |
+
for i, char_crop in enumerate(character_crops):
|
119 |
+
st.image(cv2.cvtColor(char_crop, cv2.COLOR_BGR2RGB), caption=f"Character {i+1}")
|
120 |
+
st.write(f"Predicted Character: {recognized_characters[i]}")
|
121 |
+
else:
|
122 |
+
st.write("No characters detected in this license plate.")
|
123 |
else:
|
124 |
+
st.write("No license plates detected.")
|
|
|
|
|
125 |
|
126 |
st.success("Processing complete!")
|