将图像从opencv上传到s3存储桶

zpf6vheq  于 2023-01-02  发布在  其他
关注(0)|答案(5)|浏览(201)

我试图上传图像到s3后,检测到一张脸使用opencv。jpg文件被上传到s3,但我无法打开图像。
我可以先将图像保存到本地磁盘,然后再上传到s3,但我想在检测到人脸后直接上传,你知道怎么做吗?

# import the necessary packages

# capture frames from the camera

for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
    # grab the raw NumPy array representing the image, then initialize the timestamp
    # and occupied/unoccupied text
    image = frame.array

    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    faces = face_cascade.detectMultiScale(
        gray,
        scaleFactor=1.1,
        minNeighbors=5,
        minSize=(30, 30),
        flags=cv2.CASCADE_SCALE_IMAGE
    )

    # Draw a rectangle around the faces
    for (x, y, w, h) in faces:
        cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)

    # cv2.imwrite('newobama.png', image)

    if len(faces):
        imageName = str(time.strftime("%Y_%m_%d_%H_%M")) + '.jpg'
        #This is not working
        s3.put_object(Bucket="surveillance-cam", Key = imageName, Body = bytes(image), ContentType= 'image/jpeg')   

    # show the frame
    cv2.imshow("Frame", image)
    key = cv2.waitKey(1) & 0xFF

    # clear the stream in preparation for the next frame
    rawCapture.truncate(0)

    # if the `q` key was pressed, break from the loop
    if key == ord("q"):
        break
igsr9ssn

igsr9ssn1#

替换此行:

s3.put_object(Bucket="surveillance-cam", Key = imageName, Body = bytes(image), ContentType= 'image/jpeg')

作者

image_string = cv2.imencode('.jpg', image)[1].tostring()
s3.put_object(Bucket="surveillance-cam", Key = imageName, Body=image_string)

应该会有用的。

g52tjvyc

g52tjvyc2#

我相信image对象不是JPEG编码的二进制表示形式,而是用于数学目的的NumPy对象
您应该检查Python OpenCV convert image to byte string?

imencode

将图像编码到内存缓冲区中。生成S3可以获取的对象

oxf4rvwz

oxf4rvwz3#

管道安装枕

from PIL import Image
from io import BytesIO

img = Image.fromarray(image)
out_img = BytesIO()
img.save(out_img, format='png')
out_img.seek(0)
s3.put_object(Bucket="Bucket Name", Key = imageName, Body = local_image, ContentType= 'image/png')

这段代码对我来说是有效的,但是它给上传的图像带来了一点蓝色阴影,所以我决定先将图像保存到本地磁盘,然后再上传到S3

cv2.imwrite(imageName, image)
local_image = open('./'+imageName, 'rb')
s3.put_object(Bucket="Bucket Name", Key = imageName, Body = local_image, ContentType= 'image/png')
0ejtzxu1

0ejtzxu14#

import boto3
import cv2

s3 = boto3.resource('s3')
client = boto3.client('s3')
bucket = s3.Bucket('my-bucket-name')
src_dir = 'target_directory_on_aws/'

image = cv2.imread(image_path)
image_string = cv2.imencode('.png', image)[1].tostring()

client.put_object(
    Bucket='my-bucket-name',
    Key=target_aws_path,  # format: '/my_image.png'
    Body=image_string
)
mm5n2pyu

mm5n2pyu5#

is_success, buffer = cv2.imencode('.jpg', img)
io_buf = BytesIO(buffer)
s3.upload_fileobj(io_buf, "bucket-name", "file_name")

相关问题