0

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

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 );
}

谢谢!

4

1 回答 1

1

几点...

1 正如 George 在评论中提到的,您可以减小捕获区域的大小,这将成倍减少您的草图用于分析面部跟踪的 RAM 量。尝试制作两个名为 CaptureWidth 和 CaptureHeight 的全局变量,并将它们设置为 320 和 240 - 这完全足够了。

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

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

4 如果您只想显示一定数量的面孔,您可以测试 faces[i] == 1 等,这可能会有所帮助....

但我认为循环本身不是罪魁祸首,它更有可能是内存占用。从建议 1 和 2 开始,然后报告...

于 2013-04-26T15:28:36.593 回答