Skip to content

Commit fbeaf63

Browse files
author
The TensorFlow Datasets Authors
committed
Include video support in conversion_utils.
PiperOrigin-RevId: 788365222
1 parent 02ce9fd commit fbeaf63

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

tensorflow_datasets/core/utils/conversion_utils.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,18 @@ def to_tfds_value(value: Any, feature: feature_lib.FeatureConnector) -> Any:
128128
match feature:
129129
case feature_lib.ClassLabel() | feature_lib.Scalar():
130130
return value
131+
case feature_lib.Video():
132+
match value:
133+
case dict():
134+
if 'path' in value and value['path']:
135+
return value['path']
136+
elif 'bytes' in value and value['bytes']:
137+
return value['bytes']
138+
else:
139+
raise ValueError(
140+
'Dictionary-like video features must have either a `path` or'
141+
' `bytes` key.'
142+
)
131143
case feature_lib.FeaturesDict():
132144
return {
133145
name: to_tfds_value(value.get(name), inner_feature)

tensorflow_datasets/core/utils/conversion_utils_test.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def test_convert_value_raises(value, feature):
8181
@pytest.mark.parametrize(
8282
'value,feature,expected_value',
8383
[
84-
# datetime
84+
# Datetime.
8585
(
8686
datetime.datetime(1970, 1, 1, tzinfo=datetime.timezone.utc),
8787
feature_lib.Scalar(dtype=np.int64),
@@ -92,14 +92,14 @@ def test_convert_value_raises(value, feature):
9292
feature_lib.Scalar(dtype=np.int64),
9393
86400,
9494
),
95-
# scalar
95+
# Scalar.
9696
(42, feature_lib.Scalar(dtype=np.int64), 42),
9797
(42, feature_lib.Scalar(dtype=np.int32), 42),
9898
('abc', feature_lib.Scalar(dtype=np.object_), 'abc'),
9999
(True, feature_lib.Scalar(dtype=np.bool_), True),
100100
(False, feature_lib.Scalar(dtype=np.bool_), False),
101101
(42.0, feature_lib.Scalar(dtype=np.float32), 42.0),
102-
# sequence
102+
# Sequence.
103103
([42], feature_lib.Sequence(feature=tf.int64), [42]),
104104
(42, feature_lib.Sequence(feature=tf.int64), [42]),
105105
(None, feature_lib.Sequence(feature=tf.int64), []),
@@ -111,15 +111,15 @@ def test_convert_value_raises(value, feature):
111111
),
112112
{'someint': [b'', 'string', b'']},
113113
),
114-
# image
114+
# Image.
115115
(
116116
lazy_imports_lib.lazy_imports.PIL_Image.new(mode='L', size=(4, 4)),
117117
feature_lib.Image(),
118118
lazy_imports_lib.lazy_imports.PIL_Image.new(
119119
mode='RGB', size=(4, 4)
120120
),
121121
),
122-
# dict
122+
# Dict.
123123
(
124124
{
125125
'de': b'Hallo Welt',
@@ -148,7 +148,18 @@ def test_convert_value_raises(value, feature):
148148
}),
149149
{'name': b'Name', 'age': 100},
150150
),
151-
# nan, but the feature type is not float
151+
# Video.
152+
(
153+
{'path': 'path/to/video.avi', 'bytes': None},
154+
feature_lib.Video(),
155+
'path/to/video.avi',
156+
),
157+
(
158+
{'path': None, 'bytes': b'video_bytes'},
159+
feature_lib.Video(),
160+
b'video_bytes',
161+
),
162+
# nan, but the feature type is not float.
152163
(
153164
np.nan,
154165
feature_lib.Text(),

0 commit comments

Comments
 (0)