how to make the image appear for a few moment ?

edited July 2016 in Kinect

I have this code but I can't figure how to make the image appear for more time in the screen .. So it should not disappear quickly ..Can you help please?

for (int i = 0; i < fullbody.length; i++) {
  println(fullbody[i].x + "," + fullbody[i].y);
  image(myImageArray[(int) random(10)],fullbody[i].x, fullbody[i].y, fullbody[i].width, fullbody[i].height);
  smooth();
  delay(10);
} 

Answers

  • edited July 2016

    draw() in itself loops 60 times per second.

    The screen is updated only once at the end of draw().

    So when this for-loop is within draw() you see only the last image, right?

    Instead delete the for-loop and implement i that is part of draw()

    int i=0; 
    
    void draw() { 
      //for (int i = 0; i < fullbody.length; i++) { 
    
      println(fullbody[i].x + "," + fullbody[i].y); 
    
      image(myImageArray[(int) random(10)], 
        fullbody[i].x, fullbody[i].y, fullbody[i].width, fullbody[i].height);
    
      smooth(); 
      delay(10);
    
      i++;
      if (i>=fullbody.length)
        i=0;
    
      // }
    }
    

    using delay() is not recommended

    you can use frameCount instead:

    int i=0; 
    
    void draw() { 
    
    
      println(i); 
    
    
      if (frameCount%99==0) {
        i++;
        if (i>=11)
          i=0;
      }
    }
    
  • still my images are not appear for a while .. its disappear quickly ..

  • edited June 2016

    this is my all code :

    import gab.opencv.*;
    import processing.video.*;
    import java.awt.*;
    
    PImage [] myImageArray = new PImage[10];
    PImage img0;
    PImage img1;
    PImage img2;
    PImage img3;
    PImage img4;
    PImage img5;
    PImage img6;
    PImage img7;
    PImage img8;
    PImage img9;
    int i =0;
    
    Object[] objects; 
    int currentObject=0;
    int x = 0;
    PImage[] object=new PImage[10];
    
    Capture video;
    OpenCV opencv;
    float threshold=50;
    
    void setup() {
      size(displayWidth, displayHeight);
      //fullScreen();
      // String[] cameras = Capture.list();
      video = new Capture(this, 640, 480);
      opencv = new OpenCV(this, 640, 480);
      opencv.loadCascade(OpenCV.CASCADE_FULLBODY);
      // opencv.startBackgroundSubtraction(5, 3, 0.5);
      video.start();
    
      myImageArray [0] = loadImage("0.png");
      myImageArray [1] = loadImage("1.png");
      myImageArray [2] = loadImage("2.png");
      myImageArray [3] = loadImage("3.png");
      myImageArray [4] = loadImage("4.png");
      myImageArray [5] = loadImage("4.png");
      myImageArray [6] = loadImage("6.png");
      myImageArray [7] = loadImage("7.png");
      myImageArray [8] = loadImage("8.png");
      myImageArray [9] = loadImage("9.png");
    
      //img0 = loadImage("0.png");
      //img1 = loadImage("1.png");
      //img2= loadImage("2.png");
      // img1 = loadImage("3.png");
      //  img1 = loadImage("4.png");
      //   img1 = loadImage("5.png");
      //    img1 = loadImage("6.png");
      //     img1 = loadImage("7.png");
      //      img1 = loadImage("8.png");
      //       img1 = loadImage("9.png");
    
      object[0]= img0;
      object[1]= img1;
      object[2]= img2;
      object[3]= img3;
      object[4]= img4;
      object[5]= img5;
      object[6]= img6;
      object[7]= img7;
      object[8]= img8;
      object[9]= img9;
    }
    
    void draw() {
      background(255);
    
      rect(x, height*0.2, 1, height*0.6); 
      //x = x + 2;
      video.filter(GRAY);
      video.filter(BLUR);
      video.filter(THRESHOLD);
      opencv.loadImage(video);
      image(video, 0, 0);
      Rectangle[] fullbody = opencv.detect();
      println(fullbody.length);
    
      lightFalloff(1.0, 0.00, 1.0);
      pointLight(150, 250, 150, 50, 50, 50);
      //beginShape();
      beginShape();
      // int idx=0;
      //idx++;
    
      for (int i = 0; i < fullbody.length; i++) {
        println(fullbody[i].x + "," + fullbody[i].y);
        image(myImageArray[(int) random(10)], fullbody[i].x, fullbody[i].y, fullbody[i].width, fullbody[i].height);
        smooth();
        //delay(10);
        i++;
        if (i>=fullbody.length)
          i=0;
      } 
      if (frameCount%99==0) {
        i++;
        if (i>=11)
          i=0;
      }
    }
    
    void captureEvent(Capture c) {
      c.read();
    }
    
  • ??

    you still have the for loop......

  • that for loop is a different thing - a list of detected bodies. they all have different positions so it's valid to draw them all

    there are bigger issues with the code though:

    what's the beginShape() do? you have no endShape()

    does any of the debug logging show anything?

    PImage [] myImageArray = new PImage[10];
    
    PImage img0;
    PImage img1;
    PImage img2;
    PImage img3;
    PImage img4;
    PImage img5;
    PImage img6;
    PImage img7;
    PImage img8;
    PImage img9;
    
    PImage[] object=new PImage[10];
    

    you need exactly 1 of these three things. use the first one, delete the others.

  • I didn't use the endShape and beginShape in my last update .. I did what you ask me is to delete the two and I just leave PImage [] myImageArray = new PImage[10];

    Its work .. but still all images appear only for a second and dissappear again .. do you have any idea ?

  • Post your entire code please

  • Sorry for that .. my code is above now..

  • And format the code Please

  • Is it done? I'm trying to format it 

  • Still looks bad

  • edited July 2016
    import gab.opencv.*;
    import processing.video.*;
    import java.awt.*;
    
    PImage [] myImageArray = new PImage[10];
    //PImage img0;
    //PImage img1;
    //PImage img2;
    //PImage img3;
    //PImage img4;
    //PImage img5;
    //PImage img6;
    //PImage img7;
    //PImage img8;
    //PImage img9;
    int i =0;
    
    
    Object[]objects; 
    int currentObject=0;
    int x = 0;
    //PImage[] object=new PImage[10];
    
    Capture video;
    OpenCV opencv;
    float threshold=50;
    
    void setup() {
     size(displayWidth, displayHeight);
     //fullScreen();
      // String[] cameras = Capture.list();
      video = new Capture(this, 640, 480);
      opencv = new OpenCV(this, 640, 480);
      opencv.loadCascade(OpenCV.CASCADE_FULLBODY);
    // opencv.startBackgroundSubtraction(5, 3, 0.5);
      video.start();
    
      myImageArray [0] = loadImage("0.png");
     myImageArray [1] = loadImage("1.png");
     myImageArray [2] = loadImage("2.png");
     myImageArray [3] = loadImage("3.png");
     myImageArray [4] = loadImage("4.png");
     myImageArray [5] = loadImage("4.png");
     myImageArray [6] = loadImage("6.png");
     myImageArray [7] = loadImage("7.png");
     myImageArray [8] = loadImage("8.png");
     myImageArray [9] = loadImage("9.png");
    
      //img0 = loadImage("0.png");
      //img1 = loadImage("1.png");
      //img2= loadImage("2.png");
      // img1 = loadImage("3.png");
      //  img1 = loadImage("4.png");
      //   img1 = loadImage("5.png");
      //    img1 = loadImage("6.png");
      //     img1 = loadImage("7.png");
      //      img1 = loadImage("8.png");
      //       img1 = loadImage("9.png");
    
    
       //object[0]= img0;
       //object[1]= img1;
       //object[2]= img2;
       //object[3]= img3;
       //object[4]= img4;
       //object[5]= img5;
       //object[6]= img6;
       //object[7]= img7;
       //object[8]= img8;
      //object[9]= img9;
    
    }
      void draw() {
        background(255);
    
      rect(x, height*0.2, 1, height*0.6); 
      //x = x + 2;
      video.filter(GRAY);
      video.filter(BLUR);
      video.filter(THRESHOLD);
        opencv.loadImage(video);
        image(video, 0,0);
         Rectangle[] fullbody = opencv.detect();
      println(fullbody.length);
    
      lightFalloff(1.0, 0.00, 1.0);
    pointLight(150, 250, 150, 50, 50, 50);
    //beginShape();
     // beginShape();
     // int idx=0;
      //idx++;
    
      for (int i = 0; i < fullbody.length; i++) {
        println(fullbody[i].x + "," + fullbody[i].y);
        image(myImageArray[(int) random(10)],fullbody[i].x, fullbody[i].y, fullbody[i].width, fullbody[i].height);
    smooth();
    loop();
    //delay(10);
    i++;
    if (i>=fullbody.length)
    i=0;
    
    } 
    println(i);
    if (frameCount%99==0) {
      i++;
      if(i>=11)
      i=0;
    }
     }
       void captureEvent(Capture c) {
      c.read();
    }
    
      
    
  • How is it now? sorry I don't know whats happened its not commented well.. What I have to do?

  • ??

    Go back edit your post

    Hit ctrl-a

    ctrl-o

    And save

  • Thank you .. its done :\">

  • Solved?

  • not yet :(

  • Sorry, i am on a Journey

  • =(( I really need help

  • Comment 1: Try to use different variable names in your code. You have a global and local variable i (line 16 and 93). I am not sure if you realize that those variables might not be doing what you want in line 100 and 105. I would recommend to rename the global variable j and adjust your code accordingly.

    Comment 2: Line 99-101 will make the for loop run forever - I think. When the last element of the for loop is reached, you are forcing it to go over the limit and then resetting the loop counter i back to zero. Having this for loop that runs without control in your draw would make your app behave erratically. Does it run for you? I suggest not to modify the for loop counter unless you have to.

    Comment 3: Line 97 should be move to your setup function. I am not sure what you are trying to do with this line though... I think it is valid only if you call noLoop first which you don't in your code.

    To hold the images for few seconds in your display you could do it either using frame counts as previously suggested or by keeping track of milliseconds passed.

    int endTime;
    int holdTime=2000;  // 2 secs
    
    void setup(){
    
       endTime=millis()+holdTime;  //Init
    
      //Other details skipped...
    
       image(video, 0,0);
    }
    
    void draw(){
    
      //Next line checks if time expired...
      //if it expired, load a new image
      if(millis()>endTime){
        endTime=millis() + holdTime;  
        image(video, 0,0);
      }
    
    }
    
    void captureEvent(Capture c) {
      c.read();
    }
    

    This is a partial code. It is not tested. However it shows the main idea of how to hold an image from video for two seconds before the new one is updated. You could also use line 105 from your code to accomplish this but it is a bit more tricky. You are assuming your frame rate is 30 fps but if you are processing video and applying filters it might not be processing frames this fast. To keep things simple, let's assume you are running at 30 fps. To hold the image for two seconds, it means you have to update every 60 fps (since in 1 sec you have 30 frames, 2 secs you get 60 frames) Your line in your code will look like:

    if (frameCount%60==0) {
      //LOAD images here... it will happen only every 2 seconds
    }
    

    One last thing to keep in mind. You might no need to call the background function since it will erase the images and they will not be displayed for the duration you want but only for one frame (they will flash very rapidly). You should then removed line 74 if you want to get the end effect.

    I hope this helps.

    Kf

  • Use ctrl-t in the processing editor to nicely indent the code.

    And use {} around all blocks of code, even blocks that are a single line, to avoid confusion (especially when posting code to forums like this one that may mangle the indenting). It's easy to miss lines 101 and 108, for instance.

  • thanx all for your replay .. but please Kfrajer can you tell me what you mean in your first comment (comment1) and also where I have to put this exactly.. if (frameCount%60==0) { //LOAD images here... it will happen only every 2 seconds }

  • To comment 1: in line 93 you invent a var i which is known until line 103. but since it has the same name it overshadows you var i from line 16. therefore lines 99 to101 refer to thelocal i from the for loop and bot to the global i (which is probably the one you mean but don't use). Solution: in 16 name the var j and all where it is used j including lines 99 to 101 etc.

    The code you are referring is irrelevant; in fact the relevant code is the long code he posted with millis() imho

    Did you apply all his changes and test it? Or are you just asking questions? Back to work.

  • edited July 2016

    I tried everything in all comments but still I got an issue, the video capture is stopped for a seconds not images that should track my body movement.. i think I should load images here as he mention before :

    if (frameCount%60==0) {
      //LOAD images here... it will happen only every 2 seconds
    }
    

    but where I should put this exactly and should I delete myImageArray load from void setup() ?

  • i hope he doesn't mean LOAD images every 2 seconds in draw(), loading images is too expensive to do in draw().

    and get rid of / reduce the println() calls too - too much output can kill a sketch

    there's no need for lights, no need for smooth(), no need for loop() in what you're doing.

  • I don't know what did he mean exactly for that I'm asking for clarification here.. Thanx for your replay so I will update the code for what you asked me to delete

  • ??

    I already mentioned that those lines are not the lines you are looking for.

    Instead use his lines 15 to 20

  • What do you want to achieve? Display a Video AND your 10 images from the array?

  • I want my array Images to appear with my body detection and to stay for a seconds .. It is now appear but its quickly disappear and it make people feel that nothing is follow them and their body detection

  • So .. yes the video with images

  • post your entire code please

  • edited July 2016
        import gab.opencv.*;
        import processing.video.*;
        import java.awt.*;
    
        int endTime;
        int holdTime=2000; // 2sec
    
        PImage [] myImageArray = new PImage[10];
        //PImage img0;
        //PImage img1;
        //PImage img2;
        //PImage img3;
        //PImage img4;
        //PImage img5;
        //PImage img6;
        //PImage img7;
        //PImage img8;
        //PImage img9;
        int j =0;
    
    
        Object[]objects; 
        int currentObject=0;
        int x = 0;
        //PImage[] object=new PImage[10];
    
        Capture video;
        OpenCV opencv;
        float threshold=50;
    
        void setup() {
         size(displayWidth, displayHeight);
         //fullScreen();
    
          // String[] cameras = Capture.list();
          video = new Capture(this, 640, 480);
          opencv = new OpenCV(this, 640, 480);
          opencv.loadCascade(OpenCV.CASCADE_FULLBODY);
    
           endTime=millis()+holdTime;
           image(video, 0,0);
    
    
        // opencv.startBackgroundSubtraction(5, 3, 0.5);
          video.start();
    
          myImageArray [0] = loadImage("0.png");
         myImageArray [1] = loadImage("1.png");
         myImageArray [2] = loadImage("2.png");
         myImageArray [3] = loadImage("3.png");
         myImageArray [4] = loadImage("4.png");
         myImageArray [5] = loadImage("4.png");
         myImageArray [6] = loadImage("6.png");
         myImageArray [7] = loadImage("7.png");
         myImageArray [8] = loadImage("8.png");
         myImageArray [9] = loadImage("9.png");
    
    
        //loop();
    
        //img0 = loadImage("0.png");
          //img1 = loadImage("1.png");
          //img2= loadImage("2.png");
          // img1 = loadImage("3.png");
          //  img1 = loadImage("4.png");
          //   img1 = loadImage("5.png");
          //    img1 = loadImage("6.png");
          //     img1 = loadImage("7.png");
          //      img1 = loadImage("8.png");
          //       img1 = loadImage("9.png");
    
    
           //object[0]= img0;
           //object[1]= img1;
           //object[2]= img2;
           //object[3]= img3;
           //object[4]= img4;
           //object[5]= img5;
           //object[6]= img6;
           //object[7]= img7;
           //object[8]= img8;
          //object[9]= img9;
    
        }
          void draw() {
           // background(255);
          if (millis()>endTime) {
           endTime=millis() + holdTime;
           image(video,0,0);
    
          rect(x, height*0.2, 1, height*0.6); 
          //x = x + 2;
          video.filter(GRAY);
          video.filter(BLUR);
          video.filter(THRESHOLD);
            opencv.loadImage(video);
            image(video, 0,0);
             Rectangle[] fullbody = opencv.detect();
          println(fullbody.length);
    
          //lightFalloff(1.0, 0.00, 1.0);
        //pointLight(150, 250, 150, 50, 50, 50);
        //beginShape();
         // beginShape();
         // int idx=0;
          //idx++;
    
          for (int i = 0; j < fullbody.length; i++) {
            println(fullbody[i].x + "," + fullbody[i].y);
            image(myImageArray[(int) random(10)],fullbody[i].x, fullbody[i].y, fullbody[i].width, fullbody[i].height);
        //smooth();
    
        //delay(10);
        j++;
        if (i>=fullbody.length)
        j=0;
    
        } 
        println(j);
        if (frameCount%60==0) {
          j++;
          if(j>=11)
          j=0;
        }
        else{
        }
          }
         }
           void captureEvent(Capture c) {
          c.read();
        }
    
  • this is my entire code

  • edited July 2016

    you wrote:

    I want my array Images to appear with my body detection and to stay for a seconds

    are we talking about line 41 or 110 ??? OR 97 ????? OR 89 ???

    err.... delete line 41.

    89 and 97 : decide for one here. Delete the other

    question remains: (89/97) OR 110 ???

  • import gab.opencv.*;
    import processing.video.*;
    import java.awt.*;
    
    int endTime;
    int holdTime=2000; // 2sec
    
    PImage [] myImageArray = new PImage[10];
    int j = 0;
    
    Object[]objects; 
    int currentObject=0;
    int x = 0;
    //PImage[] object=new PImage[10];
    
    Capture video;
    OpenCV opencv;
    float threshold=50;
    
    // ---------------------------------------------------
    
    void setup() {
      size(displayWidth, displayHeight);
      //fullScreen();
    
      // String[] cameras = Capture.list();
      video = new Capture(this, 640, 480);
      opencv = new OpenCV(this, 640, 480);
      opencv.loadCascade(OpenCV.CASCADE_FULLBODY);
    
      endTime=millis()+holdTime;
      // image(video, 0, 0);
    
      // opencv.startBackgroundSubtraction(5, 3, 0.5);
      video.start();
    
      myImageArray [0] = loadImage("0.png");
      myImageArray [1] = loadImage("1.png");
      myImageArray [2] = loadImage("2.png");
      myImageArray [3] = loadImage("3.png");
      myImageArray [4] = loadImage("4.png");
      myImageArray [5] = loadImage("4.png");
      myImageArray [6] = loadImage("6.png");
      myImageArray [7] = loadImage("7.png");
      myImageArray [8] = loadImage("8.png");
      myImageArray [9] = loadImage("9.png");
    }
    
    // ---------------------------------------------------
    
    void draw() {
    
      background(255);
    
      video.filter(GRAY);
      video.filter(BLUR);
      video.filter(THRESHOLD);
      opencv.loadImage(video);
      image(video, 0, 0);
    
      Rectangle[] fullbody = opencv.detect();
    
      if (millis()>endTime) {
        endTime = millis() + holdTime;
    
        for (int i = 0; j < fullbody.length; i++) {
          // println(fullbody[i].x + "," + fullbody[i].y);
          image(myImageArray[(int) random(10)], 
            fullbody[i].x, fullbody[i].y, 
            fullbody[i].width, fullbody[i].height);
        } // for
      }// if
    }// draw() 
    
    // ---------------------------------------------------
    
    void captureEvent(Capture c) {
      c.read();
    }
    
  • 110 .. this I need to appear

  • ok, 110. copy that.

    how do you like my version?

  • sorry , what do you mean by your version? I think I did exactly what you told me but still stuck .. can you show me your last update for my code please

  • import gab.opencv.*;
    import processing.video.*;
    import java.awt.*;
    
    int endTime;
    int holdTime=2000; // 2sec
    
    PImage [] myImageArray = new PImage[10];
    int j = 0;
    
    Object[]objects; 
    int currentObject=0;
    int x = 0;
    //PImage[] object=new PImage[10];
    
    Capture video;
    OpenCV opencv;
    float threshold=50;
    
    // ---------------------------------------------------
    
    void setup() {
      size(displayWidth, displayHeight);
      //fullScreen();
    
      // String[] cameras = Capture.list();
      video = new Capture(this, 640, 480);
      opencv = new OpenCV(this, 640, 480);
      opencv.loadCascade(OpenCV.CASCADE_FULLBODY);
    
      endTime=millis()+holdTime;
      // image(video, 0, 0);
    
      // opencv.startBackgroundSubtraction(5, 3, 0.5);
      video.start();
    
      myImageArray [0] = loadImage("0.png");
      myImageArray [1] = loadImage("1.png");
      myImageArray [2] = loadImage("2.png");
      myImageArray [3] = loadImage("3.png");
      myImageArray [4] = loadImage("4.png");
      myImageArray [5] = loadImage("4.png");
      myImageArray [6] = loadImage("6.png");
      myImageArray [7] = loadImage("7.png");
      myImageArray [8] = loadImage("8.png");
      myImageArray [9] = loadImage("9.png");
    }
    
    // ---------------------------------------------------
    
    void draw() {
    
      background(255);
    
      video.filter(GRAY);
      video.filter(BLUR);
      video.filter(THRESHOLD);
      opencv.loadImage(video);
      image(video, 0, 0);
    
      Rectangle[] fullbody = opencv.detect();
    
      if (millis()>endTime) {
        endTime = millis() + holdTime;
    
        for (int i = 0; j < fullbody.length; i++) {
          // println(fullbody[i].x + "," + fullbody[i].y);
          image(myImageArray[(int) random(10)], 
            fullbody[i].x, fullbody[i].y, 
            fullbody[i].width, fullbody[i].height);
        } // for
      }// if
    }// draw() 
    
    // ---------------------------------------------------
    
    void captureEvent(Capture c) {
      c.read();
    }
    
  • wait....

    mine is not correct

    display line 68 throughout but change the images and positions only after some time

    if (millis()>endTime) {
         endTime = millis() + holdTime;
         // change the images and positions 
    }
    

    this means maybe you need to store an array with all (int) random(10) and maybe also fullbody[i].x, fullbody[i].y, fullbody[i].width, fullbody[i].height

  • here is a simple way to display images throughout and change them only every 2 seconds

    hope this helps

    // Title 
    final String programTitle = "Image Show"; 
    
    // Images
    // int imageToShow=1;
    
    // images with full position and size in a own class
    ImageWithPosition[] images = new ImageWithPosition[10];
    
    // the pure images to choose from in a random fashion 
    PImage[] img = new PImage[images.length]; 
    
    // Font 
    PFont font; 
    
    // define middle of the screen in X-direction 
    int HalfOfWidth ; 
    
    // timer 
    int endTime;
    int holdTime=2000; // 2sec
    
    // ==========================================================
    
    void setup () {
      // Init: 
      size( 1200, 700 );
      // Define middle of the screen in X-direction   
      HalfOfWidth=(width / 2); 
      // Create the font on the fly 
      font = createFont("Sans Serif", 80); 
      // Font   
      textFont(font);   
    
      // images 
      for (int i=0; i < images.length; i++) {
        img[i]    = loadImage(str(i)+".jpg");
        images[i] = new ImageWithPosition(img[i], 70+ 40*i, 80+20*i, img[i].width, img[i].height);
      }
      //
    } // setup
    //
    
    void draw () {
      // Main function 
      background(217);
    
      // HeadLine
      HeadLine(programTitle);
    
      if (millis()>endTime) {
        endTime = millis() + holdTime;
        // change the images and positions
        for (int i=0; i < images.length; i++) {
          int index = int(random(10)); 
          // println(index); 
          images[i] = new ImageWithPosition( img[index], 
            random(width-img[i].width), random(33, height-img[i].height), 
            img[index].width, img[index].height );
        }
      }
    
      //
      // display images throughout 
      for (int i=0; i < images.length; i++) {
        images[i].display();
      }
    
      //
    } // draw 
    
    
    // Minor Tools  -------------------------------------
    
    void HeadLine (String HeadlineText) {
      fill (0, 0, 230);
      textAlign(CENTER);
      textSize(25) ;
      text(HeadlineText, HalfOfWidth, 30);
    } // function 
    
    
    // ==========================================================
    
    class ImageWithPosition {
    
      PImage imageThe;
    
      float x, y, 
        w, h;
    
      //constr
      ImageWithPosition (  PImage imageThe_, 
        float x_, float y_, 
        float w_, float h_) {
    
        imageThe=imageThe_.copy();
    
        x=x_;
        y=y_;
    
        w=w_;
        h=h_;
      } //constr
    
      void display() {
        image(imageThe, x, y, w, h);
      }
    }//class
    
    //
    
  • Thank you ... I tried your method but I got this problem in the screen shot .. could you please have a look? Capture1

  • images are loaded now .. but the problem is that images are appear not according to the body detection ..

  • ??

    you wrote:

    but the problem is that images are appear not according to the body detection

    are you referring to my example?

    Here I just demonstrate how we can store images with positions and display them throughout but change them only every 2 second

    you need to change my code of course to insert your kinect values

Sign In or Register to comment.