def video(self, tensor=None, videofile=None, win=None, env=None, opts=None):
"""
This function plays a video. It takes as input the filename of the video
or a `LxHxWxC` tensor containing all the frames of the video. The function
does not support any plot-specific `options`.
"""
opts = {} if opts is None else opts
opts['fps'] = opts.get('fps', 25)
_assert_opts(opts)
assert tensor is not None or videofile is not None, \
'should specify video tensor or file'
if tensor is not None:
import cv2
import tempfile
assert tensor.ndim == 4, 'video should be in 4D tensor'
videofile = '/tmp/%s.ogv' % next(tempfile._get_candidate_names())
if cv2.__version__.startswith('2'): # OpenCV 2
fourcc = cv2.cv.CV_FOURCC(
chr(ord('T')),
chr(ord('H')),
chr(ord('E')),
chr(ord('O'))
)
elif cv2.__version__.startswith('3'): # OpenCV 3
fourcc = cv2.VideoWriter_fourcc(
chr(ord('T')),
chr(ord('H')),
chr(ord('E')),
chr(ord('O'))
)
writer = cv2.VideoWriter(
videofile,
fourcc,
opts.get('fps'),
(tensor.shape[1], tensor.shape[2])
)
assert writer.isOpened(), 'video writer could not be opened'
for i in range(tensor.shape[0]):
writer.write(tensor[i, :, :, :])
writer.release()
writer = None
extension = videofile.split(".")[-1].lower()
mimetypes = dict(mp4='mp4', ogv='ogg', avi='avi', webm='webm')
mimetype = mimetypes.get(extension)
assert mimetype is not None, 'unknown video type: %s' % extension
bytestr = loadfile(videofile)
videodata = """
<video controls>
<source type="video/%s" src="data:video/%s;base64,%s">
Your browser does not support the video tag.
</video>
""" % (mimetype, mimetype, base64.b64encode(bytestr).decode('utf-8'))
return self.text(text=videodata, win=win, env=env, opts=opts)
评论列表
文章目录