forked from karlphillip/GraphicsProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgesture_handler.cpp
More file actions
217 lines (180 loc) · 6.83 KB
/
gesture_handler.cpp
File metadata and controls
217 lines (180 loc) · 6.83 KB
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
/* Copyright (C) 2013 Karl Phillip Buhr <karlphillip@gmail.com>
*
* This work is licensed under the Creative Commons Attribution-ShareAlike License.
* To view a copy of this license, visit:
* https://creativecommons.org/licenses/by-sa/2.5/legalcode
*
* Or to read the human-readable summary of the license:
* https://creativecommons.org/licenses/by-sa/2.5/
*/
#include "gesture_handler.h"
#include <iostream>
#include <QImage>
GestureHandler::GestureHandler()
: _streaming(false), _init_error(false),
_last_gesture(PXCGesture::Gesture::LABEL_ANY)
{
QObject::connect(&_fps_timer, SIGNAL(timeout()), this, SLOT(_stream()));
_pipeline.EnableImage(PXCImage::COLOR_FORMAT_RGB24);
_pipeline.EnableGesture();
if (!_pipeline.Init())
{
std::cout << "GestureHandler::GestureHandler !!! Failed initializing pipeline." << std::endl;
_init_error = true;
}
}
GestureHandler::~GestureHandler()
{
if (_init_error)
return;
stop();
_pipeline.Close();
_pipeline.Release();
}
bool GestureHandler::start()
{
if (_init_error)
return false;
if (!_streaming)
{
_streaming = true;
_elapsed_timer.start();
_fps_timer.start(1000/30);
}
return true;
}
void GestureHandler::stop()
{
if (_init_error || !_streaming)
return;
_streaming = false;
_fps_timer.stop();
}
QSize GestureHandler::image_size()
{
if (_init_error)
return QSize();
pxcU32 img_w = 0, img_h = 0;
if (!_pipeline.QueryImageSize(PXCImage::IMAGE_TYPE_COLOR, img_w, img_h))
return QSize();
//std::cout << "GestureHandler::image_size Video Image size:" <<
// img_w << "x" << img_h << std::endl;
return QSize(img_w, img_h);
}
/*
*_stream() reads a single frame
*/
void GestureHandler::_stream()
{
if (!_pipeline.AcquireFrame(true)) // true - blocking
return;
// Retrieve QueryImage() as PXCSmartPtr<PXCImage> will crash later on
PXCImage* image = _pipeline.QueryImage(PXCImage::IMAGE_TYPE_COLOR);
PXCImage::ImageData image_data;
pxcStatus sts = image->AcquireAccess(PXCImage::ACCESS_READ,
PXCImage::COLOR_FORMAT_RGB24,
&image_data);
if (sts < PXC_STATUS_NO_ERROR)
{
std::cout << "!!! Failed AcquireAccess." << std::endl;
_pipeline.ReleaseFrame();
return;
}
PXCImage::ImageInfo image_info;
image->QueryInfo(&image_info);
if (sts < PXC_STATUS_NO_ERROR)
{
std::cout << "!!! Failed QueryInfo." << std::endl;
_pipeline.ReleaseFrame();
return;
}
if (image_info.format == PXCImage::COLOR_FORMAT_RGB24)
{ // BGR layout on little-endian machine
unsigned char* bgr_img_data = (unsigned char*)image_data.planes[0];
QImage temp(bgr_img_data,
image_info.width,
image_info.height,
QImage::Format_RGB888);
// Convert data from BGR (PXCImage format) to RGB (QPixmap)
QPixmap pixmap = QPixmap::fromImage(temp.rgbSwapped());
emit notify_image(pixmap);
}
else
{
std::cout << "!!! TODO: support other image formats besides RGB24." << std::endl;
_pipeline.ReleaseFrame();
return;
}
image->ReleaseAccess(&image_data);
if (sts < PXC_STATUS_NO_ERROR)
{
std::cout << "!!! Failed ReleaseAccess." << std::endl;
_pipeline.ReleaseFrame();
return;
}
/* Detect gestures */
PXCGesture* gesture = _pipeline.QueryGesture();
if (gesture)
{
// get the node data for the whole primary hand
// (the one that shows up in the view first)
PXCGesture::GeoNode node;
sts = gesture->QueryNodeData(0, PXCGesture::GeoNode::LABEL_BODY_HAND_PRIMARY, &node);
if (sts == PXC_STATUS_ITEM_UNAVAILABLE)
{
//std::cout << "!!! QueryNodeData: PXC_STATUS_ITEM_UNAVAILABLE" << std::endl;
_pipeline.ReleaseFrame();
return;
}
//std::cout << "* Hand at " << node.positionWorld.x << "x" << node.positionWorld.y << " z = " << node.positionWorld.z << std::endl;
PXCGesture::Gesture gest;
sts = gesture->QueryGestureData(0, PXCGesture::GeoNode::LABEL_BODY_HAND_PRIMARY, 0, &gest);
if (sts == PXC_STATUS_ITEM_UNAVAILABLE)
{
//std::cout << "!!! QueryGestureData: PXC_STATUS_ITEM_UNAVAILABLE" << std::endl;
_pipeline.ReleaseFrame();
return;
}
if (gest.active)
{
// how many ms has passed since last gesture detection
qint64 elapsed = _elapsed_timer.elapsed();
switch (gest.label)
{
case PXCGesture::Gesture::LABEL_HAND_WAVE:
case PXCGesture::Gesture::LABEL_NAV_SWIPE_LEFT:
case PXCGesture::Gesture::LABEL_NAV_SWIPE_RIGHT:
case PXCGesture::Gesture::LABEL_NAV_SWIPE_UP:
case PXCGesture::Gesture::LABEL_NAV_SWIPE_DOWN:
case PXCGesture::Gesture::LABEL_HAND_CIRCLE:
default:
emit notify_gesture(gest);
break;
case PXCGesture::Gesture::LABEL_POSE_THUMB_UP:
if ( (_last_gesture == PXCGesture::Gesture::LABEL_POSE_THUMB_UP && elapsed > 1000) ||
(_last_gesture != PXCGesture::Gesture::LABEL_POSE_THUMB_UP) )
emit notify_gesture(gest);
break;
case PXCGesture::Gesture::LABEL_POSE_THUMB_DOWN:
if ( (_last_gesture == PXCGesture::Gesture::LABEL_POSE_THUMB_DOWN && elapsed > 1000) ||
(_last_gesture != PXCGesture::Gesture::LABEL_POSE_THUMB_DOWN) )
emit notify_gesture(gest);
break;
case PXCGesture::Gesture::LABEL_POSE_PEACE:
if ( (_last_gesture == PXCGesture::Gesture::LABEL_POSE_PEACE && elapsed > 1000) ||
(_last_gesture != PXCGesture::Gesture::LABEL_POSE_PEACE) )
emit notify_gesture(gest);
break;
case PXCGesture::Gesture::LABEL_POSE_BIG5:
if ( (_last_gesture == PXCGesture::Gesture::LABEL_POSE_BIG5 && elapsed > 1000) ||
(_last_gesture != PXCGesture::Gesture::LABEL_POSE_BIG5) )
emit notify_gesture(gest);
break;
}
_last_gesture = gest.label;
if (elapsed > 1000)
_elapsed_timer.restart();
}
}
_pipeline.ReleaseFrame();
}