简体   繁体   English

OpenCV无法获得haarcascade

[英]OpenCV can't get a haarcascade

I'm writing a Python Script using OpenCV library.我正在使用 OpenCV 库编写 Python 脚本。 The code works flawlessly, except one bit.代码工作完美,除了一位。 I'm going to build the script with pyinstaller so I need to reference the haarcascade.我将使用 pyinstaller 构建脚本,因此我需要引用 haarcascade。

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')

This works, but this:这行得通,但是:

def resource_path(relative_path):
    try:
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")

    return os.path.join(base_path, relative_path)


face_cascade = cv2.CascadeClassifier(resource_path('haarcascade_frontalface_alt.xml'))

doesn't.没有。 Need help with solving this problem需要帮助解决这个问题

Here is the problem: the haarcascade won't load and the "detectMultiScale" will fail.问题是:haarcascade 不会加载,“detectMultiScale”会失败。

The same solution worked for my another project相同的解决方案适用于我的另一个项目

Here's the eror I'm getting:这是我得到的错误:[我得到的错误] I tried using "face_cascade.load()" And it works like this:我尝试使用“face_cascade.load()”,它是这样工作的:

face_cascade.load('haarcascade_frontalface_alt.xml')

But again doesn't work with "resource_path" function但同样不适用于“resource_path”功能

Doing this这样做

import os.path
print(os.path.isfile(resource_path('haarcascade_frontalface_alt.xml')))

Prints "True" in the console在控制台中打印“True”

Also I tried deleting all the code related to detecting faces, and I found that OpenCV successfully captures my webcam我也尝试删除所有与检测人脸相关的代码,我发现 OpenCV 成功捕获了我的网络摄像头

Here is all the code: if someone's interested这是所有代码:如果有人感兴趣

import numpy as np
import cv2, os



cap = cv2.VideoCapture(0)
xe = 0
ye = 0
we = 0
he = 0
def resource_path(relative_path):
    try:
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")

    return os.path.join(base_path, relative_path)


import os.path
print(os.path.isfile(resource_path('haarcascade_frontalface_alt.xml')))



face_cascade = cv2.CascadeClassifier(resource_path('haarcascade_frontalface_alt.xml'))
face_cascade.load(resource_path('haarcascade_frontalface_alt.xml'))


while 1:
    ret = cap.set(3,640);
    ret = cap.set(4,480);
    ret, img = cap.read()

    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.2, 5)
    dst = img

    for (x,y,w,h) in faces:
        dst = img
        rows,cols,channels = img.shape
        xe = x
        ye = y
        we = w
        he = h

    rows,cols,channels = img.shape
    pts1 = np.float32([[xe-100,ye-100],[xe+2*we+200,ye-100],[xe-100,ye+2*he+200],[xe+2*we+200,ye+2*he+200]])
    pts2 = np.float32([[0,0],[cols,0],[0,rows],[cols,rows]])
    M = cv2.getPerspectiveTransform(pts1,pts2)
    dst = cv2.warpPerspective(img,M,(300,300))
    dst = cv2.resize(dst,(cols, rows), interpolation = cv2.INTER_CUBIC)

    cv2.imshow('img',img)
    k = cv2.waitKey(30) & 0xff
    if k == 27:
        break

cap.release()
cv2.destroyAllWindows()

Edit:编辑:

I found the reason for the error: the pass to haarcascade contained cyrillic symbols that's why OpenCV had difficulties with loading the file我找到了错误的原因:传递给 haarcascade 包含西里尔符号,这就是 OpenCV 加载文件时遇到困难的原因

Is the haarcascade file actually in the place you expect it to be? haarcascade 文件是否真的在您期望的位置?

In your resource_path function you could check whether the file actually exists at the full path that is created to be sure.在您的 resource_path 函数中,您可以检查文件是否确实存在于创建的完整路径中以确保。

Get rid of all cyrillic symbols in the path to haarcascade if you get this problem如果遇到此问题,请删除 haarcascade 路径中的所有西里尔符号

Okay so even I was facing the same problem.好吧,即使我也面临着同样的问题。 the thing is you have to externally provide the directory link of haarcascade to the classifier function.问题是你必须在外部提供 haarcascade 的目录链接到分类器功能。

for me using anaconda it was at this directory:对我来说使用 anaconda 的是这个目录:

C:\Users\lhari\anaconda3\pkgs\libopencv-3.4.2-h20b85fd_0\Library\etc\haarcascades C:\Users\lhari\anaconda3\pkgs\libopencv-3.4.2-h20b85fd_0\Library\etc\haarcascades

Then you can join your paths and put it like below as it worked for me!!然后你可以加入你的路径并像下面这样对我有用!

path = os.path.join(r"<Your directory to the opencv package>",  r"haarcascade_frontalface_default.xml")
face_classifier = cv2.CascadeClassifier(path)

What I can understand is that you need to pass the facecascade file while building the exe.我能理解的是你需要在构建exe时传递facecascade文件。

TRY THIS:试试这个:

pyinstaller <filename> --add-data="<location of cascade>:.>"

by running this the application will have the cascade file.通过运行此应用程序将具有级联文件。 more details can be found here可以在此处找到更多详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM