Kinect for windows v2 gesture recognition tool of development of Visual Gesture Builder matching program (c ++ version)

Reprinted to: https://www.parful.com/blog/article/110

Before you start studying this tutorial, you first need to understand how to use the tool to create a suffix VGB .gba or .gbd posture database files. Without this foundation, please read in order:

Develop your own first Kinect Gestrue matching program

You need your vs project set up a directory and library directory, the directory containing as Kinect installation inc directory of the library installation directory Lib files in the directory Kinect folder (include x64 x86, select the version you need ).

It includes the header file and import library

 
  1. #include "Kinect.h"
  2. #include "Kinect.VisualGestureBuilder.h"
  3.  
  4. #pragma comment(lib,"Kinect20.lib")
  5. #pragma comment(lib,"Kinect20.VisualGestureBuilder.lib")

Open Kinect sensor

 
  1. IKinectSensor* pSensor = nullptr;
  2. if (GetDefaultKinectSensor(&pSensor) != S_OK)
  3. {
  4. return 1;
  5. }
  6. pSensor->Open();

Read gesture database

 
  1. wstring gestureDatabasePath = L"Seat.gbd";
  2. IVisualGestureBuilderDatabase* pGestureDatabase = nullptr;
  3. if (CreateVisualGestureBuilderDatabaseInstanceFromFile(gestureDatabasePath.c_str(), &pGestureDatabase)!=S_OK)
  4. {
  5. wcout << "Can not read the database, Please check if it exit!" << endl;
  6. return 1;
  7. }

Gets the number of gesture gesture and gesture information database

 
  1. UINT numGesture = 0;
  2. //Get how many gestures in database
  3. pGestureDatabase->get_AvailableGesturesCount(&numGesture);
  4. wcout << "Total gesture count:" << numGesture << endl;
  5. //Get the list of gestures
  6. IGesture** gestureList = new IGesture*[numGesture];
  7. pGestureDatabase->get_AvailableGestures(numGesture, gestureList);
  8. //GestureType enum
  9. GestureType gestureType;
  10. wchar_t gestureName[300];
  11. for (int index = 0; index < numGesture; ++index)
  12. {
  13. if (gestureList[index]->get_GestureType(&gestureType) == S_OK)
  14. {
  15. if (gestureType == GestureType::GestureType_Discrete)
  16. {
  17. //Get discrete gesture name
  18. gestureList[index]->get_Name(300, gestureName);
  19. wcout << "discrete_gesture_" << index << "_name:" << gestureName << endl;
  20. }
  21. else if (gestureType == GestureType::GestureType_Continuous)
  22. {
  23. //Get continuous gesture name
  24. gestureList[index]->get_Name(300, gestureName);
  25. wcout << "continuous_gesture_" << index << "_name" << gestureName << endl;
  26. }
  27. }
  28. }

Creating gesture data stream and reader

A data stream corresponding to a stream reader, the maximum somatosensory body 6 can detect data (numBody = 6) simultaneously. In order to achieve simultaneous detection of more than posture, so here create six gesture stream 6 flow gesture reader

 
  1. IVisualGestureBuilderFrameSource** gestureSources = new IVisualGestureBuilderFrameSource*[numBody];
  2. IVisualGestureBuilderFrameReader** gestureReaders = new IVisualGestureBuilderFrameReader*[numBody];
  3.  
  4. for (int bodyIndex = 0; bodyIndex < numBody; ++bodyIndex)
  5. {
  6. CreateVisualGestureBuilderFrameSource(pSensor, bodyIndex, &gestureSources[bodyIndex]);
  7. gestureSources[bodyIndex]->AddGestures(numGesture, gestureList);
  8. gestureSources[bodyIndex]->OpenReader(&gestureReaders[bodyIndex]);
  9. }

