Tyr
Tyr

Reputation: 610

How to write decoded data matrix to dataframe

I'm trying to read all data matrixes from an image and write to dataframe. I can print barcode number and location via pylibdmtx but I can't figure out how to store in dataframe

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' variable stored as list with 2 elements in this case and when I try to convert pandas Dataframe 'data' column is blank but 'rect' column is proper like above. (Rect(left=984, top=1172, width=290, height=287))

Dataframe looks like below;

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

How can I fill data column or any other method do you suggest?

My second question is, this library seems very slow any suggestion how to make it quicker?

Thanks in advance,

Upvotes: 2

Views: 907

Answers (1)

the_good_pony
the_good_pony

Reputation: 500

This might be helpful

I used this test image

enter image description here

Import the packages

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

Use the code you provided above

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)

I get this output,

[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

To add to a dataframe I start with populating a list to represent the shape I would like the dataframe to be

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

Make list into a dataframe

df_msg = pd.DataFrame(ls_msg)

Add column names

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

This produces a dataframe which looks like this

enter image description here

Never used this package before it was fascinating to install and play!

Upvotes: 2

Related Questions