在自动识别时将图片转正并上传到云端
This commit is contained in:
@@ -3,6 +3,7 @@ import logging
|
||||
import os
|
||||
import tempfile
|
||||
import time
|
||||
from collections import defaultdict
|
||||
from time import sleep
|
||||
|
||||
import cv2
|
||||
@@ -67,25 +68,25 @@ def request_ie_result(task_enum, phrecs):
|
||||
|
||||
|
||||
# 关键信息提取
|
||||
def information_extraction(ie, phrecs):
|
||||
def information_extraction(ie, phrecs, identity):
|
||||
result = {}
|
||||
# 同一批图的标识
|
||||
identity = int(time.time())
|
||||
for phrec in phrecs:
|
||||
img_path = ufile.get_private_url(phrec.cfjaddress)
|
||||
if not img_path:
|
||||
continue
|
||||
split_results = image_util.split(img_path)
|
||||
|
||||
image = image_util.read(img_path)
|
||||
angles = image_util.parse_rotation_angles(image)
|
||||
angle_count = defaultdict(int, {"0": 0})
|
||||
split_results = image_util.split(image)
|
||||
for split_result in split_results:
|
||||
angles = image_util.parse_rotation_angles(split_result["img"])
|
||||
rotated_img = image_util.rotate(split_result["img"], int(angles[0]))
|
||||
ie_results = [{"result": ie_temp_image(ie, OCR, rotated_img), "angle": angles[0]}]
|
||||
if not ie_results[0] or len(ie_results[0]) < len(ie.kwargs.get("schema")):
|
||||
if not ie_results[0]["result"] or len(ie_results[0]["result"]) < len(ie.kwargs.get("schema")):
|
||||
rotated_img = image_util.rotate(split_result["img"], int(angles[1]))
|
||||
ie_results.append({"result": ie_temp_image(ie, OCR, rotated_img), "angle": angles[1]})
|
||||
|
||||
now = util.get_default_datetime()
|
||||
best_angle = ["0", 0]
|
||||
for ie_result in ie_results:
|
||||
result_json = json.dumps(ie_result["result"], ensure_ascii=False)
|
||||
if len(result_json) > 5000:
|
||||
@@ -101,6 +102,23 @@ def information_extraction(ie, phrecs):
|
||||
|
||||
result = merge_result(result, ie_result["result"])
|
||||
|
||||
if len(ie_result["result"]) > best_angle[1]:
|
||||
best_angle = [ie_result["angle"], len(ie_result["result"])]
|
||||
|
||||
angle_count[best_angle[0]] += 1
|
||||
|
||||
img_angle = max(angle_count, key=angle_count.get)
|
||||
if img_angle != "0":
|
||||
image = image_util.rotate(image, int(img_angle))
|
||||
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file:
|
||||
cv2.imwrite(temp_file.name, image)
|
||||
try:
|
||||
ufile.upload_file(phrec.cfjaddress, temp_file.name)
|
||||
except Exception as e:
|
||||
logging.error(f"上传图片({phrec.cfjaddress})失败", exc_info=e)
|
||||
finally:
|
||||
util.delete_temp_file(temp_file.name)
|
||||
|
||||
return result
|
||||
|
||||
|
||||
@@ -159,8 +177,8 @@ def save_or_update_ie(table, pk_phhd, data):
|
||||
session.close()
|
||||
|
||||
|
||||
def settlement_task(pk_phhd, settlement_list):
|
||||
settlement_list_ie_result = information_extraction(SETTLEMENT_IE, settlement_list)
|
||||
def settlement_task(pk_phhd, settlement_list, identity):
|
||||
settlement_list_ie_result = information_extraction(SETTLEMENT_IE, settlement_list, identity)
|
||||
settlement_data = {
|
||||
"pk_phhd": pk_phhd,
|
||||
"name": handle_name(get_best_value_in_keys(settlement_list_ie_result, PATIENT_NAME)),
|
||||
@@ -192,8 +210,8 @@ def settlement_task(pk_phhd, settlement_list):
|
||||
save_or_update_ie(ZxIeSettlement, pk_phhd, settlement_data)
|
||||
|
||||
|
||||
def discharge_task(pk_phhd, discharge_record):
|
||||
discharge_record_ie_result = information_extraction(DISCHARGE_IE, discharge_record)
|
||||
def discharge_task(pk_phhd, discharge_record, identity):
|
||||
discharge_record_ie_result = information_extraction(DISCHARGE_IE, discharge_record, identity)
|
||||
discharge_data = {
|
||||
"pk_phhd": pk_phhd,
|
||||
"hospital": handle_hospital(get_best_value_in_keys(discharge_record_ie_result, HOSPITAL)),
|
||||
@@ -233,8 +251,8 @@ def discharge_task(pk_phhd, discharge_record):
|
||||
save_or_update_ie(ZxIeDischarge, pk_phhd, discharge_data)
|
||||
|
||||
|
||||
def cost_task(pk_phhd, cost_list):
|
||||
cost_list_ie_result = information_extraction(COST_IE, cost_list)
|
||||
def cost_task(pk_phhd, cost_list, identity):
|
||||
cost_list_ie_result = information_extraction(COST_IE, cost_list, identity)
|
||||
cost_data = {
|
||||
"pk_phhd": pk_phhd,
|
||||
"name": handle_name(get_best_value_in_keys(cost_list_ie_result, PATIENT_NAME)),
|
||||
@@ -266,9 +284,11 @@ def photo_review(pk_phhd):
|
||||
elif phrec.cRectype == "4":
|
||||
cost_list.append(phrec)
|
||||
|
||||
settlement_task(pk_phhd, settlement_list)
|
||||
discharge_task(pk_phhd, discharge_record)
|
||||
cost_task(pk_phhd, cost_list)
|
||||
# 同一批图的标识
|
||||
identity = int(time.time())
|
||||
settlement_task(pk_phhd, settlement_list, identity)
|
||||
discharge_task(pk_phhd, discharge_record, identity)
|
||||
cost_task(pk_phhd, cost_list, identity)
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
Reference in New Issue
Block a user