利用PicoZense RGBD相机+dlib库做人脸特征点检测

用了Pico的DCAM710和dlib做了68个人脸特征点检测(目前先用了RGB图做,以后再对齐到深度图)

为了不写天书,还是给下环境吧:

主要配了PicoZense的SDK和dlib库以及opencv库

 

  直接上代码:

#include <iostream>
#include <opencv2/opencv.hpp>
#include "dlib/opencv.h"
#include "dlib/image_processing/frontal_face_detector.h"
#include "dlib/image_processing/render_face_detections.h"
#include "dlib/image_processing.h"
#include "dlib/gui_widgets.h"
#include "PicoZense_api.h"

using namespace dlib;
using namespace std;

PsReturnStatus status;
int32_t deviceIndex = 0;

int main()
{
    status = PsInitialize();
    if (status != PsReturnStatus::PsRetOK)
    {
        cerr << "Initialize failed!" << endl;
        exit(0);
    }
    int32_t deviceCount = 0;
    status = PsGetDeviceCount(&deviceCount);
    if (deviceCount == 0)
    {
        cout << "Not find any camera!" << endl;
        return -1;
    }
    status = PsOpenDevice(deviceIndex);
    if (status != PsReturnStatus::PsRetOK)
    {
        cerr << "OpenDevice 0 failed!" << endl;
        exit(0);
    }
    PsDepthRange DepthRange;
    PsFrameMode rgbFrameMode;

    PsGetDepthRange(deviceIndex, &DepthRange);
    PsGetFrameMode(deviceIndex, PsRGBFrame, &rgbFrameMode);
    PsSetColorPixelFormat(deviceIndex, PsPixelFormatBGR888);
    image_window win;
    cv::Mat imageMat;
    // Load face detection and pose estimation models.
    frontal_face_detector detector = get_frontal_face_detector();
    shape_predictor pose_model;
    deserialize("shape_predictor_68_face_landmarks.dat") >> pose_model;

// Grab and process frames until the main window is closed by the user. !win.is_closed()
    while (!win.is_closed())
    {
        PsFrame rgbFrame = { 0 };
        status = PsReadNextFrame(deviceIndex);
        PsGetFrame(deviceIndex, PsRGBFrame, &rgbFrame);
        if (rgbFrame.pFrameData != NULL)
        {
            imageMat = cv::Mat(rgbFrameMode.resolutionHeight, rgbFrameMode.resolutionWidth, CV_8UC3, rgbFrame.pFrameData);
            
            // Turn OpenCV's Mat into something dlib can deal with.  Note that this just
            // wraps the Mat object, it doesn't copy anything.  So cimg is only valid as
            // long as temp is valid.  Also don't do anything to temp that would cause it
            // to reallocate the memory which stores the image as that will make cimg
            // contain dangling pointers.  This basically means you shouldn't modify temp
            // while using cimg.
            cv_image<bgr_pixel> cimg(imageMat);

            //// Detect faces
            std::vector<rectangle> faces = detector(cimg);
            // Find the pose of each face.
            std::vector<full_object_detection> shapes;
            for (unsigned long i = 0; i < faces.size(); ++i)
                shapes.push_back(pose_model(cimg, faces[i]));

            //// Display it all on the screen
            win.clear_overlay();
            win.set_image(cimg);
            win.add_overlay(render_face_detections(shapes));
        }
        imageMat.release();

    }
    PsCloseDevice(deviceIndex);
    PsShutdown();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_30396367/article/details/86538659