I am trying to test to decode a qr code real time using python and openCV 3.0.But now I am getting an error message on my terminal. I tried to search on the internet but I still unable to solve it. Can I know whats the error.
我正在尝试使用python和openCV 3.0进行测试以实时解码qr代码。但现在我在终端上收到错误消息。我试图在互联网上搜索,但我仍然无法解决它。我能知道这个错误吗?
This is my Python code:
这是我的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)
This is the error code:
这是错误代码:
AttributeError: 'module' object has no attribute 'QueryFrame'
This is the traceback message:
这是回溯消息:
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'
Is it the error because of the opencv version? Thank you.
这是因为opencv版本的错误吗?谢谢。
2 个解决方案
#1
0
When you use cv2
you should use
当你使用cv2时,你应该使用
cv2.VideoCapture.read
instead of QueryFrame
. For more info see here.
You can try this code
而不是QueryFrame。有关详情,请参阅此处。您可以尝试此代码
capture = cv.VideoCapture(0)
set_zbar = zbar.ImageScanner()
while True:
_,frame = capture.read()
Updated
This error message
此错误消息
AttributeError: 'numpy.ndarray' object has no attribute 'width'
AttributeError:'numpy.ndarray'对象没有属性'width'
you get because that changed the format of the return value. With cv frame
was an object with some type, and with cv2 frame
is np.ndarray
. Instead of width
attr you can get its dimensions with shape
method. Migration from cv to cv2 is not simple process and requires rewriting of a part of the code.
你得到因为它改变了返回值的格式。 cv框架是一个具有某种类型的对象,而cv2框架是np.ndarray。您可以使用shape方法获取其尺寸,而不是width attr。从cv迁移到cv2并不是一个简单的过程,需要重写部分代码。
#2
0
I solved it by installing the python-opencv package
我通过安装python-opencv包解决了这个问题
sudo apt-get install python-opencv
That solved my whole problem. Thanks to kvorobiev for helping me all the way.
这解决了我的整个问题。感谢kvorobiev一直帮助我。
#1
0
When you use cv2
you should use
当你使用cv2时,你应该使用
cv2.VideoCapture.read
instead of QueryFrame
. For more info see here.
You can try this code
而不是QueryFrame。有关详情,请参阅此处。您可以尝试此代码
capture = cv.VideoCapture(0)
set_zbar = zbar.ImageScanner()
while True:
_,frame = capture.read()
Updated
This error message
此错误消息
AttributeError: 'numpy.ndarray' object has no attribute 'width'
AttributeError:'numpy.ndarray'对象没有属性'width'
you get because that changed the format of the return value. With cv frame
was an object with some type, and with cv2 frame
is np.ndarray
. Instead of width
attr you can get its dimensions with shape
method. Migration from cv to cv2 is not simple process and requires rewriting of a part of the code.
你得到因为它改变了返回值的格式。 cv框架是一个具有某种类型的对象,而cv2框架是np.ndarray。您可以使用shape方法获取其尺寸,而不是width attr。从cv迁移到cv2并不是一个简单的过程,需要重写部分代码。
#2
0
I solved it by installing the python-opencv package
我通过安装python-opencv包解决了这个问题
sudo apt-get install python-opencv
That solved my whole problem. Thanks to kvorobiev for helping me all the way.
这解决了我的整个问题。感谢kvorobiev一直帮助我。