Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read an video file object from S3 and use it for further processing through Opencv

import boto3

import cv2
import numpy as np
s3 = boto3.resource('s3')
vid = (s3.Object('bucketname', 'video.blob').get()['Body'].read())
cap = cv2.VideoCapture(vid)

This is my code. I have a video file in an s3 bucket. I want to do some processing on it with OpenCV and I don't want to download it. So I'm trying to store that video file into vid. Now the problem is that type(vid) is byte which is the reason to result in this error TypeError: an integer is required (got type bytes) on line 6. I tried converting it into an integer or a string but was unable to.

On an attempt to convert byte to an integer: I referred to this and was getting length issues. This is just a sample video file. The actual file I want to do processing on will be huge when converted to byte object.

On an attempt to get the object as a string and then convert it to an integer: I referred to this. Even this doesn't seem to work for me.

If anyone can help me solve this issue, I will be grateful. Please comment if anything is uncertain to you regarding my issue and I'll try to provide more details.

like image 731
Vinay Varma Avatar asked Nov 27 '22 19:11

Vinay Varma


1 Answers

If streaming the video from a url is an acceptable solution, I think that is the easiest solution. You just need to generate a url to read the video from.

import boto3
import cv2

s3_client = boto3.client('s3')
        
bucket = 'bucketname'      
key = 'video.blob' 
            
url = s3_client.generate_presigned_url('get_object', 
                                       Params = {'Bucket': bucket, 'Key': key}, 
                                       ExpiresIn = 600) #this url will be available for 600 seconds
    
cap = cv2.VideoCapture(url)
       
ret, frame = cap.read() 

You should see that you are able to read and process frames from that url.

like image 129
Keith Johnson Avatar answered Nov 29 '22 08:11

Keith Johnson