Last active
January 3, 2016 23:57
-
-
Save edsonsoares/83ccea75ff37b550e342 to your computer and use it in GitHub Desktop.
Record and Save Junction Positions w/ Kinect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void setup_kinect(){ | |
context = new SimpleOpenNI(this); | |
if (context.isInit() == false) { | |
println("Can't Init SimpleOpenNI, maybe the camera is not connected."); | |
exit(); | |
return; | |
} | |
// recording | |
//enable depthMap generation | |
context.enableDepth(); | |
//enable regular camera | |
context.enableRGB(); | |
//setup the recording | |
context.enableRecorder(recordPath); | |
//select the recording channels | |
context.addNodeToRecording(SimpleOpenNI.NODE_DEPTH, true); | |
context.addNodeToRecording(SimpleOpenNI.NODE_IMAGE, true); | |
//enable user | |
context.enableUser(); | |
} | |
//Call functions to draw skeleton and Points | |
void drawSkeleton (int userId) { | |
drawLimb( userId); | |
drawPoints( userId); | |
} | |
// Function to convert Kinect number into positions on the canvas | |
PVector convertToScreenCoordinates(PVector oldVector) { | |
PVector emptyVector = new PVector(); | |
context.convertRealWorldToProjective(oldVector, emptyVector); | |
return emptyVector; | |
} | |
//start skeleton tracking | |
void onNewUser(SimpleOpenNI curContext, int userId) | |
{ | |
println("onNewUser - userId: " + userId); | |
println("\tstart tracking skeleton"); | |
curContext.startTrackingSkeleton(userId); | |
} | |
void drawPoints(int userId){ | |
//Get kinect numbers | |
/*Roopa tip to make the code cleaner: | |
positions[0] = context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_LEFT_SHOULDER, leftShoulder);*/ | |
context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_LEFT_SHOULDER, leftShoulder); | |
context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_RIGHT_SHOULDER, rightShoulder); | |
context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_LEFT_ELBOW, leftElbow); | |
context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_RIGHT_ELBOW, rightElbow); | |
context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_LEFT_HAND, leftHand); | |
context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_RIGHT_HAND, rightHand); | |
context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_LEFT_HIP, leftHip); | |
context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_RIGHT_HIP, rightHip); | |
context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_LEFT_KNEE, leftKnee); | |
context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_RIGHT_KNEE, rightKnee); | |
context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_HEAD, head); | |
context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_NECK, neck); | |
context.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_NECK, torso); | |
/*Roopa tips to make the code cleaner | |
for int (i = 0; i < positions.length; i++) { | |
positions[i] = con | |
}*/ | |
//convert to positions on the canvas | |
leftShoulder = convertToScreenCoordinates(leftShoulder); | |
rightShoulder = convertToScreenCoordinates(rightShoulder); | |
leftElbow = convertToScreenCoordinates(leftElbow); | |
rightElbow = convertToScreenCoordinates(rightElbow); | |
leftHand = convertToScreenCoordinates(leftHand); | |
rightHand = convertToScreenCoordinates(rightHand); | |
leftHip = convertToScreenCoordinates(leftHip); | |
rightHip = convertToScreenCoordinates(rightHip); | |
leftKnee = convertToScreenCoordinates(leftKnee); | |
rightKnee = convertToScreenCoordinates(rightKnee); | |
head = convertToScreenCoordinates(head); | |
neck = convertToScreenCoordinates(neck); | |
torso = convertToScreenCoordinates(torso); | |
/*println("left shoulder, x:" + leftShoulder.x + ", y:" + leftShoulder.y + " || right shoulder, x:" + rightShoulder.x + ", y:" + rightShoulder.y + | |
" || left elbow, x:" + leftElbow.x + ", y:" + leftElbow.y + " || right elbow, x:" + rightElbow.x + ", y:" + rightElbow.y + | |
" || left hip, x:" + leftHip.x + ", y:" + leftHip.y + " || right hip, x:" + rightHip.x + ", y:" + rightHip.y + | |
" || left knee, x:" + leftKnee.x + ", y:" + leftKnee.y + " || right knee, x:" + rightKnee.x + ", y:" + rightKnee.y + | |
" || head, x:" + head.x + ", y:" + head.y + | |
" || neck, x:" + neck.x + ", y:" + neck.y + | |
" || torso, x:" + torso.x + ", y:" + torso.y);*/ | |
//draw circles | |
fill(255,0,0); | |
ellipse(head.x, head.y, 20, 20); | |
ellipse(neck.x, neck.y, 20, 20); | |
ellipse(torso.x, torso.y, 20, 20); | |
ellipse(leftShoulder.x, leftShoulder.y, 20, 20); | |
ellipse(rightShoulder.x, rightShoulder.y, 20, 20); | |
ellipse(leftElbow.x, leftElbow.y, 20, 20); | |
ellipse(rightElbow.x, rightElbow.y, 20, 20); | |
ellipse(leftHand.x, leftHand.y, 20, 20); | |
ellipse(rightHand.x, rightHand.y, 20, 20); | |
ellipse(leftHip.x, leftHip.y, 20, 20); | |
ellipse(rightHip.x, rightHip.y, 20, 20); | |
ellipse(leftKnee.x, leftKnee.y, 20, 20); | |
ellipse(rightKnee.x, rightKnee.y, 20, 20); | |
} | |
void drawLimb(int userId){ | |
context.drawLimb(userId, SimpleOpenNI.SKEL_HEAD, SimpleOpenNI.SKEL_NECK); | |
context.drawLimb(userId, SimpleOpenNI.SKEL_NECK, SimpleOpenNI.SKEL_LEFT_SHOULDER); | |
context.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_SHOULDER, SimpleOpenNI.SKEL_LEFT_ELBOW); | |
context.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_ELBOW, SimpleOpenNI.SKEL_LEFT_HAND); | |
context.drawLimb(userId, SimpleOpenNI.SKEL_NECK, SimpleOpenNI.SKEL_RIGHT_SHOULDER); | |
context.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_SHOULDER, SimpleOpenNI.SKEL_RIGHT_ELBOW); | |
context.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_ELBOW, SimpleOpenNI.SKEL_RIGHT_HAND); | |
context.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_SHOULDER, SimpleOpenNI.SKEL_TORSO); | |
context.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_SHOULDER, SimpleOpenNI.SKEL_TORSO); | |
context.drawLimb(userId, SimpleOpenNI.SKEL_TORSO, SimpleOpenNI.SKEL_LEFT_HIP); | |
context.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_HIP, SimpleOpenNI.SKEL_LEFT_KNEE); | |
context.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_KNEE, SimpleOpenNI.SKEL_LEFT_FOOT); | |
context.drawLimb(userId, SimpleOpenNI.SKEL_TORSO, SimpleOpenNI.SKEL_RIGHT_HIP); | |
context.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_HIP, SimpleOpenNI.SKEL_RIGHT_KNEE); | |
context.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_KNEE, SimpleOpenNI.SKEL_RIGHT_FOOT); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SimpleOpenNI.*; | |
SimpleOpenNI context; | |
Table table; | |
String recordPath = "nameofthevideofile.oni"; | |
PVector head = new PVector(); | |
PVector neck = new PVector(); | |
PVector torso = new PVector(); | |
PVector leftShoulder = new PVector(); | |
PVector rightShoulder = new PVector(); | |
PVector leftElbow = new PVector(); | |
PVector rightElbow = new PVector(); | |
PVector leftHand = new PVector(); | |
PVector rightHand = new PVector(); | |
PVector leftHip = new PVector(); | |
PVector rightHip = new PVector(); | |
PVector rightKnee = new PVector(); | |
PVector leftKnee = new PVector(); | |
void setup() { | |
size(2 * 640 + 10, 480); | |
frameRate(24); | |
background (200, 0, 0); | |
smooth(); | |
setup_kinect(); | |
//create table w/ the position of the junctions | |
table = new Table(); | |
table.addColumn("timestamp"); | |
table.addColumn("framecount"); | |
table.addColumn("head.x"); | |
table.addColumn("head.y"); | |
table.addColumn("neck.x"); | |
table.addColumn("neck.y"); | |
table.addColumn("torso.x"); | |
table.addColumn("torso.y"); | |
table.addColumn("leftShoulder.x"); | |
table.addColumn("leftShoulder.y"); | |
table.addColumn("rightShoulder.x"); | |
table.addColumn("rightShoulder.y"); | |
table.addColumn("leftElbow.x"); | |
table.addColumn("leftElbow.y"); | |
table.addColumn("rightElbow.x"); | |
table.addColumn("rightElbow.y"); | |
table.addColumn("leftHand.x"); | |
table.addColumn("leftHand.y"); | |
table.addColumn("rightHand.x"); | |
table.addColumn("rightHand.y"); | |
table.addColumn("leftHip.x"); | |
table.addColumn("leftHip.y"); | |
table.addColumn("rightHip.x"); | |
table.addColumn("rightHip.y"); | |
table.addColumn("leftKnee.x"); | |
table.addColumn("leftKnee.y"); | |
table.addColumn("rightKnee.x"); | |
table.addColumn("rightKnee.y"); | |
table.addColumn("depthMap"); | |
} | |
void draw() { | |
// println(frameCount); | |
//update | |
context.update(); | |
int[] userList = context.getUsers(); | |
//get distance from the camera: | |
int[] dmap = context.depthMap(); | |
int dsize = context.depthMapSize(); | |
println("depthMap" + dmap[0]); | |
println("depthMapSize" + dsize); | |
//draw the cam data | |
if ((context.nodes() & SimpleOpenNI.NODE_DEPTH) != 0) | |
{ | |
if ((context.nodes() & SimpleOpenNI.NODE_IMAGE) != 0) { | |
image(context.depthImage(), 0, 0); | |
image(context.rgbImage(), context.depthWidth() + 10, 0); | |
for (int i = 0; i < userList.length; i++) { | |
if (context.isTrackingSkeleton(userList[i])) { | |
drawSkeleton(userList[i]); | |
// Fill in table with data changes during draw | |
TableRow newRow = table.addRow(); | |
newRow.setInt("timestamp", millis()); | |
newRow.setInt("framecount", frameCount); | |
newRow.setFloat("head.x", head.x); | |
newRow.setFloat("head.y", head.y); | |
newRow.setFloat("neck.x", neck.x); | |
newRow.setFloat("neck.y", neck.y); | |
newRow.setFloat("torso.x", torso.x); | |
newRow.setFloat("torso.y", torso.y); | |
newRow.setFloat("leftShoulder.x", leftShoulder.x); | |
newRow.setFloat("leftShoulder.y", leftShoulder.y); | |
newRow.setFloat("rightShoulder.x", rightShoulder.x); | |
newRow.setFloat("rightShoulder.y", rightShoulder.y); | |
newRow.setFloat("leftElbow.x", leftElbow.x); | |
newRow.setFloat("leftElbow.y", leftElbow.y); | |
newRow.setFloat("rightElbow.x", rightElbow.x); | |
newRow.setFloat("rightElbow.y", rightElbow.y); | |
newRow.setFloat("leftHand.x", leftHand.x); | |
newRow.setFloat("leftHand.y", leftHand.y); | |
newRow.setFloat("rightHand.x", rightHand.x); | |
newRow.setFloat("rightHand.y", rightHand.y); | |
newRow.setFloat("leftHip.x", leftHip.x); | |
newRow.setFloat("leftHip.y", leftHip.y); | |
newRow.setFloat("rightHip.x", rightHip.x); | |
newRow.setFloat("rightHip.y", rightHip.y); | |
newRow.setFloat("leftKnee.x", leftKnee.x); | |
newRow.setFloat("leftKnee.y", leftKnee.y); | |
newRow.setFloat("rightKnee.x", rightKnee.x); | |
newRow.setFloat("rightKnee.y", rightKnee.y); | |
newRow.setInt("depthMap", dmap[0]); | |
} | |
} | |
} | |
} | |
} | |
void keyPressed() { | |
//Save table | |
if (key == 's') { | |
saveTable(table, "data/nameofthespreadsheet.csv"); | |
} | |
// Create Marker | |
/*if ( key == 'r') { | |
TableRow newRow = table.addRow(); | |
newRow.setInt("timestamp", millis()); | |
newRow.setInt("framecount", frameCount); | |
newRow.setFloat("head.x", 0); | |
newRow.setFloat("head.y", 0); | |
newRow.setFloat("neck.x", 0); | |
newRow.setFloat("neck.y", 0); | |
newRow.setFloat("torso.x", 0); | |
newRow.setFloat("torso.y", 0); | |
newRow.setFloat("leftShoulder.x", 0); | |
newRow.setFloat("leftShoulder.y", 0); | |
newRow.setFloat("rightShoulder.x", 0); | |
newRow.setFloat("rightShoulder.y", 0); | |
newRow.setFloat("leftElbow.x", 0); | |
newRow.setFloat("leftElbow.y", 0); | |
newRow.setFloat("rightElbow.x", 0); | |
newRow.setFloat("rightElbow.y", 0); | |
newRow.setFloat("leftHand.x", 0); | |
newRow.setFloat("leftHand.y", 0); | |
newRow.setFloat("rightHand.x", 0); | |
newRow.setFloat("rightHand.y", 0); | |
newRow.setFloat("leftHip.x", 0); | |
newRow.setFloat("leftHip.y", 0); | |
newRow.setFloat("rightHip.x", 0); | |
newRow.setFloat("rightHip.y", 0); | |
newRow.setFloat("leftKnee.x", 0); | |
newRow.setFloat("leftKnee.y", 0); | |
newRow.setFloat("rightKnee.x", 0); | |
newRow.setFloat("rightKnee.y", 0); | |
newRow.setInt("depthMap", dmap[0]); | |
newRow.setInt("rightKnee.y", dsize); | |
}*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment