Revert "自动识别耗时检测"

This reverts commit b0340c36de.
This commit is contained in:
2024-09-02 11:01:13 +08:00
parent b0340c36de
commit 50e1cceb2d
2 changed files with 0 additions and 71 deletions

View File

@@ -1,41 +0,0 @@
x-env:
&template
image: fcb_photo_review:2.0.0
restart: always
services:
det_api:
<<: *template
build:
context: .
container_name: det_api
hostname: det_api
volumes:
- ./log:/app/log
- ./model:/app/model
command: [ "det_api.py" ]
deploy:
resources:
reservations:
devices:
- device_ids: [ "0" ]
capabilities: [ "gpu" ]
driver: "nvidia"
photo_time_test:
<<: *template
container_name: photo_time_test
hostname: photo_time_test
volumes:
- ./log:/app/log
- ./model:/app/model
depends_on:
- det_api
command: [ "photo_review.py" ]
deploy:
resources:
reservations:
devices:
- device_ids: [ "0", "1" ]
capabilities: [ "gpu" ]
driver: "nvidia"

View File

@@ -76,47 +76,32 @@ def request_ie_result(task_enum, phrecs):
def information_extraction(ie, phrecs, identity): def information_extraction(ie, phrecs, identity):
result = {} result = {}
for phrec in phrecs: for phrec in phrecs:
ufile_start = time.time()
img_path = ufile.get_private_url(phrec.cfjaddress) img_path = ufile.get_private_url(phrec.cfjaddress)
if not img_path: if not img_path:
continue continue
image = image_util.read(img_path) image = image_util.read(img_path)
logging.info(f"获取云图片耗时:{time.time() - ufile_start}s")
target_images = [] target_images = []
book_start = time.time()
target_images += detector.request_book_areas(image) # 识别文档区域并裁剪 target_images += detector.request_book_areas(image) # 识别文档区域并裁剪
if not target_images: if not target_images:
target_images.append(image) # 识别失败 target_images.append(image) # 识别失败
logging.info(f"检测文档耗时:{time.time() - book_start}s")
angle_count = defaultdict(int, {"0": 0}) # 分割后图片的最优角度统计 angle_count = defaultdict(int, {"0": 0}) # 分割后图片的最优角度统计
for target_image in target_images: for target_image in target_images:
dewarp_start = time.time()
dewarped_image = dewarp.dewarp_image(target_image) # 去扭曲 dewarped_image = dewarp.dewarp_image(target_image) # 去扭曲
logging.info(f"去扭曲耗时:{time.time() - dewarp_start}s")
rotate_start = time.time()
angles = image_util.parse_rotation_angles(dewarped_image) angles = image_util.parse_rotation_angles(dewarped_image)
logging.info(f"检测图片旋转耗时:{time.time() - rotate_start}s")
zx_ie_results = [] zx_ie_results = []
split_start = time.time()
split_results = image_util.split(dewarped_image) split_results = image_util.split(dewarped_image)
logging.info(f"分割图片耗时:{time.time() - split_start}s")
for split_result in split_results: for split_result in split_results:
if split_result["img"] is None or split_result["img"].size == 0: if split_result["img"] is None or split_result["img"].size == 0:
continue continue
rotated_start = time.time()
rotated_img = image_util.rotate(split_result["img"], int(angles[0])) rotated_img = image_util.rotate(split_result["img"], int(angles[0]))
logging.info(f"旋转图片耗时:{time.time() - rotated_start}s")
ie_start = time.time()
ie_results = [{"result": ie_temp_image(ie, OCR, rotated_img), "angle": angles[0]}] ie_results = [{"result": ie_temp_image(ie, OCR, rotated_img), "angle": angles[0]}]
logging.info(f"信息抽取耗时:{time.time() - ie_start}s")
if not ie_results[0]["result"] or len(ie_results[0]["result"]) < 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])) 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]}) ie_results.append({"result": ie_temp_image(ie, OCR, rotated_img), "angle": angles[1]})
now = util.get_default_datetime() now = util.get_default_datetime()
best_angle = ["0", 0] best_angle = ["0", 0]
result_start = time.time()
for ie_result in ie_results: for ie_result in ie_results:
if not ie_result["result"]: if not ie_result["result"]:
continue continue
@@ -137,7 +122,6 @@ def information_extraction(ie, phrecs, identity):
best_angle = [ie_result["angle"], len(ie_result["result"])] best_angle = [ie_result["angle"], len(ie_result["result"])]
angle_count[best_angle[0]] += 1 angle_count[best_angle[0]] += 1
logging.info(f"构建结果耗时:{time.time() - result_start}s")
img_angle = max(angle_count, key=angle_count.get) img_angle = max(angle_count, key=angle_count.get)
if img_angle != "0": if img_angle != "0":
@@ -145,23 +129,19 @@ def information_extraction(ie, phrecs, identity):
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file: with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file:
cv2.imwrite(temp_file.name, image) cv2.imwrite(temp_file.name, image)
try: try:
upload_start = time.time()
ufile.upload_file(phrec.cfjaddress, temp_file.name) ufile.upload_file(phrec.cfjaddress, temp_file.name)
# 修正旋转角度 # 修正旋转角度
for zx_ie_result in zx_ie_results: for zx_ie_result in zx_ie_results:
zx_ie_result.rotation_angle -= int(img_angle) zx_ie_result.rotation_angle -= int(img_angle)
logging.info(f"上传图片耗时:{time.time() - upload_start}s")
except Exception as e: except Exception as e:
logging.error(f"上传图片({phrec.cfjaddress})失败", exc_info=e) logging.error(f"上传图片({phrec.cfjaddress})失败", exc_info=e)
finally: finally:
util.delete_temp_file(temp_file.name) util.delete_temp_file(temp_file.name)
db_start = time.time()
session = MysqlSession() session = MysqlSession()
session.add_all(zx_ie_results) session.add_all(zx_ie_results)
session.commit() session.commit()
session.close() session.close()
logging.info(f"写入数据库耗时:{time.time() - db_start}s")
return result return result
@@ -280,7 +260,6 @@ def search_department(department):
def settlement_task(pk_phhd, settlement_list, identity): def settlement_task(pk_phhd, settlement_list, identity):
settlement_list_ie_result = information_extraction(SETTLEMENT_IE, settlement_list, identity) settlement_list_ie_result = information_extraction(SETTLEMENT_IE, settlement_list, identity)
settlement_start = time.time()
settlement_data = { settlement_data = {
"pk_phhd": pk_phhd, "pk_phhd": pk_phhd,
"name": handle_name(get_best_value_in_keys(settlement_list_ie_result, PATIENT_NAME)), "name": handle_name(get_best_value_in_keys(settlement_list_ie_result, PATIENT_NAME)),
@@ -310,12 +289,10 @@ def settlement_task(pk_phhd, settlement_list, identity):
settlement_data["medical_expenses_str"] = handle_original_data(parse_money_result[0]) settlement_data["medical_expenses_str"] = handle_original_data(parse_money_result[0])
settlement_data["medical_expenses"] = parse_money_result[1] settlement_data["medical_expenses"] = parse_money_result[1]
save_or_update_ie(ZxIeSettlement, pk_phhd, settlement_data) save_or_update_ie(ZxIeSettlement, pk_phhd, settlement_data)
logging.info(f"处理结算单耗时: {time.time() - settlement_start}s")
def discharge_task(pk_phhd, discharge_record, identity): def discharge_task(pk_phhd, discharge_record, identity):
discharge_record_ie_result = information_extraction(DISCHARGE_IE, discharge_record, identity) discharge_record_ie_result = information_extraction(DISCHARGE_IE, discharge_record, identity)
discharge_start = time.time()
hospitals = get_values_of_keys(discharge_record_ie_result, HOSPITAL) hospitals = get_values_of_keys(discharge_record_ie_result, HOSPITAL)
departments = get_values_of_keys(discharge_record_ie_result, DEPARTMENT) departments = get_values_of_keys(discharge_record_ie_result, DEPARTMENT)
discharge_data = { discharge_data = {
@@ -384,12 +361,10 @@ def discharge_task(pk_phhd, discharge_record, identity):
if best_match: if best_match:
discharge_data["pk_ylks"] = best_match[2] discharge_data["pk_ylks"] = best_match[2]
save_or_update_ie(ZxIeDischarge, pk_phhd, discharge_data) save_or_update_ie(ZxIeDischarge, pk_phhd, discharge_data)
logging.info(f"处理出院记录耗时: {time.time() - discharge_start}s")
def cost_task(pk_phhd, cost_list, identity): def cost_task(pk_phhd, cost_list, identity):
cost_list_ie_result = information_extraction(COST_IE, cost_list, identity) cost_list_ie_result = information_extraction(COST_IE, cost_list, identity)
cost_start = time.time()
cost_data = { cost_data = {
"pk_phhd": pk_phhd, "pk_phhd": pk_phhd,
"name": handle_name(get_best_value_in_keys(cost_list_ie_result, PATIENT_NAME)), "name": handle_name(get_best_value_in_keys(cost_list_ie_result, PATIENT_NAME)),
@@ -401,11 +376,9 @@ def cost_task(pk_phhd, cost_list, identity):
cost_data["discharge_date"] = handle_date(cost_data["discharge_date_str"]) cost_data["discharge_date"] = handle_date(cost_data["discharge_date_str"])
cost_data["medical_expenses"] = handle_decimal(cost_data["medical_expenses_str"]) cost_data["medical_expenses"] = handle_decimal(cost_data["medical_expenses_str"])
save_or_update_ie(ZxIeCost, pk_phhd, cost_data) save_or_update_ie(ZxIeCost, pk_phhd, cost_data)
logging.info(f"处理费用记录耗时: {time.time() - cost_start}s")
def photo_review(pk_phhd): def photo_review(pk_phhd):
phrec_start = time.time()
settlement_list = [] settlement_list = []
discharge_record = [] discharge_record = []
cost_list = [] cost_list = []
@@ -422,7 +395,6 @@ def photo_review(pk_phhd):
discharge_record.append(phrec) discharge_record.append(phrec)
elif phrec.cRectype == "4": elif phrec.cRectype == "4":
cost_list.append(phrec) cost_list.append(phrec)
logging.info(f"图片分类耗时:{time.time() - phrec_start}s")
# 同一批图的标识 # 同一批图的标识
identity = int(time.time()) identity = int(time.time())
@@ -433,7 +405,6 @@ def photo_review(pk_phhd):
def main(): def main():
while 1: while 1:
db_start = time.time()
session = MysqlSession() session = MysqlSession()
phhds = (session.query(ZxPhhd.pk_phhd) phhds = (session.query(ZxPhhd.pk_phhd)
.join(ZxPhrec, ZxPhhd.pk_phhd == ZxPhrec.pk_phhd, isouter=True) .join(ZxPhrec, ZxPhhd.pk_phhd == ZxPhrec.pk_phhd, isouter=True)
@@ -446,7 +417,6 @@ def main():
session.execute(update_flag) session.execute(update_flag)
session.commit() session.commit()
session.close() session.close()
logging.info(f"数据库查询耗时:{time.time() - db_start}s")
if phhds: if phhds:
for phhd in phhds: for phhd in phhds:
pk_phhd = phhd.pk_phhd pk_phhd = phhd.pk_phhd