1

Hi I am capturing few frames from video file. Then, I want to upload those images(frames) to amazon s3. Normally, I am able to upload image to s3. But here I don't want to write image to local just want to upload it directly.

But the problem is s3.Bucket(BUCKET_NAME).put_object function has a 'body' parameter and requires bytes file.

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!")
Rick M.
  • 3,045
  • 1
  • 21
  • 39
Anas Mansour
  • 357
  • 1
  • 3
  • 12

1 Answers1

1

You can get bytes directly from frame, like below:

bytes(frame)

So final result will be:

s3.Bucket(BUCKET_NAME).put_object(Key = 'rpi_img/img{}.jpg', Body = bytes(frame))
Majd Sabah
  • 81
  • 3
  • 3