Search code examples
pythonreal-timeobject-detectionqr-codedetection

Pyzbar library for image processing (for QR code) not working


I want to perform real-time QR code detection and here is the code I use for it:

import cv2
import numpy as np
from pyzbar.pyzbar import decode

cap = cv2.VideoCapture(0)

cap.set(3, 640)
cap.set(4, 480)

while True:
    success, img = cap.read()
    for barcode in decode(img):
        myData = barcode.data.decode('utf-8')
        print (myData)
        pts = np.array([barcode.polygon], np.int32)
        pts = pts.reshape((-1,1,2))
        cv2.polylines(img, [pts], True, (255,0,255),5)
        
    cv2.imshow('Result',img)
    cv2. waitKey(1)

But this is error output:

Traceback (most recent call last):
  File "C:\Users\BEYZANUR\AppData\Local\Programs\Python\Python39\lib\site-packages\pyzbar\zbar_library.py", line 58, in load
    dependencies, libzbar = load_objects(Path(''))
  File "C:\Users\BEYZANUR\AppData\Local\Programs\Python\Python39\lib\site-packages\pyzbar\zbar_library.py", line 50, in load_objects
    deps = [
  File "C:\Users\BEYZANUR\AppData\Local\Programs\Python\Python39\lib\site-packages\pyzbar\zbar_library.py", line 51, in <listcomp>
    cdll.LoadLibrary(str(directory.joinpath(dep)))
  File "C:\Users\BEYZANUR\AppData\Local\Programs\Python\Python39\lib\ctypes\__init__.py", line 452, in LoadLibrary
    return self._dlltype(name)
  File "C:\Users\BEYZANUR\AppData\Local\Programs\Python\Python39\lib\ctypes\__init__.py", line 374, in __init__
    self._handle = _dlopen(self._name, mode)
FileNotFoundError: Could not find module 'libiconv.dll' (or one of its dependencies). Try using 
the full path with constructor syntax.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\BEYZANUR\Projeler\Teknofest\e.py", line 3, in <module>
    from pyzbar.pyzbar import decode
  File "C:\Users\BEYZANUR\AppData\Local\Programs\Python\Python39\lib\site-packages\pyzbar\pyzbar.py", line 7, in <module>
    from .wrapper import (
  File "C:\Users\BEYZANUR\AppData\Local\Programs\Python\Python39\lib\site-packages\pyzbar\wrapper.py", line 151, in <module>
    zbar_version = zbar_function(
  File "C:\Users\BEYZANUR\AppData\Local\Programs\Python\Python39\lib\site-packages\pyzbar\wrapper.py", line 148, in zbar_function
    return prototype((fname, load_libzbar()))
  File "C:\Users\BEYZANUR\AppData\Local\Programs\Python\Python39\lib\site-packages\pyzbar\wrapper.py", line 127, in load_libzbar
    libzbar, dependencies = zbar_library.load()
  File "C:\Users\BEYZANUR\AppData\Local\Programs\Python\Python39\lib\site-packages\pyzbar\zbar_library.py", line 60, in load
    dependencies, libzbar = load_objects(Path(__file__).parent)
  File "C:\Users\BEYZANUR\AppData\Local\Programs\Python\Python39\lib\site-packages\pyzbar\zbar_library.py", line 54, in load_objects
    libzbar = cdll.LoadLibrary(str(directory.joinpath(fname)))
  File "C:\Users\BEYZANUR\AppData\Local\Programs\Python\Python39\lib\ctypes\__init__.py", line 452, in LoadLibrary
    return self._dlltype(name)
  File "C:\Users\BEYZANUR\AppData\Local\Programs\Python\Python39\lib\ctypes\__init__.py", line 374, in __init__
    self._handle = _dlopen(self._name, mode)
FileNotFoundError: Could not find module 'C:\Users\BEYZANUR\AppData\Local\Programs\Python\Python39\lib\site-packages\pyzbar\libzbar-64.dll' (or one of its dependencies). Try using the full path with constructor syntax.

Solution

  • Based on the paths of the logs, I assume you are on Windows.

    It seems you require C++ packages. If true, you could have found out by just googleing the meaningful part of the first element of the stacktrace:

    I had the same issue, till I read all the README and then I found:

    Windows error message
    If you see an ugly ImportError when importing pyzbar on Windows you will most likely need the Visual C++ Redistributable Packages for
    

    Visual Studio 2013. Install vcredist_x64.exe if using 64-bit Python, vcredist_x86.exe if using 32-bit Python.

    Installing vcredist_x64.exe solved the issue for me.