优化命名,类与模块最好不要重名

This commit is contained in:
2024-09-20 14:32:31 +08:00
parent 7b6e78373c
commit f0c03e763b
9 changed files with 50 additions and 32 deletions

View File

@@ -11,7 +11,7 @@ from tenacity import retry, stop_after_attempt, wait_random
from paddle_detection import PADDLE_DET from paddle_detection import PADDLE_DET
from paddle_detection.deploy.third_engine.onnx.infer import PredictConfig from paddle_detection.deploy.third_engine.onnx.infer import PredictConfig
from paddle_detection.deploy.third_engine.onnx.preprocess import Compose from paddle_detection.deploy.third_engine.onnx.preprocess import Compose
from util import image_util, util from util import image_util, common_util
def predict_image(infer_config, predictor, img_path): def predict_image(infer_config, predictor, img_path):
@@ -45,7 +45,7 @@ def get_book_areas(image):
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)
detect_result = detect_image(temp_file.name) detect_result = detect_image(temp_file.name)
util.delete_temp_file(temp_file.name) common_util.delete_temp_file(temp_file.name)
book_areas = detect_result[73] book_areas = detect_result[73]
result = [] result = []
for book_area in book_areas: for book_area in book_areas:

View File

@@ -12,7 +12,7 @@ from db.mysql import ZxPhrec, ZxPhhd
from log import HOSTNAME from log import HOSTNAME
from photo_mask import OCR, PHHD_BATCH_SIZE, SLEEP_MINUTES, NAME_KEYS, ID_CARD_NUM_KEYS, SIMILAR_CHAR from photo_mask import OCR, PHHD_BATCH_SIZE, SLEEP_MINUTES, NAME_KEYS, ID_CARD_NUM_KEYS, SIMILAR_CHAR
from ucloud import BUCKET, ufile from ucloud import BUCKET, ufile
from util import image_util, util from util import image_util, common_util
def find_boxes(content, layout, offset=0, length=None, improve=False, image_path=None, extra_content=None): def find_boxes(content, layout, offset=0, length=None, improve=False, image_path=None, extra_content=None):
@@ -56,13 +56,13 @@ def find_boxes(content, layout, offset=0, length=None, improve=False, image_path
# 再次识别,提高精度 # 再次识别,提高精度
image = cv2.imread(image_path) image = cv2.imread(image_path)
# 截图时偏大一点 # 截图时偏大一点
capture_box = util.zoom_rectangle(box, 0.2) capture_box = common_util.zoom_rectangle(box, 0.2)
captured_image = image_util.capture(image, capture_box) captured_image = image_util.capture(image, capture_box)
with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as temp_file: with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as temp_file:
captured_image, offset_x, offset_y = image_util.expand_to_a4_size(captured_image) captured_image, offset_x, offset_y = image_util.expand_to_a4_size(captured_image)
cv2.imwrite(temp_file.name, captured_image) cv2.imwrite(temp_file.name, captured_image)
try: try:
layouts = util.get_ocr_layout(OCR, temp_file.name) layouts = common_util.get_ocr_layout(OCR, temp_file.name)
except TypeError: except TypeError:
# 如果是类型错误,大概率是没识别到文字 # 如果是类型错误,大概率是没识别到文字
layouts = [] layouts = []
@@ -86,7 +86,7 @@ def find_boxes(content, layout, offset=0, length=None, improve=False, image_path
temp_box[3] + capture_box[1] - offset_y, temp_box[3] + capture_box[1] - offset_y,
]) ])
break break
util.delete_temp_file(temp_file.name) common_util.delete_temp_file(temp_file.name)
if not boxes: if not boxes:
boxes.append(box) boxes.append(box)
@@ -100,7 +100,7 @@ def get_mask_layout(image, name, id_card_num):
result = [] result = []
try: try:
try: try:
layouts = util.get_ocr_layout(OCR, temp_file.name) layouts = common_util.get_ocr_layout(OCR, temp_file.name)
# layouts = OCR.parse({"doc": temp_file.name})["layout"] # layouts = OCR.parse({"doc": temp_file.name})["layout"]
except TypeError: except TypeError:
# 如果是类型错误,大概率是没识别到文字 # 如果是类型错误,大概率是没识别到文字
@@ -164,7 +164,7 @@ def get_mask_layout(image, name, id_card_num):
logging.error("涂抹时出错!", exc_info=e) logging.error("涂抹时出错!", exc_info=e)
return result return result
finally: finally:
util.delete_temp_file(temp_file.name) common_util.delete_temp_file(temp_file.name)
def handle_image_for_mask(split_result): def handle_image_for_mask(split_result):
@@ -241,14 +241,14 @@ def photo_mask(pk_phhd, name, id_card_num):
session = MysqlSession() session = MysqlSession()
update_flag = (update(ZxPhrec).where(ZxPhrec.pk_phrec == phrec.pk_phrec).values( update_flag = (update(ZxPhrec).where(ZxPhrec.pk_phrec == phrec.pk_phrec).values(
paint_user=HOSTNAME, paint_user=HOSTNAME,
paint_date=util.get_default_datetime())) paint_date=common_util.get_default_datetime()))
session.execute(update_flag) session.execute(update_flag)
session.commit() session.commit()
session.close() session.close()
except Exception as e: except Exception as e:
logging.error("上传图片出错", exc_info=e) logging.error("上传图片出错", exc_info=e)
finally: finally:
util.delete_temp_file(temp_file.name) common_util.delete_temp_file(temp_file.name)
def main(): def main():
@@ -278,7 +278,7 @@ def main():
update_flag = (update(ZxPhhd).where(ZxPhhd.pk_phhd == pk_phhd).values( update_flag = (update(ZxPhhd).where(ZxPhhd.pk_phhd == pk_phhd).values(
paint_flag="8", paint_flag="8",
paint_user=HOSTNAME, paint_user=HOSTNAME,
paint_date=util.get_default_datetime(), paint_date=common_util.get_default_datetime(),
fZcfwfy=time.time() - start_time)) fZcfwfy=time.time() - start_time))
session.execute(update_flag) session.execute(update_flag)
session.commit() session.commit()

