test07.py
2.1 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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===")