本文介绍了如何使用网络摄像头解码 Datamatrix?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

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

I have read datamatrix from images and now am trying to read that from webcam but it is not working and I keep getting errors. I have tried barcode/qrcode detection from pyimagesearch site.

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

I have used Zbar library which works well but doesn't support datamatrix. Now I am trying with pylibdmtx which works well for images but lags and doesn't detect in video.

代码 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 运行但速度太慢并且没有检测到任何东西.

code 1 runs but it is too slow and doesn't detect anything.

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

Result: should a read datamatrix shown at the camera.

推荐答案

  • 将第 3 行更改为:
  • 从 pylibdmtx 导入 pylibdmtx

    from pylibdmtx import pylibdmtx

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

    • as i didnt have zxing i have commented those lines(4 and 13) in the code.

    Datamatrix 解码器没有类似条码解码器的类型.因此将第 52 行更改为如下所示:

    Datamatrix decoder doesnt have a Type like bardcode decoder. So change the line 52 as below:

    barcodeType = "DMC" #barcode.type

    barcodeType = "DMC" #barcode.type

    添加了一个打印语句来查看循环内的解码输出,如下所示:

    Added a print statement to see the decoded output as shown below inside the loop:

    对于条码中的条码:

        print(barcode)
    

    输出:

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

    Type : DMCDecoded(data=b'800400547311010400109085566', rect=Rect(left=241, top=238, width=65, height=-83))Data : 800400547311010400109085566

    类型:DMC解码后(数据=b'800040547311010400102325376',rect=Rect(左=259,顶部=140,宽度=-119,高度=110))数据:800040547311010400102325376

    Type : DMCDecoded(data=b'800040547311010400102325376', rect=Rect(left=259, top=140, width=-119, height=110))Data : 800040547311010400102325376

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

    PLEASE SOMEONE HELP WITH SLOWNESS ISSUE AS I COULDN'T FIND A REASON FOR THAT.

    这篇关于如何使用网络摄像头解码 Datamatrix?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    1403页,肝出来的..

09-06 21:24