File size: 5,092 Bytes
44ad3d8
41356b7
44ad3d8
41356b7
 
 
8eea83b
41356b7
a1c2179
8eea83b
41356b7
a1c2179
8eea83b
41356b7
8eea83b
a1c2179
 
 
 
 
 
 
41356b7
a1c2179
 
 
 
 
41356b7
8eea83b
 
 
 
 
 
 
a1c2179
 
8eea83b
a1c2179
 
 
 
63bf93b
2e77031
ad1e8d1
a1c2179
41356b7
 
a1c2179
 
ad1e8d1
63bf93b
 
41356b7
ad1e8d1
a1c2179
ad1e8d1
 
 
8eea83b
a1c2179
 
 
 
ad1e8d1
 
8eea83b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44ad3d8
a1c2179
 
4fcae56
41356b7
a1c2179
e50eca6
4fcae56
1f4a388
a1c2179
ad1e8d1
40f8621
a1c2179
ad1e8d1
 
 
1f4a388
 
ad1e8d1
8eea83b
ad1e8d1
 
 
 
 
 
 
 
1f4a388
ad1e8d1
1f4a388
ad1e8d1
 
 
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
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 and crop license plates
def detect_and_crop_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_images = []
    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_image = img_bgr[y1:y2, x1:x2]
                cropped_images.append(cropped_image)

    return cropped_images, detected_image

# Function to detect and crop characters
def detect_and_crop_characters(image):
    results = character_model(image)
    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)
                character_crops.append(image[y1:y2, x1:x2])
    return 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..."):
        cropped_plates, detected_image = detect_and_crop_license_plate(image)

        if cropped_plates:
            st.image(cv2.cvtColor(detected_image, cv2.COLOR_BGR2RGB), caption="Detected License Plates", use_container_width=True)
            st.write(f"Detected {len(cropped_plates)} license plate(s).")

            for idx, cropped_plate in enumerate(cropped_plates, 1):
                st.write(f"Processing License Plate {idx}:")
                character_crops = detect_and_crop_characters(cropped_plate)

                if character_crops:
                    recognized_characters = recognize_characters(character_crops)
                    st.write("Recognized Characters:", "".join(recognized_characters))
                else:
                    st.write("No characters detected in this license plate.")
        else:
            st.write("No license plates detected. Running character detection on the full image.")
            character_crops = detect_and_crop_characters(cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR))

            if character_crops:
                recognized_characters = recognize_characters(character_crops)
                st.write("Recognized Characters:", "".join(recognized_characters))
            else:
                st.write("No characters detected in the full image.")

    st.success("Processing complete!")