Mattral commited on
Commit
9733b49
·
verified ·
1 Parent(s): 7bec65a

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +121 -0
  2. mm.h5 +3 -0
  3. uploaded_image.png +0 -0
app.py ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from streamlit_drawable_canvas import st_canvas
3
+ import cv2
4
+ from tensorflow.keras.models import load_model
5
+ import numpy as np
6
+ from PIL import Image
7
+ #!pip install Pillow==9.0.0
8
+ import io
9
+
10
+ import streamlit as st
11
+
12
+ ms = st.session_state
13
+ if "themes" not in ms:
14
+ ms.themes = {"current_theme": "light",
15
+ "refreshed": True,
16
+
17
+ "light": {"theme.base": "dark",
18
+ "theme.backgroundColor": "black",
19
+ "theme.primaryColor": "#c98bdb",
20
+ "theme.secondaryBackgroundColor": "#5591f5",
21
+ "theme.textColor": "white",
22
+ "theme.textColor": "white",
23
+ "button_face": "🌜"},
24
+
25
+ "dark": {"theme.base": "light",
26
+ "theme.backgroundColor": "white",
27
+ "theme.primaryColor": "#5591f5",
28
+ "theme.secondaryBackgroundColor": "#82E1D7",
29
+ "theme.textColor": "#0a1464",
30
+ "button_face": "🌞"},
31
+ }
32
+
33
+
34
+ def ChangeTheme():
35
+ previous_theme = ms.themes["current_theme"]
36
+ tdict = ms.themes["light"] if ms.themes["current_theme"] == "light" else ms.themes["dark"]
37
+ for vkey, vval in tdict.items():
38
+ if vkey.startswith("theme"): st._config.set_option(vkey, vval)
39
+
40
+ ms.themes["refreshed"] = False
41
+ if previous_theme == "dark": ms.themes["current_theme"] = "light"
42
+ elif previous_theme == "light": ms.themes["current_theme"] = "dark"
43
+
44
+
45
+ btn_face = ms.themes["light"]["button_face"] if ms.themes["current_theme"] == "light" else ms.themes["dark"]["button_face"]
46
+ st.button(btn_face, on_click=ChangeTheme)
47
+
48
+ if ms.themes["refreshed"] == False:
49
+ ms.themes["refreshed"] = True
50
+ st.rerun()
51
+
52
+ # Load the model. Ensure you have the 'compatible_mm.h5' model file in the current directory.
53
+ model = load_model('mm.h5')
54
+
55
+ class_lists = [
56
+ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "Ah", "Aha", "au2", "au3", "ay2",
57
+ "ba_htoat_chite", "ba_kone", "da_htway", "da_out_chite", "da_yay_hmote", "da_yin_kout",
58
+ "e1", "e2", "eeare", "ga_khi", "ga_nge", "ha", "hsa_lain", "hta_hsin_htu", "hta_wun_beare",
59
+ "ka_kji", "kha_khway", "la", "la_kji", "ma", "na_kji", "na_ngear", "nga", "nga_kyi", "O",
60
+ "pa_sout", "pfa_u_htoat", "sah_lone", "ta_thun_lyin_chate", "ta_wun_pu", "tha", "u1", "u2",
61
+ "un", "wa", "yah_kout", "yah_pet_let", "za_kwear", "za_myin_hsware"
62
+ ]
63
+
64
+ st.title('Burmese Character and Digit Recognizer')
65
+ st.markdown('''
66
+ Try to write something!
67
+ ''')
68
+
69
+ SIZE = 200 # Setting the size for drawing and for model input
70
+
71
+
72
+ # Uploading a file
73
+ uploaded_file = st.file_uploader("Upload your file here...")
74
+
75
+ # Initialize rescaled to None
76
+ rescaled = None
77
+
78
+ # Handle file upload
79
+ if uploaded_file is not None:
80
+ # Read the uploaded image file
81
+ bytes_data = uploaded_file.getvalue()
82
+ image = Image.open(io.BytesIO(bytes_data))
83
+ # Convert the image to RGB (in case it's not)
84
+ image = image.convert('RGB')
85
+ # Resize the image to match the model's expected input
86
+ image = image.resize((SIZE, SIZE), Image.Resampling.LANCZOS)
87
+ # Convert the image to a numpy array and expand dimensions
88
+ rescaled = np.expand_dims(np.array(image), axis=0)
89
+
90
+ # Handle canvas drawing
91
+ else:
92
+ # Setting up the canvas
93
+ canvas_result = st_canvas(
94
+ fill_color="rgba(255, 165, 0, 0.3)", # Fixed fill color with some opacity
95
+ stroke_width=10,
96
+ stroke_color='#FFFFFF',
97
+ background_color="#000000",
98
+ background_image=None,
99
+ update_streamlit=True,
100
+ height=SIZE,
101
+ width=SIZE,
102
+ drawing_mode="freedraw",
103
+ key="canvas",
104
+ )
105
+
106
+ # If the user draws on the canvas
107
+ if canvas_result.image_data is not None:
108
+ # Convert the canvas drawing to an image
109
+ canvas_image = Image.fromarray((canvas_result.image_data).astype('uint8'), mode='RGBA')
110
+ canvas_image = canvas_image.convert('RGB') # Convert to RGB
111
+ canvas_image = canvas_image.resize((SIZE, SIZE), Image.Resampling.LANCZOS) # Resize to model's expected input size
112
+ # Convert to numpy and adjust dimensions
113
+ rescaled = np.expand_dims(np.array(canvas_image), axis=0)
114
+
115
+ if st.button('Predict') and rescaled is not None:
116
+ # Predict the class of the input image
117
+ val = model.predict(rescaled)
118
+ predicted_class_index = np.argmax(val)
119
+ mm_text = class_lists[predicted_class_index]
120
+ st.write(f'Result: {mm_text}, Index: {predicted_class_index}')
121
+ st.bar_chart(val[0])
mm.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ebfcb6e61feb618a4e9bf834d9fa9b26ddd60a9560fcc2faafe4659f943c39a1
3
+ size 100657952
uploaded_image.png ADDED