18
18
from __future__ import annotations
19
19
20
20
import collections
21
+ from collections .abc import Mapping
21
22
import json
22
23
import os
23
- from typing import Any , Dict , Optional , Tuple
24
+ from typing import Any
24
25
25
26
from etils import epath
26
27
import numpy as np
52
53
}
53
54
"""
54
55
55
- NestedDict = Dict [str , Any ]
56
+ NestedDict = Mapping [str , Any ]
56
57
57
58
58
59
def _build_annotations_index (
59
60
annotations : NestedDict ,
60
- ) -> Tuple [NestedDict , NestedDict , NestedDict , NestedDict ]:
61
+ ) -> tuple [NestedDict , NestedDict , NestedDict , NestedDict ]:
61
62
"""Builds several dictionaries to aid in looking up annotations."""
62
63
vids = {x ['id' ]: x for x in annotations ['videos' ]}
63
64
images = {x ['id' ]: x for x in annotations ['images' ]}
@@ -72,7 +73,7 @@ def _build_annotations_index(
72
73
return vids , ann_to_images , track_to_anns , vid_to_tracks
73
74
74
75
75
- def _merge_categories_map (annotations : NestedDict ) -> Dict [str , str ]:
76
+ def _merge_categories_map (annotations : NestedDict ) -> dict [str , str ]:
76
77
"""Some categories should be renamed into others.
77
78
78
79
This code segment is based on the TAO provided preprocessing API.
@@ -91,7 +92,9 @@ def _merge_categories_map(annotations: NestedDict) -> Dict[str, str]:
91
92
return merge_map
92
93
93
94
94
- def _maybe_prepare_manual_data (dl_manager : tfds .download .DownloadManager ):
95
+ def _maybe_prepare_manual_data (
96
+ dl_manager : tfds .download .DownloadManager ,
97
+ ) -> tuple [epath .Path | None , epath .Path | None ]:
95
98
"""Return paths to the manually downloaded data if it is available."""
96
99
97
100
# The file has a different name each time it is downloaded.
@@ -115,7 +118,7 @@ def _maybe_prepare_manual_data(dl_manager: tfds.download.DownloadManager):
115
118
return dl_manager .extract (files )
116
119
117
120
118
- def _get_category_id_map (annotations_root ) -> Dict [str , int ]:
121
+ def _get_category_id_map (annotations_root ) -> dict [str , int ]:
119
122
"""Gets a map from the TAO category id to a tfds category index.
120
123
121
124
The tfds category index is the index which a category appears in the
@@ -150,7 +153,7 @@ def _get_category_id_map(annotations_root) -> Dict[str, int]:
150
153
151
154
152
155
def _preprocess_annotations (
153
- annotations_file : str , id_map : Dict [ int , int ]
156
+ annotations_file : str , id_map : dict [ str , int ]
154
157
) -> NestedDict :
155
158
"""Preprocesses the data to group together some category labels."""
156
159
with epath .Path (annotations_file ).open ('r' ) as f :
@@ -226,8 +229,8 @@ class TaoConfig(tfds.core.BuilderConfig):
226
229
def __init__ (
227
230
self ,
228
231
* ,
229
- height : Optional [ int ] = None ,
230
- width : Optional [ int ] = None ,
232
+ height : int | None = None ,
233
+ width : int | None = None ,
231
234
** kwargs ,
232
235
):
233
236
"""The parameters specifying how the dataset will be processed.
@@ -391,11 +394,15 @@ def _create_metadata(
391
394
return metadata
392
395
393
396
def _generate_examples (
394
- self , data_path , manual_path , annotations_path , id_map
397
+ self ,
398
+ data_path : epath .PathLike ,
399
+ manual_path : epath .Path | None ,
400
+ annotations_path : epath .Path ,
401
+ id_map : dict [str , int ],
395
402
):
396
403
"""Yields examples."""
397
404
beam = tfds .core .lazy_imports .apache_beam
398
- annotations = _preprocess_annotations (annotations_path , id_map ) # pytype: disable=wrong-arg-types # always-use-return-annotations
405
+ annotations = _preprocess_annotations (os . fspath ( annotations_path ) , id_map )
399
406
outs = _build_annotations_index (annotations )
400
407
vids , ann_to_images , track_to_anns , vid_to_tracks = outs
401
408
0 commit comments