Mattral commited on
Commit
849cfbb
·
verified ·
1 Parent(s): c0e0e46

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -119
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
- # 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.")
 
 
 
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()