The following code is adapted to the C + + version here, the implementation of the same ideas, but the details are different, does not affect the understanding
#include <iostream> #include <Windows.h> #include <NuiApi.h> #include <ctime> #include < cassert> #include <process.h> #pragma comment (lib, "Kinect10.lib") using namespace std; HANDLE detectionevent = CreateEvent (null, FALSE, FALSE, null);//record gesture current position enum Gesturepos{nonepos = 0,left,right,neutral} ;//Judgment recognition state enum Detectionstate{nonestate = 0,success,failed,inprogress};//The data struct required to determine the gesture Datastate{gesturepos Pos;// Everyone's left-handedness detectionstate state;int times;time_t timestamp;void Reset () {Pos = Gesturepos::nonepos; state = Detectionstate::nonestate;times = 0;timestamp = 0;}};/ /Complete Gesture Judgment Logic function class Gesturedetection{public:gesturedetection (float neutral_threshold, int times, double difftimes): Neutral_threshold (Neutral_threshold), Times, Difftimes (Difftimes), Left_hand (0), Right_hand (1) {for (int i = 0; i < Nui_skeleton_count; i++) {Wave_datas[i][left_hand]. Reset (); Wave_datas[i][right_hand]. Reset ();}} Function: Loop receive bone data, if recognized as waving action output: success,//recognition failure output: Failed,void Update (CONST nui_skeleton_frame* frame) {if (NULL = = FRAME) return, for (int i = 0; i < Nui_skeleton_count; i++) {Judgestate (fr Ame->skeletondata[i], Wave_datas[i][left_hand], true); Judgestate (Frame->skeletondata[i], Wave_datas[i][right_hand], false);}} Private:datastate wave_datas[nui_skeleton_count][2];//Record Each person, the state of each hand const int left_hand;//left hand idconst int right_hand;// Right hand id//Median position threshold: In this range are considered hand in the middle position (relative to the elbow x coordinate) const float neutral_threshold;//wave number threshold, reached that number of times considered to be waving const int times;//time limit, If the time difference is still not recognized the wave action is considered to recognize the failure of the const double difftimes;//to determine the current state success output: success, and generate the event: detectionevent//Failure output failed for UpDate function call void Judgestate (const nui_skeleton_data& skeletondata, datastate& DATA, bool Isleft = true) {int elbow = (IsL EFT)? Nui_skeleton_position_elbow_left:nui_skeleton_position_elbow_right; int hand = (isleft)? Nui_skeleton_position_hand_left:nui_skeleton_position_hand_right; if (! Isskeletontrackedwell (Skeletondata, isleft)) {if (data. state = = InProgress) {#ifdef _debugcout << "Not a well skeleton, detection failed!\n"; #endifdata. Reset (); return;}} float CurPos = skeletondata.skeletonpositions[hand].x; Float Center = skeletondata.skeletonpositions[elbow].x;if (! Isneutral (CurPos, center)) {if (data. Pos = = Nonepos) {#ifdef _debugcout << "found!\n"; #endifdata. times++;d ata. Pos = Isleftside (curpos,center)? Left:right;data. state = Inprogress;data.timestamp = time (NULL);} else if ((data. Pos = = left) && isrightside (CurPos, center)) | | (Data. Pos = = right) && isleftside (CurPos, center)) {ASSERT (data. state = = InProgress);d ata.times++;d ata. Pos = (data. Pos = = left)? Right:left, #ifdef _debugcout << times: << data.times<< endl;if (data. Pos = = left) {cout << ' left!\n ';} else if (data. Pos = = right) {cout << "right!\n";} Elsecout << "You can ' t see me!\n"; #endifif (Data.times >= times) {#ifdef _debugcout << "success!\n"; #endifS Etevent (detectionevent);d ATA. Reset ();} else if (Difftime (NULL), DAta.timestamp) > Difftimes) {#ifdef _debugcout << "Time Out, detection failed!\n"; cout << "Data.times:" << data.times << Endl; #endifdata. Reset ();}}} BOOL Isleftside (float curpos, float center) {return CurPos < (center-neutral_threshold);} BOOL Isrightside (float curpos, float center) {return curpos > (center + neutral_threshold);} BOOL Isneutral (float curpos, float center) {return! Isleftside (Curpos,center) &&! Isrightside (Curpos,center);} Determine bone tracking: including bone trace intact and hand position on elbow bool Isskeletontrackedwell (const nui_skeleton_data& Skeletondata, BOOL Isleft = TRUE) {int elbow = (isleft)? Nui_skeleton_position_elbow_left:nui_skeleton_position_elbow_right; int hand = (isleft)? Nui_skeleton_position_hand_left:nui_skeleton_position_hand_right; if (skeletondata.etrackingstate! = nui_skeleton_not_tracked) {if (Skeletondata.eskeletonpositiontrackingstate[hand ]!= nui_skeleton_position_not_tracked) && (Skeletondata.eskeletonpositiontrackingstate[elbOw]!= nui_skeleton_position_not_tracked) {if (Skeletondata.skeletonpositions[hand].y > SKELETONDATA.SKELETONPOSITIONS[ELBOW].Y) return true;}} return false;}}; #define CHECK_FAILED (HR, MSG) if (FAILED (HR)) {cout << msg << endl; return-1;} unsigned int __stdcall dowave (void* p) {while (WaitForSingleObject (detectionevent,infinite) = = wait_object_0) {cout < ;< "Do something when detecting wave hand\n", cout << "Hello what can I do for you?\n";} return 0;} int main () {int count;inuisensor *kinect = NULL; HRESULT hr; Nuigetsensorcount (&count); if (count < 1) {cout << "kinect\n not detected"; return-1;} hr = Nuicreatesensorbyindex (0, &kinect); if (FAILED (hr) | | kinect = = NULL) {cout << "create sensor failed \ n"; return-1;} if (Kinect->nuistatus ()! = S_OK) {cout << "Kinect sensor not ready \ n"; return-1;} hr = Kinect->nuiinitialize (Nui_initialize_flag_uses_skeleton); check_failed (HR, "initialization failed"); HANDLE next_frame_event = CreateEvent (null, True, FALSE, null); HR = Kinect->nuiskeletontrackingenable (next_frame_event, Nui_skeleton_tracking_flag_enable_in_near_range); check_failed (HR, "open bone Tracing Failed"); uintptr_t thread = _beginthreadex (null, 0, dowave, NULL, 0, NULL); if (thread <= 0) {Co UT << "thread create failed!\n"; return-1;} Nui_skeleton_frame Skeleton_frame; Gesturedetection gesture_detection (0.05, 3, 4); cout << "Start detection \ n"; while (true) {if (WaitForSingleObject (next_frame _event, INFINITE) = = wait_object_0) {kinect->nuiskeletongetnextframe (0, &skeleton_frame); Nuitransformsmooth (&skeleton_frame, NULL); gesture_detection. Update (&skeleton_frame);}} Kinect->nuishutdown (); System ("pause"); return 0;}
The wave of the Kinect gesture recognition (C + + implementation)