Update app.py
Browse files
app.py
CHANGED
|
@@ -1,76 +1,140 @@
|
|
| 1 |
-
import pandas as pd
|
| 2 |
-
import numpy as np
|
| 3 |
import streamlit as st
|
| 4 |
-
import easyocr
|
| 5 |
-
import PIL
|
| 6 |
-
from PIL import Image, ImageDraw
|
| 7 |
from streamlit_drawable_canvas import st_canvas
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
st.title(
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
st.
|
| 24 |
|
| 25 |
-
|
| 26 |
-
st.
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
canvas_result = st_canvas(
|
| 35 |
-
fill_color="rgba(255, 165, 0, 0.3)",
|
| 36 |
-
stroke_width=3,
|
| 37 |
-
stroke_color="#ffffff",
|
| 38 |
-
background_color="#000000",
|
| 39 |
-
background_image=None if file else st.session_state.get("background", None),
|
| 40 |
-
update_streamlit=True,
|
| 41 |
-
width=400,
|
| 42 |
-
height=400,
|
| 43 |
-
drawing_mode="freedraw",
|
| 44 |
-
key="canvas",
|
| 45 |
-
)
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
image = Image.open(file) # Read image with PIL library
|
| 50 |
-
elif canvas_result.image_data is not None:
|
| 51 |
-
image = Image.fromarray(canvas_result.image_data.astype('uint8'), 'RGBA').convert('RGB')
|
| 52 |
-
else:
|
| 53 |
-
st.write("Please upload an image or use the canvas to draw.")
|
| 54 |
-
image = None
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
#
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
st.table(df)
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
| 2 |
from streamlit_drawable_canvas import st_canvas
|
| 3 |
+
import cv2
|
| 4 |
+
import numpy as np
|
| 5 |
+
from tensorflow.keras.models import load_model
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import easyocr
|
| 8 |
+
import pandas as pd
|
| 9 |
+
|
| 10 |
+
# Load the model for Myanmar character recognition
|
| 11 |
+
model = load_model('mm.h5')
|
| 12 |
+
|
| 13 |
+
# Initialize EasyOCR reader for English
|
| 14 |
+
reader = easyocr.Reader(['en'], gpu=False)
|
| 15 |
|
| 16 |
+
class_lists = [
|
| 17 |
+
"0",
|
| 18 |
+
"1",
|
| 19 |
+
"2",
|
| 20 |
+
"3",
|
| 21 |
+
"4",
|
| 22 |
+
"5",
|
| 23 |
+
"6",
|
| 24 |
+
"7",
|
| 25 |
+
"8",
|
| 26 |
+
"9",
|
| 27 |
+
"Ah",
|
| 28 |
+
"Aha",
|
| 29 |
+
"au2",
|
| 30 |
+
"au3",
|
| 31 |
+
"ay2",
|
| 32 |
+
"ba_htoat_chite",
|
| 33 |
+
"ba_kone",
|
| 34 |
+
"da_htway",
|
| 35 |
+
"da_out_chite",
|
| 36 |
+
"da_yay_hmote",
|
| 37 |
+
"da_yin_kout",
|
| 38 |
+
"e1",
|
| 39 |
+
"e2",
|
| 40 |
+
"eeare",
|
| 41 |
+
"ga_khi",
|
| 42 |
+
"ga_nge",
|
| 43 |
+
"ha",
|
| 44 |
+
"hsa_lain",
|
| 45 |
+
"hta_hsin_htu",
|
| 46 |
+
"hta_wun_beare",
|
| 47 |
+
"ka_kji",
|
| 48 |
+
"kha_khway",
|
| 49 |
+
"la",
|
| 50 |
+
"la_kji",
|
| 51 |
+
"ma",
|
| 52 |
+
"na_kji",
|
| 53 |
+
"na_ngear",
|
| 54 |
+
"nga",
|
| 55 |
+
"nga_kyi",
|
| 56 |
+
"O",
|
| 57 |
+
"pa_sout",
|
| 58 |
+
"pfa_u_htoat",
|
| 59 |
+
"sah_lone",
|
| 60 |
+
"ta_thun_lyin_chate",
|
| 61 |
+
"ta_wun_pu",
|
| 62 |
+
"tha",
|
| 63 |
+
"u1",
|
| 64 |
+
"u2",
|
| 65 |
+
"un",
|
| 66 |
+
"wa",
|
| 67 |
+
"yah_kout",
|
| 68 |
+
"yah_pet_let",
|
| 69 |
+
"za_kwear",
|
| 70 |
+
"za_myin_hsware"
|
| 71 |
+
]
|
| 72 |
|
| 73 |
+
# Streamlit UI
|
| 74 |
+
st.title('Text and Character Recognizer')
|
| 75 |
+
st.markdown('''
|
| 76 |
+
Select the mode for recognition:
|
| 77 |
+
''')
|
| 78 |
|
| 79 |
+
# Choose mode
|
| 80 |
+
mode = st.radio("Mode", ('English Text Recognition', 'Myanmar Character Recognition'))
|
| 81 |
|
| 82 |
+
if mode == 'English Text Recognition':
|
| 83 |
+
uploaded_file = st.file_uploader("Upload your file here...", key="uploader_english")
|
| 84 |
+
if uploaded_file is not None:
|
| 85 |
+
image = Image.open(uploaded_file)
|
| 86 |
+
st.image(image, caption='Uploaded Image', use_column_width=True)
|
| 87 |
|
| 88 |
+
# EasyOCR to recognize text
|
| 89 |
+
result = reader.readtext(np.array(image))
|
| 90 |
+
for detection in result:
|
| 91 |
+
st.write(f'Detected text: {detection[1]}, Confidence: {detection[2]}')
|
| 92 |
|
| 93 |
+
elif mode == 'Myanmar Character Recognition':
|
| 94 |
+
col1, col2 = st.columns(2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
+
with col1:
|
| 97 |
+
uploaded_file = st.file_uploader("Upload your file here...", key="uploader_myanmar")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
+
with col2:
|
| 100 |
+
# Initialize canvas
|
| 101 |
+
canvas_result = st_canvas(
|
| 102 |
+
fill_color="rgba(255, 165, 0, 0.3)",
|
| 103 |
+
stroke_width=3,
|
| 104 |
+
stroke_color="#ffffff",
|
| 105 |
+
background_color="#000000",
|
| 106 |
+
update_streamlit=True,
|
| 107 |
+
width=200,
|
| 108 |
+
height=200,
|
| 109 |
+
drawing_mode="freedraw",
|
| 110 |
+
key="canvas",
|
| 111 |
+
)
|
| 112 |
|
| 113 |
+
# Process the image for prediction
|
| 114 |
+
image_data = None
|
| 115 |
+
if uploaded_file is not None:
|
| 116 |
+
image_data = Image.open(uploaded_file).convert('RGB')
|
| 117 |
+
elif canvas_result.image_data is not None:
|
| 118 |
+
image_data = Image.fromarray(np.uint8(canvas_result.image_data)).convert('RGB')
|
| 119 |
|
| 120 |
+
if image_data is not None:
|
| 121 |
+
# Convert PIL image to OpenCV format
|
| 122 |
+
image_cv = np.array(image_data)
|
| 123 |
+
image_cv = cv2.cvtColor(image_cv, cv2.COLOR_RGB2BGR)
|
| 124 |
+
resized_image = cv2.resize(image_cv, (200, 200))
|
| 125 |
+
# Prepare image for model input
|
| 126 |
+
model_input = resized_image[np.newaxis, :, :, :3]
|
| 127 |
|
| 128 |
+
st.write('Model Input')
|
| 129 |
+
st.image(model_input, width=200) # Display the input image to model
|
|
|
|
| 130 |
|
| 131 |
+
if st.button('Predict Myanmar Character'):
|
| 132 |
+
# Predict the class
|
| 133 |
+
val = model.predict(model_input)
|
| 134 |
+
predicted_class_index = np.argmax(val)
|
| 135 |
+
mm_text = class_lists[predicted_class_index]
|
| 136 |
+
st.write(f'Result: {mm_text}, Index: {predicted_class_index}')
|
| 137 |
+
st.bar_chart(val[0])
|
| 138 |
+
else:
|
| 139 |
+
if mode == 'Myanmar Character Recognition':
|
| 140 |
+
st.write("Please upload an image or draw in the canvas above.")
|