Skip to content

Commit

Permalink
Merge pull request #92 from DiamondLightSource/odd_chars
Browse files Browse the repository at this point in the history
Odd chars
  • Loading branch information
UrszulaNeuman authored Aug 25, 2023
2 parents 736b95f + 69cb7f7 commit ead24ca
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 12 deletions.
27 changes: 18 additions & 9 deletions dls_barcode/datamatrix/datamatrix.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import logging
from dls_util.image.image import Image
from .locate import Locator
import string
from string import ascii_lowercase

from pylibdmtx.pylibdmtx import decode
import cv2

from dls_util.image.image import Image

from .locate import Locator

# We predict the location of the center of each square (pixel/bit) in the datamatrix based on the
# size and location of the finder pattern, but this can sometimes be slightly off. If the initial
Expand All @@ -25,6 +28,8 @@ class DataMatrix:
"""
DEFAULT_SIZE = 14
DEFAULT_SIDE_SIZES = [12, 14]
# allow only capitol letters, digits and dash in the decoded string
ALLOWED_CHARS = set(string.ascii_uppercase + string.ascii_lowercase + string.digits + '-' + '_')

def __init__(self, finder_pattern):
""" Initialize the DataMatrix object with its finder pattern location in an image. To actually
Expand Down Expand Up @@ -107,16 +112,17 @@ def _read(self, gray_image):
given by the datamatrix finder pattern.
"""
try:


result = decode(gray_image, max_count = 1)
if len(result) > 0:
d = result[0].data
decoded = d.decode('UTF-8')
new_line_removed = decoded.replace("\n","")
self._data = new_line_removed
self._read_ok = True
self._error_message = ""
if self._contains_allowed_chars_only(decoded):
new_line_removed = decoded.replace("\n","")
self._data = new_line_removed
self._read_ok = True
self._error_message = ""
else:
self._read_ok = False
else:
self._read_ok = False
#cv2.imshow("Erode", gray_image)
Expand All @@ -133,6 +139,9 @@ def draw(self, img, color):
img.draw_line(fp.c1, fp.c2, color)
img.draw_line(fp.c1, fp.c3, color)

def _contains_allowed_chars_only(self, text):
return (set(text)).issubset(self.ALLOWED_CHARS)

@staticmethod
def locate_all_barcodes_in_image(grayscale_img, matrix_sizes=[DEFAULT_SIZE]):
""" Searches the image for all datamatrix finder patterns
Expand Down
2 changes: 1 addition & 1 deletion dls_barcode/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION = "v1.8.0"
VERSION = "v1.9.0"
4 changes: 2 additions & 2 deletions docs/release-notes/release-notes-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ Changes merged into master
--------------------------
| Jira Task | GitHub Issue | Type | Description |
|-----------|--------------|------|------------------------------------|
| - | - | |Use python 3.7 |
|I04_1-1036 | - |Minor |Use pylibdtmx to read barcodes |
| - | - | | |
| - | - | | |

Change Types:
* Major - Backward incompatible change
Expand Down
16 changes: 16 additions & 0 deletions docs/release-notes/release-notes-v1_9_0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Release Notes (Development)
===========================

Changes merged into master
--------------------------
| Jira Task | GitHub Issue | Type | Description |
|-----------|--------------|------|---------------------------------------|
|I04_1-1062 | - |Minor |Only letters, numbers, _ and - allowed|

Change Types:
* Major - Backward incompatible change
* Minor - Backward compatible change in API/functionality
* Patch - Bug fix, no change in functionality



Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import unittest

from mock import Mock, patch

from dls_barcode.datamatrix.datamatrix import DataMatrix

class TestDatamatrix(unittest.TestCase):

def test_contains_allowed_characers(self):
datamatrix = DataMatrix(Mock)
test_string_good = "AbcD123_-"
self.assertTrue(datamatrix._contains_allowed_chars_only(test_string_good))

test_string_bad = "AbcD123_-$<"
self.assertFalse(datamatrix._contains_allowed_chars_only(test_string_bad))




0 comments on commit ead24ca

Please sign in to comment.