新增和完成了录入修改,查询

This commit is contained in:
2025-10-04 11:51:08 +08:00
parent 657365f9de
commit 30645e46ff
3 changed files with 683 additions and 73 deletions

83
app.py
View File

@@ -153,10 +153,10 @@ def index():
@app.route('/upload', methods=['POST'])
def upload_image():
"""
处理图片上传请求调用OCR识别存储结果
处理图片上传请求调用OCR识别但不存储结果
返回:
JSON: 上传成功或失败的响应
JSON: 识别结果,供用户编辑确认
"""
# 获取上传的文件
file = request.files.get('file')
@@ -173,20 +173,13 @@ def upload_image():
print(f"开始处理图片: {image_path}")
original_data = ocr_and_extract_info(image_path) # 获取原始JSON数据
if original_data:
# 使用json_converter将JSON数据转换为字符串
data_string = json_to_string(original_data)
print(f"转换后的数据字符串: {data_string}")
# 构造新的数据结构只包含data和image字段
processed_data = {
"data": data_string,
"image": filename # 存储图片文件名
}
print(f"准备存储的数据: {processed_data}")
insert_data(processed_data) # 存入ES
print("✓ 数据成功存储到Elasticsearch")
return jsonify({"message": "成功录入", "data": original_data, "processed": processed_data})
print(f"识别成功: {original_data}")
# 返回识别结果和图片文件名,供用户编辑确认
return jsonify({
"message": "识别成功,请确认数据后点击录入",
"data": original_data,
"image": filename
})
else:
print("✗ 无法识别图片内容")
return jsonify({"error": "无法识别图片内容"}), 400
@@ -194,6 +187,49 @@ def upload_image():
print(f"✗ 处理过程中发生错误: {str(e)}")
return jsonify({"error": str(e)}), 500
# 确认录入路由
@app.route('/confirm', methods=['POST'])
def confirm_data():
"""
确认并录入用户编辑后的数据
返回:
JSON: 录入成功或失败的响应
"""
try:
# 获取前端提交的数据
request_data = request.get_json()
if not request_data:
return jsonify({"error": "没有接收到数据"}), 400
# 获取编辑后的数据和图片文件名
edited_data = request_data.get('data', {})
image_filename = request_data.get('image', '')
if not edited_data:
return jsonify({"error": "数据不能为空"}), 400
# 使用json_converter将JSON数据转换为字符串
data_string = json_to_string(edited_data)
print(f"转换后的数据字符串: {data_string}")
# 构造新的数据结构只包含data和image字段
processed_data = {
"data": data_string,
"image": image_filename # 存储图片文件名
}
print(f"准备存储的数据: {processed_data}")
# 存入ES
insert_data(processed_data)
print("✓ 数据成功存储到Elasticsearch")
return jsonify({"message": "数据录入成功", "data": edited_data})
except Exception as e:
print(f"✗ 录入过程中发生错误: {str(e)}")
return jsonify({"error": str(e)}), 500
# 搜索路由
@app.route('/search')
def search():
@@ -276,6 +312,21 @@ def show_all():
return render_template('all.html', data=processed_data)
# 添加图片路由
@app.route('/image/<filename>')
def serve_image(filename):
"""
提供图片文件服务
参数:
filename (str): 图片文件名
返回:
Response: 图片文件响应
"""
from flask import send_from_directory
return send_from_directory('image', filename)
# 删除数据路由
@app.route('/delete/<doc_id>', methods=['POST'])
def delete_entry(doc_id):