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 = "

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) | Github Repo

" 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='file', label='Input'), "checkbox", "checkbox", lang, ocr_version], [gr.outputs.Image(type='file', 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)