rogerxavier commited on
Commit
5b0ef18
1 Parent(s): 0a12bd5

Update ofa_ocr.py

Browse files
Files changed (1) hide show
  1. ofa_ocr.py +101 -91
ofa_ocr.py CHANGED
@@ -1,91 +1,101 @@
1
- import os
2
- os.system('cd ezocr;'
3
- 'pip install .; cd ..')
4
-
5
-
6
- import gradio as gr
7
- import pandas as pd
8
- from PIL import ImageDraw
9
- from easyocrlite import ReaderLite
10
- from PIL import Image
11
- from modelscope.pipelines import pipeline
12
- from modelscope.utils.constant import Tasks
13
- from modelscope.outputs import OutputKeys
14
-
15
- # step 1. orc detection to find boxes
16
- reader = ReaderLite(gpu=True)
17
-
18
- # step 2. recognize ocr result according to ocr detection results
19
- ocr_recognize = pipeline(Tasks.ocr_recognition,
20
- model='damo/ofa_ocr-recognition_general_base_zh', model_revision='v1.0.0')
21
-
22
-
23
- def get_images(img: str, reader: ReaderLite, **kwargs):
24
- results = reader.process(img, **kwargs)
25
- return results
26
-
27
-
28
- def draw_boxes(image, bounds, color='red', width=4):
29
- draw = ImageDraw.Draw(image)
30
- for i, bound in enumerate(bounds):
31
- p0, p1, p2, p3 = bound
32
- draw.text((p0[0]+5, p0[1]+5), str(i+1), fill=color, align='center')
33
- draw.line([*p0, *p1, *p2, *p3, *p0], fill=color, width=width)
34
- return image
35
-
36
-
37
- def ofa_ocr_gr():
38
- def ocr_api(img):
39
- results = get_images(img, reader, max_size=4000, text_confidence=0.7, text_threshold=0.4,
40
- link_threshold=0.4, slope_ths=0., add_margin=0.04)
41
- box_list, image_list = zip(*results)
42
- draw_boxes(img, box_list)
43
-
44
- ocr_result = []
45
- for i, (box, image) in enumerate(zip(box_list, image_list)):
46
- image = Image.fromarray(image)
47
- result = ocr_recognize(image)[OutputKeys.TEXT][0].replace(" ", "")
48
- ocr_result.append([str(i + 1), result.replace(' ', '')])
49
-
50
- result = pd.DataFrame(ocr_result, columns=['Box ID', 'Text'])
51
-
52
- return img, result, box_list ##返回box_list 方便微调模型
53
-
54
- examples = [
55
- "http://xingchen-data.oss-cn-zhangjiakou.aliyuncs.com/maas/ocr/qiaodaima.png",
56
- "http://xingchen-data.oss-cn-zhangjiakou.aliyuncs.com/maas/ocr/shupai.png",
57
- "http://xingchen-data.oss-cn-zhangjiakou.aliyuncs.com/maas/ocr/ocr_essay.jpg",
58
- "http://xingchen-data.oss-cn-zhangjiakou.aliyuncs.com/maas/ocr/chinese.jpg",
59
- "http://xingchen-data.oss-cn-zhangjiakou.aliyuncs.com/maas/ocr/benpao.jpeg",
60
- "http://xingchen-data.oss-cn-zhangjiakou.aliyuncs.com/maas/ocr/gaidao.jpeg",
61
- ]
62
-
63
- title = "<h1 align='center'>基于OFA的OCR识别的应用</h1>"
64
- description = '中文OCR体验区,欢迎上传图片,静待检测文字返回~ 相关OCR代码和模型都已在ModelScope开源,支持finetune,欢迎大家在平台上使用!(注:受资源限制,这里只部署了通用OCR模型。)'
65
-
66
- ocr_input_image = gr.components.Image(label='图片', type='pil')
67
-
68
- ocr_output_image = gr.components.Image(label='图片')
69
- ocr_output_text = gr.components.Dataframe(label='OCR结果', headers=['ID', '文本'])
70
-
71
- ocr_demo = gr.Interface(
72
- fn=ocr_api,
73
- inputs=[ocr_input_image],
74
- outputs=[ocr_output_image, ocr_output_text],
75
- title=title,
76
- description=description,
77
- allow_flagging='never',
78
- examples=examples,
79
- examples_per_page=5,
80
- cache_examples=True
81
- )
82
-
83
- return ocr_demo
84
-
85
-
86
- if __name__ == "__main__":
87
- ocr_demo = ofa_ocr_gr()
88
- ocr_demo.launch(
89
- share=True,
90
- enable_queue=True,
91
- )
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ os.system('cd ezocr;'
3
+ 'pip install .; cd ..')
4
+
5
+
6
+ import gradio as gr
7
+ import pandas as pd
8
+ from PIL import ImageDraw
9
+ from easyocrlite import ReaderLite
10
+ from PIL import Image
11
+ from modelscope.pipelines import pipeline
12
+ from modelscope.utils.constant import Tasks
13
+ from modelscope.outputs import OutputKeys
14
+
15
+ # step 1. orc detection to find boxes
16
+ reader = ReaderLite(gpu=True)
17
+
18
+ # step 2. recognize ocr result according to ocr detection results
19
+ ocr_recognize = pipeline(Tasks.ocr_recognition,
20
+ model='damo/ofa_ocr-recognition_general_base_zh', model_revision='v1.0.0')
21
+
22
+
23
+ def get_images(img: str, reader: ReaderLite, **kwargs):
24
+ results = reader.process(img, **kwargs)
25
+ return results
26
+
27
+
28
+ def draw_boxes(image, bounds, color='red', width=4):
29
+ draw = ImageDraw.Draw(image)
30
+ for i, bound in enumerate(bounds):
31
+ p0, p1, p2, p3 = bound
32
+ draw.text((p0[0]+5, p0[1]+5), str(i+1), fill=color, align='center')
33
+ draw.line([*p0, *p1, *p2, *p3, *p0], fill=color, width=width)
34
+ return image
35
+
36
+
37
+ def ofa_ocr_gr():
38
+ def ocr_api(img):
39
+ results = get_images(img, reader, max_size=4000, text_confidence=0.7, text_threshold=0.4,
40
+ link_threshold=0.4, slope_ths=0., add_margin=0.04)
41
+ box_list, image_list = zip(*results)
42
+ draw_boxes(img, box_list)
43
+
44
+ ocr_result = []
45
+ for i, (box, image) in enumerate(zip(box_list, image_list)):
46
+ image = Image.fromarray(image)
47
+ result = ocr_recognize(image)[OutputKeys.TEXT][0].replace(" ", "")
48
+ ocr_result.append([str(i + 1), result.replace(' ', '')])
49
+
50
+ result = pd.DataFrame(ocr_result, columns=['Box ID', 'Text'])
51
+
52
+ return img, result, box_list ##返回box_list 方便微调模型
53
+
54
+ examples = [
55
+ "http://xingchen-data.oss-cn-zhangjiakou.aliyuncs.com/maas/ocr/qiaodaima.png",
56
+ "http://xingchen-data.oss-cn-zhangjiakou.aliyuncs.com/maas/ocr/shupai.png",
57
+ "http://xingchen-data.oss-cn-zhangjiakou.aliyuncs.com/maas/ocr/ocr_essay.jpg",
58
+ "http://xingchen-data.oss-cn-zhangjiakou.aliyuncs.com/maas/ocr/chinese.jpg",
59
+ "http://xingchen-data.oss-cn-zhangjiakou.aliyuncs.com/maas/ocr/benpao.jpeg",
60
+ "http://xingchen-data.oss-cn-zhangjiakou.aliyuncs.com/maas/ocr/gaidao.jpeg",
61
+ ]
62
+
63
+ title = "<h1 align='center'>基于OFA的OCR识别的应用</h1>"
64
+ description = '中文OCR体验区,欢迎上传图片,静待检测文字返回~ 相关OCR代码和模型都已在ModelScope开源,支持finetune,欢迎大家在平台上使用!(注:受资源限制,这里只部署了通用OCR模型。)'
65
+
66
+ ocr_input_image = gr.components.Image(label='图片', type='pil')
67
+
68
+ ocr_output_image = gr.components.Image(label='图片')
69
+ ocr_output_text = gr.components.Dataframe(label='OCR结果', headers=['ID', '文本'])
70
+
71
+ # ocr_demo = gr.Interface(
72
+ # fn=ocr_api,
73
+ # inputs=[ocr_input_image],
74
+ # outputs=[ocr_output_image, ocr_output_text],
75
+ # title=title,
76
+ # description=description,
77
+ # allow_flagging='never',
78
+ # examples=examples,
79
+ # examples_per_page=5,
80
+ # cache_examples=True
81
+ # )
82
+ ocr_demo = gr.Interface(
83
+ fn=ocr_api,
84
+ inputs=[ocr_input_image],
85
+ outputs=[ocr_output_image, ocr_output_text],
86
+ title=title,
87
+ description=description,
88
+ allow_flagging='never',
89
+ examples=examples,
90
+ examples_per_page=5
91
+ )
92
+
93
+ return ocr_demo
94
+
95
+
96
+ if __name__ == "__main__":
97
+ ocr_demo = ofa_ocr_gr()
98
+ ocr_demo.launch(
99
+ share=True,
100
+ enable_queue=True,
101
+ )