-
Notifications
You must be signed in to change notification settings - Fork 36
/
ofxKinectCommonBridge.h
318 lines (240 loc) · 6.98 KB
/
ofxKinectCommonBridge.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#pragma once
//#define KCB_ENABLE_FT
//#define KCB_ENABLE_SPEECH
#include "KinectCommonBridgeLib.h"
#include "NuiSensor.h"
#include "ofMain.h" // this MUST come after KCB!!! Not sure you need NuiSensor.h if using KCB
#pragma comment (lib, "KinectCommonBridge.lib") // add path to lib additional dependency dir $(TargetDir)
#ifdef KCB_ENABLE_FT
#pragma comment(lib, "FaceTrackLib.lib")
#endif
class ofxKCBFace {
public:
enum FACE_POSITIONS {
};
enum FEATURE {
LEFT_EYE, RIGHT_EYE, MOUTH, NOSE, CHIN, LEFT_EAR, RIGHT_EAR
};
ofxKCBFace & operator=(const ofxKCBFace & rhs) {
rotation = rhs.rotation;
translation = rhs.translation;
mesh = rhs.mesh;
rect = rhs.rect;
return *this;
};
ofVec3f rotation, translation;
ofRectangle rect;
ofMesh mesh;
ofVec3f getLocationByIdentifier(FACE_POSITIONS position);
ofRectangle getFeatureBounding(FACE_POSITIONS position);
};
// if you want to use events, subscribe to this, otherwise
class ofxKCBSpeechEvent : public ofEventArgs
{
public:
std::string detectedSpeech;
int confidence;
static ofEvent<ofxKCBSpeechEvent> event;
};
// poll for this very simple data object for speech data
class SpeechData {
public:
std::string detectedSpeech;
int confidence;
};
class SkeletonBone
{
public:
enum TrackingState {
NotTracked,
Tracked,
Inferred
};
// lots of constness because we're putting these in a map and that
// copies stuff all over the place
const ofQuaternion getCameraRotation();
const ofMatrix4x4 getCameraRotationMatrix();
const ofVec3f& getStartPosition();
const ofVec3f getScreenPosition();
const ofQuaternion& getRotation();
const ofMatrix4x4& getRotationMatrix();
const int getStartJoint();
int getEndJoint();
TrackingState getTrackingState();
SkeletonBone( const Vector4& inPosition, const _NUI_SKELETON_BONE_ORIENTATION& bone, const NUI_SKELETON_POSITION_TRACKING_STATE& trackingState );
private:
ofMatrix4x4 cameraRotation;
int endJoint;
int startJoint;
ofVec3f position;
ofMatrix4x4 rotation;
ofVec2f screenPosition;
TrackingState trackingState;
};
typedef map<_NUI_SKELETON_POSITION_INDEX, SkeletonBone> Skeleton;
class ofxKinectCommonBridge : protected ofThread {
public:
ofxKinectCommonBridge();
// new API
bool initSensor( int id = 0 );
bool createDepthPixels( int width = 0, int height = 0 );
bool initDepthStream( int width, int height, bool nearMode = false, bool mapColorToDepth = false );
bool createColorPixels( int width = 0, int height = 0 );
bool initColorStream( int width, int height, bool mapColorToDepth = false );
bool initIRStream( int width, int height );
bool initSkeletonStream( bool seated );
bool start();
KCBHANDLE getHandle();
INuiSensor & getNuiSensor();
#ifdef KCB_ENABLE_FT
bool initFaceTracking(); // no params, can't use with other stuff either.
#endif
// audio functionality
#ifdef KCB_ENABLE_SPEECH
bool startAudioStream();
bool initSpeech();
bool loadGrammar(string filename);
// speech
bool hasNewSpeechData();
SpeechData getNewSpeechData();
#endif
void stop();
/// is the current frame new?
bool isFrameNew();
bool isFrameNewVideo();
bool isFrameNewDepth();
bool isNewSkeleton();
bool isFaceNew() {
return bIsFaceNew;
}
void setDepthClipping(float nearClip=500, float farClip=4000);
/// updates the pixel buffers and textures
///
/// make sure to call this to update to the latest incoming frames
void update();
ofPixels& getColorPixelsRef();
ofPixels & getDepthPixelsRef(); ///< grayscale values
ofShortPixels & getRawDepthPixelsRef(); ///< raw 11 bit values
NUI_DEPTH_IMAGE_PIXEL* getNuiDepthPixelsRef();
/// enable/disable frame loading into textures on update()
void setUseTexture(bool bUse);
// you can just not use any streams if you want
void setUseStreams(bool bUse);
/// draw the video texture
void draw(float x, float y, float w, float h);
void draw(float x, float y);
void draw(const ofPoint& point);
void draw(const ofRectangle& rect);
/// draw the grayscale depth texture
void drawRawDepth(float x, float y, float w, float h);
void drawRawDepth(float x, float y);
void drawRawDepth(const ofPoint& point);
void drawRawDepth(const ofRectangle& rect);
/// draw the grayscale depth texture
void drawDepth(float x, float y, float w, float h);
void drawDepth(float x, float y);
void drawDepth(const ofPoint& point);
void drawDepth(const ofRectangle& rect);
void drawIR( float x, float y, float w, float h );
vector<Skeleton> &getSkeletons();
void drawSkeleton(int index);
ofTexture &getRawDepthTexture() {
return rawDepthTex;
}
ofTexture &getDepthTexture() {
return depthTex;
}
ofTexture &getColorTexture() {
return videoTex;
}
// face tracking
ofTexture &getFaceTrackingTexture()
{
return faceTrackingTexture;
}
ofxKCBFace& getFaceData();
void setSpeechGrammarFile(string path) {
grammarFile = path;
}
bool initAudio();
private:
KCBHANDLE hKinect;
KINECT_IMAGE_FRAME_FORMAT depthFormat;
KINECT_IMAGE_FRAME_FORMAT colorFormat;
NUI_SKELETON_FRAME k4wSkeletons;
bool bInitedColor, bInitedDepth, bInitedIR;
bool bStarted;
vector<Skeleton> skeletons;
//quantize depth buffer to 8 bit range
vector<unsigned char> depthLookupTable;
void updateDepthLookupTable();
void updateDepthPixels();
void updateIRPixels();
bool bNearWhite;
float nearClipping, farClipping;
#ifdef KCB_ENABLE_FT
void updateFaceTrackingData( IFTResult* ftResult );
#endif
bool bUseTexture;
ofTexture depthTex; ///< the depth texture
ofTexture rawDepthTex; ///<
ofTexture videoTex; ///< the RGB texture
// face
ofTexture faceTrackingTexture;
//ofTexture irTex;
ofPixels videoPixels;
ofPixels videoPixelsBack; ///< rgb back
ofPixels depthPixels;
ofPixels depthPixelsBack;
ofShortPixels depthPixelsRaw;
ofShortPixels depthPixelsRawBack; ///< depth back
NUI_DEPTH_IMAGE_PIXEL* depthPixelsNui;
ofPixels irPixels;
ofPixels irPixelsBack;
bool bIsFrameNewVideo;
bool bNeedsUpdateVideo;
bool bIsFrameNewDepth;
bool bNeedsUpdateDepth;
bool bNeedsUpdateSkeleton;
bool bIsSkeletonFrameNew;
bool bUsingFaceTrack;
bool bUpdateSpeech;
bool bUpdateFaces;
bool bIsFaceNew;
bool bUseStreams;
bool bProgrammableRenderer;
// speech
bool bHasLoadedGrammar;
bool bUsingSpeech;
string grammarFile;
SpeechData speechData;
float speechConfidenceThreshold; // make a way to set this
// audio
bool bUsingAudio;
// face
bool bIsTrackingFace;
#ifdef KCB_ENABLE_FT
// double buffer for faces
ofxKCBFace faceDataBack, faceData; // only single face at the moment
// camera params for face tracking
FT_CAMERA_CONFIG depthCameraConfig;
FT_CAMERA_CONFIG videoCameraConfig;
FLOAT pSUCoef;
FLOAT zoomFactor;
#endif
#ifdef KCB_ENABLE_SPEECH
bool bInitedSpeech;
#endif
bool bVideoIsInfrared;
bool bUsingSkeletons;
bool bUsingDepth;
BYTE *irPixelByteArray;
void threadedFunction();
bool mappingColorToDepth;
bool mappingDepthToColor;
bool beginMappingColorToDepth, beginMappingDepthToColor;
NUI_IMAGE_RESOLUTION colorRes;
NUI_IMAGE_RESOLUTION depthRes;
INuiSensor *nuiSensor;
INuiCoordinateMapper *mapper;
};