play a movie on marker detection

edited February 2015 in Library Questions

Hello Every One, I have developed a sketch of Augmented reality in which by showing the marker to the camera , a specified movie is played..

System : Mac OSX 10.7.5 Processing-1.5.1. For live camera capturing - GSCapture library and for mirror the captured image - OpenCV library is used . for marker detection - NyAR4Psg library ,
and for playing a movie - GSMovie library is used , coz of the movie is in alpha channel , I am using GLTexture class to display the movie. And GLGraphics library - for rendering the image...

But the problem is , some times this program runs very well , and some time when I start the program and set the camera angle , then a movie is played at first time without the marker detection . Instead , I want that, the movie should play only on the detection of the marker...

This is the source code:

import jp.nyatla.nyar4psg.*;

import codeanticode.glgraphics.*;

import processing.opengl.*;

import hypermedia.video.*;

import codeanticode.gsvideo.*;

OpenCV opencv;
GSCapture cam;
GSMovie movie;
GLTexture tex;
PImage img;
PImage bg;
MultiMarker nya;


void setup() {
    size( 1280, 720,GLConstants.GLGRAPHICS );

    cam = new GSCapture(this, width/2,height/2);
    cam.start();
    opencv = new OpenCV( this );
    opencv.allocate(width/2,height/2);   

    tex=new GLTexture(this);
    movie=new GSMovie(this,"astrum_render_2.mov");
    movie.setPixelDest(tex);

  nya=new MultiMarker(this,width/2,height/2,"camera_para.dat",NyAR4PsgConfig.CONFIG_PSG);
  nya.addARMarker("patt.hiro",80);
//  nya.addARMarker("patt.kanji",80);
  nya.addARMarker("patt.e",80);

 // nya.addNyIdMarker(0,80);//id=2
 // nya.addNyIdMarker(1,80);//id=3


  bg=new PImage(width/2,height/2);

  img=loadImage("2.png");


}
void movieEvent(GSMovie m){
  m.read();
}
void captureEvent(GSCapture c) {
  c.read();
}

public void stop() {
    opencv.stop();
    super.stop();
}
boolean s=false;
boolean r=false;
boolean detect_r=false;
void draw() {

  opencv.copy(cam);
  opencv.flip(OpenCV.FLIP_HORIZONTAL);        //  Flips the image horizontally

  bg=opencv.image();
  nya.detect(bg);
  background(0);
  nya.drawBackground(bg);
  //  opencv.flip(OpenCV.FLIP_HORIZONTAL);        //  Flips the image horizontally

  for(int i=0;i<2;i++){
    if((!nya.isExistMarker(i))){

      continue;
    //  movie.pause();

    }
  }

    if(!(nya.isExistMarker(1))&&(!nya.isExistMarker(0))){
   //   movie.pause();

         detect_r=false;
    }

    if((nya.isExistMarker(0))&&(!nya.isExistMarker(1))){
    nya.beginTransform(0);
    println("seen");
    nya.endTransform();
    }


    if((nya.isExistMarker(1))&&(!nya.isExistMarker(0))){
    nya.beginTransform(1);
   // if(!movie.isPlaying()){
    println("not seen");
    detect_r=true;
    // movie.play();
   // }
   if(detect_r==true){
     movie.play();
   }else{
   //  detect_r=false;
   }


    nya.endTransform();
    }

      if(movie.isPlaying()){
     if(movie.time()==10.033334){
       movie.jump(0);

     }
   }


    if(tex.putPixelsIntoTexture()){
      image(tex,0,0,width,height);  
    }




        println(movie.time());


}

You can check this code , by using any alpha channel movie ...(you have to specify the time of movie to jump it again)

Can any one suggest me , what should I do to solve this problem ... ?

Your valuable help must be appreciated ...

Thanks , Archana...

