OutOfMemoryError: Too many images loaded?
in
Core Library Questions
•
11 months ago
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.
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".
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.
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);
}
}
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);
}
}
1