launch() won't work inside selectInput() function;

edited April 2016 in Kinect

Hello, I've come across a weird problem in my program. I am making a program that launches a video after I use the function launch() inside it that initializes another program called video_segmentation. This program generates a txt file with information about when each shot and scene of the selected video begins and ends.

The problem here is that this program will not launch inside my selectInput() unless I put another selectInput() before it. Here's my code:

  import processing.video.*;
  import gab.opencv.*;
  import java.awt.*;
  import java.util.*;
  import java.io.*;

  OpenCV openCv;

  PrintWriter output;
  StringList kf;
  StringList [] movieScenes;
  int c=0;
  BufferedReader HowManyScenes;
  int lines;
  String line = "1";
  String [] scenes;

  Movie theMov; 
  File selectionF, selectionT;
  String selectText;
  int fc;
  ArrayList <PImage> frames= new ArrayList();
  int avgFrameColor;

  Boolean trufals=false;
  Boolean process=true;

  void setup() {

    size(1280, 720);
    //frameRate(200);
    selectInput("Select a film to process:", "selectFilm");

  }

  void selectFilm(File selection) {
    println("ola");
    if (selection == null) {
      println("Window was closed or you hit cancel.");
    } else {
      theMov = new Movie (this, selection.getAbsolutePath());
      // println(selection.getAbsolutePath());

      String [] params = split (selection.getAbsolutePath(), "\\");
      String [] segmentation = {"C:/Users/Nuno/Desktop/Segmentation/windows64/video_segmentation", params[params.length-1]};

      launch(segmentation);
      println(segmentation);
      //process=true;

    }
  }  

  void draw() { 

    if (theMov == null || selectText == null) {
      trufals=false;
    } else {
      trufals=true;
    }

//rest of my code which doesnt start untill I select both a video file and a txt file

}

The problem I have is in the setup I believe, because if I put another random selectInput() before it all works fine and video_segmentation.exe will run. Example:

void setup(){
  size(1280,720);
  selectInput("hi","hi");
  selectInput("Select a film to process:", "selectFilm", selectionF)

}

I discovered this because I was running another selectInput() that initializes the function selectScenes()which reads the file generated by video_segmentation.exe.

I would appreciate some help if anyone has any idea what this is about.

Answers

  • edited April 2016
    • selectInput() happens concurrently in some separate Thread.
    • Anything running outside sketch's main "Animation" Thread shouldn't deal w/ Processing's API related to the canvas.
    • Also notice that anything created by launch() also gets its own Thread.
    • Due to all of those motives, avoid doing anything not absolutely necessary while in a foreign Thread.
    • Just declare global boolean variables which function as task flags for the main draw().
    • Set them true when in some foreign Thread. Then set them to false & execute the task while finally in "Animation" Thread.
  • edited April 2016

    Hello, and thank for your response. I read what you said so I made a little sketch following your instructions. This is what I have:

    import processing.core.*;
    import processing.video.*;
    import gab.opencv.*;
    import java.awt.*;
    import java.util.*;
    import java.io.*;
    
    Movie theMov;
    File SelectionF1;
    String SelectionF;
    String [] params = new String[7];
    String [] segmentation = new String[2];
    
    
    Boolean notnull=false;
    int n=0;
    void setup() {
      //selectInput("hi", "hi");
      selectInput("Select a film to process:", "selectFilm");
    }
    
    void draw() {
    
      //println(SelectionF);
      if (SelectionF!=null && n==0) {
        notnull=true;
        params = split (SelectionF, "\\");
        segmentation[0] = "C:/Users/Nuno/Desktop/Segmentation/windows64/video_segmentation";
        segmentation[1] = params[params.length-1];
    
        n=1;
      }
      if (notnull) {
        println("hi");
        launch(segmentation);
        notnull=false;
      }
    }
    
    
    void selectFilm(File selection) {
      if (selection == null) {
        println("Window was closed or you hit cancel.");
      } else {
        theMov = new Movie (this, selection.getAbsolutePath());
        println(selection.getAbsolutePath());
        SelectionF = selection.getAbsolutePath();
      }
    }
    

    Once more, the line launch(segmentation); does not run the program unless I uncomment line selectInput("hi","hi");. Also the sketch doesnt give any errors or anything. It simply doesn't initializes video_segmentation.exe.

    So I made a little test and I made him open a video instead of this program with the line launch("C:/Users/Nuno/Desktop/Segmentation/windows64/sketch.mp4") and it works. I don't know why doesnt this launch("C:/Users/Nuno/Desktop/Segmentation/windows64/ video_segmentation sketch.mp4") work aswell, unless I put that new selectInput() before the one that selects the file.

    Any ideas?

  • edited April 2016

    I think there's so many flag variables that somewhat makes difficult to reason about the sketch.
    I've tweaked your sketch in order to "dry" it a little. :D
    Although I've kept launch() there, got no idea whether it works though. :-\"

    /**
     * Select & Launch Video (v2.01)
     * by  Almeida
     * mod GoToLoop (2016-Apr-19)
     *
     * forum.Processing.org/two/discussion/16097/
     * launch-won-t-work-inside-selectinput-function 
     */
    
    static final String VIDEO_APP_PATH =
      "C:/Users/Nuno/Desktop/Segmentation/windows64/video_segmentation";
    
    final String[] segs = { VIDEO_APP_PATH, "" };
    
    import processing.video.Movie;
    Movie mov;
    
    String path;
    boolean launchReady;
    
    void setup() {
      frameRate(30);
      mouseClicked();
    }
    
    void draw() {
      if (mov != null && mov.width == width)  background(mov);
      if (launchReady)  launchVideo();
    }
    
    void mouseClicked() {
      selectInput("Select a video to process:", "selectVideo");
    }
    
    void movieEvent(final Movie mv) {
      mv.read();
    }
    
    void selectVideo(final File f) {
      if (f == null || f.isDirectory()) {
        println("Window was closed or you've hit cancel.\n");
        return;
      }
    
      path = f.getPath();
      println(path);
    
      launchReady = true;
    }
    
    void launchVideo() {
      launchReady = false;
    
      if (mov != null)  mov.dispose();
      (mov = new Movie(this, path)).play();
    
      final String[] splitPath = split(path, File.separatorChar);
      segs[1] = splitPath[splitPath.length - 1];
      println(segs[1], ENTER, launch(segs), ENTER);
    
      while (mov.height == 0)  delay(30);
      getSurface().setSize(mov.width, mov.height);
      getSurface().setTitle(segs[1]);
    }
    
  • Its way better than what I had, but still does not work. I really don't know why. It probably has something to do with video_segmentation.exe itself. I really appreciate it though.

Sign In or Register to comment.