骨架跟踪&在处理

时间:2017-01-12 19:34:07

标签: video processing kinect simple-openni

我们使用以下代码进行骨架追踪(头部追踪)和同时播放视频:

import processing.video.*;
import SimpleOpenNI.*;
import java.util.*;

SimpleOpenNI  kinect;
PImage kinectDepth;
int[] userID;

color[] userColor = new color[]{ color(255,0,0), color(0,255,0), color(0,0,255),
                                 color(255,255,0), color(255,0,255), color(0,255,255)};
 PVector headPosition = new PVector();
float headSize = 200;
float confidenceLevel = 0.5;
float confidence;
PVector confidenceVector = new PVector();
Movie movie1;

void setup()
{
  size(640, 480);
  movie1 = new Movie(this, "moon.mP4");
  kinect = new SimpleOpenNI(this);
   kinect.enableDepth();
  kinect.enableUser();
    movie1.play();
} 

void draw(){
  image(movie1, 0, 0, width, height);
  kinect.update();
  kinectDepth = kinect.depthImage();
  image(kinectDepth,0,0); 
  userID = kinect.getUsers();

  for(int i=0;i<userID.length;i++)
  {
    if(kinect.isTrackingSkeleton(userID[i]))
    {
      confidence = kinect.getJointPositionSkeleton(userID[i],SimpleOpenNI.SKEL_HEAD,confidenceVector);

      if(confidence > confidenceLevel)
      {
        // change draw color based on hand id#
        stroke(userColor[(i)]);
        // fill the ellipse with the same color
        fill(userColor[(i)]);
        // draw the rest of the body
        drawSkeleton(userID[i]);

      } 
    }
  } 
} 

/*---------------------------------------------------------------
Draw the skeleton of a tracked user.  Input is userID
----------------------------------------------------------------*/
void drawSkeleton(int userId){
  kinect.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_HEAD,headPosition);
  kinect.convertRealWorldToProjective(headPosition,headPosition);
  ellipse(headPosition.x,headPosition.y,30,30);
} 

void onNewUser(SimpleOpenNI curContext, int userId){
  println("New User Detected - userId: " + userId);
  curContext.startTrackingSkeleton(userId);
} 


void onLostUser(SimpleOpenNI curContext, int userId){
  println("User Lost - userId: " + userId);
} 

void onVisibleUser(SimpleOpenNI curContext, int userId){
} //void onVisibleUser(SimpleOpenNI curContext, int userId)


void movieEvent(Movie m) {
  m.read();
}

当我们绑定运行上面的代码时,日志文件中生成了以下错误:

  

Java框架:( J =已编译的Java代码,j =已解释,Vv = VM代码)       J 1472 SimpleOpenNI.SimpleOpenNIJNI.IntVector_size(JLSimpleOpenNI / IntVector;)J(0字节)@ 0x0000000002ebe695 [0x0000000002ebe640 + 0x55]       J 1471 C1 SimpleOpenNI.IntVector.size()J(8字节)@ 0x0000000002ebe314 [0x0000000002ebe280 + 0x94]       j SimpleOpenNI.SimpleOpenNI.getUsers()[I + 15       J 1777 C1 skeleton_track_simpleopen_video.draw()V(159字节)@ 0x0000000003004ca4 [0x0000000003004600 + 0x6a4]       j processing.core.PApplet.handleDraw()V + 161       J 1769 C1 processing.awt.PSurfaceAWT $ 12.callDraw()V(18字节)@ 0x000000000300009c [0x0000000002ffff80 + 0x11c]       j processing.core.PSurfaceNone $ AnimationThread.run()V + 30       v~StubRoutines :: call_stub

值得注意的是,在没有播放视频(processing.video库)的情况下,上述代码运行时没有任何错误。

是否可以帮助我们在上面的代码中找到问题?

1 个答案:

答案 0 :(得分:0)

这确实是一种奇怪的行为,但是要深究这一点,可能有必要从源代码编译SimpleOpenNI库,调试它的getUsers()方法正在使无效的内存引用。

如果您只想进行一些测试并开展工作,这可能并不实用。 我建议不要使用getUsers()方法。你可能会使用getNumberOfUsers()

逃脱
import processing.video.*;
import SimpleOpenNI.*;
import java.util.*;

SimpleOpenNI  kinect;
PImage kinectDepth;
int[] userID;

color[] userColor = new color[]{ color(255,0,0), color(0,255,0), color(0,0,255),
                                 color(255,255,0), color(255,0,255), color(0,255,255)};
 PVector headPosition = new PVector();
float headSize = 200;
float confidenceLevel = 0.5;
float confidence;
PVector confidenceVector = new PVector();
Movie movie1;

void setup()
{
  size(640, 480);
  movie1 = new Movie(this, "moon.mP4");
  kinect = new SimpleOpenNI(this,"/Users/George/Downloads/gaf/as/CityWall/oni/test2.oni");
   kinect.enableDepth();
  kinect.enableUser();
    movie1.loop();
} 

void draw(){
  kinect.update();
  kinectDepth = kinect.depthImage();
  image(kinectDepth,0,0); 
  //userID = kinect.getUsers();

  for(int i=0;i<kinect.getNumberOfUsers();i++)
  {
    if(kinect.isTrackingSkeleton(i+1))
    {
      confidence = kinect.getJointPositionSkeleton(i+1,SimpleOpenNI.SKEL_HEAD,confidenceVector);

      if(confidence > confidenceLevel)
      {
        // change draw color based on hand id#
        stroke(userColor[(i)]);
        // fill the ellipse with the same color
        fill(userColor[(i)]);
        // draw the rest of the body
        drawSkeleton(i+1);

      } 
    }
  } 
  image(movie1, 0, 0, movie1.width/4, movie1.height/4);
} 

/*---------------------------------------------------------------
Draw the skeleton of a tracked user.  Input is userID
----------------------------------------------------------------*/
void drawSkeleton(int userId){
  kinect.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_HEAD,headPosition);
  kinect.convertRealWorldToProjective(headPosition,headPosition);
  ellipse(headPosition.x,headPosition.y,30,30);
} 

void onNewUser(SimpleOpenNI curContext, int userId){
  println("New User Detected - userId: " + userId);
  curContext.startTrackingSkeleton(userId);
} 


void onLostUser(SimpleOpenNI curContext, int userId){
  println("User Lost - userId: " + userId);
} 

void onVisibleUser(SimpleOpenNI curContext, int userId){
} //void onVisibleUser(SimpleOpenNI curContext, int userId)


void movieEvent(Movie m) {
  m.read();
}

demo

请记住,这会告诉您有多少用户被跟踪,但不知道他们的ID是什么。而不是getNumberOfUsers()你甚至可以使用int,让我们15,可以是OpenNI支持的最大用户数。这样可行,因为您总是在检查kinect.isTrackingSkeleton