Saving Data from Kinect
in
Contributed Library Questions
•
1 year ago
Hey all -
So I'm new to processing (and javascript in general) but I spent several hours today troubleshooting with our coder and we couldn't figure out why this wasn't working.
Basically, I want to save all the data I'm getting out of the kinect. I have my script which samples points, and all I want to do is write them out to files so that I can access them in c4D or other programs.
(There are pre-made scripts out there that accomplish this, but end up greatly reducing the resolution of the kinect camera, which doesn't work for my project. Besides, I want all the data, not some of it!)
Here's my script, cobbled together from SimpleOpenNI templates and other sources.
- /* --------------------------------------------------------------------------
- * SimpleOpenNI DepthMap3d Test
- * --------------------------------------------------------------------------
- * Processing Wrapper for the OpenNI/Kinect library
- * http://code.google.com/p/simple-openni
- * --------------------------------------------------------------------------
- * prog: Max Rheiner / Interaction Design / zhdk / http://iad.zhdk.ch/
- * date: 02/16/2011 (m/d/y)
- * ----------------------------------------------------------------------------
- */
- import SimpleOpenNI.*;
- SimpleOpenNI context;
- float zoomF =0.3f;
- float rotX = radians(180); // by default rotate the hole scene 180deg around the x-axis,
- // the data from openni comes upside down
- float rotY = radians(0);
- int currentFile = 0;
- ArrayList pointList;
- ArrayList totalFrames;
- ArrayList tempArray;
- ArrayList checkerList;
- PrintWriter OUTPUT;
- boolean done;
- void setup()
- {
- frameRate(24);
- pointList = new ArrayList();
- totalFrames = new ArrayList();
- tempArray = new ArrayList();
- checkerList = new ArrayList();
- done = false;
- size(1024, 768, P3D); // strange, get drawing error in the cameraFrustum if i use P3D, in opengl there is no problem
- //context = new SimpleOpenNI(this,SimpleOpenNI.RUN_MODE_SINGLE_THREADED);
- context = new SimpleOpenNI(this, SimpleOpenNI.RUN_MODE_MULTI_THREADED);
- // disable mirror
- context.setMirror(false);
- // enable depthMap generation
- if (context.enableDepth() == false)
- {
- println("Can't open the depthMap, maybe the camera is not connected!");
- exit();
- return;
- }
- stroke(255, 255, 255);
- smooth();
- perspective(radians(45),
- float(width)/float(height),
- 10, 150000);
- }
- void draw()
- {
- // update the cam
- context.update();
- background(0, 0, 0);
- translate(width/2, height/2, 0);
- rotateX(rotX);
- rotateY(rotY);
- scale(zoomF);
- int[] depthMap = context.depthMap();
- int steps = 1; // to speed up the drawing, draw every third point
- int index;
- PVector realWorldPoint;
- translate(0, 0, -1000); // set the rotation center of the scene 1000 infront of the camera
- stroke(255);
- PVector[] realWorldMap = context.depthMapRealWorld();
- for (int y=0;y < context.depthHeight();y+=steps)
- {
- for (int x=0;x < context.depthWidth();x+=steps)
- {
- index = x + y * context.depthWidth();
- if (depthMap[index] > 0)
- {
- // draw the projected point
- // realWorldPoint = context.depthMapRealWorld()[index];
- realWorldPoint = realWorldMap[index];
- point(realWorldPoint.x, realWorldPoint.y, realWorldPoint.z); // make realworld z negative, in the 3d drawing coordsystem +z points in the direction of the eye
- pointList.add( new PVector(realWorldPoint.x, realWorldPoint.y, realWorldPoint.z) );
- }
- if (index==0) {
- if (done == false) {
- //print ("current frame: "+currentFile);
- //print (pointList.get(0));
- totalFrames.add(pointList);
- checkerList = ( (ArrayList) totalFrames.get(currentFile) );
- print ("at frame "+currentFile+", point zero is: "+checkerList.get(0)+"\n");
- pointList.clear();
- //OUTPUT = createWriter("frame"+currentFile+".txt");
- currentFile++;
- //String[] lines = new String[307200]
- //for (int i=0; i<307200; i++) {
- // lines[i]=
- }
- }
- //println("x: " + x + " y: " + y);
- }
- }
- // draw the kinect cam
- context.drawCamFrustum();
- }
- void keyPressed()
- {
- switch(key)
- {
- case ' ':
- context.setMirror(!context.mirror());
- break;
- }
- switch(keyCode)
- {
- case LEFT:
- rotY += 0.1f;
- break;
- case RIGHT:
- // zoom out
- rotY -= 0.1f;
- break;
- case UP:
- if (keyEvent.isShiftDown())
- zoomF += 0.02f;
- else
- rotX += 0.1f;
- break;
- case DOWN:
- if (keyEvent.isShiftDown())
- {
- zoomF -= 0.02f;
- if (zoomF < 0.01)
- zoomF = 0.01;
- }
- else
- rotX -= 0.1f;
- break;
- case DELETE:
- done = true;
- for (int w = 0; w <totalFrames.size(); ++w) {
- print("frame"+w+" exporting");
- OUTPUT = createWriter("frame"+w+".txt");
- OUTPUT.println("This is frame "+w);
- //print(totalFrames.get(w));
- tempArray = ( (ArrayList) totalFrames.get(w) );
- //tempArray=totalFrames.get(w);
- for (int i = 0; i < tempArray.size(); ++i) {
- PVector V = (PVector) tempArray.get(i);
- OUTPUT.println(i+", "+V.x+", "+V.y+", "+V.z); // here we export the coordinates of the vector using String concatenation!
- }
- OUTPUT.flush();
- OUTPUT.close();
- }
- }
- }
2