Answers

  • Break your problem down into smaller steps: which are you having a problem with, detecting the marker or playing the movie?

  • you can use println to find out where the sketch runs

    you can use else if instead of else in 87 and 94

    remember to use ctrl-t in processing to auto-format (indents)

    you got 2 markers and both trigger detect_r which triggers the same movie for both markers, is that correct? Then line 100 should have its }

    detection

    when the reason is a false detection, you could count the detections and only after three detections in a row start playing the movie....

  • @Chrisir,

    Thanks for your reply , I have changed my code as you have suggested me,

    this is my code :
    
    import jp.nyatla.nyar4psg.*;
    
    import codeanticode.glgraphics.*;
    
    import processing.opengl.*;
    
    import hypermedia.video.*;
    
    import codeanticode.gsvideo.*;
    
    OpenCV opencv;
    GSCapture cam;
    GSMovie movie;
    GLTexture tex;
    PImage img;
    PImage bg;
    MultiMarker nya;
    boolean s=false;
    boolean r=false;
    boolean detect_r=false;
    
    
    void setup() {
      size( 1280, 720, GLConstants.GLGRAPHICS );
    
      cam = new GSCapture(this, width/2, height/2);
      cam.start();
      opencv = new OpenCV( this );
      opencv.allocate(width/2, height/2);   
    
      tex=new GLTexture(this);
      movie=new GSMovie(this, "AstrumFinal_render.mov");
      movie.setPixelDest(tex);
    
    //  nya=new MultiMarker(this, width/2, height/2, "camera_para.dat", NyAR4PsgConfig.CONFIG_PSG);
     nya=new MultiMarker(this, width/2, height/2, "camera_para.dat");
      nya.addARMarker("patt.hiro", 80);
      nya.addARMarker("patt.e", 80);
    
      bg=new PImage(width/2, height/2);
      img=loadImage("2.png");
    }
    void movieEvent(GSMovie m) {
      m.read();
    }
    void captureEvent(GSCapture c) {
      c.read();
    }
    
    void draw() {
    
      opencv.copy(cam);
      opencv.flip(OpenCV.FLIP_HORIZONTAL);        //  Flips the image horizontally
    
      bg=opencv.image();
      nya.detect(bg);
      background(0);
      nya.drawBackground(bg);
      //  opencv.flip(OpenCV.FLIP_HORIZONTAL);        //  Flips the image horizontally
    
     for (int i=0;i<2;i++) {
        if ((!nya.isExistMarker(i))) {
    
          continue;
        }
    
    
    
      if ((!nya.isExistMarker(1))&&(!nya.isExistMarker(0))) {
        detect_r=false;
      }
    
      if ((nya.isExistMarker(0))&&(!nya.isExistMarker(1))) {
        nya.beginTransform(0);
        println("0");
        nya.endTransform();
      }
      if((nya.isExistMarker(1)) && (!nya.isExistMarker(0))) {
        nya.beginTransform(1);
        detect_r=true;
    
        if(detect_r){
          println("1");
          movie.play();
        }else if(detect_r==false){
          println("movie stop");
          movie.stop();
        }
    
        if(movie.isPlaying()){
          if(movie.time()==11.033334){     // this is when the movie is finished ...
            if(nya.isExistMarker(1)){
              println("movie is jumping");
              movie.jump(0.1);
            }else if(!nya.isExistMarker(1)){
              if(movie.isPlaying()){
                println("Playing");
                movie.pause();
                return;
               }
           }
        }
    
        nya.endTransform();
      }
      }
    
    
    
    
    
    
    
    
    
     if((!nya.isExistMarker(1)) && (!movie.isPlaying())){
        detect_r=false;
      }
    
     }
    
    
    
    
    
    
    
    
    }
    
    public void stop() {
      opencv.stop();
      super.stop();
    }
    

    I have faced a same problem , this program plays / jumps a movie , without a marker detection ... and I don't want that...

    Please help me for this....

  • did you do as KevinWorkman suggested?

    is the issue really the false detection?

    thank you!

    ;-)

  • ok, let me see

    • you haven't answered my question

    • you haven't used "else if" as I told you

    • you haven't used ctrl-t as I told you

    • you haven't used a counter (and only after three detections in a row start playing the movie....) as I told you

    What else can I do for you?

    ;-)

  • this

     for (int i=0;i<2;i++) {
        if ((!nya.isExistMarker(i))) {
    
          continue;
        }
    

    is closing in line 122 (I think)

    The whole block is in a for-loop. Do you want that?

    It think you can delete it.

  • does the println 0 and println 1 come oout right or does it come to early as well (like the movies) ?

  • I really can't help you with that since I don't have any of the librarys or AR-experience...

    I am sorry.

    ;-)

  • edited February 2015

    @Chrisir,

    I m so sorry for my late reply ..

    I have used else - if condition , auto format , but is gives me a same result , So I have created a new code , but I don't understand your last point "used a counter (and only after three detections in a row start playing the movie"

    I am printing 0 n 1 for marker detection , i.e ... if marker 0 is exist then in console it prints 0 , and if marker 1 is exist then , in console it prints 1..

    Now , when I run my program , movie plays on 1 marker detection (prints 1 in console ), again when I show the 1 marker movie is jumped (print 1 in console)... this is fine ..

    But , after some time .... without showing a marker to the camera , console get automatically printed 1 and my movie is automatically played. what does it mean that ? what a camera actually detects as a marker 1 (when I am not showing a marker to the camera... ) that's my problem .....

    Again I have created a new code :

    import jp.nyatla.nyar4psg.*;
    
    import codeanticode.glgraphics.*;
    
    import processing.opengl.*;
    
    import hypermedia.video.*;
    
    import codeanticode.gsvideo.*;
    
    OpenCV opencv;
    GSCapture cam;
    GSMovie movie;
    GLTexture tex;
    PImage img;
    PImage bg;
    MultiMarker nya;
    boolean s=false;
    boolean r=false;
    boolean detect_r=false;
    
    
    void setup() {
      size( 1280, 720, GLConstants.GLGRAPHICS );
    
      cam = new GSCapture(this, width/2, height/2);
      cam.start();
      opencv = new OpenCV( this );
      opencv.allocate(width/2, height/2);   
    
      tex=new GLTexture(this);
      movie=new GSMovie(this, "AstrumFinal_render.mov");
      movie.setPixelDest(tex);
    
      //  nya=new MultiMarker(this, width/2, height/2, "camera_para.dat", NyAR4PsgConfig.CONFIG_PSG);
      nya=new MultiMarker(this, width/2, height/2, "camera_para.dat");
      nya.addARMarker("patt.hiro", 80);
      nya.addARMarker("patt.e", 80);
      nya.addARMarker("patt.e", 80);
    
    
    
      bg=new PImage(width/2, height/2);
      img=loadImage("2.png");
    }
    void movieEvent(GSMovie m) {
      m.read();
    }
    void captureEvent(GSCapture c) {
      c.read();
    }
    
    void draw() {
    
      opencv.copy(cam);
      opencv.flip(OpenCV.FLIP_HORIZONTAL);        //  Flips the image horizontally
    
      bg=opencv.image();
      nya.detect(bg);
      background(0);
      nya.drawBackground(bg);
    
      for (int i=0;i<2;i++) {
        if ((!nya.isExistMarker(i))) {
          continue;
        }
    
        if ((nya.isExistMarker(1)) && (!nya.isExistMarker(0))) {
          nya.beginTransform(1); 
          detect_r=true;
          nya.endTransform();
        }
      }
    
    
      if (detect_r) {
        println("1");
        movie.play();
      }
      else if (detect_r==false) {
        println("movie stop");
        movie.stop();
      }
    
      if (movie.isPlaying()) {
        if (movie.time()==11.033334) {
          if (nya.isExistMarker(2)) {
            println("movie is jumping");
            movie.jump(0.1);
          }
          else if ((!nya.isExistMarker(1)) && (!nya.isExistMarker(2))) {
            if (movie.isPlaying()) {
              println("Playing");
              movie.stop();
            }
          }
        }
      }
    
      if ((!nya.isExistMarker(1)) && (!movie.isPlaying())) {
        detect_r=false;
      }
      if (tex.putPixelsIntoTexture()) {
        image(tex, -85, -10, width+158, height+38);
      }
    }
    
    public void stop() {
      opencv.stop();
      super.stop();
    }
    
  • edited February 2015

    @Chrisir ,

    In the code above , I have added a same marker in different row .. means on showing that marker , movie should jumped...

  • @Chrisir,

    I m so sorry for my late reply ..

    I have used else - if condition , auto format , but is gives me a same result , but I don't understand your last point "used a counter (and only after three detections in a row start playing the movie"

    I am printing 0 n 1 for marker detection , i.e ... if marker 0 is exist then in console it prints 0 , and if marker 1 is exist then , in console it prints 1..

    Now , when I run my program , movie plays on 1 marker detection (prints 1 in console ), again when I show the 1 marker movie is jumped (print 1 in console)... this is fine ..

    But , after some time .... without showing a marker to the camera , console get automatically printed 1 and my movie is automatically played. what does it mean that , what a camera actually detects as a marker 1 (when I am not showing a marker to the camera... ) that's my problem .....

    Again I have created a new code :

    import jp.nyatla.nyar4psg.*;
    
    import codeanticode.glgraphics.*;
    
    import processing.opengl.*;
    
    import hypermedia.video.*;
    
    import codeanticode.gsvideo.*;
    
    OpenCV opencv;
    GSCapture cam;
    GSMovie movie;
    GLTexture tex;
    PImage img;
    PImage bg;
    MultiMarker nya;
    boolean s=false;
    boolean r=false;
    boolean detect_r=false;
    
    
    void setup() {
      size( 1280, 720, GLConstants.GLGRAPHICS );
    
      cam = new GSCapture(this, width/2, height/2);
      cam.start();
      opencv = new OpenCV( this );
      opencv.allocate(width/2, height/2);   
    
      tex=new GLTexture(this);
      movie=new GSMovie(this, "AstrumFinal_render.mov");
      movie.setPixelDest(tex);
    
      //  nya=new MultiMarker(this, width/2, height/2, "camera_para.dat", NyAR4PsgConfig.CONFIG_PSG);
      nya=new MultiMarker(this, width/2, height/2, "camera_para.dat");
      nya.addARMarker("patt.hiro", 80);
      nya.addARMarker("patt.e", 80);
      nya.addARMarker("patt.e", 80);
    
    
    
      bg=new PImage(width/2, height/2);
      img=loadImage("2.png");
    }
    void movieEvent(GSMovie m) {
      m.read();
    }
    void captureEvent(GSCapture c) {
      c.read();
    }
    
    void draw() {
    
      opencv.copy(cam);
      opencv.flip(OpenCV.FLIP_HORIZONTAL);        //  Flips the image horizontally
    
      bg=opencv.image();
      nya.detect(bg);
      background(0);
      nya.drawBackground(bg);
    
      for (int i=0;i<2;i++) {
        if ((!nya.isExistMarker(i))) {
          continue;
        }
    
        if ((nya.isExistMarker(1)) && (!nya.isExistMarker(0))) {
          nya.beginTransform(1); 
          detect_r=true;
          nya.endTransform();
        }
      }
    
    
      if (detect_r) {
        println("1");
        movie.play();
      }
      else if (detect_r==false) {
        println("movie stop");
        movie.stop();
      }
    
      if (movie.isPlaying()) {
        if (movie.time()==11.033334) {
          if (nya.isExistMarker(2)) {
            println("movie is jumping");
            movie.jump(0.1);
          }
          else if ((!nya.isExistMarker(1)) && (!nya.isExistMarker(2))) {
            if (movie.isPlaying()) {
              println("Playing");
              movie.stop();
            }
          }
        }
      }
    
      if ((!nya.isExistMarker(1)) && (!movie.isPlaying())) {
        detect_r=false;
      }
      if (tex.putPixelsIntoTexture()) {
        image(tex, -85, -10, width+158, height+38);
      }
    }
    
    public void stop() {
      opencv.stop();
      super.stop();
    }
    
Sign In or Register to comment.