openni::Status HandGesture::Init(int argc, char **argv) { openni::OpenNI::initialize(); const char* deviceUri = openni::ANY_DEVICE; for (int i = 1; i < argc-1; ++i) { if (strcmp(argv[i], "-device") == 0) { deviceUri = argv[i+1]; break; } } openni::Status rc = m_device.open(deviceUri); if (rc != openni::STATUS_OK) { printf("Open Device failed:\n%s\n", openni::OpenNI::getExtendedError()); return rc; } nite::NiTE::initialize(); if (m_pHandTracker->create(&m_device) != nite::STATUS_OK) { return openni::STATUS_ERROR; } rc = m_depthStream.create(m_device, openni::SENSOR_DEPTH); if (rc == openni::STATUS_OK) { rc = m_depthStream.start(); if (rc != openni::STATUS_OK) { printf("SimpleViewer: Couldn't start depth stream:\n%s\n", openni::OpenNI::getExtendedError()); m_depthStream.destroy(); } } else { printf("SimpleViewer: Couldn't find depth stream:\n%s\n", openni::OpenNI::getExtendedError()); } m_pHandTracker->startGestureDetection(nite::GESTURE_WAVE); m_pHandTracker->startGestureDetection(nite::GESTURE_CLICK); // m_pHandTracker->startGestureDetection(nite::GESTURE_HAND_RAISE); return InitOpenCV(argc, argv); }
bool initONI2Stream( openni::Device& device, openni::SensorType sensorType, openni::VideoStream& stream, int w, int h, int fps, openni::PixelFormat format) { openni::Status rc = openni::STATUS_OK; const char* strSensor; if (sensorType == openni::SENSOR_COLOR) { strSensor = "openni::SENSOR_COLOR"; } else if (sensorType == openni::SENSOR_DEPTH) { strSensor = "openni::SENSOR_DEPTH"; } else { printf("%s:Unknown SensorType -> %d\n", __FUNCTION__, sensorType); return false; } rc = stream.create(device, sensorType); if (rc != openni::STATUS_OK) { printf( "%s:Couldn't find sensor %s: %s\n", __FUNCTION__, strSensor, openni::OpenNI::getExtendedError()); return false; } openni::VideoMode options = stream.getVideoMode(); printf( "%s:Initial resolution %s (%d, %d) FPS %d Format %d\n", __FUNCTION__, strSensor, options.getResolutionX(), options.getResolutionY(), options.getFps(), options.getPixelFormat()); if (setONI2StreamMode(stream, w, h, fps, format) == false) { printf( "%s:Can't find desired mode in the %s\n", __FUNCTION__, strSensor); return false; } options = stream.getVideoMode(); printf( " -> (%d, %d) FPS %d Format %d\n", options.getResolutionX(), options.getResolutionY(), options.getFps(), options.getPixelFormat()); return true; }
bool initONI2DepthStream(openni::Device& device, openni::VideoStream& depth, int w, int h, int fps, openni::PixelFormat format){ openni::Status rc = depth.create(device, openni::SENSOR_DEPTH); if (rc != openni::STATUS_OK){ printf("%s:Couldn't find depth stream:\n%s\n", __FUNCTION__, openni::OpenNI::getExtendedError()); return false; } openni::VideoMode options = depth.getVideoMode(); printf("Initial resolution Depth(%d, %d) FPS %d Format %d\n", options.getResolutionX(), options.getResolutionY(), options.getFps(), options.getPixelFormat()); if(setONI2StreamMode(depth, w, h, fps, format) == false){ printf("%s:Can't find desired depth mode\n", __FUNCTION__ ); return false; } options = depth.getVideoMode(); printf(" -> (%d, %d) FPS %d Format %d\n", options.getResolutionX(), options.getResolutionY(), options.getFps(), options.getPixelFormat()); depth.setMirroringEnabled(false); rc = depth.start(); if (rc != openni::STATUS_OK){ printf("Couldn't start depth stream:\n%s\n", openni::OpenNI::getExtendedError()); depth.destroy(); return false; } return true; }
void open(const char* uri) { if (device.open(uri) != openni::STATUS_OK) BOOST_THROW_EXCEPTION(GrabberException("Failed to open device") << GrabberException::ErrorInfo(openni::OpenNI::getExtendedError())); if (color_stream.create(device, openni::SENSOR_COLOR) != openni::STATUS_OK) BOOST_THROW_EXCEPTION(GrabberException("Failed to create color stream") << GrabberException::ErrorInfo(openni::OpenNI::getExtendedError())); openni::VideoMode color_mode; color_mode.setFps(30); color_mode.setResolution(color_image_resolution.width, color_image_resolution.height); color_mode.setPixelFormat(openni::PIXEL_FORMAT_RGB888); color_stream.setVideoMode(color_mode); color_image_size = color_image_resolution.width * color_image_resolution.height * 3; color_stream.setMirroringEnabled(false); if (color_stream.start() != openni::STATUS_OK) { color_stream.destroy(); BOOST_THROW_EXCEPTION(GrabberException("Failed to start color stream") << GrabberException::ErrorInfo(openni::OpenNI::getExtendedError())); } streams.push_back(&color_stream); auto control = device.getPlaybackControl(); if (control != nullptr) { // This is a file, make sure we get every frame control->setSpeed(-1.0f); control->setRepeatEnabled(false); num_frames = control->getNumberOfFrames(color_stream); is_file = true; if (num_frames == -1) BOOST_THROW_EXCEPTION(GrabberException("Unable to determine number of frames in ONI file")); } }
int SensorOpenNI::initialize() { LOG(INFO) << "Initializing OpenNI"; ///< force shutdown before starting!! kinect::OpenNI::shutdown(); kinect::Status rc; rc = kinect::STATUS_OK; /// Fetch the device URI to pass to Device::open() const char* deviceURI = kinect::ANY_DEVICE; /// Initialize the device rc = kinect::OpenNI::initialize(); if(rc!=kinect::STATUS_OK) { mDebug()<<"Initialization Errors (if any): "<< kinect::OpenNI::getExtendedError(); kinect::OpenNI::shutdown(); exit(0); } /// Open the device using the previously fetched device URI rc = device.open(deviceURI); if (rc != kinect::STATUS_OK) { mDebug()<<"Device open failed: "<<kinect::OpenNI::getExtendedError(); kinect::OpenNI::shutdown(); exit(0); } /// Create the depth stream rc = g_depthStream.create(device, kinect::SENSOR_DEPTH); if (rc == kinect::STATUS_OK) { /// start the depth stream, if its creation was successful rc = g_depthStream.start(); if (rc != kinect::STATUS_OK) { mDebug()<<"Couldn't start depth stream: "<<kinect::OpenNI::getExtendedError(); g_depthStream.destroy(); exit(0); } } else { mDebug()<<"Couldn't find depth stream: "<<kinect::OpenNI::getExtendedError(); exit(0); } if (!g_depthStream.isValid()) { mDebug()<<"No valid depth streams. Exiting"; kinect::OpenNI::shutdown(); exit(0); } /// Create the color stream rc = g_colorStream.create(device, kinect::SENSOR_COLOR); if (rc == kinect::STATUS_OK) { /// start the color stream, if its creation was successful rc = g_colorStream.start(); if (rc != kinect::STATUS_OK) { mDebug()<<"Couldn't start color stream: "<<kinect::OpenNI::getExtendedError(); g_colorStream.destroy(); exit(0); } } else { mDebug()<<"Couldn't find color stream: "<<kinect::OpenNI::getExtendedError(); exit(0); } if (!g_colorStream.isValid()) { mDebug()<<"No valid color streams. Exiting"; kinect::OpenNI::shutdown(); exit(0); } /// Configure resolutions { /// Attempt to set for depth { kinect::VideoMode mode = g_depthStream.getVideoMode(); if(((int)camera->FPS())==60) mode.setFps(60); else mode.setFps(30); mode.setResolution(camera->width(), camera->height()); rc = g_depthStream.setVideoMode(mode); if (rc != kinect::STATUS_OK) std::cerr << "error setting video mode (depth)" << std::endl; } /// Attempt to set for color { kinect::VideoMode mode = g_colorStream.getVideoMode(); if(((int)camera->FPS())==60) mode.setFps(60); else mode.setFps(30); mode.setFps(30); ///< @todo check!!! mode.setResolution(camera->width(), camera->height()); rc = g_colorStream.setVideoMode(mode); if (rc != kinect::STATUS_OK) std::cerr << "error setting video mode (color)" << std::endl; } } #ifdef THIS_CAUSES_INIT_STALLS /// Enable depth/color frame synchronization rc = device.setDepthColorSyncEnabled(true); if (rc != kinect::STATUS_OK) { qDebug()<<"Could not synchronise device"; // VGA Kinect always seems to shut down here kinect::OpenNI::shutdown(); exit(0); } #endif /// Camera settings kinect::CameraSettings* settings = g_colorStream.getCameraSettings(); settings->setAutoExposureEnabled(true); settings->setAutoWhiteBalanceEnabled(true); /// Fetch the camera intrinsics #if 0 float w = g_depthStream.getVideoMode().getResolutionX();protected:
void openCommon(openni::Device& device, bool defaultRightColor) { XnStatus nRetVal = XN_STATUS_OK; g_bIsDepthOn = false; g_bIsColorOn = false; g_bIsIROn = false; g_depthSensorInfo = device.getSensorInfo(openni::SENSOR_DEPTH); g_colorSensorInfo = device.getSensorInfo(openni::SENSOR_COLOR); g_irSensorInfo = device.getSensorInfo(openni::SENSOR_IR); if (g_depthSensorInfo != NULL) { nRetVal = g_depthStream.create(device, openni::SENSOR_DEPTH); if (nRetVal != openni::STATUS_OK) { printf("Failed to create depth stream:\n%s\n", openni::OpenNI::getExtendedError()); return; } nRetVal = g_depthStream.start(); if (nRetVal != openni::STATUS_OK) { printf("Failed to start depth stream:\n%s\n", openni::OpenNI::getExtendedError()); g_depthStream.destroy(); return; } g_bIsDepthOn = true; } if (g_colorSensorInfo != NULL) { nRetVal = g_colorStream.create(device, openni::SENSOR_COLOR); if (nRetVal != openni::STATUS_OK) { printf("Failed to create color stream:\n%s\n", openni::OpenNI::getExtendedError()); return; } if (defaultRightColor) { nRetVal = g_colorStream.start(); if (nRetVal != openni::STATUS_OK) { printf("Failed to start color stream:\n%s\n", openni::OpenNI::getExtendedError()); g_colorStream.destroy(); return; } g_bIsColorOn = true; } } if (g_irSensorInfo != NULL) { nRetVal = g_irStream.create(device, openni::SENSOR_IR); if (nRetVal != openni::STATUS_OK) { printf("Failed to create IR stream:\n%s\n", openni::OpenNI::getExtendedError()); return; } if (!g_bIsColorOn) { nRetVal = g_irStream.start(); if (nRetVal != openni::STATUS_OK) { printf("Failed to start IR stream:\n%s\n", openni::OpenNI::getExtendedError()); g_irStream.destroy(); return; } g_bIsIROn = true; } } initConstants(); readFrame(); }
// Start recording void Oni2Grabber::createAndStartStream_(openni::VideoStream &stream, openni::SensorType sensor_type) { stream.create(device_, sensor_type); stream.start(); streams_.push_back(&stream); }