python - pylibdmtx 库,尝试除了不工作 - 断言 `value >= 0 && value < 256'

标签 python opencv try-catch datamatrix

问题简述:

我们有一个特定的 png 图像,在 pylibdmtx 库的帮助下在给定位置搜索数据矩阵代码。在指定位置(代码中给定 xmin、ymin、xmax、ymax 坐标),我们裁剪图像、重新缩放并发送到库进行解码。但从 dmtxdecodescheme.c 得到断言错误,程序停止。 我们希望忽略这个错误(如果可能的话返回“??????”),但是 try/except 不起作用,并且没有办法逃脱

详细信息:

这种错误以前从未发生过,只发生在这个特定的png图像上,并给出了特定的坐标和SCALER值。 上传图片于:https://easyupload.io/3yioro ,因为由于大小限制我无法上传到 stackoverflow(3.1 Mb 超过 2Mb 限制) 如果我裁剪图像或将图像转换为另一个扩展名,错误不会重现。这确实是一个罕见且难以重现的错误。

这是简化的代码,您只搜索 1 个给定位置:

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)

输出是:

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

最佳答案

您的代码对我来说按原样工作。 我得到了这个输出

enter image description here

最初我遇到了 libdmtx 的其他问题。(这是我遇到的 libdmtx 的问题,与您的 Python 代码没有问题)

我在 win10 中使用 condas 安装了 libdmtx。但是在运行时我得到了 FileNotFoundError: Could not find module 'libdmtx-64.dll'

然后我从 https://github.com/dmtx/libdmtx 构建了库并获得 Release模式 dmtx.dll。将此 dll 重命名为 libdmtx-64.dll 并放置在“C:\Users\balu\Miniconda3\Library\bin”中。

注意:如果您的目标是二维码解码,请查看我已经回答过的旧问题。

关于python - pylibdmtx 库,尝试除了不工作 - 断言 `value >= 0 && value < 256',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71117006/

相关文章:

python - DataFrame.to_json() 的段错误

python - 如何按值(DESC)然后按键(ASC)对字典进行排序?

c++ - 在图像上放置图像

python - 如何执行200个功能的错误处理?

Java 构造函数调用被忽略/没有抛出错误

python - 将方向(矢量)转换为 R.A.和 12 月在 Skyfield

python - 如何在 pyspark dataframe 中创建嵌套字典

perl - 如何正确使用error.pm提供的perl中的try catch?

image-processing - 人车识别

matlab - OpenCV MATLAB : How to draw a line having a particular Intensity profile?