Face Detect Kinect V2 + OpenCV

asked 2015-02-09 06:58:24 -0600

Deepak Kumar gravatar image

updated 2015-02-09 07:46:15 -0600

berak gravatar image

below is the code i am using for face detection. the porblem is that whenever the new frame arrives the face is detected in new windows each time. i want to display the frame in single windows. how can i do that.

thanks !!

code :

#include "stdafx.h"
#include <Windows.h>
#include <Kinect.h>
#include <Kinect.Face.h>
#include <opencv2/opencv.hpp>

#define _USE_MATH_DEFINES
#include <math.h>


template<class Interface>
inline void SafeRelease( Interface *& pInterfaceToRelease )
{
    if( pInterfaceToRelease != NULL ){
        pInterfaceToRelease->Release();
        pInterfaceToRelease = NULL;
    }
}

// Quote from Kinect for Windows SDK v2.0 Developer Preview - Samples/Native/FaceBasics-D2D, and Partial Modification
// ExtractFaceRotationInDegrees is: Copyright (c) Microsoft Corporation. All rights reserved.
inline void ExtractFaceRotationInDegrees( const Vector4* pQuaternion, int* pPitch, int* pYaw, int* pRoll )
{
    double x = pQuaternion->x;
    double y = pQuaternion->y;
    double z = pQuaternion->z;
    double w = pQuaternion->w;

    // convert face rotation quaternion to Euler angles in degrees
    *pPitch = static_cast<int>( std::atan2( 2 * ( y * z + w * x ), w * w - x * x - y * y + z * z ) / M_PI * 180.0f );
    *pYaw = static_cast<int>( std::asin( 2 * ( w * y - x * z ) ) / M_PI * 180.0f );
    *pRoll = static_cast<int>( std::atan2( 2 * ( x * y + w * z ), w * w + x * x - y * y - z * z ) / M_PI * 180.0f );
}

int _tmain( int argc, _TCHAR* argv[] )
{
    cv::setUseOptimized( true );

    // Sensor
    IKinectSensor* pSensor;
    HRESULT hResult = S_OK;
    hResult = GetDefaultKinectSensor( &pSensor );
    if( FAILED( hResult ) ){
        std::cerr << "Error : GetDefaultKinectSensor" << std::endl;
        return -1;
    }

    hResult = pSensor->Open();
    if( FAILED( hResult ) ){
        std::cerr << "Error : IKinectSensor::Open()" << std::endl;
        return -1;
    }

    // Source
    IColorFrameSource* pColorSource;
    hResult = pSensor->get_ColorFrameSource( &pColorSource );
    if( FAILED( hResult ) ){
        std::cerr << "Error : IKinectSensor::get_ColorFrameSource()" << std::endl;
        return -1;
    }

    IBodyFrameSource* pBodySource;
    hResult = pSensor->get_BodyFrameSource( &pBodySource );
    if( FAILED( hResult ) ){
        std::cerr << "Error : IKinectSensor::get_BodyFrameSource()" << std::endl;
        return -1;
    }

    // Reader
    IColorFrameReader* pColorReader;
    hResult = pColorSource->OpenReader( &pColorReader );
    if( FAILED( hResult ) ){
        std::cerr << "Error : IColorFrameSource::OpenReader()" << std::endl;
        return -1;
    }

    IBodyFrameReader* pBodyReader;
    hResult = pBodySource->OpenReader( &pBodyReader );
    if( FAILED( hResult ) ){
        std::cerr << "Error : IBodyFrameSource::OpenReader()" << std::endl;
        return -1;
    }

    // Description
    IFrameDescription* pDescription;
    hResult = pColorSource->get_FrameDescription( &pDescription );
    if( FAILED( hResult ) ){
        std::cerr << "Error : IColorFrameSource::get_FrameDescription()" << std::endl;
        return -1;
    }

    int width = 0;
    int height = 0;
    pDescription->get_Width( &width ); // 1920
    pDescription->get_Height( &height ); // 1080
    unsigned int bufferSize = width * height * 4 * sizeof( unsigned char );

    cv::Mat bufferMat( height, width, CV_8UC4 );
    cv::Mat faceMat( height / 2, width / 2, CV_8UC4 );
    cv::namedWindow( "Face" );

    // Color Table
    cv::Vec3b color[BODY_COUNT];
    color[0] = cv::Vec3b( 255, 0, 0 );
    color[1] = cv::Vec3b( 0, 255, 0 );
    color[2] = cv::Vec3b( 0, 0, 255 );
    color[3] = cv::Vec3b( 255, 255, 0 );
    color[4] = cv::Vec3b( 255, 0, 255 );
    color[5] = cv::Vec3b( 0, 255, 255 );

    // Coordinate Mapper
    ICoordinateMapper* pCoordinateMapper;
    hResult = pSensor->get_CoordinateMapper( &pCoordinateMapper );
    if( FAILED( hResult ) ){
        std::cerr << "Error : IKinectSensor::get_CoordinateMapper()" << std::endl;
        return -1;
    }

    IFaceFrameSource* pFaceSource[BODY_COUNT];
    DWORD features = FaceFrameFeatures::FaceFrameFeatures_BoundingBoxInColorSpace
        | FaceFrameFeatures::FaceFrameFeatures_PointsInColorSpace
        | FaceFrameFeatures::FaceFrameFeatures_RotationOrientation
        | FaceFrameFeatures::FaceFrameFeatures_Happy
        | FaceFrameFeatures::FaceFrameFeatures_RightEyeClosed
        | FaceFrameFeatures::FaceFrameFeatures_LeftEyeClosed
        | FaceFrameFeatures::FaceFrameFeatures_MouthOpen
        | FaceFrameFeatures::FaceFrameFeatures_MouthMoved
        | FaceFrameFeatures::FaceFrameFeatures_LookingAway
        | FaceFrameFeatures::FaceFrameFeatures_Glasses
        | FaceFrameFeatures::FaceFrameFeatures_FaceEngagement;
    IFaceFrameReader* pFaceReader[BODY_COUNT];
    for( int count = 0; count < BODY_COUNT; count++ ){
        // Source
        hResult = CreateFaceFrameSource( pSensor, 0, features, &pFaceSource[count] );
        if( FAILED( hResult ) ){
            std::cerr << "Error : CreateFaceFrameSource" << std::endl;
            return -1;
        }

        // Reader ...
(more)
edit retag flag offensive close merge delete