Update app.py
Browse files
app.py
CHANGED
@@ -4,16 +4,19 @@ from tensorflow.keras.models import load_model
|
|
4 |
from PIL import Image
|
5 |
import requests
|
6 |
|
7 |
-
#
|
8 |
@st.cache_resource
|
9 |
def load_model_from_hf():
|
|
|
10 |
url = "https://huggingface.co/krishnamishra8848/Devanagari_Character_Recognition/resolve/main/saved_model.keras"
|
11 |
response = requests.get(url)
|
12 |
with open("saved_model.keras", "wb") as f:
|
13 |
f.write(response.content)
|
|
|
14 |
model = load_model("saved_model.keras")
|
15 |
return model
|
16 |
|
|
|
17 |
model = load_model_from_hf()
|
18 |
|
19 |
# Nepali characters mapping
|
@@ -25,24 +28,28 @@ label_mapping = [
|
|
25 |
"४", "५", "६", "७", "८", "९"
|
26 |
]
|
27 |
|
28 |
-
# Streamlit
|
29 |
st.title("Devanagari Character Recognition")
|
30 |
st.write("Upload an image of a Devanagari character or digit, and the model will predict it.")
|
31 |
|
32 |
-
# File uploader
|
33 |
-
uploaded_file = st.file_uploader("Choose
|
34 |
|
35 |
if uploaded_file is not None:
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
|
|
|
|
|
|
|
|
|
4 |
from PIL import Image
|
5 |
import requests
|
6 |
|
7 |
+
# Cache the model with st.cache_resource
|
8 |
@st.cache_resource
|
9 |
def load_model_from_hf():
|
10 |
+
# Download the model from Hugging Face
|
11 |
url = "https://huggingface.co/krishnamishra8848/Devanagari_Character_Recognition/resolve/main/saved_model.keras"
|
12 |
response = requests.get(url)
|
13 |
with open("saved_model.keras", "wb") as f:
|
14 |
f.write(response.content)
|
15 |
+
# Load the model
|
16 |
model = load_model("saved_model.keras")
|
17 |
return model
|
18 |
|
19 |
+
# Load the model
|
20 |
model = load_model_from_hf()
|
21 |
|
22 |
# Nepali characters mapping
|
|
|
28 |
"४", "५", "६", "७", "८", "९"
|
29 |
]
|
30 |
|
31 |
+
# Streamlit App
|
32 |
st.title("Devanagari Character Recognition")
|
33 |
st.write("Upload an image of a Devanagari character or digit, and the model will predict it.")
|
34 |
|
35 |
+
# File uploader for user to upload images
|
36 |
+
uploaded_file = st.file_uploader("Choose an image file", type=["jpg", "jpeg", "png"])
|
37 |
|
38 |
if uploaded_file is not None:
|
39 |
+
try:
|
40 |
+
# Preprocess the image
|
41 |
+
img = Image.open(uploaded_file).convert("L") # Convert to grayscale
|
42 |
+
img_resized = img.resize((32, 32)) # Resize to 32x32
|
43 |
+
img_array = np.array(img_resized).astype("float32") / 255.0 # Normalize pixel values
|
44 |
+
img_input = img_array.reshape(1, 32, 32, 1) # Reshape for the model
|
45 |
+
|
46 |
+
# Make prediction
|
47 |
+
prediction = model.predict(img_input)
|
48 |
+
predicted_class_index = np.argmax(prediction)
|
49 |
+
predicted_character = label_mapping[predicted_class_index]
|
50 |
+
|
51 |
+
# Display the predicted character
|
52 |
+
st.success(f"Predicted Character: {predicted_character}")
|
53 |
+
|
54 |
+
except Exception as e:
|
55 |
+
st.error(f"An error occurred: {e}")
|