Skip to content
Advertisement

Pass video uploaded via django to cv2

I am uploading video via django and want to process it using cv2. This is how video uploaded via django is accessed.

video_obj = request.FILES['file_name']

Next i want to pass it to opencv. I dont want to save video in disk first and then acess it via cv2 using following code

cap = cv2.VideoCapture(vid_path) 

I tried passing this video_obj to VideoCapture this way

video_obj = request.FILES['file_name']
cap = cv2.VideoCapture(video_obj) 

But i got following error

Can't convert object of type 'TemporaryUploadedFile' to 'str' for 'filename'
VideoCapture() missing required argument 'apiPreference' (pos 2)

Advertisement

Answer

Seems like cv2.VideoCapture can only work with filepath. So to get the path from TemporaryUploadedFile object you can use temporary_file_path() method.

Advertisement