2013-04-24 100 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並回報...