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

情到浓时终转凉″ 提交于 2019-12-11 03:09:58

https://blog.csdn.net/cherry_yu08/article/details/84551326
视频人脸跟踪
这里用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()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!