test07.py 2.1 KB
import os

import numpy as np
from paddleocr import PaddleOCR, draw_ocr
from PIL import Image
from pprint import pprint

if os.path.exists("result04.jpg"):
    os.remove("result04.jpg")

# load model
# Paddleocr目前支持中英文、英文、法语、德语、韩语、日语,可以通过修改 lang参数进行切换
# lang参数依次为`ch`, `en`, `french`, `german`, `korean`, `japan`
ocr = PaddleOCR(
    use_angle_cls=True,
    # use_angle_cls=False,
    lang="ch",
    # use_gpu=False,
    use_gpu=True,
    use_tensorrt=True,
    det_algorithm="DB",
    # det_algorithm="EAST",
    det_db_score_mode="slow",
    # use_space_char=False, #不可用
    det_model_dir="ch_ppocr_server_v4.0_det_infer/",
    cls_model_dir="ch_ppocr_mobile_v4.0_cls_infer/",
    rec_model_dir="ch_ppocr_server_v4.0_rec_infer/",
    det_db_thresh=0.1,
    det_db_box_thresh=0.1,
    det_db_unclip_ratio=2.0,
    max_batch_size=20,
    # det_db_box_thresh=0.3,
    use_dilation=True,
    image_orientation=True,
    cls=True,
    # rec_algorithm="SVTR_LCNet",
)

img_path = "./ocr_invoice/ocr-vin-0007.jpg"
# path='./enhanced_image.jpg'
original_image = Image.open(img_path)

if original_image.mode == "RGBA":
    original_image = original_image.convert("RGB")

result = ocr.ocr(np.asarray(original_image), cls=True)
# result = ocr.ocr(original_image, cls=False)

for line in result:
    print(line)
    # print("Text: ", line[-1][0])
    # print("Score: ", line[-1][1])
    # print("Location: ", line[0])

image = Image.open(img_path).convert("RGB")
boxes = [line[0] for line in result[0]]
txts = [line[1][0] for line in result[0]]
all_context = "\n".join(txts)
# all_context = "".join(txts)
# print("\n".join(txts))
print(all_context)
scores = [line[1][1] for line in result[0]]
im_show = draw_ocr(image, boxes, txts, scores, font_path="./simfang.ttf")
im_show = Image.fromarray(im_show)
# im_show.show()
im_show.save("result04.jpg")

from paddlenlp import Taskflow

schema = ["车辆类型", "账号", "机关及代码", "备注"]
ie = Taskflow("information_extraction", schema=schema)
ie.set_schema(schema)
print("one===")
pprint(ie(all_context))
print("two===")