Beispiel #1
0
BOOL FTHelper2::SubmitFraceTrackingResult(IFTResult* pResult, UINT userId)
{
    if (pResult != NULL && SUCCEEDED(pResult->GetStatus()))
    {
        if (m_CallBack)
        {
            (*m_CallBack)(m_CallBackParam, userId);
        }

        if (m_DrawMask)
        {
            FLOAT* pSU = NULL;
            UINT numSU;
            BOOL suConverged;
            m_UserContext[userId].m_pFaceTracker->GetShapeUnits(NULL, &pSU, &numSU, &suConverged);
            POINT viewOffset = {0, 0};
            FT_CAMERA_CONFIG cameraConfig;
            if (m_KinectSensorPresent)
            {
                m_KinectSensor.GetVideoConfiguration(&cameraConfig);
            }
            else
            {
                cameraConfig.Width = 640;
                cameraConfig.Height = 480;
                cameraConfig.FocalLength = 500.0f;
            }
            IFTModel* ftModel;
            HRESULT hr = m_UserContext[userId].m_pFaceTracker->GetFaceModel(&ftModel);
            if (SUCCEEDED(hr))
            {
                DWORD color = s_ColorCode[userId%6];
                hr = VisualizeFaceModel(m_colorImage, ftModel, &cameraConfig, pSU, 1.0, viewOffset, pResult, color);
                ftModel->Release();
            }
        }
    }
    return TRUE;
}
bool UKinect::pollFaceUser(int user) {
  if (user != 0 && user != 1) return false;

  FT_SENSOR_DATA sensorData;
  sensorData.pVideoFrame = colorBuffer;
  sensorData.pDepthFrame = depthBuffer;
  sensorData.ZoomFactor = 1.0f;       // Not used must be 1.0
  sensorData.ViewOffset.x = 0; // Not used must be (0,0)
  sensorData.ViewOffset.y = 0; // Not used must be (0,0)

  IFTFaceTracker* _pFaceTracker = pFaceTracker[user];	//				// An instance of a face tracker
  IFTResult*  _pFTResult = pFTResult[user];							// Face tracking result interface
  IFTResult*  _pFTResult_copy = pFTResult_copy[user];						// Copy of Face tracking result interface
  bool _isFaceTracked = isFaceTracked[user];
  //                        red          yellow
  int color = (user == 0) ? 0x00FF0000 : 0x00FFFF00;

  int trackedID = skeletonTrackedIDs.as< vector<int>>()[user];

  FT_VECTOR3D headHint[2];
  if (trackedID != 0) {
    vector<double> shoulders = skeletonJointPosition(trackedID, NUI_SKELETON_POSITION_SHOULDER_CENTER);
    if (shoulders.size() == 0) return false;
    headHint[0] = FT_VECTOR3D(shoulders[0], shoulders[1], shoulders[2]);

    vector<double> head = skeletonJointPosition(trackedID, NUI_SKELETON_POSITION_HEAD);
    if (head.size() == 0) return false;
    headHint[1] = FT_VECTOR3D(head[0], head[1], head[2]);
  } else return false;


  // Check if we are already tracking a face
  if (!_isFaceTracked) {
    // Initiate face tracking.
    // This call is more expensive and searches over the input RGB frame for a face.
    // However if hint != null id limits only to head region
    hr = _pFaceTracker->StartTracking(&sensorData, NULL, headHint, _pFTResult);
  } else {
    // Continue tracking. It uses a previously known face position.
    // This call is less expensive than StartTracking()
    hr = _pFaceTracker->ContinueTracking(&sensorData, headHint, _pFTResult);
  }

  // exit on fail
  if (FAILED(hr) || FAILED(_pFTResult->GetStatus())) {
    _pFTResult->Reset();
    return false;
  }

  _pFTResult->CopyTo(_pFTResult_copy);

  if (faceVisualization) {
    FLOAT* pSU = NULL;
    UINT numSU;
    BOOL suConverged;
    hr = _pFaceTracker->GetShapeUnits(NULL, &pSU, &numSU, &suConverged);
    if (FAILED(hr)) {
      cerr << "[UKinect] ERROR: Can not get SU units." << endl;
      throw;
    }

    POINT viewOffset = { 0, 0 };
    FT_CAMERA_CONFIG colorConfig;
    getColorConfiguration(&colorConfig);

    IFTModel* ftModel;
    HRESULT hr = _pFaceTracker->GetFaceModel(&ftModel);
    if (FAILED(hr)) {
      cerr << "[UKinect] ERROR: Can not get Face Model." << endl;
      throw;
    }

    Mat tmp;
    if (!faceVisualizationOnColor.as<int>()) {
      tmp = Mat(Size(static_cast<int>(colorBuffer->GetWidth()), static_cast<int>(colorBuffer->GetHeight())), CV_8UC4, CV_RGB(0, 0, 0));
      memcpy(colorBuffer->GetBuffer(), tmp.data, min(colorBuffer->GetBufferSize(), UINT(colorBuffer->GetBufferSize())));
    }
    if (faceVisualizationMode.as<int>())
      hr = VisualizeFacetracker(colorBuffer, _pFTResult, color);
    else
      hr = VisualizeFaceModel(colorBuffer, ftModel, &colorConfig, pSU, 1.0, viewOffset, _pFTResult, color);

    if (FAILED(hr)) {
      cerr << "[UKinect] ERROR: Cannot visualize Face Model." << endl;
      throw;
    }
    tmp = Mat(Size(static_cast<int>(colorBuffer->GetWidth()), static_cast<int>(colorBuffer->GetHeight())), CV_8UC4, colorBuffer->GetBuffer());
    cvtColor(tmp, faceCVMat, CV_BGRA2RGB); // <- alloc new memory
    // Save CV image to UImage
    faceBin.image.data = faceCVMat.data;
    faceImage = faceBin;

    ftModel->Release();
  }

  return true;

}