|
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 |
|
|
|
|
|
st.title("Nepal Vehicle License Plate and Character Recognition") |
|
|
|
|
|
st.write("Upload an image to detect license plates, segment characters, and recognize each character using advanced YOLO and CNN models.") |
|
|
|
|
|
@st.cache_resource |
|
def load_models(): |
|
|
|
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_model_path = hf_hub_download( |
|
repo_id="krishnamishra8848/Nepal_Vehicle_License_Plates_Detection_Version3", filename="best.pt" |
|
) |
|
character_model = YOLO(character_model_path) |
|
|
|
|
|
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 |
|
|
|
|
|
full_plate_model, character_model, recognition_model = load_models() |
|
|
|
|
|
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) |
|
cropped_plate = img_bgr[y1:y2, x1:x2] |
|
cropped_plates.append(cropped_plate) |
|
|
|
return detected_image, img_bgr, cropped_plates |
|
|
|
|
|
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) |
|
character_crops.append(image[y1:y2, x1:x2]) |
|
return detected_image, character_crops |
|
|
|
|
|
def recognize_characters(character_crops): |
|
class_labels = [ |
|
'क', 'को', 'ख', 'ग', 'च', 'ज', 'झ', 'ञ', 'डि', 'त', 'ना', 'प', |
|
'प्र', 'ब', 'बा', 'भे', 'म', 'मे', 'य', 'लु', 'सी', 'सु', 'से', 'ह', |
|
'०', '१', '२', '३', '४', '५', '६', '७', '८', '९' |
|
] |
|
recognized_characters = [] |
|
for char_crop in character_crops: |
|
|
|
resized = cv2.resize(char_crop, (64, 64)) |
|
normalized = resized / 255.0 |
|
reshaped = np.expand_dims(normalized, axis=0) |
|
|
|
|
|
prediction = recognition_model.predict(reshaped) |
|
predicted_class = class_labels[np.argmax(prediction)] |
|
recognized_characters.append(predicted_class) |
|
return recognized_characters |
|
|
|
|
|
uploaded_file = st.file_uploader("Choose an image file", type=["jpg", "jpeg", "png"]) |
|
|
|
if uploaded_file is not None: |
|
|
|
image = Image.open(uploaded_file) |
|
|
|
|
|
with st.spinner("Processing image..."): |
|
detected_image, original_image, cropped_plates = detect_license_plate(image) |
|
|
|
|
|
st.image(cv2.cvtColor(detected_image, cv2.COLOR_BGR2RGB), caption="Detected License Plates", use_container_width=True) |
|
|
|
|
|
max_characters = [] |
|
best_recognition_method = "" |
|
|
|
|
|
char_detected_img, character_crops_original = detect_characters(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" |
|
|
|
|
|
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}" |
|
|
|
|
|
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!") |
|
|