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

13

我正在学习使用openCV,通过编写在YouTube上发现的一个简单的人脸识别程序进行自学。我已经安装了opencv版本2以及numpy 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/


1
根据您收到的错误,似乎您正在使用的是OpenCV 3.x而不是OpenCV 2.x。在Python解释器中,输入命令“cv2.__version__”以进行验证。 - svohara
5个回答

57
最新版本的OpenCV不再支持导入传统的cv模块。此外,常量的命名约定通常省略了前缀“CV_...”,并且有几个/许多名称已经有所改变。我认为你遇到了这两个问题。
具体而言,你报告的错误涉及到代码中的这个表达式:cv2.cv.CV_HAAR_SCALE_IMAGE。该表达式试图在你导入的cv2包的cv子模块中找到名为CV_HAAR_SCALE_IMAGE的常量。但是,现在没有了cv2.cv
在OpenCV 3中,我认为可以如下引用此常量:cv2.CASCADE_SCALE_IMAGE
此外,你可能会发现这个链接很有用。它指向在OpenCV源代码中找到的facedetect.py示例脚本。你可以在此示例中看到新常量名称的用法,并且还可以检查其它与旧版本不同的地方。

2

这段代码对我来说运行良好,我正在使用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)

0

2
欢迎来到SO!当你准备回答一个老问题(这个问题已经快5年了),而且已经有一个被接受的答案(这就是这里的情况)时,请问自己:我真的有实质性的改进吗?如果没有,考虑不回答。 - Timus

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)

0

这是更新后的代码,可以在带有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()

这是@svoahra的答案。 - Stuti Verma
抱歉,我遇到了以下错误: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'. - ah bon

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接