添加纯ocr结果的显示

This commit is contained in:
2024-05-31 14:20:25 +08:00
parent fd56fb91b1
commit 8ca4ff2dda

View File

@@ -9,8 +9,8 @@ from paddlenlp.utils.doc_parser import DocParser
from photo_review.util.ucloud import get_private_url from photo_review.util.ucloud import get_private_url
def visual_model_test(task_path, test_img, schema): def write_visual_result(image, layout=None, result=None):
img = re.split(r'[\\/]', test_img)[-1] img = re.split(r'[\\/]', image)[-1]
img = img.split("?")[0] img = img.split("?")[0]
img_name = "" img_name = ""
img_type = "jpg" img_type = "jpg"
@@ -19,30 +19,42 @@ def visual_model_test(task_path, test_img, schema):
img_name = img[:last_dot_index] img_name = img[:last_dot_index]
img_type = img[last_dot_index + 1:] img_type = img[last_dot_index + 1:]
# 默认模型 if layout:
ie = Taskflow("information_extraction", schema=schema, model="uie-x-base") print(layout)
results = ie({"doc": test_img})
pprint(results[0])
DocParser.write_image_with_results( DocParser.write_image_with_results(
test_img, image,
result=results[0], layout=layout,
save_path="./img_result/" + img_name + "_default." + img_type) save_path="./img_result/" + img_name + "_layout." + img_type)
# 自己训练的模型
my_ie = Taskflow("information_extraction", schema=schema, model="uie-x-base", task_path=task_path) if result:
print(result)
DocParser.write_image_with_results(
image,
result=result,
save_path="./img_result/" + img_name + "_result." + img_type)
def visual_model_test(model_type, test_img, task_path, schema):
if model_type == "ocr":
doc_parser = DocParser(layout_analysis=True)
parsed_doc = doc_parser.parse({"doc": test_img})
write_visual_result(test_img, layout=parsed_doc["layout"])
else:
my_ie = Taskflow("information_extraction", schema=schema, model="uie-x-base", task_path=task_path,
layout_analysis=True)
my_results = my_ie({"doc": test_img}) my_results = my_ie({"doc": test_img})
pprint(my_results[0]) write_visual_result(test_img, result=my_results[0])
DocParser.write_image_with_results(
test_img,
result=my_results[0],
save_path="./img_result/" + img_name + "_my." + img_type)
def main(model_type, pic_name=None): def main(model_type, pic_name=None):
# 开始时间 # 开始时间
start_time = time.time() start_time = time.time()
# 结算清单 if model_type == "ocr":
if model_type == "settlement": task_path = None
test_img_path = get_private_url(pic_name) if pic_name else "img/PH20240511000638_1_094306_1.jpg"
schema = None
elif model_type == "settlement":
task_path = "../../config/model/settlement_list_model" task_path = "../../config/model/settlement_list_model"
test_img_path = get_private_url(pic_name) if pic_name else "img/PH20240511000638_1_094306_1.jpg" test_img_path = get_private_url(pic_name) if pic_name else "img/PH20240511000638_1_094306_1.jpg"
schema = ["姓名", "入院日期", "出院日期", "费用总额", "个人现金支付", "个人账户支付", "自费", "医保类型"] schema = ["姓名", "入院日期", "出院日期", "费用总额", "个人现金支付", "个人账户支付", "自费", "医保类型"]
@@ -54,10 +66,14 @@ def main(model_type, pic_name=None):
task_path = "../../config/model/cost_list_model" task_path = "../../config/model/cost_list_model"
test_img_path = get_private_url(pic_name) if pic_name else "img/PH20240511000648_4_094542_2.jpg" test_img_path = get_private_url(pic_name) if pic_name else "img/PH20240511000648_4_094542_2.jpg"
schema = ["姓名", "入院日期", "出院日期", "费用总额"] schema = ["姓名", "入院日期", "出院日期", "费用总额"]
elif model_type == "cost_detail":
task_path = "../../config/model/cost_list_detail_model"
test_img_path = get_private_url(pic_name) if pic_name else "img/PH20240511000648_4_094542_2.jpg"
schema = {"名称": ["类别", "规格", "单价", "数量", "金额"]}
else: else:
print("请输入正确的类型!") print("请输入正确的类型!")
return return
visual_model_test(task_path, test_img_path, schema) visual_model_test(model_type, test_img_path, task_path, schema)
# 结束时间 # 结束时间
end_time = time.time() end_time = time.time()
@@ -65,6 +81,8 @@ def main(model_type, pic_name=None):
if __name__ == '__main__': if __name__ == '__main__':
main("settlement") main("ocr")
# main("settlement")
# main("discharge") # main("discharge")
# main("cost") # main("cost")
# main("cost_detail")