forked from cvisionai/tator-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtracks.py
107 lines (91 loc) · 3.86 KB
/
tracks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python
""" This example shows how to create tracks.
"""
import logging
import sys
import random
import time
import tator
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
logger = logging.getLogger(__name__)
def random_box(video, type_id, frame):
""" Returns random localization spec for given video object and frame.
:param video: :class:`tator.models.Media` object.
:param type_id: Localization type ID.
:returns: Random localization spec.
"""
x = random.uniform(0.0, 0.95)
y = random.uniform(0.0, 0.95)
return dict(
x=x, y=y,
width=random.uniform(0.05, 1.0 - x),
height=random.uniform(0.05, 1.0 - y),
frame=frame,
media_id=video.id,
type=type_id,
)
if __name__ == '__main__':
parser = tator.get_parser()
parser.add_argument('--video_type_id', help="Video type ID.", type=int, required=True)
parser.add_argument('--video_id', help="Video ID.", type=int, required=True)
args = parser.parse_args()
tator_api = tator.get_api(args.host, args.token)
# Get the video object and video type object.
video = tator_api.get_media(args.video_id)
video_type = tator_api.get_media_type(args.video_type_id)
# Create a new localization type for the project. We set it to
# not visible because we only want to see tracks in the UI.
response = tator_api.create_localization_type(video_type.project, localization_type_spec={
'name': 'Track boxes',
'dtype': 'box',
'media_types': [video_type.id],
'visible': False,
})
loc_type_id = response.id
logger.info(response.message)
# Create a new state type for the project to represent tracks. Tracks will
# have a user-defined attribute called "Label".
response = tator_api.create_state_type(video_type.project, state_type_spec={
'name': 'Tracks',
'association': 'Localization',
'media_types': [video_type.id],
'attribute_types': [{
'name': 'Label',
'dtype': 'string',
}],
})
state_type_id = response.id
logger.info(response.message)
# Create one localization per frame.
localizations = [random_box(video, loc_type_id, frame) for frame in range(video.num_frames)]
localization_ids = []
for response in tator.util.chunked_create(
tator_api.create_localization_list, video_type.project, body=localizations
):
localization_ids += response.id
logger.info(f"Created {len(localization_ids)} localizations!")
# Create a new track for every 10 localizations.
states = [{
'type': state_type_id,
'localization_ids': localization_ids[idx:idx+10],
'media_ids': [args.video_id],
'Label': f'Track {int(idx / 10)}', # Providing values for attributes is optional
} for idx in range(0, len(localization_ids), 10)]
state_ids = []
for response in tator.util.chunked_create(
tator_api.create_state_list, video_type.project, body=states
):
state_ids += response.id
logger.info(f"Created {len(state_ids)} tracks!")
# Retrieve tracks associated to localizations.
states = tator_api.get_state_list_by_id(video_type.project,
{'localization_ids': localization_ids})
assert(len(states) == len(state_ids))
# Retrieve localizations associated to tracks.
localizations = tator_api.get_localization_list_by_id(video_type.project,
{'state_ids': state_ids})
assert(len(localizations) == len(localization_ids))
# The "state graphic", a series of localization images associated with the
# track, can be retrieved. Below this is done for the first track. Using the
# full_state_graphic utility, a list of PIL images is returned.
images = tator.util.full_state_graphic(tator_api, state_ids[0])