Skip to content

Instantly share code, notes, and snippets.

@tatsuro-ueda
Created January 31, 2012 05:56
Show Gist options
  • Save tatsuro-ueda/1709103 to your computer and use it in GitHub Desktop.
Save tatsuro-ueda/1709103 to your computer and use it in GitHub Desktop.
simple-OpenNI sample Hands3d
// 深度カメラの映像を3次元描画し、手を検出してその軌跡を描画する
import SimpleOpenNI.*;
//import processing.opengl.*;
SimpleOpenNI context;
float zoomF =0.5f;
float rotX = radians(180); // デフォルトでは視界をx軸に対して180度回転させる
// OpenNIのデータは反対から来る
float rotY = radians(0);
boolean handsTrackFlag = false;
PVector handVec = new PVector();
ArrayList handVecList = new ArrayList();
int handVecListSize = 30;
String lastGesture = "";
void setup()
{
size(1024,768,P3D); // strange, get drawing error in the cameraFrustum if i use P3D, in opengl there is no problem
//size(1024,768,OPENGL);
context = new SimpleOpenNI(this);
// 鏡像にしない
context.setMirror(false);
// 深度カメラを有効にする
if(context.enableDepth() == false)
{
println("Can't open the depthMap, maybe the camera is not connected!");
exit();
return;
}
// 手の検出とジェスチャー検出を有効にする
context.enableGesture();
context.enableHands();
// 個別のジェスチャーを追加する / Macだとうまくいかず、raiseHandしか追加できない?CPUの問題?
context.addGesture("Wave");
context.addGesture("Click");
context.addGesture("RaiseHand");
// 手のキャプチャーをどれくらいなめらかにするかを設定する
//context.setSmoothingHands(.5);
stroke(255,255,255);
smooth();
perspective(radians(45),
float(width)/float(height),
10.0f,150000.0f);
}
void draw()
{
// カメラ情報を更新する
context.update();
background(0,0,0);
// シーンの位置を設定する
translate(width/2, height/2, 0);
rotateX(rotX);
rotateY(rotY);
scale(zoomF);
// 3次元の深度マップを描画する
int[] depthMap = context.depthMap();
int steps = 3; // 描画速度を上げるため、3点ごとに描画する
int index;
PVector realWorldPoint;
translate(0,0,-1000); // 回転中心をカメラの1000mm前に設定する
stroke(200);
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)
{
// 投射された点を描画する
realWorldPoint = context.depthMapRealWorld()[index];
point(realWorldPoint.x,realWorldPoint.y,realWorldPoint.z);
}
}
}
// 追跡した手を描画する
if(handsTrackFlag)
{
pushStyle();
stroke(255,0,0,200);
noFill();
Iterator itr = handVecList.iterator();
beginShape();
while( itr.hasNext() )
{
PVector p = (PVector) itr.next();
vertex(p.x,p.y,p.z);
}
endShape();
stroke(255,0,0);
strokeWeight(4);
point(handVec.x,handVec.y,handVec.z);
popStyle();
}
// Kinectカメラを描画する
context.drawCamFrustum();
}
// -----------------------------------------------------------------
// 手のイベント
void onCreateHands(int handId,PVector pos,float time)
{
println("onCreateHands - handId: " + handId + ", pos: " + pos + ", time:" + time);
handsTrackFlag = true;
handVec = pos;
handVecList.clear();
handVecList.add(pos);
}
void onUpdateHands(int handId,PVector pos,float time)
{
//println("onUpdateHandsCb - handId: " + handId + ", pos: " + pos + ", time:" + time);
handVec = pos;
handVecList.add(0,pos);
if(handVecList.size() >= handVecListSize)
{ // 最後の点を削除する
handVecList.remove(handVecList.size()-1);
}
}
void onDestroyHands(int handId,float time)
{
println("onDestroyHandsCb - handId: " + handId + ", time:" + time);
handsTrackFlag = false;
context.addGesture(lastGesture);
}
// -----------------------------------------------------------------
// ジェスチャーイベント
void onRecognizeGesture(String strGesture, PVector idPosition, PVector endPosition)
{
println("onRecognizeGesture - strGesture: " + strGesture + ", idPosition: " + idPosition + ", endPosition:" + endPosition);
lastGesture = strGesture;
context.removeGesture(strGesture);
context.startTrackingHands(endPosition);
}
void onProgressGesture(String strGesture, PVector position,float progress)
{
//println("onProgressGesture - strGesture: " + strGesture + ", position: " + position + ", progress:" + progress);
}
// -----------------------------------------------------------------
// キーボードイベント
void keyPressed()
{
switch(key)
{
case ' ':
context.setMirror(!context.mirror());
break;
}
switch(keyCode)
{
case LEFT:
rotY += 0.1f;
break;
case RIGHT:
rotY -= 0.1f;
break;
case UP:
if(keyEvent.isShiftDown())
zoomF += 0.01f;
else
rotX += 0.1f;
break;
case DOWN:
if(keyEvent.isShiftDown())
{
zoomF -= 0.01f;
if(zoomF < 0.01)
zoomF = 0.01;
}
else
rotX -= 0.1f;
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment