2013-04-24 218 views
0

我正在使用Processing(processing.org)处理需要面部跟踪的项目。现在的问题是,由于for循环,程序将耗尽内存。我想停止循环或至少解决内存不足的问题。这是代码。如何停止for循环(OpenCV)

import hypermedia.video.*; 
import java.awt.Rectangle; 


OpenCV opencv; 

// contrast/brightness values 
int contrast_value = 0; 
int brightness_value = 0; 



void setup() { 

size(900, 600); 

opencv = new OpenCV(this); 
opencv.capture(width, height);     // open video stream 
opencv.cascade(OpenCV.CASCADE_FRONTALFACE_ALT); // load detection description, here-> front face detection : "haarcascade_frontalface_alt.xml" 


// print usage 
println("Drag mouse on X-axis inside this sketch window to change contrast"); 
println("Drag mouse on Y-axis inside this sketch window to change brightness"); 

} 


public void stop() { 
    opencv.stop(); 
    super.stop(); 
} 



void draw() { 

// grab a new frame 
// and convert to gray 
opencv.read(); 
opencv.convert(GRAY); 
opencv.contrast(contrast_value); 
opencv.brightness(brightness_value); 

// proceed detection 
Rectangle[] faces = opencv.detect(1.2, 2, OpenCV.HAAR_DO_CANNY_PRUNING, 40, 40); 

// display the image 
image(opencv.image(), 0, 0); 

// draw face area(s) 
noFill(); 
stroke(255,0,0); 
for(int i=0; i<faces.length; i++) { 
    rect(faces[i].x, faces[i].y, faces[i].width, faces[i].height); 
} 
} 

void mouseDragged() { 
contrast_value = (int) map(mouseX, 0, width, -128, 128); 
brightness_value = (int) map(mouseY, 0, width, -128, 128); 
} 

谢谢!

+0

Wheres the for loop? – 2013-04-24 12:50:18

+0

为什么当你想停止循环?对于(int i = 0; i Alexey 2013-04-24 13:26:39

+0

; } 这是循环。我想要停止它,或者释放先前所用的内存,因为如果它继续存在,我将耗尽存储空间。然后它会在20秒后显示一个MemoryError。 – user1927992 2013-04-24 15:12:37

回答

1

的几点...

1作为乔治在评论中提到的,你可以降低捕获区域的大小,这将成倍降低你的草图使用分析人脸跟踪的RAM量。尝试制作两个名为CaptureWidth和CaptureHeight的全局变量,并将它们设置为320和240--对此完全就足够了。

2您可以增加草图在Java虚拟机中默认使用的内存量。处理默认为128我认为,但如果你去的首选项,你会看到一个复选框,以“最大可用内存增加到[x]”...我通常使我的1500 mb,但这取决于你的机器,你可以处理。不要试图使它大于1800mb,除非你在64位机器上,并且在64位模式下使用Processing 2.0 ...

3要真正打破循环...使用'break'命令http://processing.org/reference/break.html ...但请理解为什么要使用,因为这只会让你跳出你的循环。

4如果你只是想表现出一定数目的面,你可以测试是否面孔[I] == 1,等等,这可能有助于....

但我认为,循环本身ISN这里是罪魁祸首,这更可能是内存占用。从建议开始1 & 2并回报...