ADIN
ADIN

Reputation: 317

CV2 OpenCL, cv2.UMat object is not iterable

I'm using pyzbar library to decode QRcodes. Now I'm trying to use uMat to make this process more quickly. The problem is that pyzbar decode cannot accept umat variable.

File "C:\Python\lib\site-packages\pyzbar\pyzbar.py", line 175, in decode pixels, width, height = image TypeError: 'cv2.UMat' object is not iterable

Here is my code sample

import cv2
import numpy as np
from pyzbar.pyzbar import decode 
import matplotlib.pyplot as plt

cv2.ocl.setUseOpenCL(True)

for subdir, dirs, files in os.walk("Images"):
    for file in sorted(files):
        filepath = subdir + os.sep + file
        if filepath.endswith(".JPG"):

            image = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE)
            image = cv2.UMat(image)

            symbols = decode(image)

            if symbols:
                plt.title(symbols[0][0])
                plt.imshow(image)
                plt.show()

Upvotes: 2

Views: 1843

Answers (1)

KimKulling
KimKulling

Reputation: 2843

IU guess the reason for that error can be found in the documentation of decode from pyzbar:

def decode(image, symbols=None, scan_locations=False):
"""Decodes datamatrix barcodes in `image`.
Args:
    image: `numpy.ndarray`, `PIL.Image` or tuple (pixels, width, height)
    symbols (ZBarSymbol): the symbol types to decode; if `None`, uses
        `zbar`'s default behaviour, which is to decode all symbol types.
    scan_locations (bool): If `True`, results will include scan
        locations.

Decode expects an image or a matrix containing the data in a specific order, which is fullfilled by a mat-instance supported by OpenCV. The UMat-format does not fullfil this requirement and so the error will be occur.

Upvotes: 1

Related Questions