Progaming Kinect Sederhana - Prosessing


Ini Contoh Program sederhana dengan prosessing menggunakan Simple Open Ni
import SimpleOpenNI.*;
SimpleOpenNI      context;
// memanggil NITE Lib

XnVSessionManager sessionManager;
XnVFlowRouter     flowRouter;

PointDrawer       pointDrawer;
void setup()
{
  context = new SimpleOpenNI(this);
  context.setMirror(true);
 
  // Mengecek Kedalaman - untuk cek kinect

  if(context.enableDepth() == false)
  {
     println("Can't open the depthMap, Kinek belum terkoneksi");
     exit();
     return;
  }
 
  // enable the hands + gesture
  context.enableGesture();
  context.enableHands();

  // setup 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();
}
// setting kinect
void draw()
{
  background(200,0,0);
  // update the cam
  context.update();
 
  // update nite
  context.update(sessionManager);
 
  // draw depthImageMap
  image(context.depthImage(),0,0);
 


// draw the list
pointDrawer.draw(); }
void keyPressed()
{
switch(key)
{

case 'e':// end sessions
    sessionManager.EndSession();
    println("end session");
    break;
  }
}

// session
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);
}


// Interface
// PMenggambar Pointnter dan vector garis


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)
  {
 
    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;
     
      // draw Handlist
      Iterator itrList = _pointLists.entrySet().iterator();
      while(itrList.hasNext())
      {
        strokeWeight(2);
        stroke(_colorList[colorIndex % (_colorList.length - 1)]);

        ArrayList curList = (ArrayList)itrList.next().getValue();    
       
        // draw line
        firstVec = null;
        Iterator itr = curList.iterator();
        beginShape();
          while (itr.hasNext())
          {
            vec = itr.next();
            if(firstVec == null)
              firstVec = vec;
             
            // calc the screen pos
            context.convertRealWorldToProjective(vec,screenPos);
            vertex(screenPos.x,screenPos.y);   
          }
        endShape();  
 
        // draw pos Tangan
        if(firstVec != null)
        {
          strokeWeight(8);
          context.convertRealWorldToProjective(firstVec,screenPos);
          point(screenPos.x,screenPos.y);
        }
        colorIndex++;
      }
     
    popStyle();
  }

}


Hasil Dari Program Bila DIjalankan Adalah Pointer Yang melekat pada tangan dan bila digerakan adakan menghasilkan garis merah yang mengikuti tangan ^^ silahakan dicoba

======================================= Don't forge Like us And Give Comment

Your support is valuable to me
Searching for

No comments:

Post a Comment