File size: 2,067 Bytes
5936e3e
 
 
 
 
 
31fec7c
7e8a9f8
31fec7c
 
5936e3e
 
 
 
31fec7c
 
 
5936e3e
31fec7c
 
ae1af43
31fec7c
5936e3e
 
 
 
 
 
 
 
ecce3f5
31fec7c
 
5936e3e
31fec7c
ecce3f5
5936e3e
 
ecce3f5
31fec7c
 
 
 
 
ae1af43
31fec7c
 
 
 
ae1af43
31fec7c
 
ecce3f5
 
31fec7c
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
import streamlit as st
import numpy as np
from tensorflow.keras.models import load_model
from PIL import Image
import requests

# Cache the model with st.cache_resource
@st.cache_resource
def load_model_from_hf():
    # Download the model from Hugging Face
    url = "https://huggingface.co/krishnamishra8848/Devanagari_Character_Recognition/resolve/main/saved_model.keras"
    response = requests.get(url)
    with open("saved_model.keras", "wb") as f:
        f.write(response.content)
    # Load the model
    model = load_model("saved_model.keras")
    return model

# Load the model
model = load_model_from_hf()

# Nepali characters mapping
label_mapping = [
    "क", "ख", "ग", "घ", "ङ", "च", "छ", "ज", "झ", "ञ",
    "ट", "ठ", "ड", "ढ", "ण", "त", "थ", "द", "ध", "न",
    "प", "फ", "ब", "भ", "म", "य", "र", "ल", "व", "श",
    "ष", "स", "ह", "क्ष", "त्र", "ज्ञ", "०", "१", "२", "३",
    "४", "५", "६", "७", "८", "९"
]

# Streamlit App
st.title("Devanagari Character Recognition")
st.write("Upload an image of a Devanagari character or digit, and the model will predict it.")

# File uploader for user to upload images
uploaded_file = st.file_uploader("Choose an image file", type=["jpg", "jpeg", "png"])

if uploaded_file is not None:
    try:
        # Preprocess the image
        img = Image.open(uploaded_file).convert("L")  # Convert to grayscale
        img_resized = img.resize((32, 32))  # Resize to 32x32
        img_array = np.array(img_resized).astype("float32") / 255.0  # Normalize pixel values
        img_input = img_array.reshape(1, 32, 32, 1)  # Reshape for the model

        # Make prediction
        prediction = model.predict(img_input)
        predicted_class_index = np.argmax(prediction)
        predicted_character = label_mapping[predicted_class_index]

        # Display the predicted character
        st.success(f"Predicted Character: {predicted_character}")

    except Exception as e:
        st.error(f"An error occurred: {e}")