python - 如何将解码后的数据矩阵写入数据帧

标签 python pandas dataframe datamatrix

我正在尝试从图像中读取所有数据矩阵并写入数据帧。 我可以通过 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' 列与上面一样正确。 (矩形(左=984,顶部=1172,宽度=290,高度=287))

数据框如下所示;

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

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

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

提前致谢,

最佳答案

这可能会有所帮助

我使用了这个测试图像

enter image description here

导入包

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))]

enter image description here

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

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']

这会生成一个如下所示的数据框

enter image description here

以前从未使用过这个软件包,安装和玩起来非常有趣!

关于python - 如何将解码后的数据矩阵写入数据帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58428146/

相关文章:

python - 将文件读入 Pandas 数据框中,其中行按日期分组

python - 填充空行

python - 有没有更有效的方法将映射应用于 pandas 系列?

python - 如果列的任何单元格中存在True,该如何测试?

python - pow 运算符的计算错误

python - 将具有多个表达式的 sed 转换为 python

python - pandas.DataFrame.round() 不会在所需位数后截断小数值

python - 如何在 df pandas 的行之间删除或填充相等的单元格值 0?

r - 如何在R中旋转包含带有部分和子部分的列的数据框

dataframe - 如何根据另一个数据帧检查 pyspark 数据帧值