Mattral commited on
Commit
8881f50
Β·
verified Β·
1 Parent(s): c02975b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -227
app.py CHANGED
@@ -1,230 +1,9 @@
1
  import os
2
- import cv2
3
- import numpy as np
4
- from PIL import Image
5
- from path import Path
6
- import streamlit as st
7
- from typing import Tuple
8
- import easyocr # Import EasyOCR
9
 
10
- from pathlib import Path
11
- import sys
 
12
 
13
- # Add the 'app' directory to the sys.path
14
- # Assuming 'app' is in the current working directory
15
- sys.path.append(str(Path(__file__).parent / 'app'))
16
- from app.dataloader_iam import Batch
17
- from app.model import Model, DecoderType
18
- from app.preprocessor import Preprocessor
19
- from streamlit_drawable_canvas import st_canvas
20
-
21
- # Set page config at the very beginning (only executed once)
22
- st.set_page_config(
23
- page_title="HTR App",
24
- page_icon=":pencil:",
25
- layout="centered",
26
- initial_sidebar_state="auto",
27
- )
28
-
29
- ms = st.session_state
30
- if "themes" not in ms:
31
- ms.themes = {"current_theme": "light",
32
- "refreshed": True,
33
-
34
- "light": {"theme.base": "dark",
35
- "theme.backgroundColor": "black",
36
- "theme.primaryColor": "#c98bdb",
37
- "theme.secondaryBackgroundColor": "#5591f5",
38
- "theme.textColor": "white",
39
- "theme.textColor": "white",
40
- "button_face": "🌜"},
41
-
42
- "dark": {"theme.base": "light",
43
- "theme.backgroundColor": "white",
44
- "theme.primaryColor": "#5591f5",
45
- "theme.secondaryBackgroundColor": "#82E1D7",
46
- "theme.textColor": "#0a1464",
47
- "button_face": "🌞"},
48
- }
49
-
50
-
51
- def ChangeTheme():
52
- previous_theme = ms.themes["current_theme"]
53
- tdict = ms.themes["light"] if ms.themes["current_theme"] == "light" else ms.themes["dark"]
54
- for vkey, vval in tdict.items():
55
- if vkey.startswith("theme"): st._config.set_option(vkey, vval)
56
-
57
- ms.themes["refreshed"] = False
58
- if previous_theme == "dark": ms.themes["current_theme"] = "light"
59
- elif previous_theme == "light": ms.themes["current_theme"] = "dark"
60
-
61
-
62
- btn_face = ms.themes["light"]["button_face"] if ms.themes["current_theme"] == "light" else ms.themes["dark"]["button_face"]
63
- st.button(btn_face, on_click=ChangeTheme)
64
-
65
- if ms.themes["refreshed"] == False:
66
- ms.themes["refreshed"] = True
67
- st.rerun()
68
-
69
-
70
- def get_img_size(line_mode: bool = False) -> Tuple[int, int]:
71
- """
72
- Auxiliary method that sets the height and width
73
- Height is fixed while width is set according to the Model used.
74
- """
75
- if line_mode:
76
- return 256, get_img_height()
77
- return 128, get_img_height()
78
-
79
- def get_img_height() -> int:
80
- """
81
- Auxiliary method that sets the height, which is fixed for the Neural Network.
82
- """
83
- return 32
84
-
85
- def infer(line_mode: bool, model: Model, fn_img: Path) -> None:
86
- """
87
- Auxiliary method that does inference using the pretrained models:
88
- Recognizes text in an image given its path.
89
- """
90
- img = cv2.imread(fn_img, cv2.IMREAD_GRAYSCALE)
91
- assert img is not None
92
-
93
- preprocessor = Preprocessor(get_img_size(line_mode), dynamic_width=True, padding=16)
94
- img = preprocessor.process_img(img)
95
-
96
- batch = Batch([img], None, 1)
97
- recognized, probability = model.infer_batch(batch, True)
98
- return [recognized, probability]
99
-
100
- def infer_super_model(image_path) -> None:
101
- reader = easyocr.Reader(['en']) # Initialize EasyOCR reader
102
- result = reader.readtext(image_path)
103
- recognized_texts = [text[1] for text in result] # Extract recognized texts
104
- probabilities = [text[2] for text in result] # Extract probabilities
105
- return recognized_texts, probabilities
106
-
107
-
108
-
109
- def main():
110
-
111
- st.title('Extract text from Image Demo')
112
-
113
- st.markdown("""
114
- Streamlit Web Interface for Handwritten Text Recognition (HTR), Optical Character Recognition (OCR)
115
- implemented with TensorFlow and trained on the IAM off-line HTR dataset.
116
- The model takes images of single words or text lines (multiple words) as input and outputs the recognized text.
117
- """, unsafe_allow_html=True)
118
-
119
- st.markdown("""
120
- Predictions can be made using one of two models:
121
- - Single_Model (Trained on Single Word Images)
122
- - Line_Model (Trained on Text Line Images)
123
- - Super_Model ( Most Robust Option for English )
124
- - Burmese (Link)
125
- """, unsafe_allow_html=True)
126
-
127
- st.subheader('Select a Model, Choose the Arguments and Draw in the box below or Upload an Image to obtain a prediction.')
128
-
129
- #Selectors for the model and decoder
130
- modelSelect = st.selectbox("Select a Model", ['Single_Model', 'Line_Model', 'Super_Model'])
131
-
132
-
133
- if modelSelect != 'Super_Model':
134
- decoderSelect = st.selectbox("Select a Decoder", ['Bestpath', 'Beamsearch', 'Wordbeamsearch'])
135
-
136
-
137
- #Mappings (dictionaries) for the model and decoder. Asigns the directory or the DecoderType of the selected option.
138
- modelMapping = {
139
- "Single_Model": '../model/word-model',
140
- "Line_Model": '../model/line-model'
141
- }
142
-
143
- decoderMapping = {
144
- 'Bestpath': DecoderType.BestPath,
145
- 'Beamsearch': DecoderType.BeamSearch,
146
- 'Wordbeamsearch': DecoderType.WordBeamSearch
147
- }
148
-
149
- #Slider for pencil width
150
- strokeWidth = st.slider("Stroke Width: ", 1, 25, 6)
151
-
152
- #Canvas/Text Box for user input. BackGround Color must be white (#FFFFFF) or else text will not be properly recognised.
153
- inputDrawn = st_canvas(
154
- fill_color="rgba(255, 165, 0, 0.3)",
155
- stroke_width=strokeWidth,
156
- update_streamlit=True,
157
- background_image=None,
158
- height = 200,
159
- width = 400,
160
- drawing_mode='freedraw',
161
- key="canvas",
162
- background_color = '#FFFFFF'
163
- )
164
-
165
- #Buffer for user input (images uploaded from the user's device)
166
- inputBuffer = st.file_uploader("Upload an Image", type=["png"])
167
-
168
- #Inference Button
169
- inferBool = st.button("Recognize Text")
170
-
171
- # After clicking the "Recognize Text" button, check if the model selected is Super_Model
172
- if inferBool:
173
- if modelSelect == 'Super_Model':
174
- inputArray = None # Initialize inputArray to None
175
-
176
- # Handling uploaded file
177
- if inputBuffer is not None:
178
- with Image.open(inputBuffer).convert('RGB') as img:
179
- inputArray = np.array(img)
180
-
181
- # Handling canvas data
182
- elif inputDrawn.image_data is not None:
183
- # Convert RGBA to RGB
184
- inputArray = cv2.cvtColor(np.array(inputDrawn.image_data, dtype=np.uint8), cv2.COLOR_RGBA2RGB)
185
-
186
- # Now check if inputArray has been set
187
- if inputArray is not None:
188
- # Initialize EasyOCR Reader
189
- reader = easyocr.Reader(['en']) # Assuming English language; adjust as necessary
190
- # Perform OCR
191
- results = reader.readtext(inputArray)
192
-
193
- # Display results
194
- all_text = ''
195
- for (bbox, text, prob) in results:
196
- all_text += f'{text} (confidence: {prob:.2f})\n'
197
-
198
- st.write("**Recognized Texts and their Confidence Scores:**")
199
- st.text(all_text)
200
- else:
201
- st.write("No image data found. Please upload an image or draw on the canvas.")
202
-
203
-
204
- else:
205
- # Handle other model selections as before
206
- if ((inputDrawn.image_data is not None or inputBuffer is not None) and inferBool == True):
207
- #We turn the input into a numpy array
208
- if inputDrawn.image_data is not None:
209
- inputArray = np.array(inputDrawn.image_data)
210
-
211
- if inputBuffer is not None:
212
- inputBufferImage = Image.open(inputBuffer)
213
- inputArray = np.array(inputBufferImage)
214
-
215
- #We turn this array into a .png format and save it.
216
- inputImage = Image.fromarray(inputArray.astype('uint8'), 'RGBA')
217
- inputImage.save('userInput.png')
218
- #We obtain the model directory and the decoder type from their mapping
219
- modelDir = modelMapping[modelSelect]
220
- decoderType = decoderMapping[decoderSelect]
221
-
222
- #Finally, we call the model with this image as attribute and display the Best Candidate and its probability on the Interface
223
- model = Model(list(open(modelDir + "/charList.txt").read()), modelDir, decoderType, must_restore=True)
224
- inferedText = infer(modelDir == '../model/line-model', model, 'userInput.png')
225
-
226
- st.write("**Best Candidate: **", inferedText[0][0])
227
- st.write("**Probability: **", str(inferedText[1][0]*100) + "%")
228
-
229
- if __name__ == "__main__":
230
- main()
 
1
  import os
2
+ import subprocess
 
 
 
 
 
 
3
 
4
+ # Set the directory path of runner.py inside the 'app' folder
5
+ app_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'app')
6
+ runner_path = os.path.join(app_dir, 'runner.py')
7
 
8
+ # Run runner.py using its directory path
9
+ subprocess.run(["python", runner_path], cwd=app_dir)