java OpenCV 3.0.0 FaceDetect 示例失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27344741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:39:40  来源:igfitidea点击:

OpenCV 3.0.0 FaceDetect Sample fails

javaopencv

提问by maxbit89

I am trying to get OpenCV running i am using the following sample code

我正在尝试运行 OpenCV 我正在使用以下 示例代码

I get the following Error line displayed:

我显示以下错误行:

OpenCV Error: Assertion failed (!empty()) in cv::CascadeClassifier::detectMultiScale, file ..\..\..\..\opencv\modules\objdetect\src\cascadedetect.cpp, line 1580
Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: ..\..\..\..\opencv\modules\objdetect\src\cascadedetect.cpp:1580: error: (-215) !empty() in function cv::CascadeClassifier::detectMultiScale
]
    at org.opencv.objdetect.CascadeClassifier.detectMultiScale_1(Native Method)
    at org.opencv.objdetect.CascadeClassifier.detectMultiScale(CascadeClassifier.java:176)
    at org.maxbit.opencv.samples.DetectFaceDemo.run(SampleB.java:29)
    at org.maxbit.opencv.samples.SampleB.main(SampleB.java:51)

Can any body tell me what that error means or how to debug this?

任何人都可以告诉我那个错误是什么意思或如何调试它?

回答by Aung Myat Hein

I also faced the problem. The problem is in the .getPath() return an absolute path of the format.

我也遇到了这个问题。问题在于 .getPath() 返回格式的绝对路径。

Eg: "/C:/Users/projects/FaceDetection/bin/com/face/detection/haarcascade_frontalface_alt.xml".

例如:“/C:/Users/projects/FaceDetection/bin/com/face/detection/haarcascade_frontalface_alt.xml”。

So change the code like this.

所以把代码改成这样。

CascadeClassifier faceDecetor = new CascadeClassifier(FaceDetection.class.getResource("haarcascade_frontalface_alt.xml").getPath().substring(1));

回答by Kiran

This happens usually for two reasons.

发生这种情况通常有两个原因。

  1. Cascade classifier file lbpcascade_frontalface.xmlnot present at specified path.
  2. Cascade classifier file is corrupted.
  1. lbpcascade_frontalface.xml指定路径中不存在级联分类器文件。
  2. 级联分类器文件已损坏。

To get an error message instead of exception during runtime, try code sample as below. The CascadeClassifierconstructoris silent, if it cannot load the cascade classifier XML. The onus is on the developer to call the empty()method and check if classifier is loaded correctly

要在运行时获取错误消息而不是异常,请尝试以下代码示例。如果CascadeClassifier构造函数无法加载级联分类器 XML,则它是静默的。开发人员有责任调用该empty()方法并检查分类器是否正确加载

CascadeClassifier cascade = new CascadeClassifier( CASCADE_CLASSIFIER_PATH );
if ( cascade.empty() ) {
    //handler error here
}

Exception you got is from OpenCV native code assertion here.

您得到的例外来自此处的OpenCV 本机代码断言。

回答by bkamery

I ran into this same error running on a Windows box. This sample runs on linux but not Windows.

我在 Windows 机器上运行时遇到了同样的错误。此示例在 linux 上运行,但不在 Windows 上运行。

The problem is in the .getPath()call after getResource()for both the xml file and the image.

问题在于对 xml 文件和图像的.getPath()调用getResource()

The problem is that the URL.getPath()and the URL.getFile()both return an absolute path of the format "/c:/...".

问题是URL.getPath()URL.getFile()都返回格式为“/c:/...”的绝对路径。

The OpenCV routines choke on this it must be "c:/..." (no leading '/'). This seems like a bug in the early part of version 3.0.0?

OpenCV 例程对此感到窒息,它必须是“c:/...”(没有前导“/”)。这似乎是 3.0.0 版本早期的一个错误?

I hope this helps, OpenCV for Java seems like a great tool ... but it is a bit frustrating when the examples don't work.

我希望这会有所帮助,用于 Java 的 OpenCV 似乎是一个很棒的工具......但是当示例不起作用时它有点令人沮丧。

回答by Imad Eddine Bekouche

There is a problem with the latest openCV it doesn't work when you have spaces in your path so do this:

最新的 openCV 存在一个问题,当您的路径中有空格时它不起作用,因此请执行以下操作:

String s =CameraPanel.class.getResource("lbpcascade_frontalface.xml").getPath().substring(1);
String[] split = s.split("%20");
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < split.length-1; i++) {
    stringBuilder.append(split[i]+" ");
}
stringBuilder.append(split[split.length-1]);
faceDetector = new CascadeClassifier(stringBuilder.toString());

回答by le fred

I ran into the same issue: On windows, OpenCV chokes on both the prepended '\' and any whitespace in the path, as both Imad and Aung have noted. My solution is a bit shorter than Imad's:

我遇到了同样的问题:正如 Imad 和 Aung 所指出的那样,在 Windows 上,OpenCV 在前置的 '\' 和路径中的任何空白处都被阻塞。我的解决方案比 Imad 的要短一点:

Change this:

改变这个:

CascadeClassifier faceDecetor = new CascadeClassifier(
     getClass().class.getResource("haarcascade_frontalface_alt.xml").getPath());

To this:

对此:

CascadeClassifier faceDecetor = new CascadeClassifier(
     getClass().class.getResource("haarcascade_frontalface_alt.xml").getPath().substring(1).replaceAll("%20", " "));

回答by OldCurmudgeon

For me the simplest solution was:

对我来说,最简单的解决方案是:

private void checkboxSelection(String classifierPath) {
    // load the classifier(s)
    faceCascade.load(classifierPath);
    // Did it work?
    if (faceCascade.empty()) {
        // Try the full path
        String resource = getClass().getResource(classifierPath).getPath();
        // Discard leading / if present.
        if ( resource.startsWith("/")) {
            resource = resource.substring(1);
        }
        faceCascade.load(resource);
    }
    // now the video capture can start
    cameraButton.setDisable(false);
}

回答by Wu Yuan Chun

I am using openCv 3.4.1 I think there's a bug in CascadeClassifier initializer. In order to get rid of this error, I must call "load" once again. Hope this solution could help.

我正在使用 openCv 3.4.1 我认为 CascadeClassifier 初始值设定项中存在错误。为了摆脱这个错误,我必须再次调用“load”。希望这个解决方案能有所帮助。

cascadeClassifier = new CascadeClassifier(mCascadeFile.getAbsolutePath());
cascadeClassifier.load(mCascadeFile.getAbsolutePath());

回答by Kishwar Kumar

I faced problem on Mac (OSX) Java.

我在 Mac (OSX) Java 上遇到了问题。

    CameraFrame.class.getResource("haarcascade_frontalface_alt.xml").getPath().substring(1) 
returned "Users/username/Desktop/JavaProjects/Camera/bin/haarcascade_frontalface_alt.xml".

whereas path should start with "/"therefore I appended "/".

而路径应该以开头,"/"因此我附加了"/".

    face = new CascadeClassifier("/" + 
CameraFrame.class.getResource("haarcascade_frontalface_alt.xml").getPath().substring(1));

It works OK now :)

现在可以正常工作了:)