yeecin commited on
Commit
83b2527
·
1 Parent(s): f037d02
Files changed (2) hide show
  1. app.py +77 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from paddleocr import PaddleOCR
2
+ import json
3
+ from PIL import Image
4
+ import gradio as gr
5
+ import numpy as np
6
+ import cv2
7
+
8
+
9
+ # 获取随机的颜色
10
+ def get_random_color():
11
+ c = tuple(np.random.randint(0 ,256 ,3).tolist())
12
+ return c
13
+
14
+
15
+ # 绘制ocr识别结果
16
+ def draw_ocr_bbox( image ,boxes ,colors ):
17
+ print(colors)
18
+ box_num = len(boxes)
19
+ for i in range(box_num):
20
+ box = np.reshape(np.array(boxes[i]) ,[-1 ,1 ,2]).astype(np.int64)
21
+ image = cv2.polylines(np.array(image) ,[box] ,True ,colors[i] ,2)
22
+ return image
23
+
24
+
25
+ # torch.hub.download_url_to_file('https://i.imgur.com/aqMBT0i.jpg', 'example.jpg')
26
+
27
+ def inference( img: Image.Image ,lang ,confidence ):
28
+ ocr = PaddleOCR(use_angle_cls = True ,lang = lang ,use_gpu = False)
29
+ # img_path = img.name
30
+ img2np = np.array(img)
31
+ result = ocr.ocr(img2np ,cls = True)[0]
32
+ # rgb
33
+ image = img.convert('RGB')
34
+ boxes = [line[0] for line in result]
35
+ txts = [line[1][0] for line in result]
36
+ scores = [line[1][1] for line in result]
37
+
38
+ # 识别结果
39
+ final_result = [dict(boxes = box ,txt = txt ,score = score ,_c = get_random_color()) for box ,txt ,score in
40
+ zip(boxes ,txts ,scores)]
41
+ # 过滤 score < 0.5 的
42
+ final_result = [item for item in final_result if item['score'] > confidence]
43
+
44
+ im_show = draw_ocr_bbox(image ,[item['boxes'] for item in final_result] ,[item['_c'] for item in final_result])
45
+ im_show = Image.fromarray(im_show)
46
+ data = [[json.dumps(item['boxes']) ,round(item['score'] ,3) ,item['txt']] for item in final_result]
47
+ return im_show ,data
48
+
49
+
50
+ title = 'PaddleOCR'
51
+ description = 'Gradio demo for PaddleOCR.'
52
+
53
+ examples = [
54
+ ['example_imgs/example.jpg' ,'en' ,0.5] ,
55
+ ['example_imgs/ch.jpg' ,'ch' ,0.7] ,
56
+ ['example_imgs/img_12.jpg' ,'en' ,0.7] ,
57
+ ]
58
+
59
+ css = ".output_image, .input_image {height: 40rem !important; width: 100% !important;}"
60
+
61
+ if __name__ == '__main__':
62
+ demo = gr.Interface(
63
+ inference ,
64
+ [gr.Image(type = 'pil' ,label = 'Input') ,
65
+ gr.Dropdown(choices = ['ch' ,'en' ,'fr' ,'german' ,'korean' ,'japan'] ,value = 'ch' ,label = 'language') ,
66
+ gr.Slider(0.1 ,1 ,0.5 ,step = 0.1 ,label = 'confidence_threshold')
67
+ ] ,
68
+ # 输出
69
+ [gr.Image(type = 'pil' ,label = 'Output') ,
70
+ gr.Dataframe(headers = ['bbox' ,'score' ,'text'] ,label = 'Result')] ,
71
+ title = title ,
72
+ description = description ,
73
+ examples = examples ,
74
+ css = css ,
75
+ )
76
+ demo.queue(max_size = 10)
77
+ demo.launch(debug = True ,server_name = "0.0.0.0")
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ paddlepaddle==2.6.1
2
+ paddleocr==2.7.3