yolo v5识别人物情绪

复制代码
# 安装 YOLOv5 依赖
git clone https://github.com/ultralytics/yolov5
cd yolov5
pip install -r requirements.txt
  1. 加载 YOLOv5-face 模型用于五官检测
python 复制代码
from yolov5_face.face_detector import YoloDetector
import cv2

# 初始化模型(下载好模型或替换为你自己的路径)
model_path = "weights/yolov5s-face.pt"
detector = YoloDetector(model_path, device='cuda')  # 或 'cpu'

img = cv2.imread("test.jpg")
bboxes = detector.predict(img)

for box in bboxes:
    x1, y1, x2, y2, conf, landmarks = box
    # landmarks = [left_eye, right_eye, nose, left_mouth, right_mouth]
    cv2.rectangle(img, (x1, y1), (x2, y2), (255,0,0), 2)
    for (x, y) in landmarks:
        cv2.circle(img, (int(x), int(y)), 2, (0,255,0), -1)
  1. 加载情绪识别模型(CNN)
python 复制代码
import torch
import torch.nn as nn
from torchvision import transforms
from PIL import Image

# 假设我们使用一个训练好的 FER 模型
class EmotionCNN(nn.Module):
    def __init__(self):
        super().__init__()
        self.net = nn.Sequential(
            nn.Conv2d(1, 32, 3), nn.ReLU(),
            nn.MaxPool2d(2),
            nn.Conv2d(32, 64, 3), nn.ReLU(),
            nn.AdaptiveAvgPool2d(1),
            nn.Flatten(),
            nn.Linear(64, 7)  # 7类表情
        )

    def forward(self, x):
        return self.net(x)

emotion_model = EmotionCNN()
emotion_model.load_state_dict(torch.load("emotion_model.pth", map_location="cpu"))
emotion_model.eval()

transform = transforms.Compose([
    transforms.Resize((48, 48)),
    transforms.Grayscale(),
    transforms.ToTensor()
])

# 裁剪人脸区域
face_crop = img[y1:y2, x1:x2]
face_pil = Image.fromarray(cv2.cvtColor(face_crop, cv2.COLOR_BGR2RGB))
input_tensor = transform(face_pil).unsqueeze(0)

with torch.no_grad():
    output = emotion_model(input_tensor)
    pred = torch.argmax(output, dim=1).item()

emotions = ["愤怒", "厌恶", "恐惧", "高兴", "难过", "惊讶", "平静"]
emotion_label = emotions[pred]
cv2.putText(img, emotion_label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
  1. 完整流程打包整合
python 复制代码
def detect_face_and_emotion(image_path):
    img = cv2.imread(image_path)
    faces = detector.predict(img)
    
    for box in faces:
        x1, y1, x2, y2, conf, landmarks = box
        face_crop = img[y1:y2, x1:x2]
        face_pil = Image.fromarray(cv2.cvtColor(face_crop, cv2.COLOR_BGR2RGB))
        input_tensor = transform(face_pil).unsqueeze(0)
        output = emotion_model(input_tensor)
        pred = torch.argmax(output, dim=1).item()
        emotion_label = emotions[pred]
        
        # 显示框和文字
        cv2.rectangle(img, (x1, y1), (x2, y2), (255,0,0), 2)
        cv2.putText(img, emotion_label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0,0,255), 2)
    
    cv2.imshow("Result", img)
    cv2.waitKey(0)
相关推荐
Uopiasd1234oo8 小时前
上下文引导模块改进YOLOv26局部与全局特征融合能力双重提升
深度学习·yolo·机器学习
懷淰メ9 小时前
【AI加持】基于PyQt+YOLO+DeepSeek的钢材焊接缺陷检测系统(详细介绍)
yolo·目标检测·计算机视觉·pyqt·缺陷检测·deepseek·钢材缺陷
动物园猫10 小时前
工业织物缺陷目标检测数据集分享(适用于YOLO系列深度学习分类检测任务)
深度学习·yolo·目标检测
迪菲赫尔曼11 小时前
从 0 到 1 打造工业级推理控制台:UltraConsole(Ultralytics + FastAPI + React)开源啦!
前端·yolo·react.js·计算机视觉·开源·fastapi
极智视界14 小时前
分类数据集 - 遥感航空影像云量检测图像分类数据集下载
yolo·数据集·图像分类·算法训练·遥感航空影像云量检测
极智视界14 小时前
分类数据集 - 伪造人脸和真实人脸分类数据集下载
人工智能·yolo·数据集·图像分类·算法训练·人脸伪造检测
深度学习lover15 小时前
<数据集>yolo 常见对象检测<目标检测>
人工智能·python·yolo·目标检测·计算机视觉·常见对象检测
Westward-sun.16 小时前
YOLOv2算法全方位解析:从BatchNorm到聚类先验框的九大改进
算法·yolo·聚类
动物园猫17 小时前
工业粉尘检测数据集分享(适用于YOLO系列深度学习分类检测任务)
深度学习·yolo·分类
jay神1 天前
VisDrone2019-DET 无人机小目标检测数据集
人工智能·深度学习·yolo·目标检测·计算机视觉·毕业设计·无人机