pylibdmtx library, try except not working - Assertion `value >= 0 && value < 256'

Viewed 398

Question in short:

We have a specific png image, where searching the datamatrix codes on a given location with the help of pylibdmtx library. At that specified location (given xmin, ymin, xmax, ymax coordinates in the code), we crop the image, rescale and send to the library to decode. but getting assertion error from dmtxdecodescheme.c, and program halts. we wish to neglect the error (return "?????" if possible), but try/except not working, and no way to escape.

Details:

This kind of error had never happened before, only on this specific png image, and given specific coordinates & SCALER value. Uploaded the image at: https://easyupload.io/3yioro , because i can't upload to stackoverflow due to size constraints (3.1 Mb is over the 2Mb limit) if i crop or convert image to another extension, error doesn't reproduce. this is really a rare and hard to duplicate error.

here is the simplified code, where you search only 1 given location:

from pylibdmtx.pylibdmtx import decode as decoder
import cv2

xmin = 755
ymin = 501
xmax = 830
ymax = 576
squareDim=150
SCALER=2

def bruteDMSearch(croppedImage):
    try:
        barcode = decoder(croppedImage)
        #brute force cv2.threshold
        threshh=50
        while((not barcode) and (threshh<250)):
            threshh=threshh+15
            ret, thresholdy = cv2.threshold(croppedImage, threshh, 255, cv2.THRESH_BINARY)
            barcode=decoder(thresholdy)
        if(barcode):
            code = ((barcode[0])[0]).decode("utf-8")
            return code
        else:
            return "??????"
    except:
        return "?????"

img = cv2.imread("img.png",0)
sheight=int(img.shape[0]/SCALER)
swidth=int(img.shape[1]/SCALER)
smaller_img=cv2.resize(img,(swidth,sheight))

croppy = smaller_img[ymin:ymax,xmin:xmax]
#cv2.imshow("croppy",croppy)
#cv2.waitKey(0)
code = bruteDMSearch(croppy)

output is:

python3: dmtxdecodescheme.c:115: PushOutputWord: Assertion `value >= 0 && value < 256' failed.
Aborted (core dumped)
2 Answers

Your code worked as-is for me. I got this output

enter image description here

Initially I had other problems with libdmtx.(This is an issue with libdmtx I faced and has no issue with your Python code)

I installed libdmtx using condas in win10. But during runtime I got FileNotFoundError: Could not find module 'libdmtx-64.dll'

then I built the library from https://github.com/dmtx/libdmtx and got release mode dmtx.dll. renamed this dll to libdmtx-64.dll and placed in `C:\Users\balu\Miniconda3\Library\bin'.

Note: if QR decoding is your goal, check your old questions I have answered.

I managed to reproduce this issue on ubuntu 21.10 with libdmtx-dev 0.7.5-3 and pylibdmtx 0.1.9:

python: dmtxdecodescheme.c:128: PushOutputWord: Assertion `value >= 0 && value < 256' failed.
Aborted (core dumped)

Given that the library is written in C and the C module itself is throwing a segfault, the try-except logic will not help here. You will need to run this line:

barcode = decoder(croppedImage)

in a separate process (via subprocess or multiprocessing). Here is an example of how you'd use a decorator to run the decoder in a segfault-proof way via multiprocessing.

Related