SWHL commited on
Commit
7bb3fff
·
verified ·
1 Parent(s): efb6cb2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -3
app.py CHANGED
@@ -1,9 +1,13 @@
1
  # -*- encoding: utf-8 -*-
2
  # @Author: SWHL
3
  # @Contact: liekkaskono@163.com
 
4
  from typing import List, Union
5
 
6
  import gradio as gr
 
 
 
7
  from rapidocr import (
8
  EngineType,
9
  LangCls,
@@ -191,6 +195,66 @@ def create_examples() -> List[List[Union[str, float]]]:
191
  return examples
192
 
193
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
194
  custom_css = """
195
  body {font-family: body {font-family: 'Helvetica Neue', Helvetica;}
196
  .gr-button {background-color: #4CAF50; color: white; border: none; padding: 10px 20px; border-radius: 5px;}
@@ -222,7 +286,7 @@ with gr.Blocks(
222
  )
223
  img_input = gr.Image(label="Upload or Select Image", sources="upload")
224
 
225
- with gr.Accordion("Advanced Setting", open=False):
226
  with gr.Row():
227
  text_score = gr.Slider(
228
  label="text_score",
@@ -345,9 +409,12 @@ with gr.Blocks(
345
  ["Yes", "No"], label="Return word box (返回单字符)", value="No"
346
  )
347
 
348
- with gr.Row():
349
  btn_export_cfg = gr.Button("Export Config YAML")
350
- run_btn = gr.Button("Run")
 
 
 
 
351
 
352
  img_output = gr.Image(label="Output Image")
353
  elapse = gr.Textbox(label="Elapse(s)")
@@ -382,6 +449,15 @@ with gr.Blocks(
382
  get_ocr_result, inputs=ocr_inputs, outputs=[img_output, ocr_results, elapse]
383
  )
384
 
 
 
 
 
 
 
 
 
 
385
  examples = gr.Examples(
386
  examples=create_examples(),
387
  examples_per_page=5,
 
1
  # -*- encoding: utf-8 -*-
2
  # @Author: SWHL
3
  # @Contact: liekkaskono@163.com
4
+ from pathlib import Path
5
  from typing import List, Union
6
 
7
  import gradio as gr
8
+ from omegaconf import OmegaConf
9
+
10
+ import rapidocr
11
  from rapidocr import (
12
  EngineType,
13
  LangCls,
 
195
  return examples
196
 
197
 
198
+ def export_yaml(
199
+ img_input,
200
+ text_score,
201
+ box_thresh,
202
+ unclip_ratio,
203
+ det_engine,
204
+ lang_det,
205
+ det_model_type,
206
+ det_ocr_version,
207
+ cls_engine,
208
+ lang_cls,
209
+ cls_model_type,
210
+ cls_ocr_version,
211
+ rec_engine,
212
+ lang_rec,
213
+ rec_model_type,
214
+ rec_ocr_version,
215
+ is_word,
216
+ use_module,
217
+ ):
218
+ default_yaml_path = Path(rapidocr.__file__).parent / "config.yaml"
219
+ cfg = OmegaConf.load(default_yaml_path)
220
+
221
+ params = {
222
+ "Global": {
223
+ "use_det": "use_det" in use_module,
224
+ "use_cls": "use_cls" in use_module,
225
+ "use_rec": "use_rec" in use_module,
226
+ "return_word_box": "Yes" in is_word,
227
+ "text_score": text_score,
228
+ "box_thresh": box_thresh,
229
+ },
230
+ "Det": {
231
+ "engine_type": det_engine,
232
+ "lang_type": lang_det,
233
+ "model_type": det_model_type,
234
+ "ocr_version": det_ocr_version,
235
+ "box_thresh": box_thresh,
236
+ "unclip_ratio": unclip_ratio,
237
+ },
238
+ "Cls": {
239
+ "engine_type": cls_engine,
240
+ "lang_type": lang_cls,
241
+ "model_type": cls_model_type,
242
+ "ocr_version": cls_ocr_version,
243
+ },
244
+ "Rec": {
245
+ "engine_type": rec_engine,
246
+ "lang_type": lang_rec,
247
+ "model_type": rec_model_type,
248
+ "ocr_version": rec_ocr_version,
249
+ },
250
+ }
251
+ cfg = OmegaConf.merge(cfg, params)
252
+
253
+ save_path = Path(__file__).resolve().parent / "config.yaml"
254
+ OmegaConf.save(cfg, save_path)
255
+ return save_path
256
+
257
+
258
  custom_css = """
259
  body {font-family: body {font-family: 'Helvetica Neue', Helvetica;}
260
  .gr-button {background-color: #4CAF50; color: white; border: none; padding: 10px 20px; border-radius: 5px;}
 
286
  )
287
  img_input = gr.Image(label="Upload or Select Image", sources="upload")
288
 
289
+ with gr.Accordion("Parameter Setting", open=False):
290
  with gr.Row():
291
  text_score = gr.Slider(
292
  label="text_score",
 
409
  ["Yes", "No"], label="Return word box (返回单字符)", value="No"
410
  )
411
 
 
412
  btn_export_cfg = gr.Button("Export Config YAML")
413
+ download_btn_hidden = gr.DownloadButton(
414
+ visible=False, elem_id="download_btn_hidden"
415
+ )
416
+
417
+ run_btn = gr.Button("Run")
418
 
419
  img_output = gr.Image(label="Output Image")
420
  elapse = gr.Textbox(label="Elapse(s)")
 
449
  get_ocr_result, inputs=ocr_inputs, outputs=[img_output, ocr_results, elapse]
450
  )
451
 
452
+ btn_export_cfg.click(
453
+ fn=export_yaml, inputs=ocr_inputs, outputs=[download_btn_hidden]
454
+ ).then(
455
+ fn=None,
456
+ inputs=None,
457
+ outputs=None,
458
+ js="() => document.querySelector('#download_btn_hidden').click()",
459
+ )
460
+
461
  examples = gr.Examples(
462
  examples=create_examples(),
463
  examples_per_page=5,