File size: 5,786 Bytes
44ad3d8 41356b7 44ad3d8 41356b7 8eea83b 41356b7 a1c2179 8eea83b 41356b7 a1c2179 8eea83b 41356b7 8eea83b a1c2179 41356b7 a1c2179 41356b7 8eea83b a1c2179 8eea83b a1c2179 2fc3066 a1c2179 63bf93b 2e77031 ad1e8d1 bf1d858 41356b7 a1c2179 ad1e8d1 bf1d858 41356b7 bf1d858 a1c2179 2fc3066 ad1e8d1 2fc3066 8eea83b a1c2179 2fc3066 ad1e8d1 2fc3066 8eea83b 44ad3d8 a1c2179 4fcae56 41356b7 a1c2179 e50eca6 4fcae56 2fc3066 a1c2179 bf1d858 40f8621 2fc3066 ba9d2c1 ad1e8d1 bf1d858 2fc3066 bf1d858 8eea83b bf1d858 ad1e8d1 bf1d858 a1c2179 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
import streamlit as st
from huggingface_hub import hf_hub_download
from ultralytics import YOLO
import cv2
import numpy as np
from PIL import Image
from tensorflow.keras.models import load_model
# Title for the Streamlit App
st.title("Nepal Vehicle License Plate and Character Recognition")
# Description
st.write("Upload an image to detect license plates, segment characters, and recognize each character using advanced YOLO and CNN models.")
# Download YOLO and CNN model weights from Hugging Face
@st.cache_resource
def load_models():
# Full license plate detection model
full_plate_model_path = hf_hub_download(
repo_id="krishnamishra8848/Nepal-Vehicle-License-Plate-Detection", filename="last.pt"
)
full_plate_model = YOLO(full_plate_model_path)
# Character detection model
character_model_path = hf_hub_download(
repo_id="krishnamishra8848/Nepal_Vehicle_License_Plates_Detection_Version3", filename="best.pt"
)
character_model = YOLO(character_model_path)
# Character recognition model
recognition_model_path = hf_hub_download(
repo_id="krishnamishra8848/Nepal_Vehicle_License_Plates_Character_Recognisation", filename="model.h5"
)
recognition_model = load_model(recognition_model_path)
return full_plate_model, character_model, recognition_model
# Load models
full_plate_model, character_model, recognition_model = load_models()
# Function to detect license plates
def detect_license_plate(image):
img_bgr = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
results = full_plate_model(img_bgr)
detected_image = img_bgr.copy()
cropped_plates = []
for result in results:
if hasattr(result, 'boxes') and result.boxes is not None:
for box in result.boxes.xyxy:
x1, y1, x2, y2 = map(int, box)
cv2.rectangle(detected_image, (x1, y1), (x2, y2), (255, 0, 0), 2) # Draw bounding box
cropped_plate = img_bgr[y1:y2, x1:x2]
cropped_plates.append(cropped_plate)
return detected_image, img_bgr, cropped_plates
# Function to detect characters
def detect_characters(image):
results = character_model(image)
detected_image = image.copy()
character_crops = []
for result in results:
if hasattr(result, 'boxes') and result.boxes is not None:
for box in result.boxes.xyxy:
x1, y1, x2, y2 = map(int, box)
cv2.rectangle(detected_image, (x1, y1), (x2, y2), (0, 255, 0), 2) # Draw bounding box
character_crops.append(image[y1:y2, x1:x2])
return detected_image, character_crops
# Function to recognize characters
def recognize_characters(character_crops):
class_labels = [
'क', 'को', 'ख', 'ग', 'च', 'ज', 'झ', 'ञ', 'डि', 'त', 'ना', 'प',
'प्र', 'ब', 'बा', 'भे', 'म', 'मे', 'य', 'लु', 'सी', 'सु', 'से', 'ह',
'०', '१', '२', '३', '४', '५', '६', '७', '८', '९'
]
recognized_characters = []
for char_crop in character_crops:
# Preprocess the cropped character for recognition model
resized = cv2.resize(char_crop, (64, 64))
normalized = resized / 255.0
reshaped = np.expand_dims(normalized, axis=0) # Add batch dimension
# Predict the character
prediction = recognition_model.predict(reshaped)
predicted_class = class_labels[np.argmax(prediction)]
recognized_characters.append(predicted_class)
return recognized_characters
# Upload an image file
uploaded_file = st.file_uploader("Choose an image file", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
# Load image
image = Image.open(uploaded_file)
# Detect license plates
with st.spinner("Processing image..."):
detected_image, original_image, cropped_plates = detect_license_plate(image)
# Show the detected license plate image with bounding boxes
st.image(cv2.cvtColor(detected_image, cv2.COLOR_BGR2RGB), caption="Detected License Plates", use_container_width=True)
# Initialize variables for comparison
max_characters = []
best_recognition_method = ""
# Process original image for character detection
char_detected_img, character_crops_original = detect_characters(original_image)
# Recognize characters from original image
recognized_original = recognize_characters(character_crops_original)
if recognized_original:
if len(recognized_original) > len(max_characters):
max_characters = recognized_original
best_recognition_method = "Original Image"
# Process cropped plates for character detection
for idx, cropped_plate in enumerate(cropped_plates, 1):
st.write(f"Processing Cropped Plate {idx}:")
cropped_char_detected_img, character_crops_cropped = detect_characters(cropped_plate)
recognized_cropped = recognize_characters(character_crops_cropped)
if recognized_cropped:
if len(recognized_cropped) > len(max_characters):
max_characters = recognized_cropped
best_recognition_method = f"Cropped Plate {idx}"
# Show the result
st.image(cv2.cvtColor(char_detected_img, cv2.COLOR_BGR2RGB), caption="Detected Characters from Original Image", use_container_width=True)
if best_recognition_method:
st.write(f"Best Recognition Method: {best_recognition_method}")
st.write("Recognized Characters:", "".join(max_characters))
else:
st.write("No characters detected in any method.")
st.success("Processing complete!")
|