tfds.features.Video Usage for video decoding in tensorflow 2

Viewed 890

I am trying to decode a video in tensorflow 2 using tfds.features.Video , so that the output is a "tf.Tensor of type tf.uint8 and shape [num_frames, height, width, channels]" using following code :

import numpy as np
import pandas as pd
import tensorflow as tf
import tensorflow_datasets as tfds
df_trains= pd.DataFrame()
df_trains['video_files']= ['aa.mp4']

files_ds = tf.data.Dataset.from_tensor_slices(df_trains.video_files)

video_class = tfds.features.Video(shape=(None, 1080, 1920,3), encoding_format='png', ffmpeg_extra_args=())

a= video_class.decode_example(files_ds)

However it generates following error: "AssertionError: Feature Video can only be decoded when defined as top-level feature, through info.features.decode_example()"

I am unable to solve it, please help in this regard.

1 Answers

You can't use tfds.features.Video like that. tfds.features.Video is commonly used with tfds.builder or tfds.load.

So when you use tfds_download_dataset, you feel comfortable, but hard to use custom dataset.

features = tfds.features.FeaturesDict({
    'video': tfds.features.Video(shape=(None, 1080, 1920,3)),
})
tfds.load(..., decoders=features, ...)

But tfds.features.Video can be used by making my_dataset including your data.

However, it's very annoying. So cv2 is better than that.

Related