Why doesn't two different PFrames work?

edited March 2014 in Questions about Code

Working on the Staying alive code from the book Making things see, I've got two poses (pose2 and pose3). So I want start a movie when pose2 is true and load a picture while pose3 is true. When I execute the following code, then it loads the picture even when I don't do the right pose, after user-detection thirdApplet is always executed…

//part of the code
    void draw() {
      background(0);
      context.update();
      image(context.depthImage(), 0, 0);
      IntVector userList = new IntVector();
      context.getUsers(userList);
      if (userList.size() > 0) {
        int userId = userList.get(0);
        if ( context.isTrackingSkeleton(userId)) {
          // check to see if the user
          // is in the second pose
            if (pose2.check(userId)) {
            // if they are, set the color yellow
            stroke(255,255,0);
            // open second frame with movie
            if(f==null)f = new PFrame();
          } else {
          // check to see if the user
          // is in the third pose
            if (pose3.check(userId)) {
            //if they are, set the color yellow
            stroke(255,255,0);
            // open second frame with the image
            if(g==null)g = new PFrame2();
          }  else {
            // otherwise set the color to red
            // and don’t start a thing
            stroke(255, 0, 0);
            }
          // draw the skeleton in whatever color we chose
          drawSkeleton(userId);
         }
       }
     }
    }

public class PFrame extends JFrame {
    public PFrame() {
        setBounds(2048,480,1920,1080);
        frame1 = new secondApplet();
        add(frame1);
        frame1.init();
        show();
        setTitle("This is war");
    }
}

public class PFrame2 extends JFrame {
    public PFrame2() {
        setBounds(2048,480,1920,1080);
        frame2 = new thirdApplet();
        add(frame2);
        frame2.init();
        show();
        setTitle("This is hope");
    }
}    

public class secondApplet extends PApplet {
  public void setup() {
    size(1920, 1080);
    //noLoop();
    myMovie = new Movie(this, "/Users/nomac/Documents/Kinect project/sketch_140326/data/war.mov");
    }

    // Called every time a new frame is available to read
    void movieEvent(Movie m) {
    m.read();
    }

    public void draw() {
    myMovie.play();
    // add this from forum.processing.org/two/discussion/comment/6789
    image(myMovie, 0, 0, 1920, 1080);
    }   
}

public class thirdApplet extends PApplet {
  public void setup() {
    size(1920, 1080);
    img = loadImage("/Users/nomac/Documents/Kinect project/sketch_140326/data/hope.jpg");
    }

    public void draw() {
    image(img, 0, 0, 1920, 1080);
    }   
}
Tagged:

