gpt4 book ai didi

java opencv错误内存不足且断言失败

转载 作者:行者123 更新时间:2023-12-02 04:27:31 26 4
gpt4 key购买 nike

我正在用java开发一个程序,该程序使用opencv库来获取图像文件夹并仅裁剪脸部。它使用 opencv 的人脸识别。我让它工作了,但是当我尝试使用更大的图像文件夹时,它会停止并出现以下错误:

OpenCV Error: Insufficient memory (Failed to allocate 411068928 bytes) in cv::OutOfMemoryError, file C:\builds\master_PackSlaveAddon-win32-vc12-static\opencv\modules\core\src\alloc.cpp, line 52 OpenCV Error: Assertion failed (u != 0) in cv::Mat::create, file C:\builds\master_PackSlaveAddon-win32-vc12-static\opencv\modules\core\src\matrix.cpp, line 411 Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: C:\builds\master_PackSlaveAddon-win32-vc12-static\opencv\modules\core\src\matrix.cpp:411: error: (-215) u != 0 in function cv::Mat::create ] at org.opencv.objdetect.CascadeClassifier.detectMultiScale_1(Native Method) at org.opencv.objdetect.CascadeClassifier.detectMultiScale(CascadeClassifier.java:103) at FaceDetector.main(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.");

}
}

最佳答案

您也应该释放image对象。我建议使用 try catch finally 来正确释放。

image.release();

关于java opencv错误内存不足且断言失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31947190/

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