gpt4 book ai didi

opencv - 如何停止for循环(OpenCV)

转载 作者:行者123 更新时间:2023-12-02 17:52:49 28 4
gpt4 key购买 nike

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

谢谢!

最佳答案

几点...
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开始,然后进行报告...

关于opencv - 如何停止for循环(OpenCV),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16192452/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com