Commit 9de9d798 by 杨林龙

项目初始化

0 parents
myvenv
.vscode
ch_ppocr_mobile_v4.0_cls_infer
ch_ppocr_server_v4.0_det_infer
ch_ppocr_server_v4.0_rec_infer
__pycache__
table/1.png

758 KB

from PIL import Image
import numpy as np
from paddleocr import PaddleOCR
# 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,
use_dilation=True,
# rec_algorithm="SVTR_LCNet",
)
# path='./ocr_invoice/ocr-vin-0007.jpg'
path='./enhanced_image.jpg'
original_image = Image.open(path)
if original_image.mode == "RGBA":
original_image = original_image.convert("RGB")
result = ocr.ocr(np.asarray(original_image), cls=True)
for line in result:
print(line)
print('Text: ', line[-1][0])
print('Score: ', line[-1][1])
print('Location: ', line[0])
# 图像方向分类+版面分析+表格识别
import os
import cv2
from paddleocr import PPStructure,draw_structure_result,save_structure_res
table_engine = PPStructure(show_log=True, image_orientation=True)
save_folder = "./output"
# img_path = './table/1.png'
img_path = "./ocr_invoice/ocr-vin-0007.jpg"
img = cv2.imread(img_path)
result = table_engine(img)
save_structure_res(result, save_folder,os.path.basename(img_path).split('.')[0])
for line in result:
line.pop("img")
print(line)
from PIL import Image
font_path = 'doc/fonts/simfang.ttf' # PaddleOCR下提供字体包
image = Image.open(img_path).convert('RGB')
im_show = draw_structure_result(image, result,font_path=font_path)
im_show = Image.fromarray(im_show)
im_show.save('result02.jpg')
# 版面分析+表格识别
import os
import shutil
import cv2
from paddleocr import PPStructure, draw_structure_result, save_structure_res
table_engine = PPStructure(
mode="structure",
layout=True,
ocr=True,
table=True,
show_log=True,
# invert=True,
use_angle_cls=True,
# det_model_dir="ch_ppocr_server_v4.0_det_infer/",
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/",
re_model_dir="re_vi_layoutxlm_xfund_infer",
# layout_model_dir=''
use_tensorrt=True,
use_dilation=True,
image_orientation=True,
recovery=True,
)
save_folder = "./output"
# img_path = './table/1.png'
img_path = "./ocr_invoice/ocr-vin-0007.jpg"
img = cv2.imread(img_path)
result = table_engine(img)
# 删除文件
print(save_folder + "/" + os.path.basename(img_path).split(".")[0])
if os.path.exists(save_folder + "/" + os.path.basename(img_path).split(".")[0]):
shutil.rmtree(save_folder + "/" + os.path.basename(img_path).split(".")[0])
save_structure_res(result, save_folder, os.path.basename(img_path).split(".")[0])
for line in result:
line.pop("img")
print(line)
from PIL import Image
font_path = "doc/fonts/simfang.ttf" # PaddleOCR下提供字体包
image = Image.open(img_path).convert("RGB")
im_show = draw_structure_result(image, result, font_path=font_path)
im_show = Image.fromarray(im_show)
os.remove("result03.jpg")
im_show.save("result03.jpg")
# 版面分析
import os
import cv2
from paddleocr import PPStructure,save_structure_res
table_engine = PPStructure(table=False, ocr=False, show_log=True)
save_folder = './output'
# img_path = './table/1.png'
img_path = "./ocr_invoice/ocr-vin-0007.jpg"
img = cv2.imread(img_path)
result = table_engine(img)
save_structure_res(result, save_folder, os.path.basename(img_path).split('.')[0])
for line in result:
line.pop('img')
print(line)
# 表格识别
import os
import cv2
from paddleocr import PPStructure, save_structure_res
table_engine = PPStructure(layout=False, show_log=True)
save_folder = "./output"
img_path = './table/table.jpg'
img = cv2.imread(img_path)
result = table_engine(img)
save_structure_res(result, save_folder, os.path.basename(img_path).split('.')[0])
for line in result:
line.pop('img')
print(line)
# 版面恢复
import os
import cv2
from paddleocr import PPStructure,save_structure_res
from paddleocr.ppstructure.recovery.recovery_to_doc import sorted_layout_boxes, convert_info_docx
# 中文测试图
table_engine = PPStructure(recovery=True)
# 英文测试图
# table_engine = PPStructure(recovery=True, lang='en')
save_folder = './output'
# img_path = "./table/1.png"
img_path = "./ocr_invoice/ocr-vin-0007.jpg"
img = cv2.imread(img_path)
result = table_engine(img)
save_structure_res(result, save_folder, os.path.basename(img_path).split('.')[0])
for line in result:
line.pop('img')
print(line)
h, w, _ = img.shape
res = sorted_layout_boxes(result, w)
convert_info_docx(img, res, save_folder, os.path.basename(img_path).split(".")[0])
import os
import numpy as np
from paddleocr import PaddleOCR, draw_ocr
from PIL import Image
from pprint import pprint
# 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()
if os.path.exists("result04.jpg"):
os.remove("result04.jpg")
im_show.save("result04.jpg")
from paddlenlp import Taskflow
schema = ["车辆类型", "账号", "机关及代码", "备注"]
ie = Taskflow("information_extraction",schema=schema)
ie.set_schema(schema)
one = ie(all_context)
print("one===")
pprint(one)
from paddleocr import PaddleOCR, draw_ocr
# paddleocr目前支持中英文、英文、法语、德语、韩语、日语等80个语种,可以通过修改lang参数进行切换
ocr = PaddleOCR(
use_angle_cls=False, lang="ch"
) # need to run only once to download and load model into memory
img_path = "./ocr_invoice/ocr-vin-0007.jpg"
result = ocr.ocr(img_path, cls=False)
print("result===")
print(result)
# for line in result:
# print(line)
# 可视化结果
from PIL import Image
image = Image.open(img_path).convert("RGB")
boxes = [line[0] for line in result[0]]
txts = [line[1][0] for line in result[0]]
scores = [line[1][1] for line in result[0]]
print("scores===")
print(scores)
im_show = draw_ocr(image, boxes, txts, scores, font_path="./simfang.ttf")
im_show = Image.fromarray(im_show)
im_show.show()
from pprint import pprint
from paddlenlp import Taskflow
docprompt = Taskflow("document_intelligence")
pprint(
docprompt(
[
{
"doc": "https://bj.bcebos.com/paddlenlp/taskflow/document_intelligence/images/invoice.jpg",
"prompt": ["发票号码是多少?", "校验码是多少?"],
}
]
)
)
from pprint import pprint
from paddlenlp import Taskflow
docprompt = Taskflow("document_intelligence")
pprint(
docprompt(
[
{
"doc": "https://user-images.githubusercontent.com/40840292/194748579-f9e8aa86-7f65-4827-bfae-824c037228b3.png",
"prompt": [
"五百丁本次想要担任的是什么职位?",
"五百丁是在哪里上的大学?",
"大学学的是什么专业?",
],
}
]
)
)
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!