2

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,

Tyr
  • 580
  • 4
  • 26
  • hey! i was trying to run this code. This is a fascinating package! can i ask, how did you get issues with [Unable to find dmtx shared library] when trying to import from pylibdmtx.pylibdmtx import decode. I tried this as the output was failing when trying to store a variable. – the_good_pony Oct 17 '19 at 16:03
  • Hi! I figured out my issues with **[Unable to find dmtx shared library]** and provided an answer which _might_ be useful – the_good_pony Oct 17 '19 at 22:34

1 Answers1

2

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!

the_good_pony
  • 490
  • 5
  • 12
  • Thanks for the solution, I want to ask that does 'decode' process is long in your system? Just checking if I do sth wrong or missing, because it takes approximately 2 minutes which is very long comparing the barcode process results – Tyr Oct 18 '19 at 06:20
  • Hey @Tyr , for that particular image it ran quickly, under 5 seconds – the_good_pony Oct 18 '19 at 06:36