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

标签 python amazon-web-services opencv amazon-s3 raspberry-pi

我正在尝试使用 opencv 检测到人脸后将图像上传到 s3。 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

最佳答案

替换这一行:

    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)

它应该可以工作。

关于python - 将图像从 opencv 上传到 s3 存储桶,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55583861/

相关文章:

python - 创建一个由 1x3 随机数数组组成的 NxN 数组 (python)

python - 我已经设置了 RNG 和继续按钮,但继续后不会更新结果

python - IO错误 : [Errno 0] Error in Python

python - 从图像创建弦乐艺术

python - 在 Python 中暂停和重新启动视频

Python-权限错误: [WinError 32] The process cannot access the file because it is being used by another process:

python - 使用 AWS S3 数据在 Django 中创建模型并指向它们

amazon-web-services - AWS IAM 角色 : How to identify specific permission for each AWS service

amazon-web-services - 使用 AWS CLI 将嵌套目录上传到 S3?

python - 查找与 +10.000 个其他具有相似特征的图像最接近的匹配