gpt4 book ai didi

java - OpenCV java 人脸检测 NullPointerException

转载 作者:太空宇宙 更新时间:2023-11-03 21:33:44 25 4
gpt4 key购买 nike

我正在 mac 中学习 OpenCV java。我正在在线试用该教程,但在线程“主”java.lang.NullPointerException 中出现异常

这是我的代码

import org.opencv.core.*;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;


public class testApp {
public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
CascadeClassifier faceDetector = new CascadeClassifier(testApp.class
.getResource("/Users/myName/Downloads/opencv/data/haarcascades/haarcascade_frontalface_alt2.xml")
.getPath());

Mat image = Imgcodecs.imread(testApp.class
.getResource("lena.png")
.getPath());

MatOfRect faceD = new MatOfRect();
faceDetector.detectMultiScale(image,faceD);
for (Rect rect:faceD.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));
}

String outputFile = "OUTPUT.png";
//System.out.println(String.format("writing %s", outputFile));
Imgcodecs.imwrite(outputFile,image);

}
}

错误

Exception in thread "main" java.lang.NullPointerException
at testApp.main(testApp.java:13)

请问如何解决,谢谢

最佳答案

好像.getResource("/Users/myName/Downloads/opencv/data/haarcascades/haarcascade_frontalface_alt2.xml")返回null,因为找不到资源。因此,我建议您检查上面的路径是否正确。

关于java - OpenCV java 人脸检测 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43705388/

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