【问题标题】:OpenCV haarcascades loading is NOT working at allOpenCV haarcascades加载根本不起作用
【发布时间】:2020-09-27 14:18:11
【问题描述】:

我正在使用带有 OpenCV 4.4.0 的 Visual Studio 2019 每件事都很棒,但是当我想开始人脸检测时,级联分类器不会加载 haarcascade

你也必须知道我在c分区安装了openCV,这是一个简单的代码


#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <opencv2\opencv.hpp>
#include <opencv2/highgui/highgui.hpp>


#include <Windows.h>
#include <vector>
#include <stdio.h>

using namespace std;
using namespace cv;

int main()
{
    
    VideoCapture cam(0);
    Mat img;
    CascadeClassifier detector;
    vector<Rect> faces;
    Point p[2];
    bool cap = false;

    if (!detector.load("c:\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_default.xml"))
    {
        cout << "Image Detector Doesn't work\n";
        return EXIT_FAILURE;
    }

    if (!cam.isOpened())
    {
        cout << "Can't Open Camera\n";
        return EXIT_FAILURE;
    }

    while (!cap)
    {
        cam.read(img);
        imshow("Cam", img);
        waitKey(0);
        if (GetAsyncKeyState(VK_ESCAPE))
            cap = true;
    }

    destroyWindow("Cam");

    cout << "Detecting Face...\n";
    detector.detectMultiScale(img, faces);
    
    for (int i = 0; i < faces.size(); i++)
    {
        p[0] = Point(faces[i].x,faces[i].y);
        p[1] = Point(faces[i].x + faces[i].height,faces[i].y + faces[i].width);
        rectangle(img,p[0],p[1],Scalar(0,0,255),3);
    }

    imwrite("Result.jpg",img);

    return EXIT_SUCCESS;
}

此代码不会加载 haarcascade,它会在 cmd 中返回“无法加载”

所以我真的需要帮助并感谢大家

【问题讨论】:

    标签: c++ opencv visual-studio-2019


    【解决方案1】:

    \ 在 C++ 字符串文字中用作转义序列。

    因此,您应该使用\\ 在其中放置一个字符\

    if (!dec.load("c:\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_default.xml"))
    

    【讨论】:

    • 好吧,你是写的,但我昨天真的离开了,现在打开它,它工作了,所以谢谢你,但是如果你现在为什么还有另一个文件仍然不能工作,所以请帮忙,我现在把它写成一个问题
    猜你喜欢
    • 1970-01-01
    • 2013-10-02
    • 2014-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-14
    • 2015-07-21
    相关资源
    最近更新 更多