I am writing simple REST API using Flask, which reads uploaded video and returns some JSON. Video is sent as file by POST request, with content type application/x-www-form-urlencoded.
I want to process it using opencv, but I do not know how to read it from raw binary string. The only solution I came up with, is to save video to file and read it using opencv VideoCapture class. However I would like to avoid IO operations, because I think it will slow down my API.
So my question is:
How can I transform video from raw bytes representation to opencv representation (list of numpy arrays)?
You can use the imdecode function from OpenCV to decode the raw bytes string into a numpy array:
import cv2 import numpy as np raw_bytes = request.data # assume this is the raw bytes string of the video file arr = np.fromstring(raw_bytes, np.uint8) # create a numpy array from the raw bytes image = cv2.imdecode(arr, cv2.IMREAD_UNCHANGED) # decode the array into an image
Then you can use the cv2.VideoCapture class to read the video frame by frame:
cap = cv2.VideoCapture(image) while cap.isOpened(): ret, frame = cap.read() if ret: # process the frame here else: breakcap.release()
Note that this will only work if the raw bytes string is a valid video file. If you are receiving the video in a different format (e.g. base64-encoded), you will need to first decode it into raw bytes before using imdecode.