File size: 2,402 Bytes
96c3387
1be5f36
 
ab59b01
96c3387
 
 
 
 
 
 
 
 
163694c
96c3387
5fec972
96c3387
 
52b559d
5fec972
 
0e39010
52b559d
 
 
 
 
 
e195121
96c3387
 
 
 
 
 
fa68695
96c3387
f00f3d6
e195121
52b559d
96c3387
 
 
 
 
fa68695
 
 
 
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
import os
os.system('pip install paddlepaddle==2.4.2')
# os.system('pip install paddlepaddle==0.0.0 -f https://www.paddlepaddle.org.cn/whl/linux/cpu-mkl/develop.html')
os.system('pip install paddleocr')
from paddleocr import PaddleOCR, draw_ocr
from PIL import Image
import gradio as gr
import torch

torch.hub.download_url_to_file('https://i.imgur.com/aqMBT0i.jpg', 'example.jpg')

def inference(img, lang):
    ocr = PaddleOCR(use_angle_cls=True, lang=lang,use_gpu=False)
    img_path = img
    result = ocr.ocr(img_path, cls=True)[0]

    boxes = [line[0] for line in result]
    txts = [line[1][0] for line in result]
    scores = [line[1][1] for line in result]
    
    image = Image.open(img_path).convert('RGB')
    im_show = draw_ocr(image, boxes, txts=None, scores=None, # https://github.com/PaddlePaddle/PaddleOCR/blob/release/2.7/tools/infer/utility.py#L365
                       font_path='simfang.ttf')
    im_show = Image.fromarray(im_show)
    im_show.save('result.jpg')
    
    return 'result.jpg', result, '\n'.join(txts)
    
    # return 'result.jpg'

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 = [['example.jpg','en']]
css = ".output_image, .input_image {height: 40rem !important; width: 100% !important;}"
app = gr.Interface(
    inference,
    [gr.Image(type='filepath', label='Input'),gr.Dropdown(choices=['ch', 'en', 'fr', 'german', 'korean', 'japan', 'ar'], type="value", value='ch', label='language')],
    # gr.outputs.Image(type='file', label='Output'),
    outputs=["image", "text", "text"],
    title=title,
    description=description,
    article=article,
    examples=examples,
    css=css,
    # enable_queue=True
    )
app.queue(max_size=10)
app.launch(debug=True)