We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpVideo Capture,  Movie Playback,  Vision Libraries › video work freezes after running for a while
Page Index Toggle Pages: 1
video work freezes after running for a while (Read 690 times)
video work freezes after running for a while
Sep 29th, 2008, 5:50am
 
I have been working on a 'time slice delay' type of thing for an artwork im making. It takes a video feed from an attached camera device, puts the images into a video buffer and then plays it back across the screen in slices, where each slice is delayed from the previous one by x amount of frames.

The program does work for the most part, but occasionally does not start properly, and occasionally will freeze while it is already running.

Here is the code as it is so far, its very crude as i havent worked out how to dynamically name each slice yet, so there is a lot of repetative code..... which might be the problem!

It seems to more reliably start up when it has been exported, rarely will it start first time from within the processing environment. Any help would be much appreciated!

The code was too long to post in this thread so here is a link to it.

http://users.tpg.com.au/grant456/


Re: video work freezes after running for a while
Reply #1 - Sep 30th, 2008, 7:58am
 
I think this is due to a timing issue during initialization.

It seems that since you are instantiating the camera object *before* the video buffer, occasionally the camera capture event will fire before the video buffer has been instantiated.

Switching line number 13 and 14 fixes the problem for me, give that a try.
Re: video work freezes after running for a while
Reply #2 - Sep 30th, 2008, 8:20am
 
Also, here's your sketch with a loop to remove all that copy/paste code.

Quote:


import processing.video.*;

VideoBuffer vb; //video buffer class
Capture cam;

int w = 640;
int h = 480;
int offset = 0;

void setup()
{
 size(1280,800, P3D);

 background(0);

 vb = new VideoBuffer(137, 48, h);
 cam = new Capture(this, w, h);
}

void captureEvent(Capture cam)
{
 cam.read();
 PImage blog = cam.get(300, 0, 48, h);
 vb.addFrame( blog );
 
 offset++;
 if(offset > 137)
   offset = 0;
}

void draw()
{
 int yPos = 150;

 for(int i = 0; i < 27; i++)
 {
   image( vb.getFrame( 135 - (i * 5) + offset), i*48, yPos );
 }

}


class VideoBuffer
{
 PImage[] buffer;

 int inputFrame = 0;

 int frameWidth = 0;
 int frameHeight = 0;

/*
parameters:

frames - the number of frames in the buffer (fps * duration)
width - the width of the video
height - the height of the video
*/

 VideoBuffer( int frames, int width, int height )
 {
   buffer = new PImage[frames];
   for(int i = 0; i < frames; i++)
   {
     buffer[i] = new PImage(width, height);
   }

   inputFrame = frames-1;

   frameWidth = width;
   frameHeight = height;
 }

 // return the current "playback" frame.
 PImage getFrame( int frame )
 {
   int f = frame;
 
   while(f >= buffer.length)
   {
     f -= buffer.length;
   }
 
   return buffer[f];
 }


 // Add a new frame to the buffer.
 void addFrame( PImage frame )
 {
   // copy the new frame into the buffer.
   arraycopy(frame.pixels, 0, buffer[inputFrame].pixels, 0, frameWidth * frameHeight);

   // advance the input and output indexes
   inputFrame++;

   // wrap the values..
   if(inputFrame >= buffer.length)
   {
     inputFrame = 0;
   }
 }
}
Re: video work freezes after running for a while
Reply #3 - Oct 7th, 2008, 3:48am
 
Thanks rrrufusss,

switching lines 13 and 14 did work in fixing the initialization problem. Though the work does still freeze after about half an hour or so and then needs restarting. (that is using my old cold with the lines switched, not your new looped version)

the code that you refined with a loop works fine (though i havent run it for an extended period) but after about 20 seconds or so it takes the first slice and seems to place it at the end, and the original first slice becomes the last to show delay. after about another 20 seconds it takes the second last slice and makes its the first, pushing the whole sequence along, kind of displacing the flow.

i was wondering if you knew why this was? some number tweaking perhaps.

also, just as a backup if i cant get this not to freeze on me, i was wondering if there is a way to make a program periodically shut itself down and restart itself, after a given period of time. 20 mins for example?

thanks for all your help.
Re: video work freezes after running for a while
Reply #4 - Oct 8th, 2008, 1:16am
 
The various limits didn't match (some were 137, some 135) so eventually the input and output from the buffer were getting out of sync.

Try this..

Code:

import processing.video.*;

VideoBuffer vb; //video buffer class
Capture cam;

int w = 640;
int h = 480;
int offset = 134;

void setup()
{
frameRate(20);
size(1296,800, P3D);

background(0);

vb = new VideoBuffer(135, 48, h);
cam = new Capture(this, w, h);
}

void captureEvent(Capture cam)
{
cam.read();
cam.updatePixels();
PImage blog = cam.get(300, 0, 48, h);
vb.addFrame( blog );

offset++;
if(offset >= 135)
offset = 0;
}

void draw()
{
int yPos = 150;

for(int i = 0; i < 27; i++)
{
image( vb.getFrame( 135 - (i * 5) + offset), i*48, yPos );
}

}


class VideoBuffer
{
PImage[] buffer;

int inputFrame = 0;

int frameWidth = 0;
int frameHeight = 0;

/*
parameters:

frames - the number of frames in the buffer (fps * duration)
width - the width of the video
height - the height of the video
*/

VideoBuffer( int frames, int width, int height )
{
buffer = new PImage[frames];
for(int i = 0; i < frames; i++)
{
buffer[i] = new PImage(width, height);
}

inputFrame = 0;

frameWidth = width;
frameHeight = height;
}

// return the current "playback" frame.
PImage getFrame( int frame )
{
int f = frame;

while(f >= buffer.length)
{
f -= buffer.length;
}

return buffer[f];
}


// Add a new frame to the buffer.
void addFrame( PImage frame )
{
// copy the new frame into the buffer.
arraycopy(frame.pixels, 0, buffer[inputFrame].pixels, 0, frameWidth * frameHeight);

// advance the input and output indexes
inputFrame++;

// wrap the values..
if(inputFrame >= buffer.length)
{
inputFrame = 0;
}
}
}
Re: video work freezes after running for a while
Reply #5 - Oct 8th, 2008, 6:25am
 
works perfectly. thankyou for your help!
Page Index Toggle Pages: 1