Created
January 31, 2012 05:16
-
-
Save tatsuro-ueda/1708998 to your computer and use it in GitHub Desktop.
simple-OpenNI sample Hands
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
// 手の軌跡を描く。手の位置はWaveなどで検出する。 | |
import SimpleOpenNI.*; | |
SimpleOpenNI context; | |
// NITEモジュールを使う | |
XnVSessionManager sessionManager; | |
XnVFlowRouter flowRouter; | |
PointDrawer pointDrawer; | |
void setup() | |
{ | |
context = new SimpleOpenNI(this); | |
// デフォルトでは鏡像(左右反転) | |
context.setMirror(true); | |
// 深度カメラを有効にする | |
if(context.enableDepth() == false) | |
{ | |
println("Can't open the depthMap, maybe the camera is not connected!"); | |
exit(); | |
return; | |
} | |
// ジェスチャー検出と手検出を有効にする | |
context.enableGesture(); | |
context.enableHands(); | |
// NITEモジュールをセットアップする | |
sessionManager = context.createSessionManager("Click,Wave", "RaiseHand"); | |
pointDrawer = new PointDrawer(); | |
flowRouter = new XnVFlowRouter(); | |
flowRouter.SetActive(pointDrawer); | |
sessionManager.AddListener(flowRouter); | |
size(context.depthWidth(), context.depthHeight()); | |
smooth(); | |
} | |
void draw() | |
{ | |
background(200,0,0); | |
// カメラ情報を更新する | |
context.update(); | |
// NITEモジュールの諸検出を更新する | |
context.update(sessionManager); | |
// 深度映像を描画する | |
image(context.depthImage(),0,0); | |
// 手の軌跡を描画する | |
pointDrawer.draw(); | |
} | |
void keyPressed() | |
{ | |
switch(key) | |
{ | |
case 'e': | |
// 「e」が押されたらセッションを終える | |
sessionManager.EndSession(); | |
println("end session"); | |
break; | |
} | |
} | |
///////////////////////////////////////////////////////////////////////////////////////////////////// | |
// セッションのコールバック | |
void onStartSession(PVector pos) | |
{ | |
println("onStartSession: " + pos); | |
} | |
void onEndSession() | |
{ | |
println("onEndSession: "); | |
} | |
void onFocusSession(String strFocus,PVector pos,float progress) | |
{ | |
println("onFocusSession: focus=" + strFocus + ",pos=" + pos + ",progress=" + progress); | |
} | |
///////////////////////////////////////////////////////////////////////////////////////////////////// | |
// PointDrawerはhandpointsの追跡を継続する | |
class PointDrawer extends XnVPointControl | |
{ | |
HashMap _pointLists; | |
int _maxPoints; | |
color[] _colorList = { color(255,0,0),color(0,255,0),color(0,0,255),color(255,255,0)}; | |
public PointDrawer() | |
{ | |
_maxPoints = 30; | |
_pointLists = new HashMap(); | |
} | |
public void OnPointCreate(XnVHandPointContext cxt) | |
{ | |
// 手の軌跡を描き始める | |
addPoint(cxt.getNID(),new PVector(cxt.getPtPosition().getX(),cxt.getPtPosition().getY(),cxt.getPtPosition().getZ())); | |
println("OnPointCreate, handId: " + cxt.getNID()); | |
} | |
public void OnPointUpdate(XnVHandPointContext cxt) | |
{ | |
//println("OnPointUpdate " + cxt.getPtPosition()); | |
addPoint(cxt.getNID(),new PVector(cxt.getPtPosition().getX(),cxt.getPtPosition().getY(),cxt.getPtPosition().getZ())); | |
} | |
public void OnPointDestroy(long nID) | |
{ | |
println("OnPointDestroy, handId: " + nID); | |
// remove list | |
if(_pointLists.containsKey(nID)) | |
_pointLists.remove(nID); | |
} | |
public ArrayList getPointList(long handId) | |
{ | |
ArrayList curList; | |
if(_pointLists.containsKey(handId)) | |
curList = (ArrayList)_pointLists.get(handId); | |
else | |
{ | |
curList = new ArrayList(_maxPoints); | |
_pointLists.put(handId,curList); | |
} | |
return curList; | |
} | |
public void addPoint(long handId,PVector handPoint) | |
{ | |
ArrayList curList = getPointList(handId); | |
curList.add(0,handPoint); | |
if(curList.size() > _maxPoints) | |
curList.remove(curList.size() - 1); | |
} | |
public void draw() | |
{ | |
if(_pointLists.size() <= 0) | |
return; | |
pushStyle(); | |
noFill(); | |
PVector vec; | |
PVector firstVec; | |
PVector screenPos = new PVector(); | |
int colorIndex=0; | |
// 手の軌跡を描く | |
Iterator<Map.Entry> itrList = _pointLists.entrySet().iterator(); | |
while(itrList.hasNext()) | |
{ | |
strokeWeight(2); | |
stroke(_colorList[colorIndex % (_colorList.length - 1)]); | |
ArrayList curList = (ArrayList)itrList.next().getValue(); | |
// 線を描く | |
firstVec = null; | |
Iterator<PVector> itr = curList.iterator(); | |
beginShape(); | |
while (itr.hasNext()) | |
{ | |
vec = itr.next(); | |
if(firstVec == null) | |
firstVec = vec; | |
// 画面位置を計算する | |
context.convertRealWorldToProjective(vec,screenPos); | |
vertex(screenPos.x,screenPos.y); | |
} | |
endShape(); | |
// 現在の手の位置を描画する | |
if(firstVec != null) | |
{ | |
strokeWeight(8); | |
context.convertRealWorldToProjective(firstVec,screenPos); | |
point(screenPos.x,screenPos.y); | |
} | |
colorIndex++; | |
} | |
popStyle(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment