Beispiel #1
2
class DM:
    def __init__(self, max_count, timeout):
        self.max_count = max_count
        self.timeout = timeout
        self.loadReader()
        self.symbols = []   #a symbol is a list of the content and four (x,y) points as a sub list
        return
        
    def loadReader(self):
        self.read = DataMatrix(max_count = self.max_count, timeout = self.timeout, shape = DataMatrix.DmtxSymbol10x10)
        return
            
    def scan(self, img):
        self.read.decode(size(img, 1), size(img, 0), buffer(img.tostring()))
        self.symbols = []
        for idx in range(1, self.read.count()+1):
            self.symbols.append(self.read.stats(idx))
        
    def setTimeout(self, v):
        self.timeout = v
        self.loadReader()
        return
        
    def setMaxCount(self, v):
        self.max_count = v
        self.loadReader()
        return
        
    def writeDM(self):
        # Write a Data Matrix barcode
        dm_write = DataMatrix()
        dm_write.encode("Hello, world!")
        dm_write.save("hello.png", "png")
        return
Beispiel #2
0
def decode(imgcv2):
    code = ""
    try:
        st = datetime.datetime.now()
        img = Image.fromarray(imgcv2)
        if img.mode != 'RGB':
            img = img.convert('RGB')
        dm_read = DataMatrix()
        code = dm_read.decode(
            img.size[0],
            img.size[1],
            buffer(
                img.tostring()),
            timeout=80,
            maxcount=1,
            corrections=3)
        end = datetime.datetime.now()
        if dm_read.count() == 1:
            # print code, end - st
            time = end - st

            print dm_read.stats(1), "t:", str(time)[6:]
            points = dm_read.stats(1)[1]
            # print "points:", points
            # os.system("beep -f1296 -l10")
    except Exception:
        print "error decode"
        pass

    return code, points
Beispiel #3
0
from pydmtx import DataMatrix
from PIL import Image

# Write a Data Matrix barcode
dm_write = DataMatrix()
dm_write.encode("Hello, world!")
dm_write.save("hello.png", "png")

# Read a Data Matrix barcode
dm_read = DataMatrix()
img = Image.open("hello.png")

