添加人脸接口
This commit is contained in:
parent
b10f002d56
commit
5d551b4631
11
ai_demo.py
11
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__':
|
||||
#自己录制一段视频
|
||||
|
||||
@ -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__':
|
||||
70
example_ai_info.py
Executable file
70
example_ai_info.py
Executable file
@ -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
|
||||
24
util.py
24
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):
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user