1

I am using Processing IDE to generate hundred short video clips for a computer vision project. Right now, I am using Python to create a .pde file and run it. This looks roughly like:

PATH = "/my/local/director/"
list_of_variables = [1, 2, etc.]
for i in list_of_variables:
    naming = "p5_{:02d}_myfile".format(i)
    os.mkdir(PATH + naming)
    with open(PATH + naming + "/" + naming + ".pde", 'w') as pdefile:
        pdefile.write("contents of file go here " + i ";\n")
        pdefile.write("saveFrame(\"frames/######.tif\");\n")
    subprocess.Popen(["processing-ide", "--sketch=" + PATH + naming, "--run"], stdout=subprocess.DEVNULL)
    subprocess.call(["ffmpeg", "-i", PATH + naming + "/frames/%06d.tif", PATH + naming + "out.mp4"], stdout=subprocess.DEVNULL)
    shutil.rmtree(PATH + naming + "/frames/")    

Every time code is executed, Processing IDE opens a preview window to show what is happening. Is there an option I can pass in the execution step or in the .pde file creation that will prevent the preview window from showing. This is taking a long time, and I'm hoping this would speed things up.

Note: Yes, I have considered that there are better options for generating these videos. In retrospect, I should have used OpenCV in Python to speed things up, but that isn't the thrust of this question.

WesH
  • 460
  • 5
  • 15
  • 2
    This guide might be helpful: https://github.com/processing/processing/wiki/Running-without-a-Display – Kevin Workman Jan 16 '21 at 23:01
  • @KevinWorkman I was incredulous of this suggestion at first, since I assumed shunting the display output to the netherworld would still take time to "create" the images and wait for them. But I did a quickie run of my code with and without ``xvfb-run`` and the time to execute went from 23.4s to 0.00368s. Please make an answer with this so I can give you credit. – WesH Jan 19 '21 at 18:42

1 Answers1

2

@KevinWorkman's advice is great! If you really really really need to do this with Processing and headless, that's the way forward.

What would the code look like for a sketch ? If it's simple drawing instructions you could rewrite it in multiple ways. One option would be simply use an offscreen PGraphics buffer, without the whole PApplet part.

Here's a basic example:

import processing.core.PGraphicsJava2D;

public class PGraphicsBufferTest {

    public static void main(String[] args) {
        PGraphicsJava2D pg = new PGraphicsJava2D();
        // set dimensions
        pg.setSize(300, 300);
        // set as offline (shouldn't expect a PApplet parent)
        pg.setPrimary(false);
        // draw
        pg.beginDraw();
        pg.background(0);
        pg.ellipse(150, 150, 250, 250);
        pg.endDraw();
        // save to disk: in absence of parent PApplet, must use absolute path
        pg.save("/path/to/dataset/sequence/frame.png");
    }

}

If you save this as PGraphicsBufferTest.java (and have Processing's core.jar in the same folder), as an example you could:

  • compile: javac -cp core.jar PGraphicsBufferTest.java
  • run: java -cp .:core.jar PGraphicsBufferTest This may still briefly generate a window, but hopefully will be a simpler/faster setup.

(I deliberately chose Java2D because it's already part of core.jar. You can use PGraphics2D of course, just remember to also add the OpenGL dependencies (gluegen-rt, jogl), and set the classpath (-cp), but also the native path (-Djava.library.path) arguments)

For example, there's an older pyprocessing pip package which uses Pyglet (OpenGL wrapper). Because Pyglet can run headless, hopefully, so would pyprocessing. I haven't tested this myself, however if it does work, you can keep a very similar syntax.

If it's simple 2D drawing instruction and this if for a computer vision project, by all means, OpenCV's drawing functions should suffice.

There are other Python packages that offer 2D drawing features with headless configurations (Pillow's ImageDraw module comes to mind).

Another option I can think that is just as, if not even more so overcomplicated than what you're already doing, is to use openFrameworks. It's C++, but inspired by Processing therefore very similar (e.g. setup() = setup(), draw() = update() + draw(), line(x1,y1,x2,y2) = ofDrawLine(x1,y1,x2,y2), etc.) and can run headless.

George Profenza
  • 50,687
  • 19
  • 144
  • 218
  • "Choose the right tool for the job." These are excellent suggestions for someone starting a new iterative video creation project, but it doesn't offer anything more to the original question which acknowledges that the wrong tool was chosen originally, I'm just trying to speed it up. – WesH Jan 19 '21 at 18:45