diff --git a/paddle_detection/detector.py b/paddle_detection/detector.py index 703f2fc..896800b 100644 --- a/paddle_detection/detector.py +++ b/paddle_detection/detector.py @@ -11,7 +11,7 @@ from tenacity import retry, stop_after_attempt, wait_random from paddle_detection import PADDLE_DET from paddle_detection.deploy.third_engine.onnx.infer import PredictConfig 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): @@ -45,7 +45,7 @@ def get_book_areas(image): with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file: cv2.imwrite(temp_file.name, image) 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] result = [] for book_area in book_areas: diff --git a/photo_mask/auto_photo_mask.py b/photo_mask/auto_photo_mask.py index fd1c18d..9eeb23d 100644 --- a/photo_mask/auto_photo_mask.py +++ b/photo_mask/auto_photo_mask.py @@ -12,7 +12,7 @@ from db.mysql import ZxPhrec, ZxPhhd from log import HOSTNAME from photo_mask import OCR, PHHD_BATCH_SIZE, SLEEP_MINUTES, NAME_KEYS, ID_CARD_NUM_KEYS, SIMILAR_CHAR 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): @@ -56,13 +56,13 @@ def find_boxes(content, layout, offset=0, length=None, improve=False, 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) with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as temp_file: captured_image, offset_x, offset_y = image_util.expand_to_a4_size(captured_image) cv2.imwrite(temp_file.name, captured_image) try: - layouts = util.get_ocr_layout(OCR, temp_file.name) + layouts = common_util.get_ocr_layout(OCR, temp_file.name) except TypeError: # 如果是类型错误,大概率是没识别到文字 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, ]) break - util.delete_temp_file(temp_file.name) + common_util.delete_temp_file(temp_file.name) if not boxes: boxes.append(box) @@ -100,7 +100,7 @@ def get_mask_layout(image, name, id_card_num): result = [] 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"] except TypeError: # 如果是类型错误,大概率是没识别到文字 @@ -164,7 +164,7 @@ def get_mask_layout(image, name, id_card_num): logging.error("涂抹时出错!", exc_info=e) return result finally: - util.delete_temp_file(temp_file.name) + common_util.delete_temp_file(temp_file.name) def handle_image_for_mask(split_result): @@ -241,14 +241,14 @@ def photo_mask(pk_phhd, name, id_card_num): session = MysqlSession() update_flag = (update(ZxPhrec).where(ZxPhrec.pk_phrec == phrec.pk_phrec).values( paint_user=HOSTNAME, - paint_date=util.get_default_datetime())) + paint_date=common_util.get_default_datetime())) session.execute(update_flag) session.commit() session.close() except Exception as e: logging.error("上传图片出错", exc_info=e) finally: - util.delete_temp_file(temp_file.name) + common_util.delete_temp_file(temp_file.name) def main(): @@ -278,7 +278,7 @@ def main(): update_flag = (update(ZxPhhd).where(ZxPhhd.pk_phhd == pk_phhd).values( paint_flag="8", paint_user=HOSTNAME, - paint_date=util.get_default_datetime(), + paint_date=common_util.get_default_datetime(), fZcfwfy=time.time() - start_time)) session.execute(update_flag) session.commit() diff --git a/photo_mask/photo_mask_error_check.py b/photo_mask/photo_mask_error_check.py index ca5ecc8..fe62a40 100644 --- a/photo_mask/photo_mask_error_check.py +++ b/photo_mask/photo_mask_error_check.py @@ -8,7 +8,7 @@ from db import MysqlSession from db.mysql import ZxIeOcrerror, ZxPhrec from photo_mask.auto_photo_mask import mask_photo from ucloud import ufile -from util import image_util, util +from util import image_util, common_util def check_error(error_ocr): @@ -91,7 +91,7 @@ if __name__ == '__main__': session = MysqlSession() 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.commit() session.close() diff --git a/photo_mask/photo_mask_error_report.py b/photo_mask/photo_mask_error_report.py index c458171..1074e7c 100644 --- a/photo_mask/photo_mask_error_report.py +++ b/photo_mask/photo_mask_error_report.py @@ -7,7 +7,7 @@ from sqlalchemy import update, and_ from db import MysqlSession from db.mysql import ZxIeOcrerror from photo_mask.photo_mask_error_check import auto_check_error -from util import util +from util import common_util if __name__ == '__main__': today = date.today() @@ -29,7 +29,7 @@ if __name__ == '__main__': if error_descript == "未知错误": check_time = None else: - check_time = util.get_default_datetime() + check_time = common_util.get_default_datetime() session = MysqlSession() update_error = (update(ZxIeOcrerror).where(ZxIeOcrerror.pk_phrec == ocr_error.pk_phrec).values( @@ -41,5 +41,5 @@ if __name__ == '__main__': print(result) 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(util.get_default_datetime()) + file.write(common_util.get_default_datetime()) print("结果已保存。") diff --git a/photo_review/auto_photo_review.py b/photo_review/auto_photo_review.py index 41520ac..67aa845 100644 --- a/photo_review/auto_photo_review.py +++ b/photo_review/auto_photo_review.py @@ -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, \ UPPERCASE_MEDICAL_EXPENSES, HOSPITAL_ALIAS, HOSPITAL_FILTER, DEPARTMENT_ALIAS, DEPARTMENT_FILTER 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, \ handle_insurance_type, handle_original_data, handle_hospital, handle_department, handle_id, handle_age, parse_money, \ parse_hospital @@ -42,7 +42,7 @@ def ie_temp_image(ie, ocr, image): ie_result = [] try: - layout = util.get_ocr_layout(ocr, temp_file.name) + layout = common_util.get_ocr_layout(ocr, temp_file.name) if not layout: # 无识别结果 ie_result = [] @@ -103,7 +103,7 @@ def get_better_image_from_qrcode(image, image_id, dpi=150): if pdf_file: pdf_file.close() 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' try: @@ -168,7 +168,7 @@ def information_extraction(ie, phrecs, identity): info_extract = ie_temp_image(ie, OCR, image) ie_result = {'result': info_extract, 'angle': '0'} - now = util.get_default_datetime() + now = common_util.get_default_datetime() if not ie_result['result']: 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')): 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() + now = common_util.get_default_datetime() best_angle = ['0', 0] for ie_result in ie_results: if not ie_result['result']: @@ -242,7 +242,7 @@ def information_extraction(ie, phrecs, identity): except Exception as e: logging.error(f'上传图片({phrec.cfjaddress})失败', exc_info=e) finally: - util.delete_temp_file(temp_file.name) + common_util.delete_temp_file(temp_file.name) session = MysqlSession() session.add_all(zx_ie_results) @@ -289,7 +289,7 @@ def save_or_update_ie(table, pk_phhd, data): obj = table(**data) session = MysqlSession() 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: # 更新 db_data.update_time = now @@ -536,7 +536,7 @@ def main(): update_flag = (update(ZxPhhd).where(ZxPhhd.pk_phhd == pk_phhd).values( exsuccess_flag="8", ref_id1=HOSTNAME, - checktime=util.get_default_datetime(), + checktime=common_util.get_default_datetime(), fFSYLFY=time.time() - start_time)) session.execute(update_flag) session.commit() diff --git a/photo_review/photo_review_error_report.py b/photo_review/photo_review_error_report.py index 11d0eba..5667587 100644 --- a/photo_review/photo_review_error_report.py +++ b/photo_review/photo_review_error_report.py @@ -6,7 +6,7 @@ from sqlalchemy.sql.functions import count from db import MysqlSession from db.mysql import ZxPhhd, ViewErrorReview -from util import util +from util import common_util def handle_reason(reason): @@ -95,5 +95,5 @@ if __name__ == '__main__': print(result) 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(util.get_default_datetime()) + file.write(common_util.get_default_datetime()) print("结果已保存。") diff --git a/util/util.py b/util/common_util.py similarity index 93% rename from util/util.py rename to util/common_util.py index 29b9737..6afe668 100644 --- a/util/util.py +++ b/util/common_util.py @@ -2,6 +2,7 @@ import logging import os from datetime import datetime +from flask import jsonify from opencc import OpenCC from util import string_util @@ -217,3 +218,19 @@ def chinese_money_to_number(chinese_money_amount): def traditional_to_simple_chinese(traditional_chinese): converter = OpenCC('t2s') 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 diff --git a/util/data_util.py b/util/data_util.py index 1504b5c..94e80a8 100644 --- a/util/data_util.py +++ b/util/data_util.py @@ -2,7 +2,7 @@ import logging import re 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): if capital_num: try: - money = util.chinese_money_to_number(capital_num) + money = common_util.chinese_money_to_number(capital_num) return capital_num, money except Exception as e: logging.warning("大写金额解析失败", exc_info=e) @@ -172,7 +172,7 @@ def parse_hospital(string): if not string: 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_company = string_without_brackets.replace("有限公司", "") split_hospitals = string_without_company.replace("医院", "医院 ") diff --git a/visual_model_test/visual_model_test.py b/visual_model_test/visual_model_test.py index cef670a..6af72d3 100644 --- a/visual_model_test/visual_model_test.py +++ b/visual_model_test/visual_model_test.py @@ -11,7 +11,7 @@ from paddlenlp.utils.doc_parser import DocParser from paddleocr import PaddleOCR 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): @@ -63,7 +63,7 @@ def visual_model_test(model_type, test_img, task_path, schema): img["y_offset"] -= offset_y 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, save_crop_res=False), temp_file.name) @@ -121,7 +121,8 @@ def main(model_type, pic_name=None): if model_type == "ocr": 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 elif model_type == "settlement": task_path = "../model/settlement_list_model"