When trying to read a QRcode using the following code:

detector = cv2.QRCodeDetector()
while True:
        _, img = cap.read()
        data, _, _ = detector.detectAndDecode(img)

I get this exception upon trying to read a QR code:

Exception in thread Thread-2:
Traceback (most recent call last):
  File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
  File "/usr/lib/python3.5/threading.py", line 862, in run
    self._target(*self._args, **self._kwargs)
  File "main.py", line 100, in qr_checker
    jwt, _, _ = detector.detectAndDecode(img)
cv2.error: OpenCV(4.1.1) /home/pi/opencv-python/opencv/modules/core/src/alloc.cpp:72: error: (-4:Insufficient memory) Failed to allocate 152100e0000 bytes in function 'OutOfMemoryError'

The error is fairly descriptive: insufficient memory, but I don't know how to avoid it. Is there a more efficient approach I can take that avoids this?

It does work with a simpler QR-code that encodes less data, but I need it to work wit more complex QR-codes as well.

  • Have you try to read the QR code outside your while loop ? – Ephemeral May 25 at 17:34
  • You could try forcing a garbage collection with import gc gc.collect() just before the call to the read. – Andyroo May 25 at 18:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.