【问题标题】:How to write decoded data matrix to dataframe如何将解码的数据矩阵写入数据帧
【发布时间】:2019-10-17 08:28:52
【问题描述】:

我正在尝试从图像中读取所有数据矩阵并写入数据帧。 我可以通过 pylibdmtx 打印条形码编号和位置,但我不知道如何存储在数据框中

image = cv2.imread('IMG-4951.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
plt.imshow(gray)
ret,thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
msg = pylibdmtx.decode(thresh)
print(msg)


Output:
[Decoded(data=b'01086995460155972150046789702417240229109LB02199', rect=Rect(left=984, top=1172, width=290, height=287)), Decoded(data=b'01086995460155972154702360250417240229109LB02199', rect=Rect(left=899, top=2242, width=279, height=272))]

'msg' 变量在这种情况下存储为具有 2 个元素的列表,当我尝试转换 pandas Dataframe 'data' 列是空白但 'rect' 列像上面一样正确。 (Rect(left=984, top=1172, width=290, height=287))

数据框如下所示;

data      rect
          Rect(left=984, top=1172, width=290, height=287)
          Rect(left=899, top=2242, width=279, height=272)

如何填写数据列或您建议的任何其他方法?

我的第二个问题是,这个库似乎很慢,有什么建议可以让它更快吗?

提前致谢,

【问题讨论】:

  • 嘿!我试图运行这段代码。这是一个迷人的包裹!我可以问一下,在尝试从 pylibdmtx.pylibdmtx 导入解码导入时,您是如何遇到 [无法找到 dmtx 共享库] 的问题的。我尝试了这个,因为尝试存储变量时输出失败。
  • 嗨!我发现了 [Unable to find dmtx shared library] 的问题,并提供了一个可能有用的答案

标签: python pandas dataframe datamatrix


【解决方案1】:

这可能会有所帮助

我使用了这个测试图像

导入包

import cv2
import matplotlib.pyplot as plt
from pylibdmtx.pylibdmtx import decode
import pandas as pd 

使用你上面提供的代码

image = cv2.imread('datamatrix.png')
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
plt.imshow(gray)
ret,thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
msg = decode(thresh)
print(msg)

我得到这个输出,

[Decoded(data=b'Stegosaurus', rect=Rect(left=5, top=6, width=96, height=95)), Decoded(data=b'Plesiosaurus', rect=Rect(left=298, top=6, width=95, height=95))]

要添加到数据框,我首先填充一个列表来表示我希望数据框成为的形状

ls_msg = []
for item in msg:
    ls_msg.append([item[0], item[1][0], item[1][1], item[1][2]])

将列表变成数据框

df_msg = pd.DataFrame(ls_msg)

添加列名

df_msg.columns = ['data', 'left','top','width']

这会产生一个看起来像这样的数据框

在安装和玩这款游戏之前从未使用过这个包!

【讨论】:

  • 感谢您的解决方案,我想问一下您的系统中的“解码”过程是否很长?只是检查我是否做错或遗漏,因为比较条形码处理结果大约需要 2 分钟,这非常长
  • 嘿@Tyr,对于那个特定的图像,它运行得很快,不到 5 秒
猜你喜欢
  • 2016-04-19
  • 1970-01-01
  • 2018-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-11
相关资源
最近更新 更多