from paddleocr import PaddleOCR, draw_ocr
from PIL import Image
import gradio as gr

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]
    image = Image.open(img_path).convert('RGB')
    boxes = [line[0] for line in result]
    txts = [line[1][0] for line in result]
    scores = [line[1][1] for line in result]
    im_show = draw_ocr(image, boxes, txts, scores,
                       font_path='BreezeSans-Light.ttf')
    im_show = Image.fromarray(im_show)
    im_show.save('result.jpg')
    
    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='http://claireye.com.tw'>Claireye</a> | 2023</p>"
examples = [
       ["02.png", 'tr']
]
gr.Interface(
    inference,
    [gr.inputs.Image(type='filepath', label='Input'),
    gr.inputs.Dropdown(choices=['tr', 'az'], type="value", default='tr', label='language')],
    gr.outputs.Image(type='filepath', label='Output'),
    title=title,
    description=description,
    article=article,
    examples=examples,
    cache_examples=True,
    enable_queue=True
    ).launch(debug=True)