Spaces:
Sleeping
Sleeping
rogerxavier
commited on
Commit
•
9d8da58
1
Parent(s):
1ea52be
Update ofa_ocr.py
Browse files- ofa_ocr.py +1 -45
ofa_ocr.py
CHANGED
@@ -25,60 +25,16 @@ def get_images(img: str, reader: ReaderLite, **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 |
-
|
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=False
|
81 |
-
)
|
82 |
|
83 |
return ocr_api#返回子函数最后才能获取box ,至于ocr_demo这个gradio可有可无
|
84 |
|
|
|
25 |
return results
|
26 |
|
27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
def ofa_ocr_gr():
|
30 |
def ocr_api(img):
|
31 |
results = get_images(img, reader, max_size=4000, text_confidence=0.7, text_threshold=0.4,
|
32 |
link_threshold=0.4, slope_ths=0., add_margin=0.04)
|
33 |
box_list, image_list = zip(*results)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
+
return box_list ##返回box_list 方便作为api结果
|
36 |
|
|
|
|
|
37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
return ocr_api#返回子函数最后才能获取box ,至于ocr_demo这个gradio可有可无
|
40 |
|