项目架构调整,模型全部采用接口调用

This commit is contained in:
2024-09-25 14:46:37 +08:00
parent 7647df7d74
commit b8c1202957
25 changed files with 467 additions and 222 deletions

View File

@@ -2,10 +2,9 @@ import logging
import os
from datetime import datetime
from flask import jsonify
from opencc import OpenCC
from util import string_util
from util import string_util, model_util
# 获取yyyy-MM-dd HH:mm:ss格式的当前时间
@@ -37,7 +36,7 @@ def get_ocr_layout(ocr, img_path):
return True
layout = []
ocr_result = ocr.ocr(img_path, cls=False)
ocr_result = model_util.request_ocr(img_path)
ocr_result = ocr_result[0]
if not ocr_result:
return layout
@@ -218,19 +217,3 @@ 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

View File

@@ -283,9 +283,3 @@ def save_to_local(img_url, save_path=None):
file.write(response.content)
return save_path
def parse_path(img_path):
dirname = os.path.dirname(img_path)
img_name, ext = os.path.basename(img_path).rsplit('.', 1)
return dirname, img_name, ext

View File

@@ -1,9 +1,93 @@
import json
import logging
import requests
from tenacity import retry, stop_after_attempt, wait_random
@retry(stop=stop_after_attempt(3), wait=wait_random(1, 3), reraise=True,
after=lambda x: logging.warning('OCR识别失败'))
def request_ocr(img_path):
"""
请求图片OCR识别接口
:param img_path: 待识别图片路径
:return: 识别结果
"""
url = 'http://ocr_api:5001/ocr'
response = requests.post(url, {'img_path': img_path})
if response.status_code == 200:
return response.json()
else:
return None
@retry(stop=stop_after_attempt(3), wait=wait_random(1, 3), reraise=True,
after=lambda x: logging.warning('抽取基本医保结算单失败!'))
def request_discharge_info(img_path, layout):
"""
请求基本医保结算单信息抽取接口
:param img_path: 待抽取图片路径
:param layout: 图片ocr信息
:return: 抽取结果
"""
url = 'http://settlement_api:5002/nlp/settlement'
response = requests.post(url, {'img_path': img_path, 'layout': json.dumps(layout)})
if response.status_code == 200:
return response.json()
else:
return None
@retry(stop=stop_after_attempt(3), wait=wait_random(1, 3), reraise=True,
after=lambda x: logging.warning('抽取出院记录失败!'))
def request_discharge_info(img_path, layout):
"""
请求出院记录信息抽取接口
:param img_path: 待抽取图片路径
:param layout: 图片ocr信息
:return: 抽取结果
"""
url = 'http://discharge_api:5003/nlp/discharge'
response = requests.post(url, {'img_path': img_path, 'layout': json.dumps(layout)})
if response.status_code == 200:
return response.json()
else:
return None
@retry(stop=stop_after_attempt(3), wait=wait_random(1, 3), reraise=True,
after=lambda x: logging.warning('抽取费用清单失败!'))
def request_cost_info(img_path, layout):
"""
请求费用清单信息抽取接口
:param img_path: 待抽取图片路径
:param layout: 图片ocr信息
:return: 抽取结果
"""
url = 'http://cost_api:5004/nlp/cost'
response = requests.post(url, {'img_path': img_path, 'layout': json.dumps(layout)})
if response.status_code == 200:
return response.json()
else:
return None
@retry(stop=stop_after_attempt(3), wait=wait_random(1, 3), reraise=True,
after=lambda x: logging.warning('获取图片方向失败!'))
def request_image_orientation(img_path):
"""
请求图片方向分类接口
:param img_path: 待分类图片路径
:return: 最有可能的两个图片方向
"""
url = 'http://clas_api:5005/clas/orientation'
response = requests.post(url, {'img_path': img_path})
if response.status_code == 200:
return response.json()
else:
return ['0', '90']
@retry(stop=stop_after_attempt(3), wait=wait_random(1, 3), reraise=True,
after=lambda x: logging.warning('获取文档区域失败!'))
def request_book_areas(img_path):
@@ -12,7 +96,7 @@ def request_book_areas(img_path):
:param img_path: 待识别图片路径
:return: 文档图片路径列表
"""
url = 'http://det_api:5000/det/books'
url = 'http://det_api:5006/det/books'
response = requests.post(url, {'img_path': img_path})
if response.status_code == 200:
return response.json()
@@ -28,25 +112,9 @@ def request_dewarped_image(img_path):
:param img_path: 待矫正图片路径
:return: 矫正后的图片路径
"""
url = 'http://det_api:5001/dewarp'
url = 'http://127.0.0.1:5007/dewarp'
response = requests.post(url, {'img_path': img_path})
if response.status_code == 200:
return response.json()
else:
return img_path
@retry(stop=stop_after_attempt(3), wait=wait_random(1, 3), reraise=True,
after=lambda x: logging.warning('获取图片方向失败!'))
def request_image_orientation(img_path):
"""
请求图片方向分类接口
:param img_path: 待分类图片路径
:return: 最有可能的两个图片方向
"""
url = 'http://det_api:5002/clas/orientation'
response = requests.post(url, {'img_path': img_path})
if response.status_code == 200:
return response.json()
else:
return ['0', '90']