《计算机视觉》—— 表情识别

  • 根据计算眼睛、嘴巴的变化,判断是什么表情

  • 结合以下两篇文章来理解表情识别的实现方法

  • 完整代码如下:

    python 复制代码
    import numpy as np
    import dlib
    import cv2
    from sklearn.metrics.pairwise import euclidean_distances
    from PIL import Image, ImageDraw, ImageFont
    
    # 计算眼睛的宽高比
    def eye_aspect_ratio(eye):
        A = euclidean_distances(eye[1].reshape(1, 2), eye[5].reshape(1, 2))
        B = euclidean_distances(eye[2].reshape(1, 2), eye[4].reshape(1, 2))
        C = euclidean_distances(eye[0].reshape(1, 2), eye[3].reshape(1, 2))
        ear = ((A + B) / 2.0) / C
        return ear
    
    
    # 计算嘴的宽高比
    def MAR(shape):
        x = shape[50]
        y = shape[50].reshape(1, 2)
    
        A = euclidean_distances(shape[50].reshape(1, 2), shape[58].reshape(1, 2))
        B = euclidean_distances(shape[51].reshape(1, 2), shape[57].reshape(1, 2))
        C = euclidean_distances(shape[52].reshape(1, 2), shape[56].reshape(1, 2))
        D = euclidean_distances(shape[48].reshape(1, 2), shape[54].reshape(1, 2))
        return ((A + B + C) / 3) / D
    
    
    # 计算嘴宽度与脸颊宽度的比值
    def MJR(shape):
        M = euclidean_distances(shape[48].reshape(1, 2), shape[54].reshape(1, 2))  # 嘴宽度
        J = euclidean_distances(shape[3].reshape(1, 2), shape[13].reshape(1, 2))  # 下颌的宽度
        return M / J
    
    """ 向图片中添加中文 """
    def cv2AddChineseText(img, text, position, textColor=(0, 255, 0), textSize=30):
        if (isinstance(img, np.ndarray)):  # 判断是否是OpenCV图片类型
            img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))  # 实现 array 到 image 的转换
        draw = ImageDraw.Draw(img)  # 在img图片上创建一个绘图的对象
        # 字体的格式                       C 盘中的 Windows/Fonts 中,复制到此文件夹下可看到文件名
        fontStyle = ImageFont.truetype("simsun.ttc", textSize, encoding="utf-8")
        draw.text(position, text, textColor, font=fontStyle)  # 绘制文本
        return cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)  # 转换回 OpenCV 格式
    
    
    # 构建脸部位置检测器
    detector = dlib.get_frontal_face_detector()
    
    # 读取人脸关键点定位模型
    predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
    
    # 打开摄像头或视频
    cap = cv2.VideoCapture(0)
    
    while True:
        ret, frame = cap.read()
        frame = cv2.flip(frame, 1)
    
        faces = detector(frame, 0)  # 获取图片中全部人脸位置
    
        for face in faces:
            shape = predictor(frame, face)  # 获取关键点
            # 将关键点转换为坐标(x,y)的形式
            shape = np.array([[p.x, p.y] for p in shape.parts()])
            # 计算嘴部的高宽比
            mar = MAR(shape)
            # 计算 "最宽/脸颊宽"
            mjr = MJR(shape)
    
            rightEye = shape[36:42]  # 右眼,关键点索引从36到41(不包含42)
            leftEye = shape[42:48]  # 左眼,关键点索引从42到47(不包含48)
            rightEAR = eye_aspect_ratio(rightEye)  # 计算右眼纵横比
            leftEAR = eye_aspect_ratio(leftEye)  # 计算左眼纵横比
    
            ear = (rightEAR + leftEAR) / 2.0  # 均值处理
    
            result = "正常"  # 默认是正常表情
    
            # 打印出实际值,可以根据该值更改阈值
            print("mar", mar, "\tmjr", mjr, "\tear", ear)
    
            if mar > 0.5 and ear < 0.28:
                result = "大笑"
    
            elif mar > 0.5 and ear > 0.28:
                result = "愤怒"
    
            elif mjr > 0.45:
                result = "微笑"
            # 输出中文
            # frame = cv2AddChineseText(frame, result, (50, 100))
    
            # cv2.putText()#输出英文
    
            mouthHull = cv2.convexHull(shape[48:61])  # 嘴型构造凸包
    
            frame = cv2AddChineseText(frame, result, mouthHull[0, 0])  # 多人脸
    
            cv2.drawContours(frame, [mouthHull], -1, (0, 255, 0), 1)  # 画出凸包
    
        cv2.imshow("Frame", frame)
        if cv2.waitKey(1) == 27:
            break
    
    cv2.destroyAllWindows()
    cap.release()
相关推荐
qinyia1 小时前
Wisdom SSH 是一款创新性工具,通过集成 AI 助手,为服务器性能优化带来极大便利。
服务器·人工智能·ssh
硬件学长森哥3 小时前
Android影像基础--cameraAPI2核心流程
android·计算机视觉
昨日之日20063 小时前
Wan2.2-S2V - 音频驱动图像生成电影级质量的数字人视频 ComfyUI工作流 支持50系显卡 一键整合包下载
人工智能·音视频
深圳市快瞳科技有限公司4 小时前
小场景大市场:猫狗识别算法在宠物智能设备中的应用
算法·计算机视觉·宠物
一个天蝎座 白勺 程序猿5 小时前
Python爬虫(47)Python异步爬虫与K8S弹性伸缩:构建百万级并发数据采集引擎
爬虫·python·kubernetes
XiaoMu_0016 小时前
基于Django+Vue3+YOLO的智能气象检测系统
python·yolo·django
SEO_juper6 小时前
大型语言模型SEO(LLM SEO)完全手册:驾驭搜索新范式
人工智能·语言模型·自然语言处理·chatgpt·llm·seo·数字营销
攻城狮7号7 小时前
腾讯混元翻译模型Hunyuan-MT-7B开源,先前拿了30个冠军
人工智能·hunyuan-mt-7b·腾讯混元翻译模型·30个冠军
zezexihaha7 小时前
从“帮写文案”到“管生活”:个人AI工具的边界在哪?
人工智能
算家云7 小时前
nano banana官方最强Prompt模板来了!六大场景模板详解
人工智能·谷歌·ai大模型·算家云·ai生图·租算力,到算家云·nano banana 提示词