Robin Chiu commited on
Commit
8be172e
·
1 Parent(s): 27183cf

fix the app.py and upload some images

Browse files
Files changed (5) hide show
  1. app.py +48 -11
  2. images/img_10.jpg +0 -0
  3. images/img_11.jpg +0 -0
  4. images/img_12.jpg +0 -0
  5. images/img_195.jpg +0 -0
app.py CHANGED
@@ -4,33 +4,70 @@ os.system('pip install paddleocr')
4
  from paddleocr import PaddleOCR, draw_ocr
5
  from PIL import Image
6
  import gradio as gr
7
- import torch
 
8
 
9
- torch.hub.download_url_to_file('https://i.imgur.com/aqMBT0i.jpg', 'example.jpg')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
- def inference(img, lang):
12
- ocr = PaddleOCR(use_angle_cls=True, lang=lang,use_gpu=False)
13
  img_path = img.name
 
14
  result = ocr.ocr(img_path, cls=True)
15
- image = Image.open(img_path).convert('RGB')
 
 
16
  boxes = [line[0] for line in result]
17
  txts = [line[1][0] for line in result]
18
  scores = [line[1][1] for line in result]
19
- im_show = draw_ocr(image, boxes, txts, scores,
20
- font_path='simfang.ttf')
 
 
 
 
21
  im_show = Image.fromarray(im_show)
22
  im_show.save('result.jpg')
23
- return 'result.jpg'
 
24
 
25
  title = 'PaddleOCR'
26
  description = 'Gradio demo for PaddleOCR. PaddleOCR demo supports Chinese, English, French, German, Korean and Japanese.To use it, simply upload your image and choose a language from the dropdown menu, or click one of the examples to load them. Read more at the links below.'
27
  article = "<p style='text-align: center'><a href='https://www.paddlepaddle.org.cn/hub/scene/ocr'>Awesome multilingual OCR toolkits based on PaddlePaddle (practical ultra lightweight OCR system, support 80+ languages recognition, provide data annotation and synthesis tools, support training and deployment among server, mobile, embedded and IoT devices)</a> | <a href='https://github.com/PaddlePaddle/PaddleOCR'>Github Repo</a></p>"
28
- examples = [['example.jpg','en']]
 
 
 
 
 
 
 
 
 
 
29
  css = ".output_image, .input_image {height: 40rem !important; width: 100% !important;}"
 
 
30
  gr.Interface(
31
  inference,
32
- [gr.inputs.Image(type='file', label='Input'),gr.inputs.Dropdown(choices=['ch', 'en', 'fr', 'german', 'korean', 'japan'], type="value", default='en', label='language')],
33
- gr.outputs.Image(type='file', label='Output'),
34
  title=title,
35
  description=description,
36
  article=article,
 
4
  from paddleocr import PaddleOCR, draw_ocr
5
  from PIL import Image
6
  import gradio as gr
7
+ import cv2
8
+ import numpy as np
9
 
10
+ def draw_number(img, boxes):
11
+ overlay = img.copy()
12
+ alpha = 0.8
13
+ count = 1
14
+ for box in boxes:
15
+ x = int(box[0][0])
16
+ y = int(box[0][1])-3
17
+ if y<10:
18
+ y =10
19
+ retval, baseLine = cv2.getTextSize(str(count),fontFace=cv2.FONT_HERSHEY_PLAIN,fontScale=2, thickness=2)
20
+ cv2.rectangle(overlay, (x, y-retval[1]-3), (x+retval[0], y), (0, 0, 0), -1)
21
+ cv2.putText(overlay, str(count), (x, y), cv2.FONT_HERSHEY_PLAIN, 2, (255, 0, 0), 2, cv2.LINE_AA)
22
+ count = count + 1
23
+
24
+ img = cv2.addWeighted(img, 1-alpha, overlay, alpha, 0)
25
+
26
+ return img
27
 
28
+ def inference(img, use_angle_cls, is_draw_number, lang, ocr_version):
29
+ ocr = PaddleOCR(use_angle_cls=use_angle_cls, lang=lang, ocr_version=ocr_version, use_gpu=False)
30
  img_path = img.name
31
+ print("img_path:", img_path)
32
  result = ocr.ocr(img_path, cls=True)
33
+
34
+ # get the result
35
+ result = result[0]
36
  boxes = [line[0] for line in result]
37
  txts = [line[1][0] for line in result]
38
  scores = [line[1][1] for line in result]
39
+
40
+ # draw the image
41
+ image = Image.open(img_path).convert('RGB')
42
+ if is_draw_number:
43
+ image = draw_number(np.array(image), boxes)
44
+ im_show = draw_ocr(image, boxes, txts, scores, font_path='./simfang.ttf')
45
  im_show = Image.fromarray(im_show)
46
  im_show.save('result.jpg')
47
+ return im_show, result
48
+
49
 
50
  title = 'PaddleOCR'
51
  description = 'Gradio demo for PaddleOCR. PaddleOCR demo supports Chinese, English, French, German, Korean and Japanese.To use it, simply upload your image and choose a language from the dropdown menu, or click one of the examples to load them. Read more at the links below.'
52
  article = "<p style='text-align: center'><a href='https://www.paddlepaddle.org.cn/hub/scene/ocr'>Awesome multilingual OCR toolkits based on PaddlePaddle (practical ultra lightweight OCR system, support 80+ languages recognition, provide data annotation and synthesis tools, support training and deployment among server, mobile, embedded and IoT devices)</a> | <a href='https://github.com/PaddlePaddle/PaddleOCR'>Github Repo</a></p>"
53
+
54
+ examples = []
55
+ path = './images'
56
+
57
+ files = os.listdir(path)
58
+ files.sort()
59
+ for f in files:
60
+ file = os.path.join(path, f)
61
+ if os.path.isfile(file):
62
+ examples.append([file, True, True, 'en', 'PP-OCRv3'])
63
+
64
  css = ".output_image, .input_image {height: 40rem !important; width: 100% !important;}"
65
+ lang = gr.inputs.Dropdown(choices=['ch', 'en', 'fr', 'german', 'korean', 'japan'], type="value", default='en', label='language')
66
+ ocr_version = gr.inputs.Dropdown(choices=['PP-OCRv3', 'PP-OCRv2', 'PP-OCR'], type="value", default='PP-OCRv3', label='ocr_version')
67
  gr.Interface(
68
  inference,
69
+ [gr.inputs.Image(type='file', label='Input'), "checkbox", "checkbox", lang, ocr_version],
70
+ [gr.outputs.Image(type='file', label='Output'), gr.outputs.Textbox(type='str', label='Prediction')],
71
  title=title,
72
  description=description,
73
  article=article,
images/img_10.jpg ADDED
images/img_11.jpg ADDED
images/img_12.jpg ADDED
images/img_195.jpg ADDED