Update app.py
Browse files
app.py
CHANGED
@@ -3,32 +3,23 @@ import numpy as np
|
|
3 |
from tensorflow.keras.models import load_model
|
4 |
from PIL import Image
|
5 |
import requests
|
6 |
-
from ultralytics import YOLO
|
7 |
-
import cv2
|
8 |
|
9 |
-
# Cache the
|
10 |
@st.cache_resource
|
11 |
-
def
|
|
|
12 |
url = "https://huggingface.co/krishnamishra8848/Devanagari_Character_Recognition/resolve/main/saved_model.keras"
|
13 |
response = requests.get(url)
|
14 |
with open("saved_model.keras", "wb") as f:
|
15 |
f.write(response.content)
|
16 |
-
|
|
|
|
|
17 |
|
18 |
-
#
|
19 |
-
|
20 |
-
def load_detection_model():
|
21 |
-
weights_path = "https://huggingface.co/krishnamishra8848/Nepal-Vehicle-License-Plate-Detection/resolve/main/last.pt"
|
22 |
-
response = requests.get(weights_path)
|
23 |
-
with open("last.pt", "wb") as f:
|
24 |
-
f.write(response.content)
|
25 |
-
return YOLO("last.pt")
|
26 |
|
27 |
-
#
|
28 |
-
character_model = load_character_model()
|
29 |
-
detection_model = load_detection_model()
|
30 |
-
|
31 |
-
# Nepali character mapping
|
32 |
label_mapping = [
|
33 |
"क", "ख", "ग", "घ", "ङ", "च", "छ", "ज", "झ", "ञ",
|
34 |
"ट", "ठ", "ड", "ढ", "ण", "त", "थ", "द", "ध", "न",
|
@@ -38,48 +29,27 @@ label_mapping = [
|
|
38 |
]
|
39 |
|
40 |
# Streamlit App
|
41 |
-
st.title("
|
42 |
-
st.write("Upload an image
|
43 |
|
44 |
-
# File uploader
|
45 |
uploaded_file = st.file_uploader("Choose an image file", type=["jpg", "jpeg", "png"])
|
46 |
|
47 |
if uploaded_file is not None:
|
48 |
try:
|
49 |
-
#
|
50 |
-
img = Image.open(uploaded_file).convert("
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
# Detect bounding boxes with YOLO
|
55 |
-
results = detection_model(img_bgr)
|
56 |
-
|
57 |
-
# Initialize recognized text
|
58 |
-
recognized_text = ""
|
59 |
-
|
60 |
-
# Iterate through detected bounding boxes
|
61 |
-
for result in results:
|
62 |
-
if hasattr(result, 'boxes') and result.boxes is not None:
|
63 |
-
for box in result.boxes.xyxy:
|
64 |
-
x1, y1, x2, y2 = map(int, box) # Extract bounding box coordinates
|
65 |
-
cropped_img = img_bgr[y1:y2, x1:x2] # Crop the detected region
|
66 |
-
|
67 |
-
# Preprocess the cropped image
|
68 |
-
cropped_resized = cv2.resize(cropped_img, (32, 32), interpolation=cv2.INTER_AREA)
|
69 |
-
cropped_gray = cv2.cvtColor(cropped_resized, cv2.COLOR_BGR2GRAY)
|
70 |
-
cropped_normalized = cropped_gray.astype("float32") / 255.0
|
71 |
-
cropped_input = cropped_normalized.reshape(1, 32, 32, 1)
|
72 |
-
|
73 |
-
# Predict text for the cropped region
|
74 |
-
prediction = character_model.predict(cropped_input)
|
75 |
-
predicted_index = np.argmax(prediction)
|
76 |
-
predicted_character = label_mapping[predicted_index]
|
77 |
|
78 |
-
|
79 |
-
|
|
|
|
|
80 |
|
81 |
-
# Display the
|
82 |
-
st.success(f"
|
83 |
|
84 |
except Exception as e:
|
85 |
-
st.error(f"An error occurred: {e}")
|
|
|
3 |
from tensorflow.keras.models import load_model
|
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
|
|
|
|
|
|
|
|
|
23 |
label_mapping = [
|
24 |
"क", "ख", "ग", "घ", "ङ", "च", "छ", "ज", "झ", "ञ",
|
25 |
"ट", "ठ", "ड", "ढ", "ण", "त", "थ", "द", "ध", "न",
|
|
|
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}")
|