cemicel
cemicel

Reputation: 35

Read video frames as byte data

So I need to read a video source (a file/stream) frame by frame and than, send each frame over the internet in real time. The issues is that when I'm using OpenCV VideCapture it returns numpy arrays which are difficult to handle (a frame can take up to 20 Mb, but when I save it as png it's around 300kb). (it would be cool if using some flag VideCapture could return bytes instead of numpy array )

The question is, how can I read a video source as raw bytes, so I don't need to convert numpy to bytes in case of using OpenCV.

Upvotes: 2

Views: 3844

Answers (1)

thshea
thshea

Reputation: 1087

You can get bytes by encoding as a png and then taking it as bytes. If you are storing many frames in memory this will help (as long as you can encode fast enough), but if the size of an individual frame is a problem, this may not be useful.

There is not a way I can find to return a non-numpy array from videocapture.

frameBytes = cv2.imencode('.png', frame)[1].tobytes()
del frame  # clear from memory

Upvotes: 3

Related Questions