Spaces:
Sleeping
Sleeping
test docker & added singeltons
Browse files- Dockerfile +1 -1
- app.py +3 -343
- helper/examples/examples.py +12 -4
- src/htr_pipeline/gradio_backend.py +4 -2
- htr_tool.py → tabs/htr_tool.py +0 -0
- tabs/stepwise_htr_tool.py +342 -0
Dockerfile
CHANGED
@@ -33,4 +33,4 @@ WORKDIR $HOME/app
|
|
33 |
# Copy the current directory contents into the container at $HOME/app setting the owner to the user
|
34 |
COPY --chown=user . $HOME/app
|
35 |
|
36 |
-
CMD ["python", "app
|
|
|
33 |
# Copy the current directory contents into the container at $HOME/app setting the owner to the user
|
34 |
COPY --chown=user . $HOME/app
|
35 |
|
36 |
+
CMD ["python", "app.py"]
|
app.py
CHANGED
@@ -1,17 +1,9 @@
|
|
1 |
-
import os
|
2 |
-
import shutil
|
3 |
-
|
4 |
import gradio as gr
|
5 |
|
6 |
-
from helper.examples.examples import DemoImages
|
7 |
from helper.gradio_config import css, js, theme
|
8 |
from helper.text import TextAbout, TextApp, TextHowTo, TextRiksarkivet, TextRoadmap
|
9 |
-
from htr_tool import htr_tool_tab
|
10 |
-
from
|
11 |
-
|
12 |
-
model_loader = SingletonModelLoader()
|
13 |
-
custom_track = CustomTrack(model_loader)
|
14 |
-
images_for_demo = DemoImages()
|
15 |
|
16 |
with gr.Blocks(title="HTR Riksarkivet", theme=theme, css=css) as demo:
|
17 |
gr.Markdown(TextApp.title_markdown)
|
@@ -21,239 +13,7 @@ with gr.Blocks(title="HTR Riksarkivet", theme=theme, css=css) as demo:
|
|
21 |
htr_tool_tab.render()
|
22 |
|
23 |
with gr.Tab("Stepwise HTR Tool"):
|
24 |
-
|
25 |
-
with gr.Tab("1. Region Segmentation"):
|
26 |
-
with gr.Row():
|
27 |
-
with gr.Column(scale=2):
|
28 |
-
vis_data_folder_placeholder = gr.Markdown(visible=False)
|
29 |
-
name_files_placeholder = gr.Markdown(visible=False)
|
30 |
-
|
31 |
-
with gr.Row():
|
32 |
-
input_region_image = gr.Image(
|
33 |
-
label="Image to Region segment",
|
34 |
-
# type="numpy",
|
35 |
-
tool="editor",
|
36 |
-
).style(height=350)
|
37 |
-
|
38 |
-
with gr.Accordion("Region segment settings:", open=False):
|
39 |
-
with gr.Row():
|
40 |
-
reg_pred_score_threshold_slider = gr.Slider(
|
41 |
-
minimum=0.4,
|
42 |
-
maximum=1,
|
43 |
-
value=0.5,
|
44 |
-
step=0.05,
|
45 |
-
label="P-threshold",
|
46 |
-
info="""Filter and determine the confidence score
|
47 |
-
required for a prediction score to be considered""",
|
48 |
-
)
|
49 |
-
reg_containments_threshold_slider = gr.Slider(
|
50 |
-
minimum=0,
|
51 |
-
maximum=1,
|
52 |
-
value=0.5,
|
53 |
-
step=0.05,
|
54 |
-
label="C-threshold",
|
55 |
-
info="""The minimum required overlap or similarity
|
56 |
-
for a detected region or object to be considered valid""",
|
57 |
-
)
|
58 |
-
|
59 |
-
with gr.Row():
|
60 |
-
region_segment_model_dropdown = gr.Dropdown(
|
61 |
-
choices=["Riksarkivet/RmtDet_region"],
|
62 |
-
value="Riksarkivet/RmtDet_region",
|
63 |
-
label="Region segment model",
|
64 |
-
info="Will add more models later!",
|
65 |
-
)
|
66 |
-
|
67 |
-
with gr.Row():
|
68 |
-
clear_button = gr.Button("Clear", variant="secondary", elem_id="clear_button")
|
69 |
-
|
70 |
-
region_segment_button = gr.Button(
|
71 |
-
"Segment Region",
|
72 |
-
variant="primary",
|
73 |
-
elem_id="region_segment_button",
|
74 |
-
) # .style(full_width=False)
|
75 |
-
|
76 |
-
with gr.Row():
|
77 |
-
with gr.Accordion("Example images to use:", open=False) as example_accord:
|
78 |
-
gr.Examples(
|
79 |
-
examples=images_for_demo.examples_list,
|
80 |
-
inputs=[name_files_placeholder, input_region_image],
|
81 |
-
label="Example images",
|
82 |
-
examples_per_page=2,
|
83 |
-
)
|
84 |
-
|
85 |
-
with gr.Column(scale=3):
|
86 |
-
output_region_image = gr.Image(label="Segmented regions", type="numpy").style(height=600)
|
87 |
-
|
88 |
-
##############################################
|
89 |
-
with gr.Tab("2. Line Segmentation"):
|
90 |
-
image_placeholder_lines = gr.Image(
|
91 |
-
label="Segmented lines",
|
92 |
-
# type="numpy",
|
93 |
-
interactive="False",
|
94 |
-
visible=True,
|
95 |
-
).style(height=600)
|
96 |
-
|
97 |
-
with gr.Row(visible=False) as control_line_segment:
|
98 |
-
with gr.Column(scale=2):
|
99 |
-
with gr.Box():
|
100 |
-
regions_cropped_gallery = gr.Gallery(
|
101 |
-
label="Segmented regions",
|
102 |
-
show_label=False,
|
103 |
-
elem_id="gallery",
|
104 |
-
).style(
|
105 |
-
columns=[2],
|
106 |
-
rows=[2],
|
107 |
-
# object_fit="contain",
|
108 |
-
height=400,
|
109 |
-
preview=True,
|
110 |
-
container=False,
|
111 |
-
)
|
112 |
-
|
113 |
-
input_region_from_gallery = gr.Image(
|
114 |
-
label="Region segmentation to line segment", interactive="False", visible=False
|
115 |
-
).style(height=400)
|
116 |
-
with gr.Row():
|
117 |
-
with gr.Accordion("Line segment settings:", open=False):
|
118 |
-
with gr.Row():
|
119 |
-
line_pred_score_threshold_slider = gr.Slider(
|
120 |
-
minimum=0.3,
|
121 |
-
maximum=1,
|
122 |
-
value=0.4,
|
123 |
-
step=0.05,
|
124 |
-
label="Pred_score threshold",
|
125 |
-
info="""Filter and determine the confidence score
|
126 |
-
required for a prediction score to be considered""",
|
127 |
-
)
|
128 |
-
line_containments_threshold_slider = gr.Slider(
|
129 |
-
minimum=0,
|
130 |
-
maximum=1,
|
131 |
-
value=0.5,
|
132 |
-
step=0.05,
|
133 |
-
label="Containments threshold",
|
134 |
-
info="""The minimum required overlap or similarity
|
135 |
-
for a detected region or object to be considered valid""",
|
136 |
-
)
|
137 |
-
with gr.Row().style(equal_height=False):
|
138 |
-
line_segment_model_dropdown = gr.Dropdown(
|
139 |
-
choices=["Riksarkivet/RmtDet_lines"],
|
140 |
-
value="Riksarkivet/RmtDet_lines",
|
141 |
-
label="Line segment model",
|
142 |
-
info="Will add more models later!",
|
143 |
-
)
|
144 |
-
with gr.Row():
|
145 |
-
clear_line_segment_button = gr.Button(
|
146 |
-
" ",
|
147 |
-
variant="Secondary",
|
148 |
-
# elem_id="center_button",
|
149 |
-
).style(full_width=True)
|
150 |
-
|
151 |
-
line_segment_button = gr.Button(
|
152 |
-
"Segment Lines",
|
153 |
-
variant="primary",
|
154 |
-
# elem_id="center_button",
|
155 |
-
).style(full_width=True)
|
156 |
-
|
157 |
-
with gr.Column(scale=3):
|
158 |
-
# gr.Markdown("""lorem ipsum""")
|
159 |
-
|
160 |
-
output_line_from_region = gr.Image(
|
161 |
-
label="Segmented lines",
|
162 |
-
type="numpy",
|
163 |
-
interactive="False",
|
164 |
-
).style(height=600)
|
165 |
-
|
166 |
-
###############################################
|
167 |
-
with gr.Tab("3. Transcribe Text"):
|
168 |
-
image_placeholder_htr = gr.Image(
|
169 |
-
label="Transcribed lines",
|
170 |
-
# type="numpy",
|
171 |
-
interactive="False",
|
172 |
-
visible=True,
|
173 |
-
).style(height=600)
|
174 |
-
|
175 |
-
with gr.Row(visible=False) as control_htr:
|
176 |
-
inputs_lines_to_transcribe = gr.Variable()
|
177 |
-
|
178 |
-
with gr.Column(scale=2):
|
179 |
-
image_inputs_lines_to_transcribe = gr.Image(
|
180 |
-
label="Transcribed lines",
|
181 |
-
type="numpy",
|
182 |
-
interactive="False",
|
183 |
-
visible=False,
|
184 |
-
).style(height=470)
|
185 |
-
|
186 |
-
with gr.Row():
|
187 |
-
with gr.Accordion("Transcribe settings:", open=False):
|
188 |
-
transcriber_model = gr.Dropdown(
|
189 |
-
choices=["Riksarkivet/SATRN_transcriber", "microsoft/trocr-base-handwritten"],
|
190 |
-
value="Riksarkivet/SATRN_transcriber",
|
191 |
-
label="Transcriber model",
|
192 |
-
info="Will add more models later!",
|
193 |
-
)
|
194 |
-
with gr.Row():
|
195 |
-
clear_transcribe_button = gr.Button(" ", variant="Secondary", visible=True).style(
|
196 |
-
full_width=True
|
197 |
-
)
|
198 |
-
transcribe_button = gr.Button(
|
199 |
-
"Transcribe lines", variant="primary", visible=True
|
200 |
-
).style(full_width=True)
|
201 |
-
|
202 |
-
donwload_txt_button = gr.Button(
|
203 |
-
"Download text", variant="secondary", visible=False
|
204 |
-
).style(full_width=True)
|
205 |
-
|
206 |
-
with gr.Row():
|
207 |
-
txt_file_downlod = gr.File(label="Download text", visible=False)
|
208 |
-
|
209 |
-
with gr.Column(scale=3):
|
210 |
-
with gr.Row():
|
211 |
-
transcribed_text_df = gr.Dataframe(
|
212 |
-
headers=["Transcribed text"],
|
213 |
-
max_rows=15,
|
214 |
-
col_count=(1, "fixed"),
|
215 |
-
wrap=True,
|
216 |
-
interactive=False,
|
217 |
-
overflow_row_behaviour="paginate",
|
218 |
-
).style(height=600)
|
219 |
-
|
220 |
-
#####################################
|
221 |
-
with gr.Tab("4. Explore Results"):
|
222 |
-
image_placeholder_explore_results = gr.Image(
|
223 |
-
label="Cropped transcribed lines",
|
224 |
-
# type="numpy",
|
225 |
-
interactive="False",
|
226 |
-
visible=True,
|
227 |
-
).style(height=600)
|
228 |
-
|
229 |
-
with gr.Row(visible=False) as control_results_transcribe:
|
230 |
-
with gr.Column(scale=1, visible=True):
|
231 |
-
with gr.Box():
|
232 |
-
temp_gallery_input = gr.Variable()
|
233 |
-
|
234 |
-
gallery_inputs_lines_to_transcribe = gr.Gallery(
|
235 |
-
label="Cropped transcribed lines",
|
236 |
-
show_label=True,
|
237 |
-
elem_id="gallery_lines",
|
238 |
-
).style(
|
239 |
-
columns=[3],
|
240 |
-
rows=[3],
|
241 |
-
# object_fit="contain",
|
242 |
-
# height="600",
|
243 |
-
preview=True,
|
244 |
-
container=False,
|
245 |
-
)
|
246 |
-
with gr.Column(scale=1, visible=True):
|
247 |
-
mapping_dict = gr.Variable()
|
248 |
-
transcribed_text_df_finish = gr.Dataframe(
|
249 |
-
headers=["Transcribed text", "HTR prediction score"],
|
250 |
-
max_rows=15,
|
251 |
-
col_count=(2, "fixed"),
|
252 |
-
wrap=True,
|
253 |
-
interactive=False,
|
254 |
-
overflow_row_behaviour="paginate",
|
255 |
-
).style(height=600)
|
256 |
-
|
257 |
with gr.Tab("How to use"):
|
258 |
with gr.Tabs():
|
259 |
with gr.Tab("HTR Tool"):
|
@@ -339,104 +99,6 @@ with gr.Blocks(title="HTR Riksarkivet", theme=theme, css=css) as demo:
|
|
339 |
# flagging_button.click(lambda: (gr.update(value="Flagged")), outputs=flagging_button)
|
340 |
# fast_track_input_region_image.change(lambda: (gr.update(value="Flag")), outputs=flagging_button)
|
341 |
|
342 |
-
# custom track
|
343 |
-
region_segment_button.click(
|
344 |
-
custom_track.region_segment,
|
345 |
-
inputs=[input_region_image, reg_pred_score_threshold_slider, reg_containments_threshold_slider],
|
346 |
-
outputs=[output_region_image, regions_cropped_gallery, image_placeholder_lines, control_line_segment],
|
347 |
-
)
|
348 |
-
|
349 |
-
regions_cropped_gallery.select(
|
350 |
-
custom_track.get_select_index_image, regions_cropped_gallery, input_region_from_gallery
|
351 |
-
)
|
352 |
-
|
353 |
-
transcribed_text_df_finish.select(
|
354 |
-
fn=custom_track.get_select_index_df,
|
355 |
-
inputs=[transcribed_text_df_finish, mapping_dict],
|
356 |
-
outputs=gallery_inputs_lines_to_transcribe,
|
357 |
-
)
|
358 |
-
|
359 |
-
line_segment_button.click(
|
360 |
-
custom_track.line_segment,
|
361 |
-
inputs=[input_region_from_gallery, line_pred_score_threshold_slider, line_containments_threshold_slider],
|
362 |
-
outputs=[
|
363 |
-
output_line_from_region,
|
364 |
-
image_inputs_lines_to_transcribe,
|
365 |
-
inputs_lines_to_transcribe,
|
366 |
-
gallery_inputs_lines_to_transcribe,
|
367 |
-
temp_gallery_input,
|
368 |
-
# Hide
|
369 |
-
transcribe_button,
|
370 |
-
image_inputs_lines_to_transcribe,
|
371 |
-
image_placeholder_htr,
|
372 |
-
control_htr,
|
373 |
-
],
|
374 |
-
)
|
375 |
-
|
376 |
-
transcribe_button.click(
|
377 |
-
custom_track.transcribe_text,
|
378 |
-
inputs=[transcribed_text_df, inputs_lines_to_transcribe],
|
379 |
-
outputs=[
|
380 |
-
transcribed_text_df,
|
381 |
-
transcribed_text_df_finish,
|
382 |
-
mapping_dict,
|
383 |
-
txt_file_downlod,
|
384 |
-
control_results_transcribe,
|
385 |
-
image_placeholder_explore_results,
|
386 |
-
],
|
387 |
-
)
|
388 |
-
|
389 |
-
donwload_txt_button.click(
|
390 |
-
custom_track.download_df_to_txt,
|
391 |
-
inputs=transcribed_text_df,
|
392 |
-
outputs=[txt_file_downlod, txt_file_downlod],
|
393 |
-
)
|
394 |
-
|
395 |
-
# def remove_temp_vis():
|
396 |
-
# if os.path.exists("./vis_data"):
|
397 |
-
# os.remove("././vis_data")
|
398 |
-
# return None
|
399 |
-
|
400 |
-
clear_button.click(
|
401 |
-
lambda: (
|
402 |
-
(shutil.rmtree("./vis_data") if os.path.exists("./vis_data") else None, None)[1],
|
403 |
-
None,
|
404 |
-
None,
|
405 |
-
None,
|
406 |
-
gr.update(visible=False),
|
407 |
-
None,
|
408 |
-
None,
|
409 |
-
None,
|
410 |
-
gr.update(visible=False),
|
411 |
-
gr.update(visible=False),
|
412 |
-
gr.update(visible=True),
|
413 |
-
None,
|
414 |
-
gr.update(visible=False),
|
415 |
-
gr.update(visible=False),
|
416 |
-
gr.update(visible=True),
|
417 |
-
gr.update(visible=True),
|
418 |
-
),
|
419 |
-
inputs=[],
|
420 |
-
outputs=[
|
421 |
-
vis_data_folder_placeholder,
|
422 |
-
input_region_image,
|
423 |
-
regions_cropped_gallery,
|
424 |
-
input_region_from_gallery,
|
425 |
-
control_line_segment,
|
426 |
-
output_line_from_region,
|
427 |
-
inputs_lines_to_transcribe,
|
428 |
-
transcribed_text_df,
|
429 |
-
control_htr,
|
430 |
-
inputs_lines_to_transcribe,
|
431 |
-
image_placeholder_htr,
|
432 |
-
output_region_image,
|
433 |
-
image_inputs_lines_to_transcribe,
|
434 |
-
control_results_transcribe,
|
435 |
-
image_placeholder_explore_results,
|
436 |
-
image_placeholder_lines,
|
437 |
-
],
|
438 |
-
)
|
439 |
-
|
440 |
demo.load(None, None, None, _js=js)
|
441 |
|
442 |
|
@@ -445,5 +107,3 @@ demo.queue(concurrency_count=5, max_size=20)
|
|
445 |
|
446 |
if __name__ == "__main__":
|
447 |
demo.launch(server_name="0.0.0.0", server_port=7860, show_api=False, show_error=True)
|
448 |
-
if __name__ == "__main__":
|
449 |
-
demo.launch(server_name="0.0.0.0", server_port=7860, show_api=False, show_error=True)
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
|
|
|
3 |
from helper.gradio_config import css, js, theme
|
4 |
from helper.text import TextAbout, TextApp, TextHowTo, TextRiksarkivet, TextRoadmap
|
5 |
+
from tabs.htr_tool import htr_tool_tab
|
6 |
+
from tabs.stepwise_htr_tool import stepwise_htr_tool_tab
|
|
|
|
|
|
|
|
|
7 |
|
8 |
with gr.Blocks(title="HTR Riksarkivet", theme=theme, css=css) as demo:
|
9 |
gr.Markdown(TextApp.title_markdown)
|
|
|
13 |
htr_tool_tab.render()
|
14 |
|
15 |
with gr.Tab("Stepwise HTR Tool"):
|
16 |
+
stepwise_htr_tool_tab.render()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
with gr.Tab("How to use"):
|
18 |
with gr.Tabs():
|
19 |
with gr.Tab("HTR Tool"):
|
|
|
99 |
# flagging_button.click(lambda: (gr.update(value="Flagged")), outputs=flagging_button)
|
100 |
# fast_track_input_region_image.change(lambda: (gr.update(value="Flag")), outputs=flagging_button)
|
101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
demo.load(None, None, None, _js=js)
|
103 |
|
104 |
|
|
|
107 |
|
108 |
if __name__ == "__main__":
|
109 |
demo.launch(server_name="0.0.0.0", server_port=7860, show_api=False, show_error=True)
|
|
|
|
helper/examples/examples.py
CHANGED
@@ -5,10 +5,18 @@ from PIL import Image
|
|
5 |
|
6 |
|
7 |
class DemoImages:
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
def convert_bytes_to_images(self):
|
14 |
examples_list = []
|
|
|
5 |
|
6 |
|
7 |
class DemoImages:
|
8 |
+
_instance = None
|
9 |
+
|
10 |
+
def __new__(cls, *args, **kwargs):
|
11 |
+
if not cls._instance:
|
12 |
+
cls._instance = super(DemoImages, cls).__new__(cls, *args, **kwargs)
|
13 |
+
return cls._instance
|
14 |
+
|
15 |
+
def __init__(self, url="Riksarkivet/test_images_demo", cache_dir="./helper/examples/.cache_images"):
|
16 |
+
if not hasattr(self, "images_datasets"):
|
17 |
+
self.images_datasets = datasets.load_dataset(url, cache_dir=cache_dir)
|
18 |
+
self.example_df = self.images_datasets["train"].to_pandas()
|
19 |
+
self.examples_list = self.convert_bytes_to_images()
|
20 |
|
21 |
def convert_bytes_to_images(self):
|
22 |
examples_list = []
|
src/htr_pipeline/gradio_backend.py
CHANGED
@@ -16,8 +16,10 @@ class SingletonModelLoader:
|
|
16 |
return cls._instance
|
17 |
|
18 |
def __init__(self):
|
19 |
-
|
20 |
-
|
|
|
|
|
21 |
|
22 |
|
23 |
# fast track
|
|
|
16 |
return cls._instance
|
17 |
|
18 |
def __init__(self):
|
19 |
+
if not hasattr(self, "inferencer"):
|
20 |
+
self.inferencer = Inferencer(local_run=True)
|
21 |
+
if not hasattr(self, "pipeline"):
|
22 |
+
self.pipeline = Pipeline(self.inferencer)
|
23 |
|
24 |
|
25 |
# fast track
|
htr_tool.py → tabs/htr_tool.py
RENAMED
File without changes
|
tabs/stepwise_htr_tool.py
ADDED
@@ -0,0 +1,342 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
from helper.examples.examples import DemoImages
|
4 |
+
from src.htr_pipeline.gradio_backend import CustomTrack, SingletonModelLoader
|
5 |
+
|
6 |
+
model_loader = SingletonModelLoader()
|
7 |
+
|
8 |
+
custom_track = CustomTrack(model_loader)
|
9 |
+
|
10 |
+
images_for_demo = DemoImages()
|
11 |
+
|
12 |
+
with gr.Blocks() as stepwise_htr_tool_tab:
|
13 |
+
with gr.Tabs():
|
14 |
+
with gr.Tab("1. Region Segmentation"):
|
15 |
+
with gr.Row():
|
16 |
+
with gr.Column(scale=2):
|
17 |
+
vis_data_folder_placeholder = gr.Markdown(visible=False)
|
18 |
+
name_files_placeholder = gr.Markdown(visible=False)
|
19 |
+
|
20 |
+
with gr.Row():
|
21 |
+
input_region_image = gr.Image(
|
22 |
+
label="Image to Region segment",
|
23 |
+
# type="numpy",
|
24 |
+
tool="editor",
|
25 |
+
).style(height=350)
|
26 |
+
|
27 |
+
with gr.Accordion("Region segment settings:", open=False):
|
28 |
+
with gr.Row():
|
29 |
+
reg_pred_score_threshold_slider = gr.Slider(
|
30 |
+
minimum=0.4,
|
31 |
+
maximum=1,
|
32 |
+
value=0.5,
|
33 |
+
step=0.05,
|
34 |
+
label="P-threshold",
|
35 |
+
info="""Filter and determine the confidence score
|
36 |
+
required for a prediction score to be considered""",
|
37 |
+
)
|
38 |
+
reg_containments_threshold_slider = gr.Slider(
|
39 |
+
minimum=0,
|
40 |
+
maximum=1,
|
41 |
+
value=0.5,
|
42 |
+
step=0.05,
|
43 |
+
label="C-threshold",
|
44 |
+
info="""The minimum required overlap or similarity
|
45 |
+
for a detected region or object to be considered valid""",
|
46 |
+
)
|
47 |
+
|
48 |
+
with gr.Row():
|
49 |
+
region_segment_model_dropdown = gr.Dropdown(
|
50 |
+
choices=["Riksarkivet/RmtDet_region"],
|
51 |
+
value="Riksarkivet/RmtDet_region",
|
52 |
+
label="Region segment model",
|
53 |
+
info="Will add more models later!",
|
54 |
+
)
|
55 |
+
|
56 |
+
with gr.Row():
|
57 |
+
clear_button = gr.Button("Clear", variant="secondary", elem_id="clear_button")
|
58 |
+
|
59 |
+
region_segment_button = gr.Button(
|
60 |
+
"Segment Region",
|
61 |
+
variant="primary",
|
62 |
+
elem_id="region_segment_button",
|
63 |
+
) # .style(full_width=False)
|
64 |
+
|
65 |
+
with gr.Row():
|
66 |
+
with gr.Accordion("Example images to use:", open=False) as example_accord:
|
67 |
+
gr.Examples(
|
68 |
+
examples=images_for_demo.examples_list,
|
69 |
+
inputs=[name_files_placeholder, input_region_image],
|
70 |
+
label="Example images",
|
71 |
+
examples_per_page=2,
|
72 |
+
)
|
73 |
+
|
74 |
+
with gr.Column(scale=3):
|
75 |
+
output_region_image = gr.Image(label="Segmented regions", type="numpy").style(height=600)
|
76 |
+
|
77 |
+
##############################################
|
78 |
+
with gr.Tab("2. Line Segmentation"):
|
79 |
+
image_placeholder_lines = gr.Image(
|
80 |
+
label="Segmented lines",
|
81 |
+
# type="numpy",
|
82 |
+
interactive="False",
|
83 |
+
visible=True,
|
84 |
+
).style(height=600)
|
85 |
+
|
86 |
+
with gr.Row(visible=False) as control_line_segment:
|
87 |
+
with gr.Column(scale=2):
|
88 |
+
with gr.Box():
|
89 |
+
regions_cropped_gallery = gr.Gallery(
|
90 |
+
label="Segmented regions",
|
91 |
+
show_label=False,
|
92 |
+
elem_id="gallery",
|
93 |
+
).style(
|
94 |
+
columns=[2],
|
95 |
+
rows=[2],
|
96 |
+
# object_fit="contain",
|
97 |
+
height=400,
|
98 |
+
preview=True,
|
99 |
+
container=False,
|
100 |
+
)
|
101 |
+
|
102 |
+
input_region_from_gallery = gr.Image(
|
103 |
+
label="Region segmentation to line segment", interactive="False", visible=False
|
104 |
+
).style(height=400)
|
105 |
+
with gr.Row():
|
106 |
+
with gr.Accordion("Line segment settings:", open=False):
|
107 |
+
with gr.Row():
|
108 |
+
line_pred_score_threshold_slider = gr.Slider(
|
109 |
+
minimum=0.3,
|
110 |
+
maximum=1,
|
111 |
+
value=0.4,
|
112 |
+
step=0.05,
|
113 |
+
label="Pred_score threshold",
|
114 |
+
info="""Filter and determine the confidence score
|
115 |
+
required for a prediction score to be considered""",
|
116 |
+
)
|
117 |
+
line_containments_threshold_slider = gr.Slider(
|
118 |
+
minimum=0,
|
119 |
+
maximum=1,
|
120 |
+
value=0.5,
|
121 |
+
step=0.05,
|
122 |
+
label="Containments threshold",
|
123 |
+
info="""The minimum required overlap or similarity
|
124 |
+
for a detected region or object to be considered valid""",
|
125 |
+
)
|
126 |
+
with gr.Row().style(equal_height=False):
|
127 |
+
line_segment_model_dropdown = gr.Dropdown(
|
128 |
+
choices=["Riksarkivet/RmtDet_lines"],
|
129 |
+
value="Riksarkivet/RmtDet_lines",
|
130 |
+
label="Line segment model",
|
131 |
+
info="Will add more models later!",
|
132 |
+
)
|
133 |
+
with gr.Row():
|
134 |
+
clear_line_segment_button = gr.Button(
|
135 |
+
" ",
|
136 |
+
variant="Secondary",
|
137 |
+
# elem_id="center_button",
|
138 |
+
).style(full_width=True)
|
139 |
+
|
140 |
+
line_segment_button = gr.Button(
|
141 |
+
"Segment Lines",
|
142 |
+
variant="primary",
|
143 |
+
# elem_id="center_button",
|
144 |
+
).style(full_width=True)
|
145 |
+
|
146 |
+
with gr.Column(scale=3):
|
147 |
+
# gr.Markdown("""lorem ipsum""")
|
148 |
+
|
149 |
+
output_line_from_region = gr.Image(
|
150 |
+
label="Segmented lines",
|
151 |
+
type="numpy",
|
152 |
+
interactive="False",
|
153 |
+
).style(height=600)
|
154 |
+
|
155 |
+
###############################################
|
156 |
+
with gr.Tab("3. Transcribe Text"):
|
157 |
+
image_placeholder_htr = gr.Image(
|
158 |
+
label="Transcribed lines",
|
159 |
+
# type="numpy",
|
160 |
+
interactive="False",
|
161 |
+
visible=True,
|
162 |
+
).style(height=600)
|
163 |
+
|
164 |
+
with gr.Row(visible=False) as control_htr:
|
165 |
+
inputs_lines_to_transcribe = gr.Variable()
|
166 |
+
|
167 |
+
with gr.Column(scale=2):
|
168 |
+
image_inputs_lines_to_transcribe = gr.Image(
|
169 |
+
label="Transcribed lines",
|
170 |
+
type="numpy",
|
171 |
+
interactive="False",
|
172 |
+
visible=False,
|
173 |
+
).style(height=470)
|
174 |
+
|
175 |
+
with gr.Row():
|
176 |
+
with gr.Accordion("Transcribe settings:", open=False):
|
177 |
+
transcriber_model = gr.Dropdown(
|
178 |
+
choices=["Riksarkivet/SATRN_transcriber", "microsoft/trocr-base-handwritten"],
|
179 |
+
value="Riksarkivet/SATRN_transcriber",
|
180 |
+
label="Transcriber model",
|
181 |
+
info="Will add more models later!",
|
182 |
+
)
|
183 |
+
with gr.Row():
|
184 |
+
clear_transcribe_button = gr.Button(" ", variant="Secondary", visible=True).style(
|
185 |
+
full_width=True
|
186 |
+
)
|
187 |
+
transcribe_button = gr.Button("Transcribe lines", variant="primary", visible=True).style(
|
188 |
+
full_width=True
|
189 |
+
)
|
190 |
+
|
191 |
+
donwload_txt_button = gr.Button("Download text", variant="secondary", visible=False).style(
|
192 |
+
full_width=True
|
193 |
+
)
|
194 |
+
|
195 |
+
with gr.Row():
|
196 |
+
txt_file_downlod = gr.File(label="Download text", visible=False)
|
197 |
+
|
198 |
+
with gr.Column(scale=3):
|
199 |
+
with gr.Row():
|
200 |
+
transcribed_text_df = gr.Dataframe(
|
201 |
+
headers=["Transcribed text"],
|
202 |
+
max_rows=15,
|
203 |
+
col_count=(1, "fixed"),
|
204 |
+
wrap=True,
|
205 |
+
interactive=False,
|
206 |
+
overflow_row_behaviour="paginate",
|
207 |
+
).style(height=600)
|
208 |
+
|
209 |
+
#####################################
|
210 |
+
with gr.Tab("4. Explore Results"):
|
211 |
+
image_placeholder_explore_results = gr.Image(
|
212 |
+
label="Cropped transcribed lines",
|
213 |
+
# type="numpy",
|
214 |
+
interactive="False",
|
215 |
+
visible=True,
|
216 |
+
).style(height=600)
|
217 |
+
|
218 |
+
with gr.Row(visible=False) as control_results_transcribe:
|
219 |
+
with gr.Column(scale=1, visible=True):
|
220 |
+
with gr.Box():
|
221 |
+
temp_gallery_input = gr.Variable()
|
222 |
+
|
223 |
+
gallery_inputs_lines_to_transcribe = gr.Gallery(
|
224 |
+
label="Cropped transcribed lines",
|
225 |
+
show_label=True,
|
226 |
+
elem_id="gallery_lines",
|
227 |
+
).style(
|
228 |
+
columns=[3],
|
229 |
+
rows=[3],
|
230 |
+
# object_fit="contain",
|
231 |
+
# height="600",
|
232 |
+
preview=True,
|
233 |
+
container=False,
|
234 |
+
)
|
235 |
+
with gr.Column(scale=1, visible=True):
|
236 |
+
mapping_dict = gr.Variable()
|
237 |
+
transcribed_text_df_finish = gr.Dataframe(
|
238 |
+
headers=["Transcribed text", "HTR prediction score"],
|
239 |
+
max_rows=15,
|
240 |
+
col_count=(2, "fixed"),
|
241 |
+
wrap=True,
|
242 |
+
interactive=False,
|
243 |
+
overflow_row_behaviour="paginate",
|
244 |
+
).style(height=600)
|
245 |
+
|
246 |
+
# custom track
|
247 |
+
region_segment_button.click(
|
248 |
+
custom_track.region_segment,
|
249 |
+
inputs=[input_region_image, reg_pred_score_threshold_slider, reg_containments_threshold_slider],
|
250 |
+
outputs=[output_region_image, regions_cropped_gallery, image_placeholder_lines, control_line_segment],
|
251 |
+
)
|
252 |
+
|
253 |
+
regions_cropped_gallery.select(
|
254 |
+
custom_track.get_select_index_image, regions_cropped_gallery, input_region_from_gallery
|
255 |
+
)
|
256 |
+
|
257 |
+
transcribed_text_df_finish.select(
|
258 |
+
fn=custom_track.get_select_index_df,
|
259 |
+
inputs=[transcribed_text_df_finish, mapping_dict],
|
260 |
+
outputs=gallery_inputs_lines_to_transcribe,
|
261 |
+
)
|
262 |
+
|
263 |
+
line_segment_button.click(
|
264 |
+
custom_track.line_segment,
|
265 |
+
inputs=[input_region_from_gallery, line_pred_score_threshold_slider, line_containments_threshold_slider],
|
266 |
+
outputs=[
|
267 |
+
output_line_from_region,
|
268 |
+
image_inputs_lines_to_transcribe,
|
269 |
+
inputs_lines_to_transcribe,
|
270 |
+
gallery_inputs_lines_to_transcribe,
|
271 |
+
temp_gallery_input,
|
272 |
+
# Hide
|
273 |
+
transcribe_button,
|
274 |
+
image_inputs_lines_to_transcribe,
|
275 |
+
image_placeholder_htr,
|
276 |
+
control_htr,
|
277 |
+
],
|
278 |
+
)
|
279 |
+
|
280 |
+
transcribe_button.click(
|
281 |
+
custom_track.transcribe_text,
|
282 |
+
inputs=[transcribed_text_df, inputs_lines_to_transcribe],
|
283 |
+
outputs=[
|
284 |
+
transcribed_text_df,
|
285 |
+
transcribed_text_df_finish,
|
286 |
+
mapping_dict,
|
287 |
+
txt_file_downlod,
|
288 |
+
control_results_transcribe,
|
289 |
+
image_placeholder_explore_results,
|
290 |
+
],
|
291 |
+
)
|
292 |
+
|
293 |
+
donwload_txt_button.click(
|
294 |
+
custom_track.download_df_to_txt,
|
295 |
+
inputs=transcribed_text_df,
|
296 |
+
outputs=[txt_file_downlod, txt_file_downlod],
|
297 |
+
)
|
298 |
+
|
299 |
+
# def remove_temp_vis():
|
300 |
+
# if os.path.exists("./vis_data"):
|
301 |
+
# os.remove("././vis_data")
|
302 |
+
# return None
|
303 |
+
|
304 |
+
clear_button.click(
|
305 |
+
lambda: (
|
306 |
+
(shutil.rmtree("./vis_data") if os.path.exists("./vis_data") else None, None)[1],
|
307 |
+
None,
|
308 |
+
None,
|
309 |
+
None,
|
310 |
+
gr.update(visible=False),
|
311 |
+
None,
|
312 |
+
None,
|
313 |
+
None,
|
314 |
+
gr.update(visible=False),
|
315 |
+
gr.update(visible=False),
|
316 |
+
gr.update(visible=True),
|
317 |
+
None,
|
318 |
+
gr.update(visible=False),
|
319 |
+
gr.update(visible=False),
|
320 |
+
gr.update(visible=True),
|
321 |
+
gr.update(visible=True),
|
322 |
+
),
|
323 |
+
inputs=[],
|
324 |
+
outputs=[
|
325 |
+
vis_data_folder_placeholder,
|
326 |
+
input_region_image,
|
327 |
+
regions_cropped_gallery,
|
328 |
+
input_region_from_gallery,
|
329 |
+
control_line_segment,
|
330 |
+
output_line_from_region,
|
331 |
+
inputs_lines_to_transcribe,
|
332 |
+
transcribed_text_df,
|
333 |
+
control_htr,
|
334 |
+
inputs_lines_to_transcribe,
|
335 |
+
image_placeholder_htr,
|
336 |
+
output_region_image,
|
337 |
+
image_inputs_lines_to_transcribe,
|
338 |
+
control_results_transcribe,
|
339 |
+
image_placeholder_explore_results,
|
340 |
+
image_placeholder_lines,
|
341 |
+
],
|
342 |
+
)
|