View File

@@ -8,7 +8,7 @@ from db import MysqlSession
from db.mysql import ZxIeOcrerror, ZxPhrec from db.mysql import ZxIeOcrerror, ZxPhrec
from photo_mask.auto_photo_mask import mask_photo from photo_mask.auto_photo_mask import mask_photo
from ucloud import ufile from ucloud import ufile
from util import image_util, util from util import image_util, common_util
def check_error(error_ocr): def check_error(error_ocr):
@@ -91,7 +91,7 @@ if __name__ == '__main__':
session = MysqlSession() session = MysqlSession()
update_error = (update(ZxIeOcrerror).where(ZxIeOcrerror.pk_phrec == ocr_error.pk_phrec).values( update_error = (update(ZxIeOcrerror).where(ZxIeOcrerror.pk_phrec == ocr_error.pk_phrec).values(
checktime=util.get_default_datetime(), cfjaddress2=error_descript)) checktime=common_util.get_default_datetime(), cfjaddress2=error_descript))
session.execute(update_error) session.execute(update_error)
session.commit() session.commit()
session.close() session.close()

View File

@@ -7,7 +7,7 @@ from sqlalchemy import update, and_
from db import MysqlSession from db import MysqlSession
from db.mysql import ZxIeOcrerror from db.mysql import ZxIeOcrerror
from photo_mask.photo_mask_error_check import auto_check_error from photo_mask.photo_mask_error_check import auto_check_error
from util import util from util import common_util
if __name__ == '__main__': if __name__ == '__main__':
today = date.today() today = date.today()
@@ -29,7 +29,7 @@ if __name__ == '__main__':
if error_descript == "未知错误": if error_descript == "未知错误":
check_time = None check_time = None
else: else:
check_time = util.get_default_datetime() check_time = common_util.get_default_datetime()
session = MysqlSession() session = MysqlSession()
update_error = (update(ZxIeOcrerror).where(ZxIeOcrerror.pk_phrec == ocr_error.pk_phrec).values( update_error = (update(ZxIeOcrerror).where(ZxIeOcrerror.pk_phrec == ocr_error.pk_phrec).values(
@@ -41,5 +41,5 @@ if __name__ == '__main__':
print(result) print(result)
with open("photo_mask_error_report.txt", 'w', encoding='utf-8') as file: with open("photo_mask_error_report.txt", 'w', encoding='utf-8') as file:
file.write(json.dumps(result, indent=4, ensure_ascii=False)) file.write(json.dumps(result, indent=4, ensure_ascii=False))
file.write(util.get_default_datetime()) file.write(common_util.get_default_datetime())
print("结果已保存。") print("结果已保存。")

View File

@@ -23,7 +23,7 @@ from photo_review import PATIENT_NAME, ADMISSION_DATE, DISCHARGE_DATE, MEDICAL_E
ADMISSION_ID, SETTLEMENT_ID, AGE, OCR, SETTLEMENT_IE, DISCHARGE_IE, COST_IE, PHHD_BATCH_SIZE, SLEEP_MINUTES, \ ADMISSION_ID, SETTLEMENT_ID, AGE, OCR, SETTLEMENT_IE, DISCHARGE_IE, COST_IE, PHHD_BATCH_SIZE, SLEEP_MINUTES, \
UPPERCASE_MEDICAL_EXPENSES, HOSPITAL_ALIAS, HOSPITAL_FILTER, DEPARTMENT_ALIAS, DEPARTMENT_FILTER UPPERCASE_MEDICAL_EXPENSES, HOSPITAL_ALIAS, HOSPITAL_FILTER, DEPARTMENT_ALIAS, DEPARTMENT_FILTER
from ucloud import ufile from ucloud import ufile
from util import image_util, util, html_util from util import image_util, common_util, html_util
from util.data_util import handle_date, handle_decimal, parse_department, handle_name, \ from util.data_util import handle_date, handle_decimal, parse_department, handle_name, \
handle_insurance_type, handle_original_data, handle_hospital, handle_department, handle_id, handle_age, parse_money, \ handle_insurance_type, handle_original_data, handle_hospital, handle_department, handle_id, handle_age, parse_money, \
parse_hospital parse_hospital
@@ -42,7 +42,7 @@ def ie_temp_image(ie, ocr, image):
ie_result = [] ie_result = []
try: try:
layout = util.get_ocr_layout(ocr, temp_file.name) layout = common_util.get_ocr_layout(ocr, temp_file.name)
if not layout: if not layout:
# 无识别结果 # 无识别结果
ie_result = [] ie_result = []
@@ -103,7 +103,7 @@ def get_better_image_from_qrcode(image, image_id, dpi=150):
if pdf_file: if pdf_file:
pdf_file.close() pdf_file.close()
if local_pdf_path: if local_pdf_path:
util.delete_temp_file(local_pdf_path) common_util.delete_temp_file(local_pdf_path)
jsczt_base_url = 'http://einvoice.jsczt.cn' jsczt_base_url = 'http://einvoice.jsczt.cn'
try: try:
@@ -168,7 +168,7 @@ def information_extraction(ie, phrecs, identity):
info_extract = ie_temp_image(ie, OCR, image) info_extract = ie_temp_image(ie, OCR, image)
ie_result = {'result': info_extract, 'angle': '0'} ie_result = {'result': info_extract, 'angle': '0'}
now = util.get_default_datetime() now = common_util.get_default_datetime()
if not ie_result['result']: if not ie_result['result']:
continue continue
@@ -202,7 +202,7 @@ def information_extraction(ie, phrecs, identity):
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 = common_util.get_default_datetime()
best_angle = ['0', 0] best_angle = ['0', 0]
for ie_result in ie_results: for ie_result in ie_results:
if not ie_result['result']: if not ie_result['result']:
@@ -242,7 +242,7 @@ def information_extraction(ie, phrecs, identity):
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) common_util.delete_temp_file(temp_file.name)
session = MysqlSession() session = MysqlSession()
session.add_all(zx_ie_results) session.add_all(zx_ie_results)
@@ -289,7 +289,7 @@ def save_or_update_ie(table, pk_phhd, data):
obj = table(**data) obj = table(**data)
session = MysqlSession() session = MysqlSession()
db_data = session.query(table).filter_by(pk_phhd=pk_phhd).one_or_none() db_data = session.query(table).filter_by(pk_phhd=pk_phhd).one_or_none()
now = util.get_default_datetime() now = common_util.get_default_datetime()
if db_data: if db_data:
# 更新 # 更新
db_data.update_time = now db_data.update_time = now
@@ -536,7 +536,7 @@ def main():
update_flag = (update(ZxPhhd).where(ZxPhhd.pk_phhd == pk_phhd).values( update_flag = (update(ZxPhhd).where(ZxPhhd.pk_phhd == pk_phhd).values(
exsuccess_flag="8", exsuccess_flag="8",
ref_id1=HOSTNAME, ref_id1=HOSTNAME,
checktime=util.get_default_datetime(), checktime=common_util.get_default_datetime(),
fFSYLFY=time.time() - start_time)) fFSYLFY=time.time() - start_time))
session.execute(update_flag) session.execute(update_flag)
session.commit() session.commit()

View File

@@ -6,7 +6,7 @@ from sqlalchemy.sql.functions import count
from db import MysqlSession from db import MysqlSession
from db.mysql import ZxPhhd, ViewErrorReview from db.mysql import ZxPhhd, ViewErrorReview
from util import util from util import common_util
def handle_reason(reason): def handle_reason(reason):
@@ -95,5 +95,5 @@ if __name__ == '__main__':
print(result) print(result)
with open("photo_review_error_report.txt", 'w', encoding='utf-8') as file: with open("photo_review_error_report.txt", 'w', encoding='utf-8') as file:
file.write(json.dumps(result, indent=4, ensure_ascii=False)) file.write(json.dumps(result, indent=4, ensure_ascii=False))
file.write(util.get_default_datetime()) file.write(common_util.get_default_datetime())
print("结果已保存。") print("结果已保存。")

View File

@@ -2,6 +2,7 @@ import logging
import os import os
from datetime import datetime from datetime import datetime
from flask import jsonify
from opencc import OpenCC from opencc import OpenCC
from util import string_util from util import string_util
@@ -217,3 +218,19 @@ def chinese_money_to_number(chinese_money_amount):
def traditional_to_simple_chinese(traditional_chinese): def traditional_to_simple_chinese(traditional_chinese):
converter = OpenCC('t2s') converter = OpenCC('t2s')
return converter.convert(traditional_chinese) return converter.convert(traditional_chinese)
def process_request(func):
"""
api通用处理函数
"""
def wrapper(*args, **kwargs):
try:
result = func(*args, **kwargs)
return jsonify(result), 200
except Exception as e:
logging.getLogger('error').error(f'Error: {e}')
return jsonify({'error': str(e)}), 500
return wrapper

View File

@@ -2,7 +2,7 @@ import logging
import re import re
from datetime import datetime from datetime import datetime
from util import util from util import common_util
# 处理金额类数据 # 处理金额类数据
@@ -29,7 +29,7 @@ def handle_decimal(string):
def parse_money(capital_num, num): def parse_money(capital_num, num):
if capital_num: if capital_num:
try: try:
money = util.chinese_money_to_number(capital_num) money = common_util.chinese_money_to_number(capital_num)
return capital_num, money return capital_num, money
except Exception as e: except Exception as e:
logging.warning("大写金额解析失败", exc_info=e) logging.warning("大写金额解析失败", exc_info=e)
@@ -172,7 +172,7 @@ def parse_hospital(string):
if not string: if not string:
return result return result
string = util.traditional_to_simple_chinese(string) string = common_util.traditional_to_simple_chinese(string)
string_without_brackets = string.replace(")", "").replace("", "").replace("(", " ").replace("", " ") string_without_brackets = string.replace(")", "").replace("", "").replace("(", " ").replace("", " ")
string_without_company = string_without_brackets.replace("有限公司", "") string_without_company = string_without_brackets.replace("有限公司", "")
split_hospitals = string_without_company.replace("医院", "医院 ") split_hospitals = string_without_company.replace("医院", "医院 ")

View File

@@ -11,7 +11,7 @@ from paddlenlp.utils.doc_parser import DocParser
from paddleocr import PaddleOCR from paddleocr import PaddleOCR
from ucloud import ufile from ucloud import ufile
from util import image_util, util from util import image_util, common_util
def write_visual_result(image, angle=0, layout=None, result=None): def write_visual_result(image, angle=0, layout=None, result=None):
@@ -63,7 +63,7 @@ def visual_model_test(model_type, test_img, task_path, schema):
img["y_offset"] -= offset_y img["y_offset"] -= offset_y
temp_files_paths.append(temp_file.name) temp_files_paths.append(temp_file.name)
parsed_doc = util.get_ocr_layout( parsed_doc = common_util.get_ocr_layout(
PaddleOCR(det_db_box_thresh=0.3, det_db_thresh=0.1, det_limit_side_len=1248, drop_score=0.3, PaddleOCR(det_db_box_thresh=0.3, det_db_thresh=0.1, det_limit_side_len=1248, drop_score=0.3,
save_crop_res=False), save_crop_res=False),
temp_file.name) temp_file.name)
@@ -121,7 +121,8 @@ def main(model_type, pic_name=None):
if model_type == "ocr": if model_type == "ocr":
task_path = None task_path = None
test_img_path = ufile.get_private_url(pic_name, "drg103") if pic_name else "../test_img/PH20240725004467_3_185708_1.jpg" test_img_path = ufile.get_private_url(pic_name,
"drg103") if pic_name else "../test_img/PH20240725004467_3_185708_1.jpg"
schema = None schema = None
elif model_type == "settlement": elif model_type == "settlement":
task_path = "../model/settlement_list_model" task_path = "../model/settlement_list_model"