'Why OpenCV is not able to process quite similar sets of json data?

I want to transfer a data structure through a QR code. So after building this structure MyDataTest, I convert it using JSON to MyDataTest_json. Then, a QR code is created using Python QR Code.

import qrcode
import json
import cv2


A = ['Lorem ipsum dolor sit amet, consectetur ']
B = ['adipiscing elit. Cras at justo ', 'sum dolor sit am']
C = ['sum dolor sit', 'm ipsum dolor', 'ipiscing el']

A2 = ['Lorem ipsum dolor']
B2 = ['adipiscing eli', 'strwater']
C2 = ['dolor', 'btexDelta', 'strcat']

MyDataTest = {
    "A" : A,        # Replace by A2
    "B" : B,        # Replace by B2
    "C" : C,        # Replace by C2
}

MyDataTest_json = json.dumps(MyDataTest, separators=(',', ':'))

qrcode.make(MyDataTest_json).save("test.png")

d = cv2.QRCodeDetector()
retval, points, straight_qrcode = d.detectAndDecode(cv2.imread('test.png'))
print(json.loads(retval))

The error occurs when I use OpenCV to read this QR code: when MyDataTest contains A,B,C JSON is able to decode the retval value, but when I replace them with A2,B2,C2 OpenCV seem unable to open the QR code as retval is empty.

This is really puzzling me, I can't get why the behaviors are different. Here are the two QR codes generated. The first one can be opened and read, and the second one return no retval value. This can be loadedthis cannot be loaded



Solution 1:[1]

This seems to be a common issue with OpenCV, as many people reported that they couldn't load their QR code:

Eventually, I managed to load my QR by using pyzbar instead of OpenCV:

import qrcode
import json
import cv2
import pyzbar.pyzbar as pyzbar # Not in original code


A = ['Lorem ipsum dolor sit amet, consectetur ']
B = ['adipiscing elit. Cras at justo ', 'sum dolor sit am']
C = ['sum dolor sit', 'm ipsum dolor', 'ipiscing el']

A2 = ['Lorem ipsum dolor']
B2 = ['adipiscing eli', 'strwater']
C2 = ['dolor', 'btexDelta', 'strcat']

MyDataTest = {
    "A" : A,        # Replace by A2
    "B" : B,        # Replace by B2
    "C" : C,        # Replace by C2
}

MyDataTest_json = json.dumps(MyDataTest, separators=(',', ':'))

qrcode.make(MyDataTest_json).save("test.png")

im = cv2.imread("test.png")         # Not in original code
decodedObjects = pyzbar.decode(im)  # Not in original code
for obj in decodedObjects:          # Not in original code
    print('Type : ', obj.type)      # Not in original code
    print('Data : ', obj.data,'\n') # Not in original code
    print(json.loads(obj.data))     # Not in original code

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 T0T0R