SWHL commited on
Commit
3c1dbe9
·
verified ·
1 Parent(s): 5030041

Upload 3 files

Browse files
Files changed (3) hide show
  1. README.md +4 -5
  2. app.py +396 -0
  3. requirements.txt +5 -0
README.md CHANGED
@@ -1,14 +1,13 @@
1
  ---
2
  title: RapidOCRv3
3
- emoji: 📚
4
  colorFrom: blue
5
- colorTo: pink
6
  sdk: gradio
7
- sdk_version: 5.32.1
8
  app_file: app.py
9
  pinned: false
10
  license: apache-2.0
11
- short_description: Adapt PP-OCRv5
12
  ---
13
 
14
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
  title: RapidOCRv3
3
+ emoji: 📄
4
  colorFrom: blue
5
+ colorTo: gray
6
  sdk: gradio
7
+ sdk_version: 5.25.0
8
  app_file: app.py
9
  pinned: false
10
  license: apache-2.0
 
11
  ---
12
 
13
+ Check out the configuration reference at <https://huggingface.co/docs/hub/spaces-config-reference>
app.py ADDED
@@ -0,0 +1,396 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- encoding: utf-8 -*-
2
+ # @Author: SWHL
3
+ # @Contact: [email protected]
4
+ from typing import List, Union
5
+
6
+ import gradio as gr
7
+ from rapidocr import (
8
+ EngineType,
9
+ LangCls,
10
+ LangDet,
11
+ LangRec,
12
+ ModelType,
13
+ OCRVersion,
14
+ RapidOCR,
15
+ )
16
+
17
+
18
+ def get_ocr_result(
19
+ img_input,
20
+ text_score,
21
+ box_thresh,
22
+ unclip_ratio,
23
+ det_engine,
24
+ lang_det,
25
+ det_model_type,
26
+ det_ocr_version,
27
+ cls_engine,
28
+ lang_cls,
29
+ cls_model_type,
30
+ cls_ocr_version,
31
+ rec_engine,
32
+ lang_rec,
33
+ rec_model_type,
34
+ rec_ocr_version,
35
+ is_word,
36
+ use_module,
37
+ ):
38
+ return_word_box = "Yes" in is_word
39
+
40
+ use_det = "use_det" in use_module
41
+ use_cls = "use_cls" in use_module
42
+ use_rec = "use_rec" in use_module
43
+
44
+ ocr_engine = RapidOCR(
45
+ params={
46
+ "Det.engine_type": EngineType(det_engine),
47
+ "Det.lang_type": LangDet(lang_det),
48
+ "Det.model_type": ModelType(det_model_type),
49
+ "Det.ocr_version": OCRVersion(det_ocr_version),
50
+ "Cls.engine_type": EngineType(cls_engine),
51
+ "Cls.lang_type": LangCls(lang_cls),
52
+ "Cls.model_type": ModelType(cls_model_type),
53
+ "Cls.ocr_version": OCRVersion(cls_ocr_version),
54
+ "Rec.engine_type": EngineType(rec_engine),
55
+ "Rec.lang_type": LangRec(lang_rec),
56
+ "Rec.model_type": ModelType(rec_model_type),
57
+ "Rec.ocr_version": OCRVersion(rec_ocr_version),
58
+ }
59
+ )
60
+
61
+ ocr_result = ocr_engine(
62
+ img_input,
63
+ use_det=use_det,
64
+ use_cls=use_cls,
65
+ use_rec=use_rec,
66
+ text_score=text_score,
67
+ box_thresh=box_thresh,
68
+ unclip_ratio=unclip_ratio,
69
+ return_word_box=return_word_box,
70
+ )
71
+ vis_img = ocr_result.vis()
72
+ if return_word_box:
73
+ txts, scores, _ = list(zip(*ocr_result.word_results))
74
+ ocr_txts = [[i, txt, score] for i, (txt, score) in enumerate(zip(txts, scores))]
75
+ return vis_img, ocr_txts, ocr_result.elapse
76
+
77
+ if use_rec:
78
+ ocr_txts = [
79
+ [i, txt, score]
80
+ for i, (txt, score) in enumerate(zip(ocr_result.txts, ocr_result.scores))
81
+ ]
82
+ else:
83
+ ocr_txts = []
84
+
85
+ return vis_img, ocr_txts, ocr_result.elapse
86
+
87
+
88
+ def create_examples() -> List[List[Union[str, float]]]:
89
+ examples = [
90
+ [
91
+ "images/multi.jpg",
92
+ 0.5,
93
+ 0.5,
94
+ 1.6,
95
+ EngineType.ONNXRUNTIME,
96
+ LangDet.CH,
97
+ ModelType.MOBILE,
98
+ OCRVersion.PPOCRV5,
99
+ EngineType.ONNXRUNTIME,
100
+ LangCls.CH,
101
+ ModelType.MOBILE,
102
+ OCRVersion.PPOCRV4,
103
+ EngineType.ONNXRUNTIME,
104
+ LangRec.CH,
105
+ ModelType.MOBILE,
106
+ OCRVersion.PPOCRV5,
107
+ "No",
108
+ ["use_det", "use_cls", "use_rec"],
109
+ ],
110
+ [
111
+ "images/ch_en_num.jpg",
112
+ 0.5,
113
+ 0.5,
114
+ 1.6,
115
+ EngineType.ONNXRUNTIME,
116
+ LangDet.CH,
117
+ ModelType.MOBILE,
118
+ OCRVersion.PPOCRV4,
119
+ EngineType.ONNXRUNTIME,
120
+ LangCls.CH,
121
+ ModelType.MOBILE,
122
+ OCRVersion.PPOCRV4,
123
+ EngineType.ONNXRUNTIME,
124
+ LangRec.CH,
125
+ ModelType.MOBILE,
126
+ OCRVersion.PPOCRV4,
127
+ "No",
128
+ ["use_det", "use_cls", "use_rec"],
129
+ ],
130
+ [
131
+ "images/hand_writen.jpeg",
132
+ 0.5,
133
+ 0.5,
134
+ 1.6,
135
+ EngineType.ONNXRUNTIME,
136
+ LangDet.CH,
137
+ ModelType.MOBILE,
138
+ OCRVersion.PPOCRV5,
139
+ EngineType.ONNXRUNTIME,
140
+ LangCls.CH,
141
+ ModelType.MOBILE,
142
+ OCRVersion.PPOCRV4,
143
+ EngineType.ONNXRUNTIME,
144
+ LangRec.CH,
145
+ ModelType.MOBILE,
146
+ OCRVersion.PPOCRV5,
147
+ "No",
148
+ ["use_det", "use_cls", "use_rec"],
149
+ ],
150
+ [
151
+ "images/japan.jpg",
152
+ 0.5,
153
+ 0.5,
154
+ 1.6,
155
+ EngineType.ONNXRUNTIME,
156
+ LangDet.MULTI,
157
+ ModelType.MOBILE,
158
+ OCRVersion.PPOCRV4,
159
+ EngineType.ONNXRUNTIME,
160
+ LangCls.CH,
161
+ ModelType.MOBILE,
162
+ OCRVersion.PPOCRV4,
163
+ EngineType.ONNXRUNTIME,
164
+ LangRec.JAPAN,
165
+ ModelType.MOBILE,
166
+ OCRVersion.PPOCRV4,
167
+ "No",
168
+ ["use_det", "use_cls", "use_rec"],
169
+ ],
170
+ [
171
+ "images/korean.jpg",
172
+ 0.5,
173
+ 0.5,
174
+ 1.6,
175
+ EngineType.ONNXRUNTIME,
176
+ LangDet.MULTI,
177
+ ModelType.MOBILE,
178
+ OCRVersion.PPOCRV4,
179
+ EngineType.ONNXRUNTIME,
180
+ LangCls.CH,
181
+ ModelType.MOBILE,
182
+ OCRVersion.PPOCRV4,
183
+ EngineType.ONNXRUNTIME,
184
+ LangRec.KOREAN,
185
+ ModelType.MOBILE,
186
+ OCRVersion.PPOCRV4,
187
+ "No",
188
+ ["use_det", "use_cls", "use_rec"],
189
+ ],
190
+ ]
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;}
197
+ .gr-button:hover {background-color: #45a049;}
198
+ .gr-textbox {margin-bottom: 15px;}
199
+ .example-button {background-color: #1E90FF; color: white; border: none; padding: 8px 15px; border-radius: 5px; margin: 5px;}
200
+ .example-button:hover {background-color: #FF4500;}
201
+ .tall-radio .gr-radio-item {padding: 15px 0; min-height: 50px; display: flex; align-items: center;}
202
+ .tall-radio label {font-size: 16px;}
203
+ .output-image, .input-image, .image-preview {height: 300px !important}
204
+ """
205
+
206
+ with gr.Blocks(
207
+ title="Rapid⚡OCR Demo", css="custom_css", theme=gr.themes.Soft()
208
+ ) as demo:
209
+ gr.HTML(
210
+ """
211
+ <h1 style='text-align: center;font-size:40px'>Rapid⚡OCRv3</h1>
212
+
213
+ <div style="display: flex; justify-content: center; gap: 10px;">
214
+ <a href=""><img src="https://img.shields.io/badge/Python->=3.6-aff.svg"></a>
215
+ <a href="https://rapidai.github.io/RapidOCRDocs"><img src="https://img.shields.io/badge/Docs-link-aff.svg"></a>
216
+ <a href=""><img src="https://img.shields.io/badge/OS-Linux%2C%20Win%2C%20Mac-pink.svg"></a>
217
+ <a href="https://pepy.tech/project/rapidocr"><img src="https://static.pepy.tech/personalized-badge/rapidocr?period=total&units=abbreviation&left_color=grey&right_color=blue&left_text=Downloads%20rapidocr"></a>
218
+ <a href="https://pypi.org/project/rapidocr/"><img alt="PyPI" src="https://img.shields.io/pypi/v/rapidocr"></a>
219
+ <a href="https://github.com/RapidAI/RapidOCR"><img src="https://img.shields.io/github/stars/RapidAI/RapidOCR?color=ccf"></a>
220
+ </div>
221
+ """
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",
229
+ minimum=0,
230
+ maximum=1.0,
231
+ value=0.5,
232
+ step=0.1,
233
+ info="文本识别结果是正确的置信度,值越大,显示出的识别结果更准确。存在漏检时,调低该值。取值范围:[0, 1.0],默认值为0.5",
234
+ )
235
+ box_thresh = gr.Slider(
236
+ label="box_thresh",
237
+ minimum=0,
238
+ maximum=1.0,
239
+ value=0.5,
240
+ step=0.1,
241
+ info="检测到的框是文本的概率,值越大,框中是文本的概率就越大。存在漏检时,调低该值。取值范围:[0, 1.0],默认值为0.5",
242
+ )
243
+ unclip_ratio = gr.Slider(
244
+ label="unclip_ratio",
245
+ minimum=1.5,
246
+ maximum=2.0,
247
+ value=1.6,
248
+ step=0.1,
249
+ info="控制文本检测框的大小,值越大,检测框整体越大。在出现框截断文字的情况,调大该值。取值范围:[1.5, 2.0],默认值为1.6",
250
+ )
251
+
252
+ with gr.Row():
253
+ with gr.Row():
254
+ gr.Markdown("Det")
255
+ det_engine = gr.Dropdown(
256
+ choices=[v.value for v in EngineType],
257
+ label="EngineType",
258
+ value=EngineType.ONNXRUNTIME.value,
259
+ interactive=True,
260
+ scale=0,
261
+ )
262
+ lang_det = gr.Dropdown(
263
+ choices=[v.value for v in LangDet],
264
+ label="LangDet",
265
+ value=LangDet.CH.value,
266
+ interactive=True,
267
+ scale=1,
268
+ )
269
+ det_model_type = gr.Dropdown(
270
+ choices=[v.value for v in ModelType],
271
+ label="ModelType",
272
+ value=ModelType.MOBILE.value,
273
+ interactive=True,
274
+ scale=1,
275
+ )
276
+ det_ocr_version = gr.Dropdown(
277
+ choices=[v.value for v in OCRVersion],
278
+ label="OCR Version",
279
+ value=OCRVersion.PPOCRV4.value,
280
+ interactive=True,
281
+ scale=1,
282
+ )
283
+ with gr.Row():
284
+ gr.Markdown("Cls")
285
+ cls_engine = gr.Dropdown(
286
+ choices=[v.value for v in EngineType],
287
+ label="EngineType",
288
+ value=EngineType.ONNXRUNTIME.value,
289
+ interactive=True,
290
+ )
291
+ lang_cls = gr.Dropdown(
292
+ choices=[v.value for v in LangCls],
293
+ label="LangCls",
294
+ value=LangCls.CH.value,
295
+ interactive=True,
296
+ )
297
+ cls_model_type = gr.Dropdown(
298
+ choices=[ModelType.MOBILE.value],
299
+ label="ModelType",
300
+ value=ModelType.MOBILE.value,
301
+ interactive=True,
302
+ )
303
+ cls_ocr_version = gr.Dropdown(
304
+ choices=[OCRVersion.PPOCRV4.value],
305
+ label="OCR Version",
306
+ value=OCRVersion.PPOCRV4.value,
307
+ interactive=True,
308
+ )
309
+ with gr.Row():
310
+ gr.Markdown("Rec")
311
+ rec_engine = gr.Dropdown(
312
+ choices=[v.value for v in EngineType],
313
+ label="EngineType",
314
+ value=EngineType.ONNXRUNTIME.value,
315
+ interactive=True,
316
+ )
317
+ lang_rec = gr.Dropdown(
318
+ choices=[v.value for v in LangRec],
319
+ label="LangRec",
320
+ value=LangRec.CH.value,
321
+ interactive=True,
322
+ )
323
+ rec_model_type = gr.Dropdown(
324
+ choices=[v.value for v in ModelType],
325
+ label="ModelType",
326
+ value=ModelType.MOBILE.value,
327
+ interactive=True,
328
+ )
329
+ rec_ocr_version = gr.Dropdown(
330
+ choices=[v.value for v in OCRVersion],
331
+ label="OCR Version",
332
+ value=OCRVersion.PPOCRV4.value,
333
+ interactive=True,
334
+ )
335
+
336
+ with gr.Row():
337
+ use_module = gr.CheckboxGroup(
338
+ ["use_det", "use_cls", "use_rec"],
339
+ label="Use module (使用哪些模块)",
340
+ value=["use_det", "use_cls", "use_rec"],
341
+ interactive=True,
342
+ )
343
+
344
+ is_word = gr.Radio(
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)")
354
+ ocr_results = gr.Dataframe(
355
+ label="OCR Txts",
356
+ headers=["Index", "Txt", "Score"],
357
+ datatype=["number", "str", "number"],
358
+ show_copy_button=True,
359
+ )
360
+
361
+ ocr_inputs = [
362
+ img_input,
363
+ text_score,
364
+ box_thresh,
365
+ unclip_ratio,
366
+ det_engine,
367
+ lang_det,
368
+ det_model_type,
369
+ det_ocr_version,
370
+ cls_engine,
371
+ lang_cls,
372
+ cls_model_type,
373
+ cls_ocr_version,
374
+ rec_engine,
375
+ lang_rec,
376
+ rec_model_type,
377
+ rec_ocr_version,
378
+ is_word,
379
+ use_module,
380
+ ]
381
+ run_btn.click(
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,
388
+ inputs=ocr_inputs,
389
+ fn=get_ocr_result,
390
+ outputs=[img_output, ocr_results, elapse],
391
+ cache_examples=False,
392
+ )
393
+
394
+
395
+ if __name__ == "__main__":
396
+ demo.launch(debug=True)
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ rapidocr>=3.0.0,<4.0.0
2
+ torch
3
+ onnxruntime
4
+ paddlepaddle
5
+ openvino