|
| 1 | +#include "gesture_handler.h" |
| 2 | + |
| 3 | +#include <iostream> |
| 4 | + |
| 5 | +#include <QImage> |
| 6 | + |
| 7 | + |
| 8 | +GestureHandler::GestureHandler() |
| 9 | +: _streaming(false), _init_error(false), |
| 10 | +_last_gesture(PXCGesture::Gesture::LABEL_ANY) |
| 11 | +{ |
| 12 | + QObject::connect(&_fps_timer, SIGNAL(timeout()), this, SLOT(_stream())); |
| 13 | + |
| 14 | + _pipeline.EnableImage(PXCImage::COLOR_FORMAT_RGB24); |
| 15 | + _pipeline.EnableGesture(); |
| 16 | + |
| 17 | + if (!_pipeline.Init()) |
| 18 | + { |
| 19 | + std::cout << "GestureHandler::GestureHandler !!! Failed initializing pipeline." << std::endl; |
| 20 | + _init_error = true; |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +GestureHandler::~GestureHandler() |
| 25 | +{ |
| 26 | + if (_init_error) |
| 27 | + return; |
| 28 | + |
| 29 | + stop(); |
| 30 | + _pipeline.Close(); |
| 31 | + _pipeline.Release(); |
| 32 | +} |
| 33 | + |
| 34 | +bool GestureHandler::start() |
| 35 | +{ |
| 36 | + if (_init_error) |
| 37 | + return false; |
| 38 | + |
| 39 | + if (!_streaming) |
| 40 | + { |
| 41 | + _streaming = true; |
| 42 | + _elapsed_timer.start(); |
| 43 | + _fps_timer.start(1000/30); |
| 44 | + } |
| 45 | + |
| 46 | + return true; |
| 47 | +} |
| 48 | + |
| 49 | +void GestureHandler::stop() |
| 50 | +{ |
| 51 | + if (_init_error || !_streaming) |
| 52 | + return; |
| 53 | + |
| 54 | + _streaming = false; |
| 55 | + _fps_timer.stop(); |
| 56 | +} |
| 57 | + |
| 58 | +QSize GestureHandler::image_size() |
| 59 | +{ |
| 60 | + if (_init_error) |
| 61 | + return QSize(); |
| 62 | + |
| 63 | + pxcU32 img_w = 0, img_h = 0; |
| 64 | + if (!_pipeline.QueryImageSize(PXCImage::IMAGE_TYPE_COLOR, img_w, img_h)) |
| 65 | + return QSize(); |
| 66 | + |
| 67 | + //std::cout << "GestureHandler::image_size Video Image size:" << |
| 68 | + // img_w << "x" << img_h << std::endl; |
| 69 | + |
| 70 | + return QSize(img_w, img_h); |
| 71 | +} |
| 72 | + |
| 73 | +/* |
| 74 | + *_stream() reads a single frame |
| 75 | + */ |
| 76 | +void GestureHandler::_stream() |
| 77 | +{ |
| 78 | + if (!_pipeline.AcquireFrame(true)) // true - blocking |
| 79 | + return; |
| 80 | + |
| 81 | + // Retrieve QueryImage() as PXCSmartPtr<PXCImage> will crash later on |
| 82 | + PXCImage* image = _pipeline.QueryImage(PXCImage::IMAGE_TYPE_COLOR); |
| 83 | + |
| 84 | + PXCImage::ImageData image_data; |
| 85 | + pxcStatus sts = image->AcquireAccess(PXCImage::ACCESS_READ, |
| 86 | + PXCImage::COLOR_FORMAT_RGB24, |
| 87 | + &image_data); |
| 88 | + if (sts < PXC_STATUS_NO_ERROR) |
| 89 | + { |
| 90 | + std::cout << "!!! Failed AcquireAccess." << std::endl; |
| 91 | + _pipeline.ReleaseFrame(); |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + PXCImage::ImageInfo image_info; |
| 96 | + image->QueryInfo(&image_info); |
| 97 | + if (sts < PXC_STATUS_NO_ERROR) |
| 98 | + { |
| 99 | + std::cout << "!!! Failed QueryInfo." << std::endl; |
| 100 | + _pipeline.ReleaseFrame(); |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + if (image_info.format == PXCImage::COLOR_FORMAT_RGB24) |
| 105 | + { // BGR layout on little-endian machine |
| 106 | + unsigned char* bgr_img_data = (unsigned char*)image_data.planes[0]; |
| 107 | + |
| 108 | + QImage temp(bgr_img_data, |
| 109 | + image_info.width, |
| 110 | + image_info.height, |
| 111 | + QImage::Format_RGB888); |
| 112 | + |
| 113 | + // Convert data from BGR (PXCImage format) to RGB (QPixmap) |
| 114 | + QPixmap pixmap = QPixmap::fromImage(temp.rgbSwapped()); |
| 115 | + emit notify_image(pixmap); |
| 116 | + } |
| 117 | + else |
| 118 | + { |
| 119 | + std::cout << "!!! TODO: support other image formats besides RGB24." << std::endl; |
| 120 | + _pipeline.ReleaseFrame(); |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + image->ReleaseAccess(&image_data); |
| 125 | + if (sts < PXC_STATUS_NO_ERROR) |
| 126 | + { |
| 127 | + std::cout << "!!! Failed ReleaseAccess." << std::endl; |
| 128 | + _pipeline.ReleaseFrame(); |
| 129 | + return; |
| 130 | + } |
| 131 | + |
| 132 | + /* Detect gestures */ |
| 133 | + |
| 134 | + PXCGesture* gesture = _pipeline.QueryGesture(); |
| 135 | + if (gesture) |
| 136 | + { |
| 137 | + // get the node data for the whole primary hand |
| 138 | + // (the one that shows up in the view first) |
| 139 | + PXCGesture::GeoNode node; |
| 140 | + sts = gesture->QueryNodeData(0, PXCGesture::GeoNode::LABEL_BODY_HAND_PRIMARY, &node); |
| 141 | + if (sts == PXC_STATUS_ITEM_UNAVAILABLE) |
| 142 | + { |
| 143 | + //std::cout << "!!! QueryNodeData: PXC_STATUS_ITEM_UNAVAILABLE" << std::endl; |
| 144 | + _pipeline.ReleaseFrame(); |
| 145 | + return; |
| 146 | + } |
| 147 | + |
| 148 | + //std::cout << "* Hand at " << node.positionWorld.x << "x" << node.positionWorld.y << " z = " << node.positionWorld.z << std::endl; |
| 149 | + |
| 150 | + PXCGesture::Gesture gest; |
| 151 | + sts = gesture->QueryGestureData(0, PXCGesture::GeoNode::LABEL_BODY_HAND_PRIMARY, 0, &gest); |
| 152 | + if (sts == PXC_STATUS_ITEM_UNAVAILABLE) |
| 153 | + { |
| 154 | + //std::cout << "!!! QueryGestureData: PXC_STATUS_ITEM_UNAVAILABLE" << std::endl; |
| 155 | + _pipeline.ReleaseFrame(); |
| 156 | + return; |
| 157 | + } |
| 158 | + |
| 159 | + if (gest.active) |
| 160 | + { |
| 161 | + // how many ms has passed since last gesture detection |
| 162 | + qint64 elapsed = _elapsed_timer.elapsed(); |
| 163 | + |
| 164 | + switch (gest.label) |
| 165 | + { |
| 166 | + case PXCGesture::Gesture::LABEL_HAND_WAVE: |
| 167 | + case PXCGesture::Gesture::LABEL_NAV_SWIPE_LEFT: |
| 168 | + case PXCGesture::Gesture::LABEL_NAV_SWIPE_RIGHT: |
| 169 | + case PXCGesture::Gesture::LABEL_NAV_SWIPE_UP: |
| 170 | + case PXCGesture::Gesture::LABEL_NAV_SWIPE_DOWN: |
| 171 | + case PXCGesture::Gesture::LABEL_HAND_CIRCLE: |
| 172 | + default: |
| 173 | + emit notify_gesture(gest); |
| 174 | + break; |
| 175 | + |
| 176 | + case PXCGesture::Gesture::LABEL_POSE_THUMB_UP: |
| 177 | + if ( (_last_gesture == PXCGesture::Gesture::LABEL_POSE_THUMB_UP && elapsed > 1000) || |
| 178 | + (_last_gesture != PXCGesture::Gesture::LABEL_POSE_THUMB_UP) ) |
| 179 | + emit notify_gesture(gest); |
| 180 | + break; |
| 181 | + |
| 182 | + case PXCGesture::Gesture::LABEL_POSE_THUMB_DOWN: |
| 183 | + if ( (_last_gesture == PXCGesture::Gesture::LABEL_POSE_THUMB_DOWN && elapsed > 1000) || |
| 184 | + (_last_gesture != PXCGesture::Gesture::LABEL_POSE_THUMB_DOWN) ) |
| 185 | + emit notify_gesture(gest); |
| 186 | + break; |
| 187 | + |
| 188 | + case PXCGesture::Gesture::LABEL_POSE_PEACE: |
| 189 | + if ( (_last_gesture == PXCGesture::Gesture::LABEL_POSE_PEACE && elapsed > 1000) || |
| 190 | + (_last_gesture != PXCGesture::Gesture::LABEL_POSE_PEACE) ) |
| 191 | + emit notify_gesture(gest); |
| 192 | + break; |
| 193 | + |
| 194 | + case PXCGesture::Gesture::LABEL_POSE_BIG5: |
| 195 | + if ( (_last_gesture == PXCGesture::Gesture::LABEL_POSE_BIG5 && elapsed > 1000) || |
| 196 | + (_last_gesture != PXCGesture::Gesture::LABEL_POSE_BIG5) ) |
| 197 | + emit notify_gesture(gest); |
| 198 | + break; |
| 199 | + } |
| 200 | + _last_gesture = gest.label; |
| 201 | + if (elapsed > 1000) |
| 202 | + _elapsed_timer.restart(); |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + _pipeline.ReleaseFrame(); |
| 207 | +} |
| 208 | + |
0 commit comments