Loading...
Logo
Processing Forum
Hi Guys :)

I have problems with my processing memory. I already increased the memory in the settings to 5000MB, so I guess there's a bug in my code.

In the code every second a image of the webcam is saved, 360 images in total. (After 360 images it starts with image1 again). Till then the code works fine, it can run forever.

The problems start here:
By pressing 'a' or 'd', the 'old' images are loaded on the stage as small images, arranged to a circle around the webcam. (Have a look at the screenshot)
By using the keys 'a' and 'd', the code runs for about 20 seconds. After 20 seconds I get the "Out-of-Memory-Error".

I guess I have to remove the old images before I load a new one at the same prosition? But how? Can anyone help me? 
You can find the code below the screenshot.

Attention: If you try this code, you have to run it for 6 mins before you can use 'a' and 'd'. It needs 6 mins to save all the 360 images in the data-folder.

I hope anyone can help. I tried to solve this problems for some nights already...  Thanks!! :)


 

import processing.video.*;
int reality = 1; //image number of live image
int memory = 1; //image number of images of the past
int output = 1; //image which is shown then. It's a "reality-correction" of memory.
int outputSafe = 1; //Always between 1 and 360
boolean timeJorney = false;
boolean keyPressing = false;
Capture cam;


void setup() {
size(1280, 840);
frameRate(30);
String[] cameras = Capture.list();
background(0);

if (cameras.length == 0) {
println("There are no cameras available for capture.");
exit();
} else {
//println("Available cameras:");
for (int i = 0; i < cameras.length; i++) {
// println(cameras[i]);
}
// The camera can be initialized directly using an
// element from the array returned by list():
cam = new Capture(this, cameras[0]);
imageMode(CENTER);
cam.start();
}
}

void draw() {

if (cam.available() == true) {
cam.read();
}
tint(200, 200, 200, 200);
imageMode(CENTER);
image(cam, width/2, height/2, 640, 420);



//Every 30 seconds 1 image is saved
if(frameCount == 30){
image(cam, width/2, height/2, 640, 420);
PImage partialSave = get((width/2-320),(height/2-210),640,420); //Part of Screen (=> Webcam) is saved as Image 1-360
partialSave.save("data/"+reality+".jpg");

//360 images should be saved; so per image 1 degree
if(reality < 360){
reality ++;
}
else if(reality == 360){
reality = 1;
}
frameCount=0;
}


if (keyPressed){

if(key == 'd' || key == 'D'){ //Go "forward in memory"
if(memory < 360){ //memory between 1 and 360
memory++;}
else if(memory == 360){
memory =1;}
timeJorney = true;
}


if(key == 'a'|| key == 'A'){ //Go "back in memory"
if(memory > 1){
memory--;}
else if (memory == 1){
memory = 360;}
timeJorney = true;
}
output = memory + reality;
}
//Adjustment: "outputSafe is always between 1 and 360
if (0 < output && output < 361){
outputSafe = output;
}
else if(output > 360){
outputSafe = output - 360;
}
else if(output < 0){
outputSafe = output + 360 ;
}
else if(output == 0){
outputSafe = 1;
}
//Clock
//Clockhand Seconds (=="reality")
translate(width/2, height/2);
strokeWeight(0.5);
stroke(255);
float deg = reality; //reality between 1 and 360, so reality == degree of clockhand.
float rad = radians(deg); //Conversion degree >> rad
rotate(rad);
line(0, 0, 470, 0);
rotate(-rad); //rotate back to 0 (+rad-rad)
fill(255, 255);
//image(cam, 0, 0, 640, 420);
//TimeJorney started!
if(timeJorney == true){
//Redius Memory
float deg2 = (output);
float rad2 = radians(deg2);
println("Reality: "+reality +"; Memory: "+memory+"; OutputSafe: "+outputSafe+ "; Degree Memory: "+deg2+"; Degree Reality: "+deg);
//Output Memory-Images!
PImage imgM = loadImage(outputSafe+".jpg");
imgM.get(0, 0);
tint(255, 100);
image(imgM, 0, 0, 640, 420);
tint(255, 126);
rotate(+rad2);
image(imgM, 390, 0, 160, 105);
rotate(-rad2);
translate(-width/2, -height/2);
}
}

