简体   繁体   English

Python-AttributeError:“模块”对象没有属性“ QueryFrame”

[英]Python - AttributeError: 'module' object has no attribute 'QueryFrame'

I'm really new to OpenCV but I need to find a way to detect faces using a webcam. 我真的是OpenCV的新手,但我需要找到一种使用网络摄像头检测面部的方法。 I have found following code from here . 我从这里找到了以下代码。 This is the original code. 这是原始代码。 I'm using Python 2.7 and opencv 3.0.0-beta version and Windows 8.1. 我正在使用Python 2.7opencv 3.0.0-beta版本以及Windows 8.1。

import cv2 as cv
import time
import Image

def DetectFace(image, faceCascade):

    min_size = (20,20)
    image_scale = 2
    haar_scale = 1.1
    min_neighbors = 3
    haar_flags = 0

    grayscale = cv.CreateImage((image.width, image.height), 8, 1)
    smallImage = cv.CreateImage(
            (
                cv.Round(image.width / image_scale),
                cv.Round(image.height / image_scale)
            ), 8 ,1)

    cv.CvtColor(image, grayscale, cv.CV_BGR2GRAY)
    cv.Resize(grayscale, smallImage, cv.CV_INTER_LINEAR)
    cv.EqualizeHist(smallImage, smallImage)

    faces = cv.HaarDetectObjects(
            smallImage, faceCascade, cv.CreateMemStorage(0),
            haar_scale, min_neighbors, haar_flags, min_size)

    if faces:
        for ((x, y, w, h), n) in faces:
            pt1 = (int(x * image_scale), int(y * image_scale))
            pt2 = (int((x + w) * image_scale), int((y + h) * image_scale))
            cv.Rectangle(image, pt1, pt2, cv.RGB(255, 0, 0), 5, 8, 0)

    return image

capture = cv.CaptureFromCAM(0)
faceCascade = cv.Load("haarcascades/haarcascade_frontalface_alt.xml")

while (cv.WaitKey(15)==-1):
    img = cv.QueryFrame(capture)
    image = DetectFace(img, faceCascade)
    cv.ShowImage("face detection test", image)

cv.ReleaseCapture(capture)

When I was running this program I've got errors saying, No module named Image . 当我运行该程序时,出现错误提示:没有名为Image的模块。 I commented it and run again and did following changes to the code. 我对其进行了注释,然后再次运行,并在更改代码后进行了操作。

capture = cv.CaptureFromCAM(0) to capture = cv.VideoCapture(0) and WaitKey to waitKey according to the errors popped up. capture = cv.CaptureFromCAM(0)capture = cv.VideoCapture(0)WaitKeywaitKey根据弹出错误。

But now it says AttributeError: 'module' object has no attribute 'QueryFrame' 但是现在它说AttributeError: 'module' object has no attribute 'QueryFrame'

I think there is a version problem or something. 我认为存在版本问题或其他问题。 I've already included haarcascades files as well. 我已经包括了haarcascades文件。 Please help me to correct that error and run this code well. 请帮助我纠正该错误并运行此代码。 As I mention I'm a fresher to opencv. 正如我提到的,我对opencv比较新鲜。

In cv2 , you use: cv2 ,您可以使用:

result, img = capture.read()  #capture is a cv2.VideoCapture instance

instead of QueryFrame . 而不是QueryFrame

You can also queue a frame for capture using 您也可以使用

capture.grab()

followed by 其次是

result, img = capture.retrieve()

to actually retrieve it. 实际检索它。 Use this second method if you want to queue up a frame, then do other stuff while you wait for it. 如果要排队一个帧,请使用第二种方法,然后在等待时进行其他操作。

Edit: 编辑:

You clearly are just trying to run a bunch of OpenCV1 functions using OpenCV2, and are going to have a lot of problems if you don't read the OpenCV2 documentation to know what functions have changed and what hasn't. 显然,您只是尝试使用OpenCV2运行一堆OpenCV1函数,如果您不阅读OpenCV2文档以了解哪些功能已更改,哪些未更改, 则会遇到很多问题。 StackOverflow isn't a "convert this old code" service, so I'm not going to go through every single old function in your program. StackOverflow不是“转换此旧代码”服务,因此我不会遍历程序中的每个旧功能。

However, to answer your follow up question, in OpenCV2 it is recommended to just create images using the numpy.zeros() function (as opposed to the old cv.CreateImage function). 但是,要回答您的后续问题,建议在OpenCV2中仅使用numpy.zeros()函数创建图像(与旧的cv.CreateImage函数相对)。

cv2 does not have a method QueryFrame, it is located in cv. cv2没有方法QueryFrame,它位于cv中。

import cv
img = cv.QueryFrame(capture)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError:“模块”对象没有属性“ QueryFrame” - AttributeError: 'module' object has no attribute 'QueryFrame' Python:AttributeError:'module'对象没有属性'socketpair' - Python: AttributeError: 'module' object has no attribute 'socketpair' Python AttributeError:“模块”对象没有属性“获取” - Python AttributeError: 'module' object has no attribute 'get' python - AttributeError:'module'对象没有属性'lock' - python - AttributeError: 'module' object has no attribute 'lock' Python错误:AttributeError:'module'对象没有属性 - Python error: AttributeError: 'module' object has no attribute Python AttributeError:“模块”对象没有属性“ Goslate” - Python AttributeError: 'module' object has no attribute 'Goslate' Python:AttributeError:“模块”对象没有属性“ randrange” - Python: AttributeError: 'module' object has no attribute 'randrange' python - AttributeError:'module'对象没有属性 - python - AttributeError: 'module' object has no attribute python:AttributeError,“模块”对象没有属性“某物” - python: AttributeError, 'module' object has no attribute 'something' AttributeError:'module'对象没有属性'subscribe'Python - AttributeError: 'module' object has no attribute 'subscribe' Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM