zplab / zbar-py

Simple python 2 and 3 compatible interface to the zbar barcode-reading library
MIT License
82 stars 30 forks source link

ValueError: need more than 1 value to unpack #7

Closed Daltz333 closed 5 years ago

Daltz333 commented 7 years ago

EDIT: Found that I didn't assign a variable to scanner.scan(gray), assigned variable and error still there.

On Raspberry Pi3, Python 3.4, OpenCV3 I get the below error. Attached is code, error, and additional information. CODE:

import cv2
import zbar
import constants

camera = cv2.VideoCapture(0)

#initialize zbar reader
scanner = zbar.Scanner()

def capture(camera):
    while True:
        #end program if q is pressed
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

        #begin grabbing frames from the camera
        frame = camera.read()

        #convert frames to grayscale as zbar-py only accepts grayscale opencv
        gray = (frame, cv2.COLOR_BGR2GRAY)

        #scan frame for qrcode/barcode
        zbar_image = scanner.scan(gray)

        #print decoded data in frame
        for decoded in zbar_image:
            print(decoded.data)

capture(camera)

Error:

Traceback (most recent call last):
  File "Main.py", line 38, in <module>
    capture(camera)
  File "Main.py", line 32, in capture
    scanner.scan(gray)
  File "/home/pi/.virtualenvs/cv/lib/python3.4/site-packages/zbar/zbar.py", line 191, in scan
    height, width = image.shape
ValueError: need more than 1 value to unpack

When printing out the value of "gray" sometimes randomly I get

((False, None), 6)

And just as randomly I get

((True, array([[[159, 156, 166],
        [159, 156, 166],
        [161, 158, 168],
        ..., 
        [ 75,  86,  77],
        [ 82,  85,  77],
        [ 79,  82,  74]],

       [[158, 155, 164],
        [160, 157, 166],
        [156, 159, 167],
        ..., 
        [ 70,  85,  71],
        [ 79,  82,  73],
        [ 77,  80,  71]],

       [[156, 156, 166],
        [157, 157, 167],
        [156, 158, 170],
        ..., 
        [ 70,  84,  73],
        [ 74,  79,  74],
        [ 73,  78,  73]],

       ..., 
       [[231, 239, 233],
        [230, 238, 232],
        [229, 237, 231],
        ..., 
        [103, 120, 106],
        [107, 122, 108],
        [112, 127, 113]],

       [[231, 239, 233],
        [230, 238, 232],
        [229, 237, 231],
        ..., 
        [103, 122, 108],
        [106, 125, 111],
        [109, 128, 114]],

       [[232, 237, 233],
        [230, 235, 231],
        [233, 234, 231],
        ..., 
        [109, 124, 110],
        [112, 127, 113],
        [116, 131, 117]]], dtype=uint8)), 6)

But it always no matter what the result is, it always gives me the ValueError exception.

zpincus commented 5 years ago
        #convert frames to grayscale as zbar-py only accepts grayscale opencv
        gray = (frame, cv2.COLOR_BGR2GRAY)

This does not convert a frame to grayscale. It makes a 2-tuple of the frame and the cv2 constant. And sometimes frame is None, I guess. None of this is a zbar problem.