python+opencv+dlib+Intel RealSense D435 实现人脸检测和跟踪

1、单张图片人脸检测

直接贴代码:

# CNN + dlib + face detector
# CNN模型的人脸检测准确率比基于HOG的方法好,但是对计算能力要求大
# 预训练模型: mmod_human_face_detector.dat
import cv2
import dlib


if __name__ == "__main__":
    cnn_face_detector = dlib.cnn_face_detection_model_v1("mmod_human_face_detector.dat")

    img_path = "./faces/2008_007676.jpg"
    img = cv2.imread(img_path, cv2.IMREAD_COLOR)
    # opencv读取图片是BGR格式,需要将其转换成RGB格式
    b, g, r = cv2.split(img)
    img2 = cv2.merge([r, g, b])  # 融合三个颜色通道生成新图片

    # 使用cnn_face_detector进行人脸检测,并打印人脸
    dets = cnn_face_detector(img, 1)
    print("Number of faces detected: {}".format(len(dets)))
    # 便利索引
    for index, face in enumerate(dets):
        print("Detection {}: Left: {} Top: {} Right: {} Bottom: {} Confidence: {}".format(
            index, face.rect.left(), face.rect.top(), face.rect.right(), face.rect.bottom(), face.confidence))
        # 在图片中标注人脸并显示
        left = face.rect.left()
        top = face.rect.top()
        right = face.rect.right()
        bottom = face.rect.bottom()
        cv2.rectangle(img, (left, top), (right, bottom), (0, 255, 0), 3)
        cv2.namedWindow("faces", cv2.WINDOW_AUTOSIZE)
        cv2.imshow("faces", img)
    k = cv2.waitKey(0)
    cv2.destroyAllWindows()

运行结果如下图,输出人脸坐标以及置信度,并圈出人脸:
python+opencv+dlib+Intel RealSense D435 实现人脸检测和跟踪_第1张图片
python+opencv+dlib+Intel RealSense D435 实现人脸检测和跟踪_第2张图片

2、视频人脸跟踪

这里用Intel RealSense D435获取图片,用opencv显示图片,用dlib的算法实现人脸跟踪。
使用Intel RealSense D435获取RGB图像,并使用dlib的get_frontal_face_detector检测人脸,代码:

import dlib
import cv2
import pyrealsense2 as rs
import numpy as np


if __name__ == "__main__":
    # Configure depth and color streams
    pipeline = rs.pipeline()
    config = rs.config()
    config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
    config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
    # Start streaming
    pipeline.start(config)

    detector = dlib.get_frontal_face_detector()
    color_green = (0, 255, 0)
    line_width = 3
    try:
        while True:
            # Wait for a coherent pair of frames: depth and color
            frames = pipeline.wait_for_frames()
            depth_frame = frames.get_depth_frame()
            color_frame = frames.get_color_frame()
            if not depth_frame or not color_frame:
                continue
            rgb_image = np.asanyarray(color_frame.get_data())
            # 检测
            dets = detector(rgb_image)
            for det in dets:
                cv2.rectangle(rgb_image, (det.left(), det.top()), (det.right(), det.bottom()), color_green, line_width)
            cv2.imshow('my webcam', rgb_image)

            key = cv2.waitKey(1)
            # Press esc or 'q' to close the image window
            if key & 0xFF == ord('q') or key == 27:
                cv2.destroyAllWindows()
                break
    finally:
        # Stop streaming
        pipeline.stop()

运行结果,就不贴出来了,呵呵( ̄▽ ̄)"

相关说明:

python版本为3.6,dlib为19.7.0,python版本的dlib安装参考博客,Intel RealSense D435的安装、使用以及相关信息参考博客

如有错误欢迎批评指正。

你可能感兴趣的:(深度学习与计算机视觉,python,LeapMotion,&,Intel,RealSense)