python - 如何使用网络摄像头解码 Datamatrix?

标签 python opencv datamatrix

我已经从图像中读取了数据矩阵,现在正尝试从网络摄像头中读取它,但它不起作用,而且我不断收到错误。我已经尝试从 pyimagesearch 网站检测条形码/二维码。

我使用了 Zbar 库,它运行良好但不支持数据矩阵。现在我正在尝试使用 pylibdmtx,它适用于图像但滞后并且无法在视频中检测到。

代码 1

from imutils.video import VideoStream
from pyzbar import pyzbar
from pydmtx import DataMatrix
import zxing
import argparse
import datetime
import imutils
import time
import cv2

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
reader = zxing.BarCodeReader("/home/creator/.local/bin/zxing")
ap.add_argument("-o", "--output", type=str, default="barcodes.csv",
    help="path to output CSV file containing barcodes")
args = vars(ap.parse_args())

# initialize the video stream and allow the camera sensor to warm up
print("[INFO] starting video stream...")
vs = VideoStream(src=0).start()
# vs = VideoStream(usePiCamera=True).start()
time.sleep(2.0)

# open the output CSV file for writing and initialize the set of
# barcodes found thus far
csv = open(args["output"], "w")
found = set()


# loop over the frames from the video stream
while True:
    # grab the frame from the threaded video stream and resize it to
    # have a maximum width of 400 pixels
    frame = vs.read()
    frame = imutils.resize(frame, width=40)

    # find the barcodes in the frame and decode each of the barcodes
    barcodes = pylibdmtx.decode(frame)

        # loop over the detected barcodes
    for barcode in barcodes:
        # extract the bounding box location of the barcode and draw
        # the bounding box surrounding the barcode on the image
        (x, y, w, h) = barcode.rect
        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2)

        # the barcode data is a bytes object so if we want to draw it
        # on our output image we need to convert it to a string first
        barcodeData = barcode.data.decode("utf-8")
        barcodeType = barcode.type
        print("Data :",barcodeData,'\n')
        print("Type :",barcodeType)

        # draw the barcode data and barcode type on the image
        text = "{} ({})".format(barcodeData, barcodeType)
        cv2.putText(frame, text, (x, y - 10),
            cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)


        # if the barcode text is currently not in our CSV file, write
        # the timestamp + barcode to disk and update the set
        if barcodeData not in found:
            csv.write("{},{}\n".format(datetime.datetime.now(),
                barcodeData))
            csv.flush()
            found.add(barcodeData)

    # show the output frame
    cv2.imshow("Barcode Scanner", frame)
    key = cv2.waitKey(1) & 0xFF

    # if the `q` key was pressed, break from the loop
    if key == ord("q"):
        break

# close the output CSV file do a bit of cleanup
print("[INFO] cleaning up...")
csv.close()
cv2.destroyAllWindows()
vs.stop()

代码 1 运行但速度太慢且未检测到任何内容。

结果:应该在相机上显示读取的数据矩阵。

最佳答案

  • 将第 3 行更改为:

从 pylibdmtx 导入 pylibdmtx

  • 因为我没有 zxing,所以我在代码中注释了那些行(4 和 13)。

  • Datamatrix 解码器没有像 bardcode 解码器那样的类型。因此,将第 52 行更改如下:

    barcodeType = "DMC"#barcode.type

  • 在循环内添加了一条打印语句以查看解码后的输出,如下所示:

对于条形码中的条形码:

    print(barcode)

输出:

类型:DMC 解码(数据=b'800400547311010400109085566', rect=Rect(left=241, top=238, width=65, height=-83)) 数据:800400547311010400109085566

类型:DMC 解码(数据=b'800040547311010400102325376', rect=Rect(left=259, top=140, width=-119, height=110)) 数据:800040547311010400102325376


请有人帮助解决缓慢问题,因为我找不到原因。


关于python - 如何使用网络摄像头解码 Datamatrix?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56427780/

相关文章:

android - 使用 zxing 的 DataMatrix 编码仅生成 14px 位图

python - pydtmx 或 libdtmx 如何返回 FNC1 字符

python - 使用Pandas Dataframe过滤功能后的SettingWithCopyWarning

Python装饰器类: How to correctly count function calls in a with block

python - 试图将文件下载缓冲区拆分为单独的线程

python - Miniconda3 Linux Python 3.7 64 位,缺少安装程序?

java - OpenCV4Android 错误 : Assertion failed in Core. minMaxLoc(mROI) 方法

android - 为什么 openCV 的 fastNlMeansDenoisingColored() 出错?

Java OpenCV - 检测 ROI、创建 submat 并复制到原始 mat

java - 读取DataMatrix/QR码 zxing java