opencv 如何解码Datamatrix与网络摄像头?

dkqlctbz  于 2022-11-15  发布在  其他
关注(0)|答案(1)|浏览(237)

我已经从图像中读取了DataMatrix,现在正试图从网络摄像头中读取,但它不起作用,我不断收到错误。我已经尝试了pyimagesearch网站的条形码/qrcode检测。
我用过Zbar库,它运行的很好,但是不支持datamatrix。现在我正在尝试用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运行,但速度太慢,无法检测到任何内容。
结果:应读取摄像机上显示的数据矩阵。

kkbh8khc

kkbh8khc1#

  • 将第3行更改为:

从pylibdmtx导入pylibdmtx

  • 因为我没有zxing,所以我在代码中注解了这些行(4和13)。
  • Datamatrix解码器没有类似条形码解码器的类型。因此,将第52行更改如下:

条形码类型=“DMC”#条形码类型

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

对于条形码中条形码:

print(barcode)

输出:

类型:DMC解码(数据= b '800400547311010400109085566',矩形=矩形(左侧=241,顶部=238,宽度=65,高度=-83))数据:800400547311010400109085566
类型:DMC解码(数据= b '800040547311010400102325376',矩形=矩形(左侧=259,顶部=140,宽度= -119,高度=110))数据:800040547311010400102325376
请有人帮助与缓慢的问题,因为我找不到一个原因。

相关问题