robin0307/PaddleOCR
0
1import os2# os.system('pip install paddlepaddle')3# os.system('pip install paddleocr')4from paddleocr import PaddleOCR, draw_ocr5from PIL import Image6import gradio as gr7import cv28import numpy as np9 10def draw_number(img, boxes):11 overlay = img.copy()12 alpha = 0.813 count = 114 for box in boxes:15 x = int(box[0][0])16 y = int(box[0][1])-317 if y<10:18 y =1019 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 + 123 24 img = cv2.addWeighted(img, 1-alpha, overlay, alpha, 0)25 26 return img27 28def 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.name31 print("img_path:", img_path)32 result = ocr.ocr(img_path, cls=True)33 34 # get the result35 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 image41 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, result48 49 50title = 'PaddleOCR'51description = '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.'52article = "<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 54examples = []55path = './images'56 57files = os.listdir(path)58files.sort()59for 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 64css = ".output_image, .input_image {height: 40rem !important; width: 100% !important;}"65lang = gr.inputs.Dropdown(choices=['ch', 'en', 'fr', 'german', 'korean', 'japan'], type="value", default='en', label='language')66ocr_version = gr.inputs.Dropdown(choices=['PP-OCRv3', 'PP-OCRv2', 'PP-OCR'], type="value", default='PP-OCRv3', label='ocr_version')67gr.Interface(68 inference,69 [gr.inputs.Image(type='filepath', label='Input'), "checkbox", "checkbox", lang, ocr_version],70 [gr.outputs.Image(type='pil', label='Output'), gr.outputs.Textbox(type='str', label='Prediction')],71 title=title,72 description=description,73 article=article,74 examples=examples,75 css=css,76 enable_queue=True77 ).launch(debug=True)