-
Notifications
You must be signed in to change notification settings - Fork 0
/
kinect.cpp
185 lines (137 loc) · 3.78 KB
/
kinect.cpp
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
#include <Kinect.h>
#include <Eigen/Dense>
#include <pybind11/pybind11.h>
#include "filter.h"
typedef struct {
bool tracked;
TIMESPAN previous;
Joint joints[JointType_Count];
Filter *filters[JointType_Count];
} Body;
IKinectSensor *sensor;
IBodyFrameReader *bodyFrameReader;
Body bodies[BODY_COUNT];
template<class Interface>
void Release(Interface *&toRelease) {
if (toRelease) {
toRelease->Release();
toRelease = NULL;
}
}
HRESULT Open() {
if (sensor) {
return E_FAIL;
}
HRESULT result = GetDefaultKinectSensor(&sensor);
if (SUCCEEDED(result)) {
result = sensor->Open();
}
IBodyFrameSource *bodyFrameSource = NULL;
if (SUCCEEDED(result)) {
result = sensor->get_BodyFrameSource(&bodyFrameSource);
}
if (SUCCEEDED(result)) {
result = bodyFrameSource->OpenReader(&bodyFrameReader);
}
Release(bodyFrameSource);
return result;
}
HRESULT Update(const float processNoise, const float observationNoise) {
if (!sensor) {
return E_FAIL;
}
IBodyFrame* bodyFrame = NULL;
HRESULT result = bodyFrameReader->AcquireLatestFrame(&bodyFrame);
TIMESPAN current;
if (SUCCEEDED(result)) {
result = bodyFrame->get_RelativeTime(¤t);
}
Vector4 floorPlane;
if (SUCCEEDED(result)) {
result = bodyFrame->get_FloorClipPlane(&floorPlane);
}
Eigen::Matrix3f rotation;
IBody *bodiesData[BODY_COUNT] = {};
if (SUCCEEDED(result)) {
Eigen::RowVector3f y(floorPlane.x, floorPlane.y, floorPlane.z);
Eigen::RowVector3f z = Eigen::Vector3f(0, 1, -floorPlane.y / floorPlane.z).normalized();
Eigen::RowVector3f x = y.cross(z);
rotation << x, y, z;
result = bodyFrame->GetAndRefreshBodyData(BODY_COUNT, bodiesData);
}
for (int bodyIndex = 0; bodyIndex < BODY_COUNT; bodyIndex++) {
IBody *bodyData = bodiesData[bodyIndex];
BOOLEAN tracked;
if (SUCCEEDED(result)) {
result = bodyData->get_IsTracked(&tracked);
}
Body &body = bodies[bodyIndex];
if (SUCCEEDED(result)) {
body.tracked = tracked;
if (tracked) {
result = bodyData->GetJoints(JointType_Count, body.joints);
if (SUCCEEDED(result)) {
for (int jointIndex = 0; jointIndex < JointType_Count; jointIndex++) {
Joint &joint = body.joints[jointIndex];
CameraSpacePoint &point = joint.Position;
Eigen::Vector3f location = rotation * Eigen::Vector3f(point.X, point.Y, point.Z);
location[1] += floorPlane.w;
Filter *&filter = body.filters[jointIndex];
if (filter) {
filter->Update((current - body.previous) / 10000000.0, processNoise, observationNoise, location);
} else {
filter = new Filter(location);
}
point.X = location[0];
point.Y = location[1];
point.Z = location[2];
}
body.previous = current;
}
}
}
Release(bodyData);
}
Release(bodyFrame);
return result;
}
HRESULT Close() {
if (!sensor) {
return E_FAIL;
}
for (Body &body : bodies) {
for (Filter *&filter : body.filters) {
delete filter;
filter = NULL;
}
}
Release(bodyFrameReader);
HRESULT result = sensor->Close();
Release(sensor);
return result;
}
PYBIND11_MODULE(kinect, module) {
pybind11::class_<Joint>(module, "Joint")
.def_property_readonly("x", [](Joint joint){return joint.Position.X;})
.def_property_readonly("y", [](Joint joint){return joint.Position.Y;})
.def_property_readonly("z", [](Joint joint){return joint.Position.Z;});
pybind11::class_<Body>(module, "Body")
.def_readonly("tracked", &Body::tracked)
.def_property_readonly("joints", [](Body body){
pybind11::list joints;
for (Joint &joint : body.joints) {
joints.append(joint);
}
return joints;
});
module.def("open", &Open);
module.def("update", &Update);
module.def("getBodies", [](){
pybind11::list bodies;
for (Body &body : ::bodies) {
bodies.append(body);
}
return bodies;
});
module.def("close", &Close);
}