Replies(15)

You don't tell us what version of Processing you are using.
I saw a memory leak (no memory released, at least not fast enough) with 2.0b, it might be your problem.

Note that to use 5 GB of memory, you need the 64-bit version of Processing...
Thank you for your answer!

I use processing processing-2.0b3 (windows 64). So it could be a leak in the version?
I tried already to use the same code in processing 1.5, but in that version my webcam isn't working:

for the line

cam = new Capture(this, cameras[0]);

I get the error message

The constructor Capture (scetchname, String) is undefined

Maybe it's easier to fix that problem in 1.5!?






Copy code
  1. //Every 30 seconds 1 image is saved
  2. if(frameCount == 30){


nope, that would be rather


The system variable frameCount contains the number of frames that have been displayed since the program started. Inside setup() the value is 0, after the first iteration of draw it is 1, etc.



better use a counter with
millis


Do you plan to have the image-circle and then stop reading the camera or should the reading of the cam go on while the circle is shown?


Thanks!

After frameCount == 30, I save a image and then set the FrameCount back to 0.
So it counts again 29 frames until it saves one image.

What is the benefit of the counter with millis?

Yes, the reading and saving of the cam should be continued, so the circle is changed continuously


I am no expert on this, but frameCount measures the speed your PC currently runs.

So the program behaves different on different machines, which is not so good. With millis it's more stable.

Also with frameCount == 30, you can end up with saving 2 images per second. Do you want this?
Ah I see... Yes, using millis seems to make more sense then... I'll change that! 

I still have the problem that in processing 2.0b I got the error message that I have to increase my memory,
and in version 1.5 I have a problem with my camera 
("The constructor Capture (scetchname, String) is undefined")

Anyone knows how to fix that?


In 1.5, you still have a local copy of the Reference: go to Help > Reference.
Capture constuctors:
Capture(parent, width, height)
Capture(parent, width, height, fps)
Capture(parent, width, height, name, )
Capture(parent, width, height, name, fps)





this line is senseless?
Copy code
  1.  imgM.get(0, 0);


why do you save on your hard drive?
couldn't you have it in an array and just rewrite the array?
and display the array?

this line:
Copy code
  1.     float deg2 = (output);
umh... better
Copy code
  1.     float deg2 = float(output);
?


Copy code
  1.     PImage imgM = loadImage(outputSafe+".jpg");
needed
Copy code
  1.     PImage imgM = loadImage("data/"+outputSafe+".jpg");
?

You could have
Copy code
  1.     PImage imgM;
as a global var and in draw just say
Copy code
  1.     imgM = loadImage(outputSafe+".jpg");



when you use ctrl-t you got auto-format


Copy code
  1. import processing.video.*;
  2. int reality = 1; //image number of live image
  3. int memory = 1; //image number of images of the past
  4. int output = 1; //image which is shown then. It's a "reality-correction" of memory.
  5. int outputSafe = 1; //Always between 1 and 360
  6. boolean timeJorney = false;
  7. boolean keyPressing = false;
  8. Capture cam;
  9. void setup() {
  10.   size(1280, 840);
  11.   frameRate(30);
  12.   String[] cameras = Capture.list();
  13.   background(0);
  14.   if (cameras.length == 0) {
  15.     println("There are no cameras available for capture.");
  16.     exit();
  17.   }
  18.   else {
  19.     //println("Available cameras:");
  20.     for (int i = 0; i < cameras.length; i++) {
  21.       // println(cameras[i]);
  22.     }
  23.     // The camera can be initialized directly using an
  24.     // element from the array returned by list():
  25.     cam = new Capture(this, cameras[0]);
  26.     imageMode(CENTER);
  27.     cam.start();
  28.   }
  29. }
  30. void draw() {
  31.   if (cam.available() == true) {
  32.     cam.read();
  33.   }
  34.   tint(200, 200, 200, 200);
  35.   imageMode(CENTER);
  36.   image(cam, width/2, height/2, 640, 420);
  37.   //Every 30 seconds 1 image is saved
  38.   if (frameCount == 30) {
  39.     image(cam, width/2, height/2, 640, 420);
  40.     PImage partialSave = get((width/2-320), (height/2-210), 640, 420); //Part of Screen (=> Webcam) is saved as Image 1-360
  41.     partialSave.save("data/"+reality+".jpg");
  42.     //360 images should be saved; so per image 1 degree
  43.     if (reality < 360) {
  44.       reality ++;
  45.     }
  46.     else if (reality == 360) {
  47.       reality = 1;
  48.     }
  49.     frameCount=0;
  50.   }
  51.   if (keyPressed) {
  52.     if (key == 'd' || key == 'D') { //Go "forward in memory"
  53.       if (memory < 360) { //memory between 1 and 360
  54.         memory++;
  55.       }
  56.       else if (memory == 360) {
  57.         memory =1;
  58.       }
  59.       timeJorney = true;
  60.     }
  61.     if (key == 'a'|| key == 'A') { //Go "back in memory"
  62.       if (memory > 1) {
  63.         memory--;
  64.       }
  65.       else if (memory == 1) {
  66.         memory = 360;
  67.       }
  68.       timeJorney = true;
  69.     }
  70.     output = memory + reality;
  71.   }
  72.   //Adjustment: "outputSafe is always between 1 and 360
  73.   if (0 < output && output < 361) {
  74.     outputSafe = output;
  75.   }
  76.   else if (output > 360) {
  77.     outputSafe = output - 360;
  78.   }
  79.   else if (output < 0) {
  80.     outputSafe = output + 360 ;
  81.   }
  82.   else if (output == 0) {
  83.     outputSafe = 1;
  84.   }
  85.   //Clock
  86.   //Clockhand Seconds (=="reality")
  87.   translate(width/2, height/2);
  88.   strokeWeight(0.5);
  89.   stroke(255);
  90.   float deg = reality; //reality between 1 and 360, so reality == degree of clockhand.
  91.   float rad = radians(deg); //Conversion degree >> rad
  92.   rotate(rad);
  93.   line(0, 0, 470, 0);
  94.   rotate(-rad); //rotate back to 0 (+rad-rad)
  95.   fill(255, 255);
  96.   //image(cam, 0, 0, 640, 420);
  97.   //TimeJorney started!
  98.   if (timeJorney == true) {
  99.     //Redius Memory
  100.     float deg2 = (output);
  101.     float rad2 = radians(deg2);
  102.     println("Reality: "+reality +"; Memory: "+memory+"; OutputSafe: "+outputSafe+ "; Degree Memory: "+deg2+"; Degree Reality: "+deg);
  103.     //Output Memory-Images!
  104.     PImage imgM = loadImage(outputSafe+".jpg");
  105.     imgM.get(0, 0);
  106.     tint(255, 100);
  107.     image(imgM, 0, 0, 640, 420);
  108.     tint(255, 126);
  109.     rotate(+rad2);
  110.     image(imgM, 390, 0, 160, 105);
  111.     rotate(-rad2);
  112.     translate(-width/2, -height/2);
  113.   }
  114. }

Aaah right, Chrisir, I deleted the line
Copy code
  1.  imgM.get(0, 0);
and I changed
Copy code
  1. PImage imgM;
to a global var.

But I still have the memory-problem...

PhiLho, thanks, I'll try to fix the 1.5 problem right now! 
Let's see which problem is easier to fix, 1.5 or 2.0 :)

Thank you guys very very much!!! 

I could fix the Processing 1.5 video problem!
This code works now (it's just the video-part):
Copy code
  1. import processing.video.*;
  2. Capture video;
  3. void setup() {
  4. size(1280, 840);
  5. frameRate(30);
  6. background(0);
  7. video = new Capture(this, 640, 420, 15);
  8. imageMode(CENTER);
  9. video.read();
  10. }
  11. void draw() }
  12.   video.read();
  13.   image(video, width/2, height/2, 640, 420);
  14. }
And the memory-problem is gone in 1.5, my code is working finally! Thank you so much!! :)


Should this be reported as a bug?
Maybe I made some mistake with the code? But well, in Processing 1.5 it works fine...
Where can I report a bug?

Yes, I think it's here:
http://code.google.com/p/processing/issues/list

but you have to register at google etc.