-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathread_protobuf.py
80 lines (73 loc) · 3.77 KB
/
read_protobuf.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
import scenenet_pb2 as sn
import os
DATA_ROOT_PATH = 'data/train_0/train/0'
PROTOBUF_PATH = 'data/train_protobufs/scenenet_rgbd_train_0.pb'
# These functions produce a file path (on Linux systems) to the image given
# a view and render path from a trajectory. As long the DATA_ROOT_PATH to the
# root of the dataset is given. I.e. to either val or train
def photo_path_from_view(render_path,view):
photo_path = os.path.join(render_path,'photo')
image_path = os.path.join(photo_path,'{0}.jpg'.format(view.frame_num))
return os.path.join(DATA_ROOT_PATH,image_path)
def instance_path_from_view(render_path,view):
photo_path = os.path.join(render_path,'instance')
image_path = os.path.join(photo_path,'{0}.png'.format(view.frame_num))
return os.path.join(DATA_ROOT_PATH,image_path)
def depth_path_from_view(render_path,view):
photo_path = os.path.join(render_path,'depth')
image_path = os.path.join(photo_path,'{0}.png'.format(view.frame_num))
return os.path.join(DATA_ROOT_PATH,image_path)
if __name__ == '__main__':
trajectories = sn.Trajectories()
try:
with open(PROTOBUF_PATH,'rb') as f:
trajectories.ParseFromString(f.read())
except IOError:
print('Scenenet protobuf data not found at location:{0}'.format(DATA_ROOT_PATH))
print('Please ensure you have copied the pb file to the data directory')
print('Number of trajectories:{0}'.format(len(trajectories.trajectories)))
for traj in trajectories.trajectories:
layout_type = sn.SceneLayout.LayoutType.Name(traj.layout.layout_type)
layout_path = traj.layout.model
print('='*20)
print('Render path:{0}'.format(traj.render_path))
print('Layout type:{0} path:{1}'.format(layout_type,layout_path))
print('='*20)
print('')
print('Number of instances: {0}'.format(len(traj.instances)))
'''
The instances attribute of trajectories contains all of the information
about the different instances. The instance.instance_id attribute provides
correspondences with the rendered instance.png files. I.e. for a given
trajectory, if a pixel is of value 1, the information about that instance,
such as its type, semantic class, and wordnet id, is stored here.
For more information about the exact information available refer to the
scenenet.proto file.
'''
for instance in traj.instances:
instance_type = sn.Instance.InstanceType.Name(instance.instance_type)
print('='*20)
print('Instance id:{0}'.format(instance.instance_id))
print('Instance type:{0}'.format(instance_type))
if instance.instance_type != sn.Instance.BACKGROUND:
print('Wordnet id:{0}'.format(instance.semantic_wordnet_id))
print('Plain english name:{0}'.format(instance.semantic_english))
if instance.instance_type == sn.Instance.LIGHT_OBJECT:
light_type = sn.LightInfo.LightType.Name(instance.light_info.light_type)
print('Light type:{0}'.format(light_type))
if instance.instance_type == sn.Instance.RANDOM_OBJECT:
print('Object info:{0}'.format(instance.object_info))
print('-'*20)
print('')
print('Render path:{0}'.format(traj.render_path))
'''
The views attribute of trajectories contains all of the information
about the rendered frames of a scene. This includes camera poses,
frame numbers and timestamps.
'''
for view in traj.views:
print(photo_path_from_view(traj.render_path,view))
print(depth_path_from_view(traj.render_path,view))
print(instance_path_from_view(traj.render_path,view))
print(view)
break