0

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.

This is my Python code:

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.

raaj5671
  • 105
  • 4
  • 12

2 Answers2

0

When you use cv2 you should use

cv2.VideoCapture.read

instead of QueryFrame. For more info see here.
You can try this code

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'

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.

kvorobiev
  • 5,012
  • 4
  • 29
  • 35
  • well now the error shows inside the scanner_process() . The error now is `AttributeError: 'tuple' object has no attribute 'width'` – raaj5671 Jul 22 '15 at 07:38
  • @raaj5671 Return format of `capture.read()` differ from `QueryFrame`. Please, check corrected answer – kvorobiev Jul 22 '15 at 07:43
  • now i am getting this error `AttributeError: 'numpy.ndarray' object has no attribute 'width' ` – raaj5671 Jul 22 '15 at 09:04
  • i have added the traceback message on my question now – raaj5671 Jul 22 '15 at 09:40
  • @raaj5671 You trying to add it to my answer. Please, add it to your question – kvorobiev Jul 22 '15 at 09:42
  • I am sorry about that. I accidentally add in your answer. Well now i have updated it – raaj5671 Jul 22 '15 at 09:49
  • well i just have to add the shape method is it? – raaj5671 Jul 22 '15 at 10:12
  • @raaj5671 `shape` method returns tuple. `frame` is 2D array -> this tuple will contains two elements - width and height. Try replace `frame.width` with `frame.shape[0]`. – kvorobiev Jul 22 '15 at 10:15
  • well now the error is showing for the height attribute so do i have to use `frame.shape[0]` or `frame.shape[1]`. I am so sorry I am too new to this opencv world. – raaj5671 Jul 22 '15 at 10:22
  • @raaj5671 Try to use `frame.shape[0]` instead of width and `frame.shape[1]` instead of height – kvorobiev Jul 22 '15 at 10:24
  • @raaj5671 If you want migrate to cv2 from cv you need to carefully read the documentation and manually rewrite your code. Or, probably, solution described in this [link](http://stackoverflow.com/questions/9084609/how-to-copy-a-image-region-using-opencv-in-python) will help you. – kvorobiev Jul 22 '15 at 10:41
  • i tried to follow the link you post me just now but now i am getting this error `ImportError: No module named cv` Well according to the answer from the link i change my import to `import cv2.cv` – raaj5671 Jul 22 '15 at 10:54
  • @raaj5671 Try [this](http://stackoverflow.com/questions/25215102/installing-opencv-for-python-on-ubuntu-getting-importerror-no-module-named-cv2) – kvorobiev Jul 22 '15 at 11:02
0

I solved it by installing the python-opencv package

sudo apt-get install python-opencv

That solved my whole problem. Thanks to kvorobiev for helping me all the way.

raaj5671
  • 105
  • 4
  • 12