File size: 3,272 Bytes
2b70e0f
00e61f3
 
2b70e0f
 
 
8be172e
 
2b70e0f
8be172e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2b70e0f
8be172e
 
2b70e0f
8be172e
2b70e0f
8be172e
 
 
2b70e0f
 
 
8be172e
 
 
 
 
 
2b70e0f
 
8be172e
 
2b70e0f
 
 
 
8be172e
 
 
 
 
 
 
 
 
 
 
2b70e0f
8be172e
 
2b70e0f
 
3ba8f97
ace4d47
2b70e0f
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import os
# os.system('pip install paddlepaddle')
# os.system('pip install paddleocr')
from paddleocr import PaddleOCR, draw_ocr
from PIL import Image
import gradio as gr
import cv2
import numpy as np

def draw_number(img, boxes):
    overlay = img.copy()
    alpha = 0.8
    count = 1
    for box in boxes:
        x = int(box[0][0])
        y = int(box[0][1])-3
        if y<10:
            y =10
        retval, baseLine = cv2.getTextSize(str(count),fontFace=cv2.FONT_HERSHEY_PLAIN,fontScale=2, thickness=2)
        cv2.rectangle(overlay, (x, y-retval[1]-3), (x+retval[0], y), (0, 0, 0), -1)
        cv2.putText(overlay, str(count), (x, y), cv2.FONT_HERSHEY_PLAIN, 2, (255, 0, 0), 2, cv2.LINE_AA)
        count = count + 1
        
    img = cv2.addWeighted(img, 1-alpha, overlay, alpha, 0)
    
    return img

def inference(img, use_angle_cls, is_draw_number,  lang, ocr_version):
    ocr = PaddleOCR(use_angle_cls=use_angle_cls, lang=lang, ocr_version=ocr_version, use_gpu=False)
    img_path = img.name
    print("img_path:", img_path)
    result = ocr.ocr(img_path, cls=True)
    
    # get the result
    result = result[0]
    boxes = [line[0] for line in result]
    txts = [line[1][0] for line in result]
    scores = [line[1][1] for line in result]
    
    # draw the image
    image = Image.open(img_path).convert('RGB')
    if is_draw_number:
        image = draw_number(np.array(image), boxes)
    im_show = draw_ocr(image, boxes, txts, scores, font_path='./simfang.ttf') 
    im_show = Image.fromarray(im_show)
    im_show.save('result.jpg')
    return im_show, result


title = 'PaddleOCR'
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.'
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>"

examples = []
path = './images'

files = os.listdir(path)
files.sort()
for f in files:
    file = os.path.join(path, f)
    if os.path.isfile(file):
        examples.append([file, True, True, 'en', 'PP-OCRv3'])
        
css = ".output_image, .input_image {height: 40rem !important; width: 100% !important;}"
lang = gr.inputs.Dropdown(choices=['ch', 'en', 'fr', 'german', 'korean', 'japan'], type="value", default='en', label='language')
ocr_version = gr.inputs.Dropdown(choices=['PP-OCRv3', 'PP-OCRv2', 'PP-OCR'], type="value", default='PP-OCRv3', label='ocr_version')
gr.Interface(
    inference,
    [gr.inputs.Image(type='filepath', label='Input'), "checkbox", "checkbox", lang, ocr_version],
    [gr.outputs.Image(type='pil', label='Output'), gr.outputs.Textbox(type='str', label='Prediction')],
    title=title,
    description=description,
    article=article,
    examples=examples,
    css=css,
    enable_queue=True
    ).launch(debug=True)