Into the logic of the human skeleton tracking information

 
  1. IBodyFrame* pFrame = nullptr;
  2. //Gets the latest frame.
  3. if (pFrameReader->AcquireLatestFrame(&pFrame) == S_OK)
  4. {
  5. //for each frame, update the body data
  6. if (pFrame->GetAndRefreshBodyData(numBody, bodies) == S_OK)
  7. {
  8. for (int bodyIndex = 0; bodyIndex < numBody; ++bodyIndex)
  9. {
  10. BOOLEAN tracked = false;
  11. IBody* pBody = bodies[bodyIndex];
  12. //has tracked body
  13. if (pBody->get_IsTracked(&tracked) == S_OK&&tracked)
  14. {
  15. //Process body
  16. }
  17. }
  18. }
  19. pFrame->Release();
  20. }

Binding skeletal data stream id gesture tracked

 
  1. UINT64 trackingId = 0;
  2. if (pBody->get_TrackingId(&trackingId) == S_OK)
  3. {
  4. UINT64 gestureId = 0;
  5. if (gestureSources[bodyIndex]->get_TrackingId(&gestureId) == S_OK)
  6. {
  7. if (gestureId != trackingId)
  8. {
  9. //Bind tracking ID
  10. gestureSources[bodyIndex]->put_TrackingId(trackingId);
  11. wcout << "Gesture Source " << bodyIndex << " start to track user " << trackingId << endl;
  12. }
  13. }
  14. }

Gesture database and get results in real time body somatosensory match data

Finally, to get results there are two possibilities, IDiscreteGestureResult (discrete posture results) and IContinuousGestureResult (continuous posture results).

  • Results IDicreteGestureResult available for a BOOLEAN variable, discrete posture when the match is successful, a float with a value of confidence (value 0-1).
  • Results IContinuousGestureResult is accessible to a float, the representative value of 0-1 progress value of the continuous operation, this value is always present.
 
  1. IVisualGestureBuilderFrame* pGestureFrame = nullptr;
  2. //Get latest frame
  3. if (gestureReaders[bodyIndex]->CalculateAndAcquireLatestFrame(&pGestureFrame) == S_OK)
  4. {
  5. BOOLEAN bGestureTracked = false;
  6. if (pGestureFrame->get_IsTrackingIdValid(&bGestureTracked) == S_OK&&bGestureTracked)
  7. {
  8. for (UINT gestureIndex = 0; gestureIndex < numGesture; ++gestureIndex)
  9. {
  10. GestureType gestureType;
  11. gestureList[gestureIndex]->get_GestureType(&gestureType);
  12.  
  13. wchar_t gestureName[260];
  14. gestureList[gestureIndex]->get_Name(260, gestureName);
  15. //Discrete gesture
  16. if (gestureType == GestureType::GestureType_Discrete)
  17. {
  18. IDiscreteGestureResult* pGestureResult = nullptr;
  19. if (pGestureFrame->get_DiscreteGestureResult(gestureList[gestureIndex], &pGestureResult) == S_OK)
  20. {
  21. BOOLEAN detected = false;
  22. if (pGestureResult->get_Detected(&detected) == S_OK&&detected)
  23. {
  24. float confidence = 0.0f;
  25. pGestureResult->get_Confidence(&confidence);
  26. wcout <<gestureName<<"-Confidence:" << confidence<<endl;
  27. pGestureResult->Release();
  28. }
  29. }
  30. } //Continuous gesture
  31. else if (gestureType == GestureType::GestureType_Continuous)
  32. {
  33. IContinuousGestureResult* pGestureResult = nullptr;
  34. if (pGestureFrame->get_ContinuousGestureResult(gestureList[gestureIndex], &pGestureResult) == S_OK)
  35. {
  36. float progress = 0.0f;
  37. if (pGestureResult->get_Progress(&progress) == S_OK)
  38. {
  39. wcout << gestureName << "-progress:" << progress << endl;
  40. }
  41. pGestureResult->Release();
  42. }
  43. }
  44. }
  45. }
  46. pGestureFrame->Release();
  47. }
  48. Please download the complete text at the venue: https://www.parful.com/blog/article/110

Guess you like

Origin blog.csdn.net/ZDT_zdh/article/details/90473976