Esempio n. 1
0
 def __call__(self, img):
     img_temp = tempfile.NamedTemporaryFile(suffix='.png', delete=False)
     try:
         debug_print('Writing temp file [{0}] for zxing'.format(img_temp.name))
         cv2.imwrite(img_temp.name, img)
         return self.decode_file(img_temp.name)
     finally:
         # TODO LH Logic here?
         img_temp.close()
         os.unlink(img_temp.name)
Esempio n. 2
0
 def __call__(self, img):
     # Temporary files on Windows are pain
     img_temp = tempfile.NamedTemporaryFile(suffix='.png', delete=False)
     try:
         debug_print('Writing temp file [{0}] for Data Symbol'.format(img_temp.name))
         cv2.imwrite(img_temp.name, img)
         img_temp.close()
         return self.decode_file(img_temp.name)
     finally:
         os.unlink(img_temp.name)
Esempio n. 3
0
 def __call__(self, img):
     # Decode barcodes in img using zbar
     # https://github.com/ZBar/ZBar/blob/master/python/README
     # https://github.com/herbyme/zbar/blob/master/python/examples/scan_image.py
     scanner = zbar.ImageScanner()
     height,width = img.shape[:2]
     if 'uint8'!=img.dtype or 2!=len(img.shape):
         debug_print('Convert to greyscale for zbar')
         img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
     image = zbar.Image(width, height, 'Y800', img.tostring())
     scanner.scan(image)
     return [Barcode(str(s.type), unicode(s.data, 'utf8')) for s in image]
Esempio n. 4
0
def resize(img, engine):
    # Entire image at different fractions of original size

    # TODO LH try more sharpening, equalisation, other stuff?
    for sharpening in (0,1,2):
        if sharpening>0:
            img = _unsharpmask(img)
        for f in [round(x * 0.01, 2) for x in xrange(100, 0, -5)]:
            msg = 'resize: scaling factor [{0}] sharpening [{1}]'
            msg = msg.format(f, sharpening)
            debug_print(msg)
            if 1==f:
                i = img
            else:
                # Resize from the original image
                i = cv2.resize(img, (0,0), fx=f, fy=f)
            barcodes = engine(i)
            if barcodes:
                return msg, barcodes

    return None
Esempio n. 5
0
    def filter(self, rects):
        debug_print('[{0}] rectangles'.format(len(rects)))
        for index,r in enumerate(rects):
            debug_print(index, r)

        if self.min_area:
            rects = [r for r in rects if r.area>=self.min_area]
        if self.max_area:
            rects = [r for r in rects if r.area<=self.max_area]

        debug_print('[{0}] rectangles after filtering by area'.format(len(rects)))
        for index,r in enumerate(rects):
            debug_print(index, r)

        return rects
Esempio n. 6
0
    def _compute_candidates(self):
        """ Returns a tuple ({'image_name':image,},
                             [(x,y,width,height),],
                            )
        """
        # Based on http://stackoverflow.com/questions/9013703/how-to-find-the-location-of-red-region-in-an-image-using-matlab/9014569#9014569
        debug_print('Computing candidate barcode areas')

        resized = self._img
        if self.WIDTH!=resized.shape[1]:
            factor = float(self.WIDTH)/resized.shape[1]
            debug_print('Resize {0}, factor of {1:.2}'.format(self.WIDTH, factor))
            resized = cv2.resize(resized, (0,0), fx=factor, fy=factor)
        grey = resized

        if 'uint8'!=grey.dtype or 2!=len(grey.shape):
            debug_print('Convert grey')
            grey = cv2.cvtColor(grey, cv2.COLOR_BGR2GRAY)

        debug_print('Equalize')
        clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
        equalized = clahe.apply(grey)

        # http://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_imgproc/py_gradients/py_gradients.html
        debug_print('High-pass filter')
        sobelx = cv2.Sobel(equalized, cv2.CV_16S, 1, 0, 3)
        sobelx = np.abs(sobelx)

        sobely = cv2.Sobel(equalized, cv2.CV_16S, 0, 1, 3)
        sobely = np.abs(sobely)

        gradient = equalized.copy()
        gradient = cv2.convertScaleAbs(sobely-sobelx, gradient)

        debug_print('Low-pass filter')
        if True:
            gradient = cv2.GaussianBlur(gradient, (3,3), 0)
        else:
            # Better than gaussian blur at preserving edges but slower
            gradient = cv2.bilateralFilter(gradient,9,75,75)        # TODO Parameter values?

        debug_print('Threshold')
        # All scans should have good, consistent lighting so apply a very high threshold
        retval,thresh = cv2.threshold(gradient, 0xd0, 0xff, cv2.THRESH_BINARY)

        debug_print('Morphology')
        kernel = cv2.getStructuringElement(cv2.MORPH_RECT, self.structuring_kernel)
        closing = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel, iterations=2)

        debug_print('Contours')
        cont_img = closing.copy()
        contours,hierarchy = cv2.findContours(cont_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

        working_images = { 'resized':resized,
                           'grey':grey,
                           'equalized':equalized,
                           'gradient':gradient,
                           'thresh':thresh,
                           'closing':closing,
                         }

        candidates = [cv2.boundingRect(c) for c in contours]
        candidates = [Rect(x, y, width, height) for x, y, width, height in candidates]
        return (working_images, candidates)
Esempio n. 7
0
 def __call__(self, img):
     with tempfile.NamedTemporaryFile(suffix='.tiff') as img_temp:
         debug_print('Writing temp file [{0}] for Softek'.format(img_temp.name))
         cv2.imwrite(img_temp.name, img)
         return self.decode_file(img_temp.name)
Esempio n. 8
0
 def __call__(self, img):
     # Decode data matrix barcodes in img using dmtxread
     with tempfile.NamedTemporaryFile(suffix='.png') as img_temp:
         debug_print('Writing temp file [{0}] for dmtxread'.format(img_temp.name))
         cv2.imwrite(img_temp.name, img)
         return self.decode_file(img_temp.name)