print dm_read.decode(img.size[0], img.size[1], buffer(img.tostring()))
print dm_read.count()
print dm_read.message(1)
print dm_read.stats(1)
Beispiel #4
0
class DmtxFinder:
    def __init__(self):
        self.cache = {}
        self.dm = DataMatrix()

    def Cached(self, name, rows, cols, type):
        key = (name, rows, cols)
        if not key in self.cache:
            self.cache[key] = cv.CreateMat(rows, cols, type)
        return self.cache[key]

    def find0(self, img):
        started = time.time()
        self.dm.decode(
            img.width,
            img.height,
            buffer(img.tostring()),
            max_count=4,
            #min_edge = 6,
            #max_edge = 19      # Units of 2 pixels
        )
        print "brute", time.time() - started
        found = {}
        for i in range(self.dm.count()):
            stats = dm_read.stats(i + 1)
            print stats
            found[stats[0]] = stats[1]
        return found

    def find(self, img):
        started = time.time()
        gray = self.Cached('gray', img.height, img.width, cv.CV_8UC1)
        cv.CvtColor(img, gray, cv.CV_BGR2GRAY)

        sobel = self.Cached('sobel', img.height, img.width, cv.CV_16SC1)
        sobely = self.Cached('sobely', img.height, img.width, cv.CV_16SC1)

        cv.Sobel(gray, sobel, 1, 0)
        cv.Sobel(gray, sobely, 0, 1)
        cv.Add(sobel, sobely, sobel)

        sobel8 = self.Cached('sobel8', sobel.height, sobel.width, cv.CV_8UC1)
        absnorm8(sobel, sobel8)
        cv.Threshold(sobel8, sobel8, 128.0, 255.0, cv.CV_THRESH_BINARY)

        sobel_integral = self.Cached('sobel_integral', img.height + 1,
                                     img.width + 1, cv.CV_32SC1)
        cv.Integral(sobel8, sobel_integral)

        d = 16
        _x1y1 = cv.GetSubRect(
            sobel_integral,
            (0, 0, sobel_integral.cols - d, sobel_integral.rows - d))
        _x1y2 = cv.GetSubRect(
            sobel_integral,
            (0, d, sobel_integral.cols - d, sobel_integral.rows - d))
        _x2y1 = cv.GetSubRect(
            sobel_integral,
            (d, 0, sobel_integral.cols - d, sobel_integral.rows - d))
        _x2y2 = cv.GetSubRect(
            sobel_integral,
            (d, d, sobel_integral.cols - d, sobel_integral.rows - d))

        summation = cv.CloneMat(_x2y2)
        cv.Sub(summation, _x1y2, summation)
        cv.Sub(summation, _x2y1, summation)
        cv.Add(summation, _x1y1, summation)
        sum8 = self.Cached('sum8', summation.height, summation.width,
                           cv.CV_8UC1)
        absnorm8(summation, sum8)
        cv.Threshold(sum8, sum8, 32.0, 255.0, cv.CV_THRESH_BINARY)

        cv.ShowImage("sum8", sum8)
        seq = cv.FindContours(sum8, cv.CreateMemStorage(), cv.CV_RETR_EXTERNAL)
        subimg = cv.GetSubRect(img, (d / 2, d / 2, sum8.cols, sum8.rows))
        t_cull = time.time() - started

        seqs = []
        while seq:
            seqs.append(seq)
            seq = seq.h_next()

        started = time.time()
        found = {}
        print 'seqs', len(seqs)
        for seq in seqs:
            area = cv.ContourArea(seq)
            if area > 1000:
                rect = cv.BoundingRect(seq)
                edge = int((14 / 14.) * math.sqrt(area) / 2 + 0.5)
                candidate = cv.GetSubRect(subimg, rect)
                sym = self.dm.decode(
                    candidate.width,
                    candidate.height,
                    buffer(candidate.tostring()),
                    max_count=1,
                    #min_edge = 6,
                    #max_edge = int(edge)      # Units of 2 pixels
                )
                if sym:
                    onscreen = [(d / 2 + rect[0] + x, d / 2 + rect[1] + y)
                                for (x, y) in self.dm.stats(1)[1]]
                    found[sym] = onscreen
                else:
                    print "FAILED"
        t_brute = time.time() - started
        print "cull took", t_cull, "brute", t_brute
        return found
Beispiel #5
0
class DmtxFinder:
    def __init__(self):
        self.cache = {}
        self.dm = DataMatrix()

    def Cached(self, name, rows, cols, type):
        key = (name, rows, cols)
        if not key in self.cache:
            self.cache[key] = cv.CreateMat(rows, cols, type)
        return self.cache[key]

    def find0(self, img):
        started = time.time()
        self.dm.decode(img.width,
                       img.height,
                       buffer(img.tostring()),
                       max_count = 4,
                       #min_edge = 6,
                       #max_edge = 19      # Units of 2 pixels
                       )
        print "brute", time.time() - started
        found = {}
        for i in range(self.dm.count()):
            stats = dm_read.stats(i + 1)
            print stats
            found[stats[0]] = stats[1]
        return found

    def find(self, img):
        started = time.time()
        gray = self.Cached('gray', img.height, img.width, cv.CV_8UC1)
        cv.CvtColor(img, gray, cv.CV_BGR2GRAY)

        sobel = self.Cached('sobel', img.height, img.width, cv.CV_16SC1)
        sobely = self.Cached('sobely', img.height, img.width, cv.CV_16SC1)

        cv.Sobel(gray, sobel, 1, 0)
        cv.Sobel(gray, sobely, 0, 1)
        cv.Add(sobel, sobely, sobel)

        sobel8 = self.Cached('sobel8', sobel.height, sobel.width, cv.CV_8UC1)
        absnorm8(sobel, sobel8)
        cv.Threshold(sobel8, sobel8, 128.0, 255.0, cv.CV_THRESH_BINARY)

        sobel_integral = self.Cached('sobel_integral', img.height + 1, img.width + 1, cv.CV_32SC1)
        cv.Integral(sobel8, sobel_integral)

        d = 16
        _x1y1 = cv.GetSubRect(sobel_integral, (0, 0, sobel_integral.cols - d, sobel_integral.rows - d))
        _x1y2 = cv.GetSubRect(sobel_integral, (0, d, sobel_integral.cols - d, sobel_integral.rows - d))
        _x2y1 = cv.GetSubRect(sobel_integral, (d, 0, sobel_integral.cols - d, sobel_integral.rows - d))
        _x2y2 = cv.GetSubRect(sobel_integral, (d, d, sobel_integral.cols - d, sobel_integral.rows - d))

        summation = cv.CloneMat(_x2y2)
        cv.Sub(summation, _x1y2, summation)
        cv.Sub(summation, _x2y1, summation)
        cv.Add(summation, _x1y1, summation)
        sum8 = self.Cached('sum8', summation.height, summation.width, cv.CV_8UC1)
        absnorm8(summation, sum8)
        cv.Threshold(sum8, sum8, 32.0, 255.0, cv.CV_THRESH_BINARY)

        cv.ShowImage("sum8", sum8)
        seq = cv.FindContours(sum8, cv.CreateMemStorage(), cv.CV_RETR_EXTERNAL)
        subimg = cv.GetSubRect(img, (d / 2, d / 2, sum8.cols, sum8.rows))
        t_cull = time.time() - started

        seqs = []
        while seq:
            seqs.append(seq)
            seq = seq.h_next()

        started = time.time()
        found = {}
        print 'seqs', len(seqs)
        for seq in seqs:
            area = cv.ContourArea(seq)
            if area > 1000:
                rect = cv.BoundingRect(seq)
                edge = int((14 / 14.) * math.sqrt(area) / 2 + 0.5)
                candidate = cv.GetSubRect(subimg, rect)
                sym = self.dm.decode(candidate.width,
                                     candidate.height,
                                     buffer(candidate.tostring()),
                                     max_count = 1,
                                     #min_edge = 6,
                                     #max_edge = int(edge)      # Units of 2 pixels
                                     )
                if sym:
                    onscreen = [(d / 2 + rect[0] + x, d / 2 + rect[1] + y) for (x, y) in self.dm.stats(1)[1]]
                    found[sym] = onscreen
                else:
                    print "FAILED"
        t_brute = time.time() - started
        print "cull took", t_cull, "brute", t_brute
        return found
Beispiel #6
0
import os
from pydmtx import DataMatrix
from PIL import Image

#------------------------------------------------------------
# write a data matrix barcode
#------------------------------------------------------------
path = os.path.abspath('.')
path = os.path.join(path, 'hello.png')

matrix = DataMatrix()
matrix.encode("Hello, world!")
matrix.save(path, "png")

#------------------------------------------------------------
# read a data matrix Barcode
#------------------------------------------------------------
matrix = DataMatrix()
image  = Image.open(path)

print matrix.decode(image.size[0], image.size[1], buffer(image.tostring()))
print matrix.count()
print matrix.message(1)
print matrix.stats(1)
Beispiel #7
0
    if options.test:
        # re-read image
        image = Image("original.png")
    else:
        if calibrated:
            image_orig = cam.getImage()
            image = cam.undistort(image_orig)
        else:
            image = cam.getImage()

    overlay = DrawingLayer((image.width, image.height))

    dm_read.decode(image.width, image.height, buffer(image.toString()))

    for count in range(dm_read.count()):
        stats = dm_read.stats(count + 1)

        # Highlight barcode
        arrow = []
        arrow.append(stats[1][0])
        arrow.append(stats[1][1])
        arrow.append(stats[1][3])
        arrow.append(stats[1][0])
        arrow.append(stats[1][2])
        overlay.lines(arrow, Color.RED, width=2)

        # Attempt to improve the co-ords of each corner
        best = []
        zoomed = 64
        for x in range(3):
            if options.dmtx: