Beispiel #1
0
def run_barcode(video_filter, frame):
    height, width, _ = frame.shape
    """width, height = 100, 100
    frame = cv2.resize(frame, (width, height))"""

    barcodes = pyzbar_decode(frame)

    barcodes_detected = []
    for barcode in barcodes:
        (x, y, w, h) = barcode.rect
        rect = QRectF(
            x * 1.0 / width,
            y * 1.0 / height,
            w * 1.0 / width,
            h * 1.0 / height,
        )
        points = []
        for point in barcode.polygon:
            p = QPointF(point.x * 1.0 / width, point.y * 1.0 / height)
            points.append(p)

        barcodes_detected.append({
            "data": barcode.data.decode("utf-8"),
            "type": barcode.type,
            "rect": rect,
            "polygon": points,
        })
        video_filter.barcodesDetected.emit(barcodes_detected)
Beispiel #2
0
def zbar_decode(img):
    res = pyzbar_decode(img)
    if len(res) == 0:
        return None
    elif len(res) == 1:
        return res[0].data.decode("utf-8")
    else:
        return [r.data.decode("utf-8") for r in res]
Beispiel #3
0
def qr_decode(img):
    """ img: uint8, 0-255

    Args:
        img:

    Returns:

    """
    res = pyzbar_decode(img)
    if len(res) == 0:
        return None
    else:
        return res[0].data.decode("utf-8")
Beispiel #4
0
def zbar_decode(img):
    from pyzbar.pyzbar import decode as pyzbar_decode
    #img = img.mean(axis=2)
    res=pyzbar_decode(img)

    if len(res)==0:
        if len(img.shape)>2:
            imgg = img.mean(axis=2)
            res=pyzbar_decode(imgg)
        if len(res)==0:
            imgb = gaussian(img,1,preserve_range=True)
            res=pyzbar_decode(imgb)
            if len(res)==0:
                imgc = adjust_log(img)
                res=pyzbar_decode(imgc)
            #if len(res)==0:
            #    imgs = unsharp_mask(img,preserve_range=True)
            #    res=pyzbar_decode(imgs)
    if len(res)==0:
        return None
    elif len(res)==1:
        return res[0].data.decode("utf-8")
    else:
        return [r.data.decode("utf-8") for r in res]
Beispiel #5
0
    def execute(cls):
        """
        This method constantly tries to catch barcodes from the camera :
        if it does, search for info and add it to the user's grocery list.
        """
        # Intro
        print("-" * 80)
        print("[:{ Jean-Pierre's Barcode Scanner : Running")
        print("-" * 80)

        # Database connection
        Database.on()

        # Load parameters
        params = models.Params()

        # Database : end connection
        # (open it only when needed because this program is meant to be shutdown harshly)
        Database.off()

        # Camera setup
        camera = picamera.PiCamera()
        camera.sharpness = 100
        camera.brightness = 50
        camera.contrast = 25
        camera.resolution = (params.camera_res_x, params.camera_res_y)
        camera.start_preview()

        # Double beep to inform the user that Jean-Pierre is ready
        if params.buzzer_on:
            buzzer = utils.Buzzer(params.buzzer_port)
            buzzer.beep(2)

        # Capture loop
        last_scan = ''
        empty_scans = 0
        while True:
            # Get an image from the camera
            stream = BytesIO()
            camera.capture(stream, format='jpeg')
            stream.seek(0)

            # Scan image for barcodes
            barcodes = pyzbar_decode(pil_image.open(stream))[::-1]

            # Count every empty scan
            if not barcodes:
                empty_scans += 1
                # clean last_scan every 3 empty scans in a row
                # Note : this allows to add an item multiple times
                # by just leaving it in front of the camera.
                if empty_scans == 3:
                    last_scan = ''
                    empty_scans = 0

            # If something has been scanned
            if barcodes:

                # Get the first readable EAN-13 barcode from the scan
                barcode = False
                for seek in barcodes:
                    if seek.type == 'EAN13':
                        barcode = seek.data.decode()
                        break

                if not barcode:
                    continue

                # Break count of empty scans in a row
                empty_scans = 0

                # If the item has just been scanned : ignore this round
                if barcode == last_scan:
                    continue

                # Add found item to history
                last_scan = barcode
                print("Scanned and considered : {}".format(barcode))

                # Beep !
                if params.buzzer_on:
                    buzzer.beep()

                # Analyze it in a separate thread
                utils.FindProduct(barcode).start()
Beispiel #6
0
 def parse_qr_code(self, img: Image):
     decoded_texts = set()
     for decoded in pyzbar_decode(img):
         if decoded.type == "QRCODE":
             decoded_texts.add(decoded.data.decode("utf-8"))
     return decoded_texts or None