breezedeus commited on
Commit
e1274bb
β€’
1 Parent(s): fa2693b

add examples

Browse files
Files changed (2) hide show
  1. README.md +2 -2
  2. app.py +91 -3
README.md CHANGED
@@ -1,5 +1,5 @@
1
  ---
2
- title: CnOCR Demo
3
  emoji: πŸ…žπŸ…’πŸ…‘
4
  colorFrom: indigo
5
  colorTo: yellow
@@ -10,7 +10,7 @@ pinned: false
10
  license: apache-2.0
11
  ---
12
 
13
- # CnOCR (Cn-OCR)
14
 
15
  [**CnOCR**](https://github.com/breezedeus/cnocr) is an **Optical Character Recognition (OCR)** toolkit for **Python 3**. It supports recognition of common characters in **English and numbers**, **Simplified Chinese**, **Traditional Chinese** (some models), and **vertical text** recognition. It comes with [**20+ well-trained models**](https://cnocr.readthedocs.io/zh/latest/models/) for different application scenarios and can be used directly after installation. Also, CnOCR provides simple training [commands](https://cnocr.readthedocs.io/zh/latest/train/) for users to train their own models. Welcome to join the WeChat contact group.
16
 
 
1
  ---
2
+ title: Cn/En OCR Demo
3
  emoji: πŸ…žπŸ…’πŸ…‘
4
  colorFrom: indigo
5
  colorTo: yellow
 
10
  license: apache-2.0
11
  ---
12
 
13
+ # CnOCR
14
 
15
  [**CnOCR**](https://github.com/breezedeus/cnocr) is an **Optical Character Recognition (OCR)** toolkit for **Python 3**. It supports recognition of common characters in **English and numbers**, **Simplified Chinese**, **Traditional Chinese** (some models), and **vertical text** recognition. It comes with [**20+ well-trained models**](https://cnocr.readthedocs.io/zh/latest/models/) for different application scenarios and can be used directly after installation. Also, CnOCR provides simple training [commands](https://cnocr.readthedocs.io/zh/latest/train/) for users to train their own models. Welcome to join the WeChat contact group.
16
 
app.py CHANGED
@@ -16,8 +16,11 @@
16
  # KIND, either express or implied. See the License for the
17
  # specific language governing permissions and limitations
18
  # under the License.
 
19
 
20
  import os
 
 
21
 
22
  import gradio as gr
23
  import cv2
@@ -29,6 +32,7 @@ from cnocr.utils import set_logger, draw_ocr_results, download
29
 
30
 
31
  logger = set_logger()
 
32
 
33
 
34
  def plot_for_debugging(rotated_img, one_out, box_score_thresh, crop_ncols, prefix_fp):
@@ -64,15 +68,24 @@ def plot_for_debugging(rotated_img, one_out, box_score_thresh, crop_ncols, prefi
64
 
65
 
66
  def get_ocr_model(det_model_name, rec_model_name, det_more_configs):
 
 
 
 
 
67
  det_model_name, det_model_backend = det_model_name.split('::')
68
  rec_model_name, rec_model_backend = rec_model_name.split('::')
69
- return CnOcr(
70
  det_model_name=det_model_name,
71
  det_model_backend=det_model_backend,
72
  rec_model_name=rec_model_name,
73
  rec_model_backend=rec_model_backend,
74
  det_more_configs=det_more_configs,
75
  )
 
 
 
 
76
 
77
 
78
  def visualize_naive_result(img, det_model_name, std_out, box_score_thresh):
@@ -186,6 +199,66 @@ def main():
186
  'δ½œθ€…οΌš<a href="https://www.breezedeus.com" target="_blank">Breezedeus</a> ,'
187
  '<a href="https://github.com/breezedeus" target="_blank">Github</a> 。</p>'
188
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
189
 
190
  with gr.Blocks() as demo:
191
  gr.Markdown(
@@ -220,8 +293,8 @@ def main():
220
  )
221
 
222
  with gr.Column(scale=3, variant='compact'):
223
- gr.Markdown('### 选择待检桋图片')
224
- image_file = gr.Image(label='', type="pil", image_mode='RGB')
225
  sub_btn = gr.Button("Submit", variant="primary")
226
  out_image = gr.Image(label='θ―†εˆ«η»“ζžœ', interactive=False, visible=False)
227
  naive_warn = gr.Markdown(
@@ -246,6 +319,21 @@ def main():
246
  outputs=[out_image, naive_warn, out_texts],
247
  )
248
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
249
  demo.queue(concurrency_count=4)
250
  demo.launch()
251
 
 
16
  # KIND, either express or implied. See the License for the
17
  # specific language governing permissions and limitations
18
  # under the License.
19
+ # Ref: https://huggingface.co/spaces/hysts/Manga-OCR/blob/main/app.py
20
 
21
  import os
22
+ import json
23
+ import functools
24
 
25
  import gradio as gr
26
  import cv2
 
32
 
33
 
34
  logger = set_logger()
35
+ MODELS = {}
36
 
37
 
38
  def plot_for_debugging(rotated_img, one_out, box_score_thresh, crop_ncols, prefix_fp):
 
68
 
69
 
70
  def get_ocr_model(det_model_name, rec_model_name, det_more_configs):
71
+ global MODELS
72
+ config_str = json.dumps(det_more_configs)
73
+ if (det_model_name, rec_model_name, config_str) in MODELS:
74
+ return MODELS[(det_model_name, rec_model_name, config_str)]
75
+
76
  det_model_name, det_model_backend = det_model_name.split('::')
77
  rec_model_name, rec_model_backend = rec_model_name.split('::')
78
+ model = CnOcr(
79
  det_model_name=det_model_name,
80
  det_model_backend=det_model_backend,
81
  rec_model_name=rec_model_name,
82
  rec_model_backend=rec_model_backend,
83
  det_more_configs=det_more_configs,
84
  )
85
+ if len(MODELS) > 50:
86
+ MODELS = {}
87
+ MODELS[(det_model_name, rec_model_name, config_str)] = model
88
+ return model
89
 
90
 
91
  def visualize_naive_result(img, det_model_name, std_out, box_score_thresh):
 
199
  'δ½œθ€…οΌš<a href="https://www.breezedeus.com" target="_blank">Breezedeus</a> ,'
200
  '<a href="https://github.com/breezedeus" target="_blank">Github</a> 。</p>'
201
  )
202
+ example_func = functools.partial(
203
+ recognize,
204
+ # det_model_name='ch_PP-OCRv3_det::onnx',
205
+ rotated_bbox=True,
206
+ # use_angle_clf=False,
207
+ new_size=768,
208
+ box_score_thresh=0.3,
209
+ min_box_size=10,
210
+ )
211
+ examples = [
212
+ [
213
+ 'ch_PP-OCRv3_det::onnx',
214
+ True,
215
+ 'number-densenet_lite_136-fc::onnx',
216
+ False,
217
+ 'docs/examples/card1-s.jpg',
218
+ ],
219
+ [
220
+ 'ch_PP-OCRv3_det::onnx',
221
+ True,
222
+ 'number-densenet_lite_136-fc::onnx',
223
+ False,
224
+ 'docs/examples/card2-s.jpg',
225
+ ],
226
+ [
227
+ 'ch_PP-OCRv3_det::onnx',
228
+ True,
229
+ 'number-densenet_lite_136-fc::onnx',
230
+ False,
231
+ 'docs/examples/cy1-s.jpg',
232
+ ],
233
+ [
234
+ 'ch_PP-OCRv3_det::onnx',
235
+ False,
236
+ 'densenet_lite_136-gru::onnx',
237
+ False,
238
+ 'docs/examples/huochepiao.jpeg',
239
+ ],
240
+ [
241
+ 'ch_PP-OCRv3_det::onnx',
242
+ False,
243
+ 'densenet_lite_136-gru::onnx',
244
+ False,
245
+ 'docs/examples/1_res.jpg',
246
+ ],
247
+ [
248
+ 'db_shufflenet_v2::pytorch',
249
+ False,
250
+ 'en_number_mobile_v2.0::onnx',
251
+ False,
252
+ 'docs/examples/en_book1.jpeg',
253
+ ],
254
+ [
255
+ 'db_shufflenet_v2::pytorch',
256
+ False,
257
+ 'densenet_lite_136-gru::onnx',
258
+ True,
259
+ 'docs/examples/beauty0.jpg',
260
+ ],
261
+ ]
262
 
263
  with gr.Blocks() as demo:
264
  gr.Markdown(
 
293
  )
294
 
295
  with gr.Column(scale=3, variant='compact'):
296
+ gr.Markdown('### ι€‰ζ‹©εΎ…θ―†εˆ«ε›Ύη‰‡')
297
+ image_file = gr.Image(label='εΎ…θ―†εˆ«ε›Ύη‰‡', type="pil", image_mode='RGB')
298
  sub_btn = gr.Button("Submit", variant="primary")
299
  out_image = gr.Image(label='θ―†εˆ«η»“ζžœ', interactive=False, visible=False)
300
  naive_warn = gr.Markdown(
 
319
  outputs=[out_image, naive_warn, out_texts],
320
  )
321
 
322
+ gr.Examples(
323
+ label='η€ΊδΎ‹',
324
+ examples=examples,
325
+ inputs=[
326
+ det_model_name,
327
+ is_single_line,
328
+ rec_model_name,
329
+ use_angle_clf,
330
+ image_file,
331
+ ],
332
+ outputs=[out_image, naive_warn, out_texts],
333
+ fn=example_func,
334
+ cache_examples=os.getenv('CACHE_EXAMPLES') == '1',
335
+ )
336
+
337
  demo.queue(concurrency_count=4)
338
  demo.launch()
339