Update app.py
Browse files
app.py
CHANGED
@@ -1,140 +1,79 @@
|
|
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 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
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 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
|
|
|
|
|
|
|
|
92 |
|
93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
col1, col2 = st.columns(2)
|
95 |
-
|
96 |
with col1:
|
97 |
-
|
98 |
-
|
99 |
with col2:
|
100 |
-
|
101 |
canvas_result = st_canvas(
|
102 |
fill_color="rgba(255, 165, 0, 0.3)",
|
103 |
-
stroke_width=
|
104 |
stroke_color="#ffffff",
|
105 |
background_color="#000000",
|
106 |
update_streamlit=True,
|
107 |
-
width=
|
108 |
-
height=
|
109 |
drawing_mode="freedraw",
|
110 |
key="canvas",
|
111 |
)
|
112 |
|
113 |
-
# Process
|
114 |
-
|
115 |
-
|
116 |
-
image_data = Image.open(uploaded_file).convert('RGB')
|
117 |
elif canvas_result.image_data is not None:
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
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 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
else:
|
139 |
-
|
140 |
-
|
|
|
|
|
|
1 |
import streamlit as st
|
|
|
|
|
2 |
import numpy as np
|
|
|
|
|
|
|
3 |
import pandas as pd
|
4 |
+
import easyocr
|
5 |
+
from PIL import Image
|
6 |
+
from streamlit_drawable_canvas import st_canvas
|
7 |
|
8 |
+
def rectangle(image, result):
|
9 |
+
"""Draw rectangles on image based on predicted coordinates using PIL."""
|
10 |
+
from PIL import ImageDraw
|
11 |
+
draw = ImageDraw.Draw(image)
|
12 |
+
for res in result:
|
13 |
+
top_left = tuple(res[0][0])
|
14 |
+
bottom_right = tuple(res[0][2])
|
15 |
+
draw.rectangle((top_left, bottom_right), outline="blue", width=2)
|
16 |
+
return image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
+
def main():
|
19 |
+
# Set up Streamlit page
|
20 |
+
st.set_page_config(
|
21 |
+
page_title="OCR App",
|
22 |
+
page_icon=":mag:",
|
23 |
+
layout="centered",
|
24 |
+
initial_sidebar_state="auto",
|
25 |
+
)
|
26 |
|
27 |
+
st.title("Optical Character Recognition (OCR) with EasyOCR")
|
28 |
+
|
29 |
+
st.markdown("""
|
30 |
+
Upload an image or use the canvas to draw, and the app will recognize and extract text from it.
|
31 |
+
Supported languages for recognition include English and Japanese.
|
32 |
+
""")
|
33 |
+
|
34 |
+
# Upload image or draw
|
35 |
col1, col2 = st.columns(2)
|
|
|
36 |
with col1:
|
37 |
+
file = st.file_uploader("Upload Image", type=['png', 'jpg', 'jpeg'])
|
|
|
38 |
with col2:
|
39 |
+
stroke_width = st.slider("Stroke Width: ", 1, 25, 3)
|
40 |
canvas_result = st_canvas(
|
41 |
fill_color="rgba(255, 165, 0, 0.3)",
|
42 |
+
stroke_width=stroke_width,
|
43 |
stroke_color="#ffffff",
|
44 |
background_color="#000000",
|
45 |
update_streamlit=True,
|
46 |
+
width=400,
|
47 |
+
height=400,
|
48 |
drawing_mode="freedraw",
|
49 |
key="canvas",
|
50 |
)
|
51 |
|
52 |
+
# Process uploaded image or drawing
|
53 |
+
if file is not None:
|
54 |
+
image = Image.open(file).convert('RGB')
|
|
|
55 |
elif canvas_result.image_data is not None:
|
56 |
+
image = Image.fromarray(np.array(canvas_result.image_data).astype('uint8'), 'RGBA').convert('RGB')
|
57 |
+
else:
|
58 |
+
st.warning("Please upload an image or use the canvas to draw.")
|
59 |
+
st.stop()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
+
st.image(image, caption='Uploaded Image', use_column_width=True)
|
62 |
+
|
63 |
+
# OCR
|
64 |
+
reader = easyocr.Reader(['en', 'ja'], gpu=False)
|
65 |
+
result = reader.readtext(np.array(image))
|
66 |
+
|
67 |
+
# Display results
|
68 |
+
if result:
|
69 |
+
image = rectangle(image, result)
|
70 |
+
st.image(image, caption='Processed Image with Detected Text', use_column_width=True)
|
71 |
+
|
72 |
+
textdic_easyocr = {idx: {'text': res[1], 'confidence': res[2]} for idx, res in enumerate(result)}
|
73 |
+
df = pd.DataFrame.from_dict(textdic_easyocr, orient='index')
|
74 |
+
st.dataframe(df)
|
75 |
else:
|
76 |
+
st.info("No text detected.")
|
77 |
+
|
78 |
+
if __name__ == "__main__":
|
79 |
+
main()
|