Assertion error when making an MP4 video out of numpy arrays with OpenCV

279 views Asked by At

I have this python code that should make a video:

import cv2
import numpy as np

out = cv2.VideoWriter("/tmp/test.mp4",
                      cv2.VideoWriter_fourcc(*'MP4V'),
                      25,
                      (500, 500),
                      True)
data = np.zeros((500,500,3))
for i in xrange(500):
    out.write(data)

out.release()

I expect a black video but the code throws an assertion error:

$ python test.py
OpenCV(3.4.1) Error: Assertion failed (image->depth == 8) in writeFrame, file /io/opencv/modules/videoio/src/cap_ffmpeg.cpp, line 274
Traceback (most recent call last):
  File "test.py", line 11, in <module>
    out.write(data)
cv2.error: OpenCV(3.4.1) /io/opencv/modules/videoio/src/cap_ffmpeg.cpp:274: error: (-215) image->depth == 8 in function writeFrame

I tried various fourcc values but none seem to work.

2

There are 2 answers

1
fakedrake On

According to @jeru-luke and @dan-masek's comments:

import cv2
import numpy as np

out = cv2.VideoWriter("/tmp/test.mp4",
                      cv2.VideoWriter_fourcc(*'mp4v'),
                      25,
                      (1000, 500),
                      True)

data = np.transpose(np.zeros((1000, 500,3), np.uint8), (1,0,2))
for i in xrange(500):
    out.write(data)

out.release()
0
Dan Mašek On

The problem is that you did not specify the data type of elements when calling np.zeros. As the documentation states, by default numpy will use float64.

>>> import numpy as np
>>> np.zeros((500,500,3)).dtype
dtype('float64')

However, the VideoWriter implementation only supports 8 bit image depth (as the "(image->depth == 8)" part of the error message suggests).

The solution is simple -- specify the appropriate data type, in this case uint8.

data = np.zeros((500,500,3), dtype=np.uint8)