使用opencv进行人脸识别时出现属性错误

Attribute error while using opencv for face recognition

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

我在下面的视频和文章链接中完全复制了这段代码,但我总是出错。

AttributeError: 'module' object has no attribute '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 不再允许导入遗留的 cv 模块。此外,常量的命名约定通常取消了前导 "CV_..." 和 several/many 的名称已经有所改变。我认为您 运行 遇到了这两个问题。

具体来说,您报告的错误与您代码中的这个表达式有关: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 示例脚本。您可以在本示例中看到新常量名称的用法,您还可以检查它是否有旧 sources/tutorials.

的其他更改

这里是使用 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()

似乎没有下载 haarcascade。

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

下载上面 link 中可用的 haarcascades,应该适用于 Open CV 4.2.0

请阅读xml

中提到的许可协议

这段代码对我来说工作正常我正在使用 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)

这段代码很适合我:

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)