Spaces:
Sleeping
Sleeping
import streamlit as st | |
from streamlit_drawable_canvas import st_canvas | |
import cv2 | |
from tensorflow.keras.models import load_model | |
import numpy as np | |
from PIL import Image | |
#!pip install Pillow==9.0.0 | |
import io | |
import streamlit as st | |
ms = st.session_state | |
if "themes" not in ms: | |
ms.themes = {"current_theme": "light", | |
"refreshed": True, | |
"light": {"theme.base": "dark", | |
"theme.backgroundColor": "black", | |
"theme.primaryColor": "#c98bdb", | |
"theme.secondaryBackgroundColor": "#5591f5", | |
"theme.textColor": "white", | |
"theme.textColor": "white", | |
"button_face": "π"}, | |
"dark": {"theme.base": "light", | |
"theme.backgroundColor": "white", | |
"theme.primaryColor": "#5591f5", | |
"theme.secondaryBackgroundColor": "#82E1D7", | |
"theme.textColor": "#0a1464", | |
"button_face": "π"}, | |
} | |
def ChangeTheme(): | |
previous_theme = ms.themes["current_theme"] | |
tdict = ms.themes["light"] if ms.themes["current_theme"] == "light" else ms.themes["dark"] | |
for vkey, vval in tdict.items(): | |
if vkey.startswith("theme"): st._config.set_option(vkey, vval) | |
ms.themes["refreshed"] = False | |
if previous_theme == "dark": ms.themes["current_theme"] = "light" | |
elif previous_theme == "light": ms.themes["current_theme"] = "dark" | |
btn_face = ms.themes["light"]["button_face"] if ms.themes["current_theme"] == "light" else ms.themes["dark"]["button_face"] | |
st.button(btn_face, on_click=ChangeTheme) | |
if ms.themes["refreshed"] == False: | |
ms.themes["refreshed"] = True | |
st.rerun() | |
# Load the model. Ensure you have the 'compatible_mm.h5' model file in the current directory. | |
model = load_model('mm.h5') | |
class_lists = [ | |
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "Ah", "Aha", "au2", "au3", "ay2", | |
"ba_htoat_chite", "ba_kone", "da_htway", "da_out_chite", "da_yay_hmote", "da_yin_kout", | |
"e1", "e2", "eeare", "ga_khi", "ga_nge", "ha", "hsa_lain", "hta_hsin_htu", "hta_wun_beare", | |
"ka_kji", "kha_khway", "la", "la_kji", "ma", "na_kji", "na_ngear", "nga", "nga_kyi", "O", | |
"pa_sout", "pfa_u_htoat", "sah_lone", "ta_thun_lyin_chate", "ta_wun_pu", "tha", "u1", "u2", | |
"un", "wa", "yah_kout", "yah_pet_let", "za_kwear", "za_myin_hsware" | |
] | |
st.title('Burmese Character and Digit Recognizer') | |
st.markdown(''' | |
Try to write something! | |
''') | |
SIZE = 200 # Setting the size for drawing and for model input | |
# Uploading a file | |
uploaded_file = st.file_uploader("Upload your file here...") | |
# Initialize rescaled to None | |
rescaled = None | |
# Handle file upload | |
if uploaded_file is not None: | |
# Read the uploaded image file | |
bytes_data = uploaded_file.getvalue() | |
image = Image.open(io.BytesIO(bytes_data)) | |
# Convert the image to RGB (in case it's not) | |
image = image.convert('RGB') | |
# Resize the image to match the model's expected input | |
image = image.resize((SIZE, SIZE), Image.Resampling.LANCZOS) | |
# Convert the image to a numpy array and expand dimensions | |
rescaled = np.expand_dims(np.array(image), axis=0) | |
# Handle canvas drawing | |
else: | |
# Setting up the canvas | |
canvas_result = st_canvas( | |
fill_color="rgba(255, 165, 0, 0.3)", # Fixed fill color with some opacity | |
stroke_width=10, | |
stroke_color='#FFFFFF', | |
background_color="#000000", | |
background_image=None, | |
update_streamlit=True, | |
height=SIZE, | |
width=SIZE, | |
drawing_mode="freedraw", | |
key="canvas", | |
) | |
# If the user draws on the canvas | |
if canvas_result.image_data is not None: | |
# Convert the canvas drawing to an image | |
canvas_image = Image.fromarray((canvas_result.image_data).astype('uint8'), mode='RGBA') | |
canvas_image = canvas_image.convert('RGB') # Convert to RGB | |
canvas_image = canvas_image.resize((SIZE, SIZE), Image.Resampling.LANCZOS) # Resize to model's expected input size | |
# Convert to numpy and adjust dimensions | |
rescaled = np.expand_dims(np.array(canvas_image), axis=0) | |
if st.button('Predict') and rescaled is not None: | |
# Predict the class of the input image | |
val = model.predict(rescaled) | |
predicted_class_index = np.argmax(val) | |
mm_text = class_lists[predicted_class_index] | |
st.write(f'Result: {mm_text}, Index: {predicted_class_index}') | |
st.bar_chart(val[0]) | |