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)
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.