使用OpenCV进行人脸识别时出现属性错误 [英] Attribute error while using opencv for face recognition

查看:324
本文介绍了使用OpenCV进行人脸识别时出现属性错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过编写一个在youtube上找到的简单面部识别程序来教自己如何使用openCV.我已经安装了opencv版本2以及numpy 1.8.0.我正在使用python2.7.

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.

我在下面的视频和文章链接中完全复制了此代码,但仍然出现错误. AttributeError:模块"对象没有属性"cv"我在做什么错了?

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?

这是我正在使用的代码.

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://realpython.com/blog/python/face-recognition -with-python/

推荐答案

最新的openCV不再允许导入旧版cv模块.此外,常量的命名约定通常取消了开头的"CV _...",并且几个/许多名称已进行了一些更改.我认为您遇到了两个问题.

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.

具体来说,您要报告的错误与代码中的此表达式有关:cv2.cv.CV_HAAR_SCALE_IMAGE.该表达式试图在导入的cv2包的cv子模块内找到命名常量CV_HAAR_SCALE_IMAGE.但可惜的是,现在不再有cv2.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.

在openCV 3中,我相信现在可以按以下方式引用此常量:cv2.CASCADE_SCALE_IMAGE

In openCV 3, I believe this constant is now referenced as follows: cv2.CASCADE_SCALE_IMAGE

此外,您可能会发现此链接很有用 .它是在OpenCV源代码中找到的facedetect.py示例脚本.您可以在此示例中看到新常量名称的用法,也可以检查新常量名称是否有来自较早来源/教程的其他更改.

Also, you may find this link useful. 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进行人脸识别时出现属性错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