3

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)
SoajanII
  • 323
  • 5
  • 19

2 Answers2

4

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.

balu
  • 1,023
  • 12
  • 18
  • ty for the answer. someone using it on win10 would surely benefit from the answer. but the issue is i'm running this code on ubuntu 18.04/20.04. and it is really interesting to see it works on win10 but not on my operating system. also the error is not limited to this single example. happens a lot lately. bytheway, thanks god, QR decoding is way less problematic compared to DataMatrix – SoajanII Mar 01 '22 at 19:28
  • >> " QR decoding is way less problematic compared to DataMatrix" Does it mean the answer I gave here https://stackoverflow.com/questions/70337889/opencv-orb-algorithm-qr-code-match-issues useful? If so can you mark it as answer ? – balu Mar 02 '22 at 04:29
  • We have been already working with QR decoders for plenty of time, and have the previous experience. The issue is, we can't replace the DataMatrix system on this problem. – SoajanII Mar 03 '22 at 06:03
2

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.

Andrej Prsa
  • 551
  • 3
  • 14