student_server/example_info_add.py
2025-12-09 09:28:29 +08:00

73 lines
2.1 KiB
Python
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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