Answers

  • This must work, it's inside the other code:

    import SimpleOpenNI.*;
    import javax.swing.JFrame;
    PFrame2 g = null;
    thirdApplet frame2;
    PImage img;
    
    // declare our poser objects
    SkeletonPoser pose3;
    SimpleOpenNI context;
    
    void setup() {
      size(640, 480);
      context = new SimpleOpenNI(this);
      if (context.isInit() == false)
      {
        println("Can't init SimpleOpenNI, maybe the camera is not connected!"); 
        exit();
        return;
      }
      context.setMirror(true);
      context.enableDepth();
      context.enableUser();
    
      // initialize the pose object
      pose3 = new SkeletonPoser(context);
    
      // pose3
      // rules for the right arm
      pose3.addRule(SimpleOpenNI.SKEL_RIGHT_ELBOW, PoseRule.RIGHT_OF, SimpleOpenNI.SKEL_RIGHT_SHOULDER);
      pose3.addRule(SimpleOpenNI.SKEL_RIGHT_HAND, PoseRule.RIGHT_OF, SimpleOpenNI.SKEL_RIGHT_ELBOW);
      pose3.addRule(SimpleOpenNI.SKEL_RIGHT_HAND, PoseRule.RIGHT_OF, SimpleOpenNI.SKEL_RIGHT_SHOULDER);
      // rules for the left arm
      pose3.addRule(SimpleOpenNI.SKEL_LEFT_ELBOW, PoseRule.LEFT_OF, SimpleOpenNI.SKEL_LEFT_SHOULDER);
      pose3.addRule(SimpleOpenNI.SKEL_LEFT_HAND, PoseRule.LEFT_OF, SimpleOpenNI.SKEL_LEFT_ELBOW);
        pose3.addRule(SimpleOpenNI.SKEL_LEFT_HAND, PoseRule.LEFT_OF, SimpleOpenNI.SKEL_LEFT_SHOULDER);
    
      // rules for the right leg
      pose3.addRule(SimpleOpenNI.SKEL_RIGHT_KNEE, PoseRule.BELOW, SimpleOpenNI.SKEL_RIGHT_HIP);
      pose3.addRule(SimpleOpenNI.SKEL_RIGHT_FOOT, PoseRule.BELOW, SimpleOpenNI.SKEL_RIGHT_HIP);
      pose3.addRule(SimpleOpenNI.SKEL_RIGHT_FOOT, PoseRule.BELOW, SimpleOpenNI.SKEL_RIGHT_KNEE);
      // rules for the left leg
      pose3.addRule(SimpleOpenNI.SKEL_LEFT_FOOT, PoseRule.BELOW, SimpleOpenNI.SKEL_LEFT_KNEE);
      pose3.addRule(SimpleOpenNI.SKEL_LEFT_FOOT, PoseRule.BELOW, SimpleOpenNI.SKEL_LEFT_HIP);
      pose3.addRule(SimpleOpenNI.SKEL_LEFT_KNEE, PoseRule.BELOW, SimpleOpenNI.SKEL_LEFT_HIP);
      strokeWeight(3);
    }
    
    void draw() {
      background(0);
      context.update();
      image(context.depthImage(), 0, 0);
      IntVector userList = new IntVector();
      context.getUsers(userList);
      if (userList.size() > 0) {
        int userId = userList.get(0);
        if ( context.isTrackingSkeleton(userId)) {
          // check to see if the user
          // is in the third pose
            if (pose3.check(userId)) {
            //if they are, set the color yellow
            stroke(255,255,0);
            // open second frame with the image
            if(g==null)g = new PFrame2();
          }  else {
            // otherwise set the color to red
            // and don’t start a thing
            stroke(255, 0, 0);
            }
          // draw the skeleton in whatever color we chose
          drawSkeleton(userId);
         }
       }
    }
    
    public class PFrame2 extends JFrame {
        public PFrame2() {
            setBounds(2048,480,1920,1080);
            frame2 = new thirdApplet();
            add(frame2);
            frame2.init();
            show();
            setTitle("This is hope");
        }
    }
    
    public class thirdApplet extends PApplet {
      public void setup() {
        size(1920, 1080);
        img = loadImage("/Users/nomac/Documents/Kinect project/sketch_140326/data/hope.jpg");
        }
    
        public void draw() {
        image(img, 0, 0, 1920, 1080);
        }   
    }
    
    void drawSkeleton(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);
      context.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_HIP, SimpleOpenNI.SKEL_LEFT_HIP);
    }
    
    void drawLimb(int userId, int jointType1, int jointType2)
    {
      PVector jointPos1 = new PVector();
      PVector jointPos2 = new PVector();
      float confidence;
      // draw the joint position
      confidence = context.getJointPositionSkeleton(userId, jointType1, jointPos1);
      confidence = context.getJointPositionSkeleton(userId, jointType2, jointPos2);
      line(jointPos1.x, jointPos1.y, jointPos1.z, jointPos2.x, jointPos2.y, jointPos2.z);
    }
    
    // user-tracking callbacks!
    void onNewUser(SimpleOpenNI curContext, int userId) {
      println("start pose detection");
      curContext.startTrackingSkeleton(userId);
    }
    

    For some reason, when user detect (without doing the pose), the thirdApplet is executed. Anybody an idea?

  • edited March 2014 Answer ✓

    Felt so stupid: it was the pose3-discription who triggered the thirdApplet. Now changed it to:

      // pose3
      // rules for the right arm
      pose3.addRule(SimpleOpenNI.SKEL_RIGHT_ELBOW, PoseRule.RIGHT_OF, SimpleOpenNI.SKEL_RIGHT_SHOULDER);
      pose3.addRule(SimpleOpenNI.SKEL_RIGHT_HAND, PoseRule.RIGHT_OF, SimpleOpenNI.SKEL_RIGHT_ELBOW);
      pose3.addRule(SimpleOpenNI.SKEL_RIGHT_HAND, PoseRule.ABOVE, SimpleOpenNI.SKEL_NECK);
      // rules for the left arm
      pose3.addRule(SimpleOpenNI.SKEL_LEFT_ELBOW, PoseRule.LEFT_OF, SimpleOpenNI.SKEL_LEFT_SHOULDER);
      pose3.addRule(SimpleOpenNI.SKEL_LEFT_HAND, PoseRule.LEFT_OF, SimpleOpenNI.SKEL_LEFT_ELBOW);
      pose3.addRule(SimpleOpenNI.SKEL_LEFT_HAND, PoseRule.ABOVE, SimpleOpenNI.SKEL_NECK);
    
      // rules for the right leg
      pose3.addRule(SimpleOpenNI.SKEL_RIGHT_KNEE, PoseRule.BELOW, SimpleOpenNI.SKEL_RIGHT_HIP);
      pose3.addRule(SimpleOpenNI.SKEL_RIGHT_FOOT, PoseRule.BELOW, SimpleOpenNI.SKEL_RIGHT_HIP);
      pose3.addRule(SimpleOpenNI.SKEL_RIGHT_FOOT, PoseRule.BELOW, SimpleOpenNI.SKEL_RIGHT_KNEE);
      // rules for the left leg
      pose3.addRule(SimpleOpenNI.SKEL_LEFT_FOOT, PoseRule.BELOW, SimpleOpenNI.SKEL_LEFT_KNEE);
      pose3.addRule(SimpleOpenNI.SKEL_LEFT_FOOT, PoseRule.BELOW, SimpleOpenNI.SKEL_LEFT_HIP);
      pose3.addRule(SimpleOpenNI.SKEL_LEFT_KNEE, PoseRule.BELOW, SimpleOpenNI.SKEL_LEFT_HIP);
      strokeWeight(3);
    

    and the code above works. Case closed ;-)

Sign In or Register to comment.