首页 > 解决方案 > 如何获得使用 Zbar 在 AWS lambda 中工作的 python 函数?

问题描述

在一些关于如何在 python 中执行此操作的教程之后,我构建了以下 qr 阅读器函数。执行这项工作的主要库是 Zbar(或 PyZbar 或类似的派生类)。我已经在我的计算机上启动并运行了该功能,它似乎工作得很好。

现在我想用它构建一个小型应用程序,并且由于我想将事物分开,我希望 qr 解码器功能能够根据 AWS lambda 的需求工作。这个想法是,有人用他们的应用程序拍摄 QR 码的照片,上传图片(到 S3 存储桶),Lambda 函数对此上传做出反应,读取代码并从 QR 码返回数据。

我一直在做我的研究,我自己是 AWS lambda 的新手,并且已经能够弄清楚如果没有预装在 AWS 上,函数需要与依赖项一起上传。我已经能够按照此视频的说明上传它们。但是,当我对函数进行测试运行时,它会引发错误。

我开始怀疑是否真的可以将 QR 阅读器内置到 Lambda 函数中......对此有任何帮助/指向其他方法来执行此操作/现有服务可以执行此操作吗?

我的功能代码:

def scan_qr(image):
    # USAGE
    # python barcode_scanner_image.py --image barcode_example.png

    # import the necessary packages
    from pyzbar import pyzbar
    import argparse
    import cv2
    import pandas as pd

    # construct the argument parser and parse the arguments
    args=image

    # load the input image
    image = cv2.imread(args)

    # find the barcodes in the image and decode each of the barcodes
    barcodes = pyzbar.decode(image)

    # loop over the detected barcodes
    for barcode in barcodes:
        # extract the bounding box location of the barcode and draw the
        # bounding box surrounding the barcode on the image
        (x, y, w, h) = barcode.rect
        cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 2)

        # the barcode data is a bytes object so if we want to draw it on
        # our output image we need to convert it to a string first
        barcodeData = barcode.data.decode("utf-8")
        barcodeType = barcode.type

        # print the barcode type and data to the terminal
        your_list = pd.DataFrame(
        {'type': [barcodeType],
        'url': [barcodeData]
        })
        your_list = your_list[your_list.type=='QRCODE']

        return(your_list)

标签: pythonamazon-web-servicesaws-lambda

解决方案


Zbar 具有本机依赖项。新的 Lambda 功能允许您构建自己的 docker 映像。因此,您可以将 zbar 构建到图像中。


推荐阅读