Exemple #1
0
void ITA_ForcesApp::updateRS()
{
	if (mIsRunning)
	{
		if (mRS->AcquireFrame(false,0) >= PXC_STATUS_NO_ERROR)
		{
			mHandData->Update();
			getRSCursorPos();
			getRSGestureState();

			auto sample = mRS->QuerySample();
			if (sample != nullptr)
			{
				auto rgb = sample->color;
				if (rgb != nullptr)
				{
					PXCImage::ImageData rgbData;
					if (rgb->AcquireAccess(PXCImage::ACCESS_READ, PXCImage::PIXEL_FORMAT_RGB32, &rgbData) >= PXC_STATUS_NO_ERROR)
					{
						auto srf = Surface8u::create(rgbData.planes[0], RGB_SIZE.x, RGB_SIZE.y, rgbData.pitches[0], SurfaceChannelOrder::BGRA);
						auto chan = Channel8u::create(*srf);
						mTexRgb = gl::Texture2d::create(*chan);
						rgb->ReleaseAccess(&rgbData);
					}
				}
			}
			mRS->ReleaseFrame();
		}
	}
}
void RealsensePropGetListener::getValue(imaqkit::IPropInfo* propertyInfo, void* value)
{
	const char* propname = propertyInfo->getPropertyName();

	PXCSenseManager *psm = 0;
	psm = PXCSenseManager::CreateInstance();
	psm->EnableStream(PXCCapture::STREAM_TYPE_COLOR, 640, 480);
	psm->Init();
	if (strcmp(propname, "color_brightness") == 0)
	{
		PXCCapture::Sample *sample = psm->QuerySample();
		PXCCapture::Device *device = psm->QueryCaptureManager()->QueryDevice();
		pxcI32 brightness = device->QueryColorBrightness();
		*(reinterpret_cast<int*>(value)) = brightness;
	}
	else
	{
		assert(false && "Unhandled property . Need to add this new property.");
	}
	psm->Release();



}
DWORD WINAPI RealSenseAdaptor::acquireThread(void* param) {
	RealSenseAdaptor* adaptor = reinterpret_cast<RealSenseAdaptor*>(param);
	// Set the thread priority.
	SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
	MSG msg;
	while (GetMessage(&msg, NULL, 0, 0) > 0) {
		switch (msg.message) {
		case WM_USER:
		{
			// Check if a frame needs to be acquired
			std::auto_ptr<imaqkit::IAutoCriticalSection> driverSection(imaqkit::createAutoCriticalSection(adaptor->_driverGuard, false));
			// Get frame type & dimensions.

			imaqkit::frametypes::FRAMETYPE frameType =
				adaptor->getFrameType();
			int imWidth = adaptor->getMaxWidth();
			int imHeight = adaptor->getMaxHeight();
			int imBand = adaptor->getNumberOfBands();
			int camera_id = adaptor->getDeviceID();

			PXCSession *session = PXCSession_Create();
			if (!session) return 0;
			PXCSenseManager *psm = session->CreateSenseManager();

			if (imBand == 3)
			{
				if (camera_id == 1)
				{
					PXCCapture::DeviceInfo dinfo = {};
					pxcCHAR *myname = L"Intel(R) RealSense(TM) 3D Camera";
					psm->QueryCaptureManager()->FilterByDeviceInfo(myname, dinfo.did, dinfo.didx);
					psm->EnableStream(PXCCapture::STREAM_TYPE_COLOR, imWidth, imHeight);

				}
				else
				{
					PXCCapture::DeviceInfo dinfo = {};
					pxcCHAR *myname = L"Intel(R) RealSense(TM) 3D Camera R200";
					psm->QueryCaptureManager()->FilterByDeviceInfo(myname, dinfo.did, dinfo.didx);
					psm->EnableStream(PXCCapture::STREAM_TYPE_COLOR, imWidth, imHeight, 30);
				}

			}
			else
			{
				if (camera_id == 2)
				{
					PXCCapture::DeviceInfo dinfo = {};
					pxcCHAR *myname = L"Intel(R) RealSense(TM) 3D Camera";
					psm->QueryCaptureManager()->FilterByDeviceInfo(myname, dinfo.did, dinfo.didx);
					psm->EnableStream(PXCCapture::STREAM_TYPE_DEPTH, imWidth, imHeight);


				}
				else
				{
					PXCCapture::DeviceInfo dinfo = {};
					pxcCHAR *myname = L"Intel(R) RealSense(TM) 3D Camera R200";
					psm->QueryCaptureManager()->FilterByDeviceInfo(myname, dinfo.did, dinfo.didx);
					psm->EnableStream(PXCCapture::STREAM_TYPE_DEPTH, imWidth, imHeight, 30);
					psm->EnableStream(PXCCapture::STREAM_TYPE_COLOR, 640, 480, 30);
				}

			}

			UtilRender *renderColor = new UtilRender(L"COLOR STREAM");
			UtilRender *renderDepth = new UtilRender(L"DEPTH STREAM");
			psm->Init();
			PXCImage *colorIm, *depthIm;

			while (adaptor->isAcquisitionNotComplete() && adaptor->isAcquisitionActive()) {
				driverSection->enter();
				if (psm->AcquireFrame(true) < PXC_STATUS_NO_ERROR) break;
				// retrieve all available image samples
				PXCCapture::Sample *sample = psm->QuerySample();
				if (!sample) break;
				unsigned char *imBuffer1 = new unsigned char[imWidth * imHeight * imBand];
				//unsigned short *imBuffer2 = new unsigned short[imWidth * imHeight];
				unsigned char *imBuffer2 = new unsigned char[imWidth * imHeight * 3];
				if (imBand == 3)
				{
					colorIm = sample->color;
					if (!colorIm) break;
					PXCImage::ImageData cdata;
					pxcStatus sts = colorIm->AcquireAccess(PXCImage::ACCESS_READ, PXCImage::PIXEL_FORMAT_RGB32, &cdata);
					for (int ix = 0; ix < imHeight; ix++)
					{
						for (int jx = 0; jx < imWidth; jx++)
						{
							for (int c = 0; c < imBand; c++)
							{
								imBuffer1[(ix * imWidth + jx) * imBand + imBand - 1 - c] = *(pxcBYTE*)(cdata.planes[0] + ix * imWidth * 4 + 4 * jx + c);
							}
						}
					}
					colorIm->ReleaseAccess(&cdata);
					renderColor->RenderFrame(colorIm);

				}
				else
				{
					depthIm = sample->depth;
					if (!depthIm) break;
					
					PXCImage::ImageData ddata;
					pxcStatus sts = depthIm->AcquireAccess(PXCImage::ACCESS_READ, PXCImage::PIXEL_FORMAT_DEPTH, &ddata);
					for (int id = 0; id < imHeight; id++)
					{
					for (int jd = 0; jd < imWidth; jd++)
					{

					imBuffer2[id * imWidth + jd] = *(pxcU16*)(ddata.planes[0] + id * imWidth * 2 + jd * 2);

					}
					}
					depthIm->ReleaseAccess(&ddata);
					renderDepth->RenderFrame(depthIm);
			
				}

				if (adaptor->isSendFrame()) {
					// Create a frame object.
					imaqkit::IAdaptorFrame* frame =
						adaptor->getEngine()->makeFrame(frameType,
						imWidth,
						imHeight);
					// Copy data from buffer into frame object.
					if (imBand == 3)
					{
						frame->setImage(imBuffer1,
							imWidth,
							imHeight,
							0, // X Offset from origin
							0); // Y Offset from origin
					}
					else
					{
						frame->setImage(imBuffer2,
							imWidth,
							imHeight,
							0, // X Offset from origin
							0);
					}

					// Set image's timestamp.
					frame->setTime(imaqkit::getCurrentTime());
					// Send frame object to engine.
					adaptor->getEngine()->receiveFrame(frame);
				} // if isSendFrame()		
				psm->ReleaseFrame();
				// Increment the frame count.
				adaptor->incrementFrameCount();
				// Cleanup. Deallocate imBuffer.
				delete[] imBuffer1;
				delete[] imBuffer2;
				driverSection->leave();
			} // while(isAcquisitionNotComplete()
			delete renderColor;
			delete renderDepth;
			psm->Release();
		}
		break;
		} //switch-case WM_USER
	}//while message is not WM_QUIT
	return 0;
}
/*******************************************************************************
Main entry point. Here we demonstrate, after some initial housekeeping, how to
initialize the camera, start streaming, grab samples, and process them.
*******************************************************************************/
int main(int argc, char *argv[]) {
	if (!ProcessCmdArgs(argc, argv)) {
		return 1;
	}

	// Check / create file directories
	if (xdmPath != nullptr) {
		if (_mkdir(xdmPath) != 0 && errno != EEXIST){
			fprintf(stderr, "Error: Invalid XDM path. Error %d\n", errno);
			fprintf(stderr, "Terminate? [Y/n]\n");
			char choice = _getch();
			if (choice != 'n' && choice != 'N') {
				return ERROR_FILE;
			}
			xdmPath = nullptr;
		}
		else {
			// Remove any trailing '\' in the path since we'll add it later.
			if (xdmPath[strlen(xdmPath) - 1] == '\\')
				xdmPath[strlen(xdmPath) - 1] = '0';
		}
	}
	if (bufPath != nullptr) {
		if (_mkdir(bufPath) != 0 && errno != EEXIST){
			fprintf(stderr, "Error: Invalid Buffer path. Error %d\n", errno);
			fprintf(stderr, "Terminate? [Y/n]\n");
			char choice = _getch();
			if (choice != 'n' && choice != 'N') {
				return ERROR_FILE;
			}
			bufPath = nullptr;
		}
		else {
			// Remove any trailing '\' in the path since we'll add it later.
			if (bufPath[strlen(bufPath) - 1] == '\\')
				bufPath[strlen(bufPath) - 1] = '0';
		}
	}

	// Start timer for total test execution
	unsigned long long programStart = GetTickCount64();
	
	// Initialize camera and streams
	if (verbose) fprintf_s(stdout, "Initializing camera...\n");
	// The Sense Manager is the root object for interacting with the camera.
	PXCSenseManager *senseManager = nullptr;
	senseManager = PXCSenseManager::CreateInstance();
	if (!senseManager) {
		fprintf_s(stderr, "Unable to create the PXCSenseManager\n");
		return ERROR_CAMERA;
	}

	// When enabling the streams (color and depth), the parameters must match
	// the capabilities of the camera. For example, 60fps for color will fail
	// on the DS4 / R200.
	// Here we're hard-coding the resolution and frame rate
	senseManager->EnableStream(PXCCapture::STREAM_TYPE_COLOR, 320, 240, 30);
	senseManager->EnableStream(PXCCapture::STREAM_TYPE_DEPTH, 320, 240, 30);

	// Initialize the PXCSenseManager
	pxcStatus camStatus;
	camStatus = senseManager->Init();
	if (camStatus != PXC_STATUS_NO_ERROR) {
		fprintf_s(stderr, "Unable to initizlize PXCSenseManager\n");
		senseManager->Release();
		return ERROR_CAMERA;
	}
	
	PXCImage *colorImage;
	PXCImage *depthImage;
	PXCPhoto *xdmPhoto = senseManager->QuerySession()->CreatePhoto();

	// These two objects come from the Intel RealSense SDK helper for rendering
	// camera data. Any rendering technique could be used or omitted if no 
	// visual feedback is desired.
	UtilRender *renderColor = new UtilRender(L"COLOR STREAM");
	UtilRender *renderDepth = new UtilRender(L"DEPTH STREAM");
	
	// Start test
	if (verbose) fprintf_s(stdout, "Running...\n");
	// This section may be wrapped in additional code to automate 
	// repetitive tests. Closure provided just for convenience.
	{ // Beginning of single test block
		unsigned long totalFrames = 0;
		unsigned long long streamStart = GetTickCount64();
		for (unsigned int i = 0; i < framesPerTest; i++) {
			// Passing 'true' to AcquireFrame blocks until all streams are 
			// ready (depth and color).  Passing 'false' will result in 
			// frames unaligned in time.
			camStatus = senseManager->AcquireFrame(true);
			if (camStatus < PXC_STATUS_NO_ERROR) {
				fprintf_s(stderr, "Error acquiring frame: %f\n", camStatus);
				break;
			}
			// Retrieve all available image samples
			PXCCapture::Sample *sample = senseManager->QuerySample();
			if (sample == NULL) {
				fprintf_s(stderr, "Sample unavailable\n");
			}
			else {
				totalFrames++;

				colorImage = sample->color;
				depthImage = sample->depth;
				// Render the frames (not necessary)
				if (!renderColor->RenderFrame(colorImage)) break;
				if (!renderDepth->RenderFrame(depthImage)) break;

				// Save data if requested
				wchar_t filePath[MAXPATH];
				char fileName[18];
				TimeStampString(fileName);
				if (xdmPath != nullptr) { // Save XDM image
					swprintf_s(filePath, MAXPATH, L"%S\\%S%S", 
						xdmPath, 
						fileName,
						xdmExt);
					xdmPhoto->ImportFromPreviewSample(sample);
					pxcStatus saveStatus = xdmPhoto->SaveXDM(filePath);
					if (saveStatus != PXC_STATUS_NO_ERROR)
						fprintf_s(stderr, "Error: SaveXDM\n");
				}
				if (bufPath != NULL) { // Save depth buffer
					swprintf_s(filePath, MAXPATH, L"%S\\%S%S", 
						bufPath, fileName, depBufExt);
					// The WriteDepthBuf function has a lot of detail about
					// accessing image data and should be examined.
					if (!WriteDepthBuf(depthImage, filePath)) {
						fprintf(stderr, "Error: WriteDepthBuf\n");
					}
				}
			}

			// Unlock the current frame to fetch the next one
			senseManager->ReleaseFrame();

		}

		float frameRate = (float)totalFrames /
			((float)(GetTickCount64() - streamStart) / 1000);
		if (verbose) fprintf_s(stdout, "Frame Rate: %.2f\n", frameRate);
	} // End of single test block

	// Wrap up
	if(verbose) fprintf_s(stdout, "Finished in %llu seconds.\n",
		(GetTickCount64() - programStart) / 1000);

	delete renderColor;
	delete renderDepth;
	xdmPhoto->Release();
	if(senseManager != nullptr) senseManager->Release();

	return 0;
}
t_jit_err jit_realsense_grab_matrix_calc(t_jit_realsense_grab *x, void *inputs, void *outputs)
{
	t_jit_err err = JIT_ERR_NONE;
	long out_savelock;
	t_jit_matrix_info out_minfo;
	char *out_bp;
	long i, xx, yy, dimcount, planecount, dim[JIT_MATRIX_MAX_DIMCOUNT];
	//t_jit_noise_vecdata	vecdata;
	void *out_matrix;

	PXCSenseManager *sm = x->sm;

	// note: if there is no frame available, should probably send out previous frame (to do)
	if (sm->AcquireFrame(true) < PXC_STATUS_NO_ERROR)
	{
		object_error((t_object *)x, "camera not available");
		return JIT_ERR_GENERIC;
	}

	// retrieve the sample
	PXCCapture::Sample *sample = sm->QuerySample();
	PXCImage::ImageInfo info = sample->depth->QueryInfo();
	PXCImage::ImageData data;
	out_matrix = jit_object_method(outputs, _jit_sym_getindex, 0);

//	post("w %d h %d format %s", info.width, info.height, PXCImage::PixelFormatToString(info.format));

	//get zeroith outlet
	if (x && out_matrix) 
	{
		// lock output
		out_savelock = (long)jit_object_method(out_matrix, _jit_sym_lock, 1);

		// fill out matrix info structs for input and output
		jit_object_method(out_matrix, _jit_sym_getinfo, &out_minfo);
		
		// get pointer to matrix
		jit_object_method(out_matrix, _jit_sym_getdata, &out_bp);

		if (!out_bp){ err = JIT_ERR_INVALID_OUTPUT; goto out; }

		if (!(sample->depth->AcquireAccess(PXCImage::ACCESS_READ, PXCImage::PIXEL_FORMAT_DEPTH, &data) >= PXC_STATUS_NO_ERROR))
		{
			object_error((t_object *)x, "aquire error");
			goto out;
		}

		//get dimensions/planecount
		dimcount = out_minfo.dimcount;
		planecount = out_minfo.planecount;

		if (dimcount != 2){ err = JIT_ERR_MISMATCH_DIM; goto out; }
		
		//post("dim %d x %d planes %d stride %ld", out_minfo.dim[0], out_minfo.dim[1], planecount, out_minfo.dimstride);

		for (i = 0; i < dimcount; i++) {
			dim[i] = out_minfo.dim[i];
		}

		short *dpixels = (short *)data.planes[0];
		int dpitch = data.pitches[0] / sizeof(short);
		for (yy = 0; yy < dim[1]; yy++)
		{
			for (xx = 0; xx < dim[0]; xx++)
			{
				*out_bp++ = (char)dpixels[yy * dpitch + xx];
			}
		}


		//jit_parallel_ndim_simplecalc1((method)jit_realsense_grab_calculate_ndim, x, dimcount, dim, planecount, &out_minfo, out_bp, 0 /* flags1 */);
	}
	else 
		err = JIT_ERR_INVALID_PTR;

out:
	sample->depth->ReleaseAccess(&data);
	sm->ReleaseFrame();

	jit_object_method(out_matrix, _jit_sym_lock, out_savelock);
	return err;
}
int main(char* args,char* argsc) {

	pxcStatus status = PXC_STATUS_ALLOC_FAILED;

	PXCSenseManager *session =  PXCSenseManager::CreateInstance();
	if(!session) {
		printf("Instance create failed\n");
		return 10;
	}

	status = session->EnableHand(nullptr);

	if(status = PXC_STATUS_NO_ERROR) {
		printf("Hand data unavailable\n");
		return 20;
	}

	session->Init();

	if(status = PXC_STATUS_NO_ERROR) {
		printf("init failed\n");
		return 30;
	}

	PXCHandModule* handTracker = session->QueryHand();
	

	if(status = PXC_STATUS_NO_ERROR) {
		printf("no hand tracking support\n");
		return 40;
	}

	PXCHandConfiguration* handConfig = handTracker->CreateActiveConfiguration();
	handConfig->EnableAllGestures();
	handConfig->ApplyChanges();

	PXCHandData* handData = handTracker->CreateOutput();
	bool running = true;

	status = session->EnableStream(PXCCapture::StreamType::STREAM_TYPE_DEPTH,1920,1080,30.0);

	if (status = PXC_STATUS_NO_ERROR) {
		printf("Unknown error when enabling stream.");
		return 50;
	}

	while(running) {
		//printf("Acquire frame. ");
		status = session->AcquireFrame(true);
		//printf("Got frame.\n");
		if(status >= PXC_STATUS_NO_ERROR) {
			printf("He's dead Jim.\n");
			return 50;
		}

		handData->Update();
		//printf("Got %i gestures for %i hands.\n", handData->QueryFiredGesturesNumber(),handData->QueryNumberOfHands());
		for(int i=0; i<handData->QueryFiredGesturesNumber(); i++) {
			PXCHandData::GestureData gestureData;
			handData->QueryFiredGestureData(i,gestureData);
			
			
			wchar_t* name = (wchar_t*)gestureData.name;
			std::wstring nameStr(name);
			std::wcout << nameStr << std::endl;
			
			//printf("%s - len %d\n",nameStr.c_str(), nameStr.size());

			if(nameStr == L"v_sign") {
				running = false;
			}
		}

		PXCCapture::Sample* capture = session->QuerySample();
		PXCImage* depthImage = capture->depth;

		//std::this_thread::sleep_for(std::chrono::milliseconds(10));

		session->ReleaseFrame();
	}

	session->Release();

	return 0;
}