【问题标题】:Attribute error while using opencv for face recognition使用opencv进行人脸识别时出现属性错误
【发布时间】:2016-07-14 13:29:54
【问题描述】:

我正在通过编写我在 youtube 上找到的简单人脸识别程序来自学如何使用 openCV。我已经安装了opencv version 2 和numpy 1.8.0。我正在使用python 2.7。

我完全按照下面的视频和文章链接中的方式复制了这段代码,但我不断收到错误。

AttributeError: 'module' 对象没有属性 'cv'

我做错了什么? 这是我正在使用的代码。

import cv2
import sys

# Get user supplied values
imagePath = sys.argv[1]
cascPath = sys.argv[2]

# Create the haar cascade
faceCascade = cv2.CascadeClassifier(cascPath)

# Read the image
image = cv2.imread(imagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Detect faces in the image
faces = (faceCascade.detectMultiScale(
    gray,
    scaleFactor=1.1,
    minNeighbors=5,
    minSize=(30, 30),
    flags = cv2.cv.CV_HAAR_SCALE_IMAGE)
)

print "Found {0} faces!".format(len(faces))

# Draw a rectangle around the faces
for (x, y, w, h) in faces:
    cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)

cv2.imshow("Faces found", image)
cv2.waitKey(0)

https://www.youtube.com/watch?v=IiMIKKOfjqE

https://realpython.com/blog/python/face-recognition-with-python/

【问题讨论】:

  • 出现错误,看来您使用的是 OpenCV 3.x,而不是 OpenCV 2.x。在 python 解释器中,发出命令cv2.__version__ 进行验证。

标签: python opencv numpy face-recognition


【解决方案1】:

最新的 openCV 不再允许导入旧的 cv 模块。此外,常量的命名约定通常会取消前导的“CV_...”,并且一些/许多名称已经有所改变。我认为您遇到了这两个问题。

具体来说,您报告的错误与代码中的此表达式有关:cv2.cv.CV_HAAR_SCALE_IMAGE。此表达式试图在您导入的 cv2 包的 cv 子模块中查找命名常量 CV_HAAR_SCALE_IMAGE。但是很可惜,已经没有 cv2.cv 了。

在 openCV 3 中,我相信这个常量现在引用如下:cv2.CASCADE_SCALE_IMAGE

另外,您可能会发现this link 很有用。它指向 OpenCV 源代码中的 facedetect.py 示例脚本。您可以在此示例中看到新常量名称的用法,您还可以检查它是否有来自旧源/教程的其他更改。

【讨论】:

    【解决方案2】:

    这是使用 OpenCV3 进入 jupyter notebook 的更新代码:

    []
    import cv2
    import matplotlib.pyplot as plt
    %matplotlib inline 
    
    []
    # Get user supplied values
    imagePath = "abba.png"
    cascPath = "haarcascade_frontalface_default.xml"
    
    []
    # Create the haar cascade
    faceCascade = cv2.CascadeClassifier(cascPath)
    
    []
    # Create the haar cascade
    faceCascade = cv2.CascadeClassifier(cascPath)
    
    []
    # Read the image
    image = cv2.imread(imagePath)
    
    []
    plt.imshow(image)
    plt.show()
    
    []
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    
    []
    # Detect faces in the image
    faces = faceCascade.detectMultiScale(
       gray,
       scaleFactor=1.1,
       minNeighbors=5,
       minSize=(30, 30),
       flags = cv2.CASCADE_SCALE_IMAGE #flags = cv2.cv.CV_HAAR_SCALE_IMAGE
    )
    
    []
    print("Found {0} faces!".format(len(faces)))
    
    
    []
    # Draw a rectangle around the faces
    for (x, y, w, h) in faces:
        cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
    
    []
    plt.imshow(image)
    plt.show()
    

    【讨论】:

    • 这是@svohara 的回答。
    • 对不起,我收到如下错误:OpenCV(3.4.4) C:\projects\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp:1698: error: (-215 :Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'。
    【解决方案3】:

    似乎没有下载haarcascades。

    https://github.com/opencv/opencv/tree/master/data/haarcascades

    下载上述链接中可用的 haarcascades,应该适用于 Open CV 4.2.0

    请阅读xml中提到的许可协议

    【讨论】:

    • 欢迎来到 SO!当你要回答一个已经有公认答案的老问题(这个问题差不多 5 年了)时(这里就是这种情况),请问问自己:我真的有实质性的改进吗?如果没有,请考虑不要回答。
    【解决方案4】:

    这段代码对我来说运行良好,我正在使用 opencv3 库,请尝试一下

    import cv2
    import sys
     # Get user supplied values
    imagePath = sys.argv[1]
    cascPath = sys.argv[2]
    # Create the haar cascade
    faceCascade = cv2.CascadeClassifier(cascPath)
    # Read the image
    image = cv2.imread(imagePath)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    
    # Detect faces in the image
    faces = faceCascade.detectMultiScale(
        gray,
        scaleFactor=1.1,
        minNeighbors=5,
        minSize=(30, 30),
        flags=cv2.CASCADE_SCALE_IMAGE
    )
    
    # Draw a rectangle around the faces
    for (x, y, w, h) in faces:
        cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
    cv2.imshow("Faces found", image)
    cv2.waitKey(0)
    

    【讨论】:

      【解决方案5】:

      这段代码很适合我:

      import cv2
      
      imagePath = (
      "path to image")
      cascPath = ("..\haarcascade_frontalface_default.xml")
      
      # Create the haar cascade
      faceCascade = cv2.CascadeClassifier(cascPath)
      # Read the image
      image = cv2.imread(imagePath)
      gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
      # Detect faces in the image
      faces = faceCascade.detectMultiScale(
      gray,
      scaleFactor=1.1,
      minNeighbors=5,
      minSize=(30, 30),
      # flags = cv2.CV_HAAR_SCALE_IMAGE
      )
      # print "Found {0} faces!".format(len(faces))
      # Draw a rectangle around the faces
      for (x, y, w, h) in faces:
      cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
      cv2.imshow("Faces found", image)
      cv2.waitKey(0)
      

      【讨论】:

        猜你喜欢
        • 2014-01-20
        • 2011-08-01
        • 1970-01-01
        • 2023-03-25
        • 2021-03-01
        • 2012-06-15
        • 1970-01-01
        • 2015-12-02
        相关资源
        最近更新 更多