【发布时间】:2018-07-17 10:41:51
【问题描述】:
您好,我正在从视频文件中捕获几帧。然后,我想将这些图像(帧)上传到亚马逊 s3。通常,我可以将图像上传到 s3。但是这里我不想把图片写到本地只想直接上传。
但问题是s3.Bucket(BUCKET_NAME).put_object函数有一个'body'参数并且需要字节文件。
import cv2
import boto3
from botocore.client import Config
ACCESS_KEY_ID = 'A********'
ACCESS_SECRET_KEY = 't***************'
BUCKET_NAME = '*********'
s3 = boto3.resource(
's3',
aws_access_key_id=ACCESS_KEY_ID,
aws_secret_access_key=ACCESS_SECRET_KEY,
config=Config(signature_version='s3v4')
)
videoFile = "a.avi"
cap = cv2.VideoCapture(videoFile)
frameRate = cap.get(5) #frame rate
c=0
count=1
while(cap.isOpened()):
frameId = cap.get(1) #current frame number
ret, frame = cap.read()
if (ret != True):
break
if (frameId % frameRate == 0):
c +=1
if(count<frameId//30):
if((frameId/c <=5.8 and frameId/c >=5.4) and c<22):
# here is the problem. s3 put_object want an body=data (as
# bytes file)
cv2.imwrite('sil/image{}.jpg'.format(count), frame)
#data= open(imageFile, 'rb') # here what should be written
s3.Bucket(BUCKET_NAME).put_object
(Key='rpi_img/img{}.jpg'.format(count), Body=data)
count+=1
cap.release()
print ("Done!")
【问题讨论】:
标签: python opencv amazon-s3 image-capture