opencv+python+dlib人脸关键点检测、实时检测

安装的是anaconde3、python3.7.3,3.7环境安装dlib太麻烦, 

在anaconde3中新建环境python3.6.8,

在3.6环境下安装dlib-19.6.1-cp36-cp36m-win_amd64.whl,下载地址:https://pypi.org/project/dlib/19.6.1/#files

vscode更改配置

其中shape_predictor_68_face_landmarks.dat官方训练数据下载地址:http://dlib.net/files/,里面还有5点模型。

效果图如下:

# _*_ coding:utf-8 _*_

import numpy as np
import cv2
import dlib

detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("data/shape_predictor_68_face_landmarks.dat")

# cv2读取图像
img = cv2.imread("img/test3.jpg")

# 取灰度
img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)

# 人脸数rects
rects = detector(img_gray, 0)
for i in range(len(rects)):
    landmarks = np.matrix([[p.x, p.y] for p in predictor(img, rects[i]).parts()])
    for idx, point in enumerate(landmarks):
        # 68点的坐标
        pos = (point[0, 0], point[0, 1])

        # 利用cv2.circle给每个特征点画一个圈,共68个
        cv2.circle(img, pos, 2, color=(0, 255, 0))
        # 利用cv2.putText输出1-68
        font = cv2.FONT_HERSHEY_SIMPLEX
        cv2.putText(img, str(idx + 1), None, font, 0.8, (0, 0, 255), 1, cv2.LINE_AA)

cv2.namedWindow("img", 2)
cv2.imshow("img", img)
cv2.waitKey(0)

 摄像头实时检测:

 

# _*_ coding:utf-8 _*_

import numpy as np
import cv2
import dlib

cap = cv2.VideoCapture(0)
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("data/shape_predictor_68_face_landmarks.dat")

while 1:
    ret, img = cap.read()
    # 取灰度
    img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    # 人脸数rects
    rects = detector(img_gray, 0)
    for i in range(len(rects)):
        landmarks = np.matrix([[p.x, p.y] for p in predictor(img, rects[i]).parts()])
        for idx, point in enumerate(landmarks):
            # 68点的坐标
            pos = (point[0, 0], point[0, 1])

            # 利用cv2.circle给每个特征点画一个圈,共68个
            cv2.circle(img, pos, 2, color=(0, 255, 0))
            # 利用cv2.putText输出1-68
            font = cv2.FONT_HERSHEY_SIMPLEX
            cv2.putText(img, str(idx + 1), None, font, 0.8, (0, 0, 255), 1, cv2.LINE_AA)

    cv2.namedWindow("img", 2)
    cv2.imshow("img", img)
    if cv2.waitKey(1) & 0xFF == ord("q"):
        break

 

转载于:https://www.cnblogs.com/ckAng/p/10975132.html

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的面部标定、眨眼、打哈欠和疲劳检测Python代码,使用了OpenCVDlib库: ```python import cv2 import dlib import numpy as np # 初始化dlib人脸检测器和关键点检测器 detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") # 定义常量 EYE_AR_THRESH = 0.3 # 眨眼的阈值 EYE_AR_CONSEC_FRAMES = 48 # 连续多少帧达到阈值才算眨眼 YAWN_THRESH = 18 # 打哈欠的阈值 YAWN_CONSEC_FRAMES = 24 # 连续多少帧达到阈值才算打哈欠 FRAME_COUNT = 0 # 总帧数 BLINK_COUNT = 0 # 眨眼次数 YAWN_COUNT = 0 # 打哈欠次数 # 定义函数:计算两个点之间的距离 def euclidean_dist(pt1, pt2): return np.linalg.norm(pt1 - pt2) # 定义函数:计算眼睛长宽比 def eye_aspect_ratio(eye): A = euclidean_dist(eye[1], eye[5]) B = euclidean_dist(eye[2], eye[4]) C = euclidean_dist(eye[0], eye[3]) ear = (A + B) / (2.0 * C) return ear # 定义函数:检测眨眼 def detect_blink(landmarks): global BLINK_COUNT leftEye = landmarks[36:42] rightEye = landmarks[42:48] leftEAR = eye_aspect_ratio(leftEye) rightEAR = eye_aspect_ratio(rightEye) ear = (leftEAR + rightEAR) / 2.0 if ear < EYE_AR_THRESH: BLINK_COUNT += 1 # 定义函数:检测打哈欠 def detect_yawn(landmarks): global YAWN_COUNT mouth = landmarks[48:68] yawn_dist = euclidean_dist(mouth[0], mouth[6]) if yawn_dist > YAWN_THRESH: YAWN_COUNT += 1 # 打开摄像头 cap = cv2.VideoCapture(0) while True: ret, frame = cap.read() if not ret: break FRAME_COUNT += 1 # 转换为灰度图 gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 人脸检测 rects = detector(gray, 0) for rect in rects: # 关键点检测 landmarks = np.array([[p.x, p.y] for p in predictor(gray, rect).parts()]) # 绘制关键点 for i, point in enumerate(landmarks): x, y = point cv2.circle(frame, (x, y), 2, (0, 255, 0), -1) # 检测眨眼 detect_blink(landmarks) # 检测打哈欠 detect_yawn(landmarks) # 显示视频帧 cv2.imshow("Frame", frame) # 按q键退出 if cv2.waitKey(1) == ord("q"): break # 计算疲劳度 blink_rate = BLINK_COUNT / FRAME_COUNT yawn_rate = YAWN_COUNT / FRAME_COUNT fatigue = blink_rate + yawn_rate # 释放摄像头并关闭窗口 cap.release() cv2.destroyAllWindows() # 打印结果 print("Blink rate:", blink_rate) print("Yawn rate:", yawn_rate) print("Fatigue:", fatigue) ``` 这个代码使用了Dlib库进行人脸检测关键点检测,并使用了OpenCV库进行视频捕获和显示。在关键点检测之后,我们通过计算眼睛长宽比来检测眨眼,通过计算嘴巴的张开距离来检测打哈欠。最后,我们计算眨眼率、打哈欠率和疲劳度,并输出结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值