[英]Python - AttributeError: 'module' object has no attribute 'QueryFrame'
[英]AttributeError: 'module' object has no attribute 'QueryFrame'
我正在尝试使用python和openCV 3.0测试实时解码qr代码,但是现在我在终端上收到一条错误消息。 我试图在互联网上搜索,但仍然无法解决。 我能知道是什么错误。
这是我的Python代码:
import cv2 as cv
import zbar
def scanner_procces(frame,set_zbar):
set_width = 100.0 / 100
set_height = 90.0 / 100
coord_x = int(frame.width * (1 - set_width)/2)
coord_y = int(frame.height * (1 - set_height)/2)
width = int(frame.width * set_width)
height = int(frame.height * set_height)
get_sub = cv.GetSubRect(frame, (coord_x+1, coord_y+1, width-1, height-1))
cv.Rectangle(frame, (coord_x, coord_y), (coord_x + width, coord_y + height), (255,0,0))
cm_im = cv.CreateImage((get_sub.width, get_sub.height), cv.IPL_DEPTH_8U, 1)
cv.ConvertImage(get_sub, cm_im)
image = zbar.Image(cm_im.width, cm_im.height, 'Y800', cm_im.tostring())
set_zbar.scan(image)
for symbol in image:
print '\033[1;32mResult : %s symbol "%s" \033[1;m' % (symbol.type,symbol.data)
cv.ShowImage("webcam", frame)
cv.WaitKey(10)
if __name__ == "__main__":
cv.namedWindow("webcam", cv.WINDOW_AUTOSIZE)
capture = cv.VideoCapture(0)
set_zbar = zbar.ImageScanner()
while True:
frame = cv.QueryFrame(capture)
scanner_procces(frame,set_zbar)
这是错误代码:
AttributeError: 'module' object has no attribute 'QueryFrame'
这是回溯消息:
init done
opengl support available
select timeout
Traceback (most recent call last):
File "realtimetestwebcam.py", line 38, in <module>
scanner_procces(frame,set_zbar)
File "realtimetestwebcam.py", line 9, in scanner_procces
coord_x = int(frame.width * (1 - set_width)/2)
AttributeError: 'numpy.ndarray' object has no attribute 'width'
是由于opencv版本引起的错误吗? 谢谢。
使用cv2
,应使用
cv2.VideoCapture.read
而不是QueryFrame
。 有关更多信息,请参见此处 。
您可以尝试此代码
capture = cv.VideoCapture(0)
set_zbar = zbar.ImageScanner()
while True:
_,frame = capture.read()
更新
此错误讯息
AttributeError:“ numpy.ndarray”对象没有属性“ width”
您得到,因为那改变了返回值的格式。 使用cv frame
是某种类型的对象,使用cv2 frame
是np.ndarray
。 您可以使用shape
方法获取其尺寸,而不是width
attr。 从cv到cv2的迁移不是简单的过程,需要重写部分代码。
我通过安装python-opencv软件包解决了它
sudo apt-get install python-opencv
那解决了我的整个问题。 感谢kvorobiev一路为我提供帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.