简体   繁体   English

如何从 S3 存储桶计算 Lambda 中图像的像素面积?

[英]How to calculate pixel area of an image in Lambda from an S3 bucket?

I am trying to calculate the pixel area of images uploaded to a bucket in S3.我正在尝试计算上传到 S3 中存储桶的图像的像素区域。 So far, I have managed to put a trigger on my Lambda function in response to an image upload in S3, I have also managed to add all the necessary libraries.到目前为止,我已经成功地触发了我的 Lambda function 以响应 S3 中的图像上传,我还成功地添加了所有必要的库。

To read the image loaded into the bucket I used the key inside cv2.imread() , but in doing so I got a path integrity warning.为了读取加载到存储桶中的图像,我使用了cv2.imread()中的key ,但在这样做时我收到了路径完整性警告。

warning警告

[ WARN:0@0.470] global /io/opencv/modules/imgcodecs/src/loadsave.cpp
(239) findDecoder imread_('public/IMG-10.jpg'):
can't open/read file: check file path/integrity

lambda_function.py lambda_函数.py

import boto3
import base64
import numpy as np
import cv2

s3_client = boto3.client('s3')


def lambda_handler(event, context):
    print(event)


    # getting bucket and object key from event object
    source_bucket = event['Records'][0]['s3']['bucket']['name']
    key = event['Records'][0]['s3']['object']['key']

    # taking image from bucket to calculate pixel area
    image = cv2.imread(key)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    thresh = cv2.threshold(gray,0,255,cv2.THRESH_OTSU + cv2.THRESH_BINARY)[1]
    cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if len(cnts) == 2 else cnts[1]
    total = 0

    for c in cnts:
        x,y,w,h = cv2.boundingRect(c)
        mask = np.zeros(image.shape, dtype=np.uint8)
        cv2.fillPoly(mask, [c], [255,255,255])
        mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
        pixels = cv2.countNonZero(mask)
        total += pixels
        cv2.putText(image, '{}'.format(pixels), (x,y - 15), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255,255,255), 2)

    print(total)

My problem is how to correctly access the image path to be read via opencv.我的问题是如何通过opencv正确访问要读取的图片路径。

It's need to first establish a connection to S3, then download the image data, and finally decode the data with OpenCV.需要先建立到S3的连接,然后下载图像数据,最后用OpenCV解码数据。

Full sample code:完整示例代码:

from io import BytesIO
import boto3
import base64
import numpy as np
import cv2


s3_client = boto3.client('s3')


def lambda_handler(event, context):
    print(event)

    # getting bucket and object key from event object
    source_bucket = event['Records'][0]['s3']['bucket']['name']
    key = event['Records'][0]['s3']['object']['key']
    img_file = BytesIO()
    s3_client.download_fileobj(source_bucket, key, img_file)
        
    
    np_1d_array = np.frombuffer(img_file.getbuffer(), dtype="uint8")
    image = cv2.imdecode(np_1d_array, cv2.IMREAD_COLOR)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    thresh = cv2.threshold(gray,0,255,cv2.THRESH_OTSU + cv2.THRESH_BINARY)[1]
    cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if len(cnts) == 2 else cnts[1]
    total = 0

    for c in cnts:
        x,y,w,h = cv2.boundingRect(c)
        mask = np.zeros(image.shape, dtype=np.uint8)
        cv2.fillPoly(mask, [c], [255,255,255])
        mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
        pixels = cv2.countNonZero(mask)
        total += pixels
        cv2.putText(image, '{}'.format(pixels), (x,y - 15), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255,255,255), 2)

    print("ÁREA EM PIXEL:", total)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 S3 存储桶下载 Lambda 依赖项 - downloading Lambda dependencies from S3 bucket 使用 lambda 从 S3 存储桶中的图像获取图像元数据 - Get image metadata from image within S3 bucket using lambda 如何从S3 bucket中直接读取图片文件到memory? - How to read image file from S3 bucket directly into memory? 如何将图像文件从 s3 存储桶上传到 cloudinary (nodejs) - How to upload an image file from an s3 bucket to cloudinary (nodejs) 使用 Api Gateway、Lambda 函数将图像上传到 S3 存储桶 - Upload Image into S3 bucket using Api Gateway, Lambda funnction 从 opencv 上传图片到 s3 bucket - Upload image from opencv to s3 bucket 如何使用 boto3 和 lambda 从 S3 存储桶中的 html 页面上传文件? - How to upload a file from an html page in S3 bucket using boto3 and lambda? 使用 Lambda 将文件从一个 S3 存储桶复制到另一个 S3 存储桶 - 时间限制? - File Copy from one S3 bucket to other S3 bucket using Lambda - timing constraint? 使用来自另一个帐户 s3 存储桶的代码源部署 Lambda - Deploy Lambda with code source from another accounts s3 bucket 如何从 s3 存储桶解析 CSV 以在 javascript AWS Lambda function 中使用 - How to parse CSVs from s3 bucket to use in a javascript AWS Lambda function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM