我正在java上使用opencv库来获取图像文件夹并仅裁剪面部的程序。它使用opencv的人脸识别。我得到了它的工作,但当我尝试使用更大的图像文件夹时,它会因这些错误而停止:
OpenCV错误:cv :: OutOfMemoryError中的内存不足(无法分配411068928字节),文件C:\ builds \ master_PackSlaveAddon-win32-vc12-static \ opencv \ modules \ core \ src \ alloc.cpp,第52行OpenCV错误:断言失败(u!= 0)在cv :: Mat :: create中,文件C:\ builds \ master_PackSlaveAddon-win32-vc12-static \ opencv \ modules \ core \ src \ matrix.cpp,line 411线程异常“main”CvException [org.opencv.core.CvException:cv :: Exception:C:\ builds \ master_PackSlaveAddon-win32-vc12-static \ opencv \ modules \ core \ src \ matrix.cpp:411:error:(-215 )在FaceDetector.main的org.opencv.objdetect.CascadeClassifier.detectMultiScale_1(本地方法)中的org.opencv.objdetect.CascadeClassifier.detectMultiScale(CascadeClassifier.java:103)中的函数cv :: Mat :: create中的u!= 0 (FaceDetector.java:42)
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.plaf.synth.SynthSeparatorUI;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;
public class FaceDetector {
public static void main(String[] args) throws IOException {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
System.out.println("\nRunning FaceDetector");
CascadeClassifier faceDetector = new CascadeClassifier("C:/Users/Family/workspace/detect face/haarcascade_frontalface_alt.xml");
File folder = new File("C:\\Users\\Family\\Downloads\\Photos (4)");
File[] listOfFiles = folder.listFiles();
for (int j = 0; j < listOfFiles.length; j++) {
System.out.println(listOfFiles[j]);
}
for (int i = 0; i < listOfFiles.length-1; i++){
String picname = listOfFiles[i].toString();
System.out.println("reading: " + listOfFiles[i]);
Mat image = Imgcodecs.imread(picname);
MatOfRect faceDetections = new MatOfRect();
faceDetector.detectMultiScale(image, faceDetections);
System.out.println(String.format("Detected %s faces", faceDetections.toArray().length));
for (Rect rect : faceDetections.toArray()) {
Imgproc.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar (0, 255, 0));
}
Rect recta = new Rect();
for (Rect rect : faceDetections.toArray()) {
recta.x = rect.x;
recta.y = rect.y;
recta.width = rect.width;
recta.height = rect.height;
}
if (faceDetections.toArray().length == 1) {
BufferedImage picture = ImageIO.read(new File(picname));
BufferedImage croppedImage = picture.getSubimage(recta.x, recta.y, recta.width, recta.height);
// File finalCropped = new File(listOfFiles[i].toString()+"cropped.jpg");
File finalCropped = new File("C:/Users/Family/Downloads/CroppedImages/" + "cropped" + (i + 140) + ".jpg");
ImageIO.write(croppedImage, "jpg", finalCropped);
System.out.println("Cropping " + i + "/" + listOfFiles.length);
}
faceDetections.release();
}
System.out.println("Process complete.");
}
}
答案 0 :(得分:2)
您也应该释放image
个对象。我想建议最后使用try catch来正确释放。
image.release();
答案 1 :(得分:0)
您是否尝试过增加堆空间?看看here。
java.lang.OutOfMemoryError:当应用程序尝试将更多数据添加到堆空间区域时,将触发Java堆空间错误,但没有足够的空间。