73 lines
2.1 KiB
Python
Executable File
73 lines
2.1 KiB
Python
Executable File
from flask import Blueprint, request, jsonify
|
||
from db_con import con_mysql
|
||
|
||
example_info_add_bp = Blueprint('example_info_add', __name__)
|
||
|
||
"""
|
||
API接口:添加example_info数据
|
||
接口路径:POST /api/example_info/add
|
||
|
||
请求体示例:
|
||
{
|
||
"emotion_state": "开心",
|
||
"video_path": "/videos/test1.mp4",
|
||
"confidence": 0.95
|
||
}
|
||
|
||
字段说明:
|
||
- emotion_state (可选):字符串,描述emotion_state
|
||
- video_path (可选):字符串,视频文件路径
|
||
- confidence (可选):浮点数,confidence值(0-1)
|
||
"""
|
||
|
||
@example_info_add_bp.route('/api/example_info/add', methods=['POST'])
|
||
def add_example_info():
|
||
try:
|
||
data = request.get_json()
|
||
|
||
# 验证必需字段
|
||
if not data:
|
||
return jsonify({
|
||
"success": False,
|
||
"message": "请求数据不能为空"
|
||
}), 400
|
||
|
||
# 获取请求数据
|
||
emotion_state = data.get('emotion_state')
|
||
video_path = data.get('video_path')
|
||
confidence = data.get('confidence')
|
||
|
||
# 构建SQL语句
|
||
sql = """
|
||
INSERT INTO group3_emo_info (emotion_state, video_path, confidence)
|
||
VALUES (%s, %s, %s)
|
||
"""
|
||
params = (emotion_state, video_path, confidence)
|
||
|
||
# 执行SQL
|
||
result = con_mysql(sql, params)
|
||
|
||
if result['success']:
|
||
return jsonify({
|
||
"success": True,
|
||
"message": "example_info数据添加成功",
|
||
"data": {
|
||
"id": result.get('lastrowid'),
|
||
"emotion_state": emotion_state,
|
||
"video_path": video_path,
|
||
"confidence": confidence
|
||
}
|
||
}), 201
|
||
else:
|
||
return jsonify({
|
||
"success": False,
|
||
"message": "添加失败",
|
||
"error": result.get('error', '未知错误')
|
||
}), 500
|
||
|
||
except Exception as e:
|
||
return jsonify({
|
||
"success": False,
|
||
"message": "服务器内部错误",
|
||
"error": str(e)
|
||
}), 500 |