我正在教自己如何通过编写我在youtube上找到的简单的面部识别程序来使用openCV。我已经安装了opencv版本2以及numpy 1.8.0。我使用的是python2.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/
答案 0 :(得分:22)
最新的openCV不再允许导入旧版cv
模块。此外,常量的命名约定通常取消了前导“CV _...”,并且几个/许多名称已经有所改变。我认为你遇到了两个问题。
具体而言,您报告的错误与代码中的此表达式有关:cv2.cv.CV_HAAR_SCALE_IMAGE
。此表达式尝试在您导入的CV_HAAR_SCALE_IMAGE
包的cv
子模块中找到命名常量cv2
。但是,唉,再也没有cv2.cv了。
在openCV 3中,我相信这个常量现在引用如下:cv2.CASCADE_SCALE_IMAGE
此外,您可能会发现invisible()
有用。它是OpenCV源代码中的facedetect.py示例脚本。您可以在此示例中查看新常量名称的用法,也可以检查旧源/教程中的其他更改。
答案 1 :(得分:0)
似乎没有下载级联。
https://github.com/opencv/opencv/tree/master/data/haarcascades
下载上面链接中提供的haarcascades,应该适用于Open CV 4.2.0
请阅读xml中提到的许可协议
答案 2 :(得分:0)
这段代码对我来说很好用,我正在使用 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)
答案 3 :(得分: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)
答案 4 :(得分:-1)
以下是使用OpenCV3处理jupyter笔记本的更新代码:
[]
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()