From 5d551b463178baa15ac43482ae41ac56a6f281fb Mon Sep 17 00:00:00 2001 From: shuike Date: Tue, 9 Dec 2025 11:28:23 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=BA=E8=84=B8=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ai_demo.py | 11 +-- ...server_open(云).py => all_server_open.py | 2 + example_ai_info.py | 70 +++++++++++++++++++ util.py | 24 ++++--- 4 files changed, 95 insertions(+), 12 deletions(-) rename all_server_open(云).py => all_server_open.py (89%) create mode 100755 example_ai_info.py diff --git a/ai_demo.py b/ai_demo.py index 5f5519e..5930e06 100755 --- a/ai_demo.py +++ b/ai_demo.py @@ -2,6 +2,7 @@ from PIL import Image from util import * import json + detector = FaceDetector('./weights/detection.onnx') fer = HSEmotionRecognizer('./weights/emotion.onnx') @@ -26,7 +27,7 @@ def porcess_video(input_video_path): if ret: frame_counter += 1 ## 每30帧进行处理一次 - if frame_counter % 30 == 0: + if frame_counter % 60 == 0: image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) boxes = detect_face(image) if boxes is not None: @@ -38,9 +39,10 @@ def porcess_video(input_video_path): emotion, scores,pred = fer.predict_emotions(np.array(pil_image), logits=False) ###### 做应用逻辑 print("frame_id:",frame_counter) - print("emotion",emotion) print("scores",scores[pred]) - emotion_reg_list.append(emotion) + emotion_utf_8 = emotion.encode('utf-8').decode('utf-8') + print("emotion_utf_8",emotion_utf_8) + emotion_reg_list.append(emotion_utf_8) emotion_score_list.append(float(scores[pred])) print("idx_class",idx_class) ###### @@ -57,9 +59,10 @@ def porcess_video(input_video_path): "emotion_reg_list":emotion_reg_list, "emotion_score_list":emotion_score_list } + print("data:",data) json_str = json.dumps(data) print("json_str:",json_str) - return json_str + return json_str,emotion_reg_list,emotion_score_list if __name__ == '__main__': #自己录制一段视频 diff --git a/all_server_open(云).py b/all_server_open.py similarity index 89% rename from all_server_open(云).py rename to all_server_open.py index f593200..f97d9db 100755 --- a/all_server_open(云).py +++ b/all_server_open.py @@ -7,6 +7,7 @@ from datetime import datetime, date # 导入蓝图 from example_info_add import example_info_add_bp from user_add import user_add_bp +from example_ai_info import example_info_ai_bp # 极简JSON编码器(处理特殊数据类型) @@ -27,6 +28,7 @@ app.config['JSON_SORT_KEYS'] = False # 注册所有蓝图 app.register_blueprint(example_info_add_bp) +app.register_blueprint(example_info_ai_bp) app.register_blueprint(user_add_bp) if __name__ == '__main__': diff --git a/example_ai_info.py b/example_ai_info.py new file mode 100755 index 0000000..2b0020b --- /dev/null +++ b/example_ai_info.py @@ -0,0 +1,70 @@ +from flask import Blueprint, request, jsonify + +from ai_demo import porcess_video + +example_info_ai_bp = Blueprint('example_ai_info', __name__) + +""" +API接口:添加example_info数据 +接口路径:POST /api/example_info/ai + +请求体示例: +{ + "video_path": "/videos/test1.mp4" +} + +字段说明: +- video_path (可选):字符串,视频文件路径 +""" + +@example_info_ai_bp.route('/api/example_info/ai', methods=['POST']) +def add_example_ai_info(): + try: + data = request.get_json() + print("data:",data) + + # 验证必需字段 + if not data: + return jsonify({ + "success": False, + "message": "请求数据不能为空" + }), 400 + + # 获取请求数据 + video_path = data.get('video_path') + + json_info,emotion_reg_list,emotion_score_list=porcess_video(video_path) + # 构建SQL语句 + emotion_state = "happy" + confidence = 0.1 + flag = False + if len(emotion_reg_list) > 0: + emotion_state = emotion_reg_list[0] + flag = True + + if len(emotion_score_list) > 0: + confidence = emotion_score_list[0] + if flag: + # if result['success']: + return jsonify({ + "success": True, + "message": "人脸分析成功", + "data": { + "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 \ No newline at end of file diff --git a/util.py b/util.py index 24ef503..4215ec2 100755 --- a/util.py +++ b/util.py @@ -204,14 +204,22 @@ class HSEmotionRecognizer: self.mean = [0.485, 0.456, 0.406] self.std = [0.229, 0.224, 0.225] - self.idx_to_class = {0: 'ANGER', - 1: 'DISGUST', - 2: 'FEAR', - 3: 'HAPPINESS', - 4: 'NEUTRAL', - 5: 'SADNESS', - 6: 'SURPRISE'} - + # self.idx_to_class = {0: 'ANGER', + # 1: 'DISGUST', + # 2: 'FEAR', + # 3: 'HAPPINESS', + # 4: 'NEUTRAL', + # 5: 'SADNESS', + # 6: 'SURPRISE'} + self.idx_to_class = { + 0: '愤怒', + 1: '厌恶', + 2: '恐惧', + 3: '快乐', + 4: '中性', + 5: '悲伤', + 6: '惊讶' + } self.ort_session = ort.InferenceSession(path, providers=['CUDAExecutionProvider']) def preprocess(self, img):