Python 使用opencv进行人脸识别时出现属性错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36242860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Attribute error while using opencv for face recognition
提问by RatstabOfficial
I am teaching myself how to use openCV by writing a simple face recognition program I found on youtube. I have installed opencv version 2 as well as numpy 1.8.0. I am using python2.7.
我正在通过编写我在 youtube 上找到的一个简单的人脸识别程序来自学如何使用 openCV。我已经安装了 opencv 版本 2 以及 numpy 1.8.0。我正在使用 python2.7。
I copyed this code exactly how it was done in the video and article links below, yet I keep getting errors. AttributeError: 'module' object has no attribute 'cv' What am I doing wrong?
我在下面的视频和文章链接中完全复制了这段代码,但我不断收到错误消息。AttributeError: 'module' 对象没有属性 'cv' 我做错了什么?
Here is the code I'm using.
这是我正在使用的代码。
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://www.youtube.com/watch?v=IiMIKKOfjqE
https://realpython.com/blog/python/face-recognition-with-python/
https://realpython.com/blog/python/face-recognition-with-python/
回答by svohara
The latest openCV no longer allows importing the legacy cv
module. Furthermore the naming convention of the constants generally does away with the leading "CV_..." and several/many of the names have been altered somewhat. I think you are running into both problems.
最新的 openCV 不再允许导入旧cv
模块。此外,常量的命名约定通常取消了前导“CV_...”,并且一些/许多名称已有所改变。我认为你同时遇到了这两个问题。
Specifically, the error you are reporting is in regards to this expression in your code: cv2.cv.CV_HAAR_SCALE_IMAGE
. This expression is trying to find the named constant CV_HAAR_SCALE_IMAGE
within the cv
submodule of the cv2
package you imported. But alas, there is no cv2.cv anymore.
具体来说,您报告的错误与代码中的此表达式有关:cv2.cv.CV_HAAR_SCALE_IMAGE
。此表达式试图CV_HAAR_SCALE_IMAGE
在您导入cv
的cv2
包的子模块中查找命名常量。但是,唉,没有 cv2.cv 了。
In openCV 3, I believe this constant is now referenced as follows: cv2.CASCADE_SCALE_IMAGE
在 openCV 3 中,我相信这个常量现在引用如下: cv2.CASCADE_SCALE_IMAGE
Also, you may find this linkuseful. It is to the facedetect.py sample script found in the OpenCV source code. You can see the usage of the new constant name in this example, and you may also inspect it for other changes from older sources/tutorials.
此外,您可能会发现此链接很有用。它是在 OpenCV 源代码中找到的 facedetect.py 示例脚本。您可以在此示例中看到新常量名称的用法,您还可以检查它是否来自旧源/教程的其他更改。
回答by AvkashChauhan
Here is the updated code which works into jupyter notebook with OpenCV3:
这是使用 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()