Cv2.imread() not always getting the data from qr codes

Hi!
I am new here, I have a strange problem:

I used python and the module qrcode to make qr codes for 162 students. The qr codes contain the data: student number, name.

After making them, I want to check that the data in the qr code is correct, so I do this:

# show the data from each QR code

missing = [ ]
count=0
    
for f in QRfiles:
    img = cv2.imread(savepathQRcodes + f)
    detector = cv2.QRCodeDetector()
    data, bbox, straight_qrcode = detector.detectAndDecode(img)
    #text = data.split(':')
    #print('number, name: ', text[0], text[1])
    if data == '':
        count+=1
        missing.append(f)
    print(data)

Oddly, cv2.imread() cannot read the data in all of the qr codes. From 162, about 20 do not show any data in the above loop.

However, if I use my mobile phone to read the qr codes which do not show data, I can see the correct data. The data is there.

Have you had this problem? Do you know what might be the problem?

I need to be able to read these qr codes.

May be you should check if file exist before digging in opencv code.
You can use os.path.isfile( path )
If problem is in opencv may be you can share a good qr code file and a wrong qr code file

1 Like

Thanks, but the files exist.

I made them.

I can open the .png files with Image Viewer and read them with my mobile phone.

They are all good. Even the ones that show no data in my loop can be read with my phone.

Also, the QR codes which do not show data in my loop are not always the same.

I have made them many times from the same .csv file.

I see 2 possibilities: my computer has a read memory problem, or cv2.imread() has a problem

please use imshow to view the data you receive from imread.

perhaps you blame imread when the problem occurs later, such as in the QR code detector part.

also, please post the exact pictures that give you issues here or on some other site so we can investigate.

1 Like

Sorry but you have to insert os.path.isfile( savepathQRcodes + f ) in your loop :

for f in QRfiles:
    if os.path.isfile( savepathQRcodes + f ):
        img = cv2.imread(savepathQRcodes + f)
        if img is None:
            print("File exists but imread cannot read it : ", savepathQRcodes + f)
        else:
            detector = cv2.QRCodeDetector()
            data, bbox, straight_qrcode = detector.detectAndDecode(img)
            if data == '':
                count+=1
                missing.append(f)
                print("There is no data in file : ", savepathQRcodes + f)
            else :
                print(data)
    else:
        print("This file does not exist : ", savepathQRcodes + f)

What’s your platform and opencv version ?

Thanks again, really, but the files really exist.

When I first noticed this, I checked a couple of the files that showed no data.

I also made a function to read 1 file and I got back nothing.

That is why I made the list: missing

I then looked in the folder to make sure these files exist and can be read with my phone. They exist and can be found and read with my phone.

Whatever the problem is, it is not non-existence!

I use Ubuntu 18.04,

pedro@pedro-512ssd:~$ python3 -c “import cv2; print(cv2.version)”
4.2.0

So, version 4.2

4.2 update your opencv version : History for modules/objdetect/src/qrcode.cpp - opencv/opencv · GitHub

you should investigate whether imread itself fails or whether the failure happens at detector.detectAndDecode(img)

you are confusing both of these APIs and likely blaming the wrong one. you have to be precise here.

also reduce your code to a “minimal reproducible example”. that means stripping out everything that doesn’t contribute to reproducing the error, i.e. everything AFTER the error.

I’m not asking that to be mean. I am asking because that is crucial to pinning the issue down. you need to cooperate freely and willingly. you need to be forthcoming. it is in your own best interest. I will not ask you again.

1 Like

Hi,

and thanks to both of you!

Well I think I have narrowed it down to the detector.

I was thinking maybe the data read from csv was somehow confused, or the Chinese names of the students was causing trouble. So I generated random 10 figure integers, made then into strings and used them as key and value in my function makeQRcode(savefile, key)

I got 114 qrcodes.

From these, 7 do not show any data, so they were appended to my list: missing.

First thing I did was go to the folder look up each png in missing and tried to read it with my phone.

No trouble, my phone reads each file correctly.

Then I tried this:

# show the data from each QR code
missing = [ ]
count=0
for f in missing:
    img = cv2.imread(savepathQRcodes + f)
    window_name = 'qr-code'
    cv2.imshow(window_name, img)
    cv2.waitKey(2000)
    cv2.destroyAllWindows()
    detector = cv2.QRCodeDetector()
    data, bbox, straight_qrcode = detector.detectAndDecode(img)
    #text = data.split(':')
    #print('number, name: ', text[0], text[1])
    #if data == '':
        #count+=1
        #missing.append(f)
    print(data)

I see all 7 .png files in the little window, so cv2.imread() is working.

That only leaves cv2.QRCodeDetector() as the culprit!

If you know of any tweaks for cv2.QRCodeDetector(), please let me know!

Thanks again!!

Hi,

if you ever have the same problem as me (above), try using pyzbar

import pyzbar.pyzbar as pyzbar

Works for me, where cv2 fails