Mattral commited on
Commit
6dd2ec6
·
verified ·
1 Parent(s): 80f1629

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +126 -62
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
- def rectangle(image, result):
10
- """Draw rectangles on image based on predicted coordinates."""
11
- draw = ImageDraw.Draw(image)
12
- for res in result:
13
- top_left = tuple(res[0][0]) # top left coordinates as tuple
14
- bottom_right = tuple(res[0][2]) # bottom right coordinates as tuple
15
- draw.rectangle((top_left, bottom_right), outline="blue", width=2)
16
- # Display image on streamlit
17
- st.image(image)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
- # Main title
20
- st.title("Get text from image with EasyOCR")
 
 
 
21
 
22
- # Subtitle
23
- st.markdown("## EasyOCR with Streamlit")
24
 
25
- # Upload image file or draw
26
- st.markdown("## Upload an Image or Draw")
27
- col1, col2 = st.columns(2)
 
 
28
 
29
- with col1:
30
- file = st.file_uploader("Upload Here", type=['png', 'jpg', 'jpeg'])
 
 
31
 
32
- with col2:
33
- # Drawable canvas
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
- # Process uploaded image or drawing
48
- if file is not None:
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
- if image is not None:
57
- st.image(image) # Display
 
 
 
 
 
 
 
 
 
 
 
58
 
59
- # Only detect the English and Turkish part of the image as text
60
- reader = easyocr.Reader(['en','ja'], gpu=False)
61
- result = reader.readtext(np.array(image)) # Turn image to numpy array
 
 
 
62
 
63
- # Print all predicted text:
64
- for idx, res in enumerate(result):
65
- pred_text = res[1]
66
- st.write(pred_text)
67
-
68
- # Collect the results in the dictionary:
69
- textdic_easyocr = {res[1]: {'pred_confidence': res[2]} for res in result}
70
 
71
- # Create a data frame which shows the predicted text and prediction confidence
72
- df = pd.DataFrame.from_dict(textdic_easyocr).T
73
- st.table(df)
74
 
75
- # Get boxes on the image
76
- rectangle(image, result)
 
 
 
 
 
 
 
 
 
 
 
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.")