File size: 4,638 Bytes
9733b49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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])