我正在学习使用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/