forked from naurril/SUSTechPOINTS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scene_reader.py
122 lines (100 loc) · 3.48 KB
/
scene_reader.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import os
import json
this_dir = os.path.dirname(os.path.abspath(__file__))
root_dir = os.path.join(this_dir, "data")
def get_all_scenes():
all_scenes = get_scene_names()
print(all_scenes)
return list(map(get_one_scene, all_scenes))
def get_scene_names():
scenes = os.listdir(root_dir)
scenes = filter(lambda s: not os.path.exists(os.path.join(root_dir, s, "disable")), scenes)
scenes = list(scenes)
scenes.sort()
return scenes
def get_one_scene(s):
scene = {
"scene": s,
"frames": []
}
scene_dir = os.path.join(root_dir, s)
frames = os.listdir(os.path.join(scene_dir, "pcd"))
#print(s, frames)
frames.sort()
scene["pcd_ext"]="pcd"
for f in frames:
#if os.path.isfile("./data/"+s+"/pcd/"+f):
filename, fileext = os.path.splitext(f)
scene["frames"].append(filename)
scene["pcd_ext"] = fileext
point_transform_matrix=[]
if os.path.isfile(os.path.join(scene_dir, "point_transform.txt")):
with open(os.path.join(scene_dir, "point_transform.txt")) as f:
point_transform_matrix=f.read()
point_transform_matrix = point_transform_matrix.split(",")
def strip_str(x):
return x.strip()
calib={}
if os.path.exists(os.path.join(scene_dir, "calib")):
calibs = os.listdir(os.path.join(scene_dir, "calib"))
for c in calibs:
calib_file = os.path.join(scene_dir, "calib", c)
calib_name, _ = os.path.splitext(c)
if os.path.isfile(calib_file):
#print(calib_file)
with open(calib_file) as f:
cal = json.load(f)
calib[calib_name] = cal
# camera names
image = []
image_ext = ""
cam_path = os.path.join(scene_dir, "image")
if os.path.exists(cam_path):
cams = os.listdir(cam_path)
for c in cams:
cam_file = os.path.join(scene_dir, "image", c)
if os.path.isdir(cam_file):
if image:
image.append(c)
else:
image = [c]
if image_ext == "":
#detect image file ext
files = os.listdir(cam_file)
if len(files)>=2:
_,image_ext = os.path.splitext(files[0])
if image_ext == "":
image_ext = ".jpg"
scene["image_ext"] = image_ext
if not os.path.isdir(os.path.join(scene_dir, "bbox.xyz")):
scene["boxtype"] = "psr"
if point_transform_matrix:
scene["point_transform_matrix"] = point_transform_matrix
if calib:
scene["calib"] = calib
if image:
scene["image"] = image
else:
scene["boxtype"] = "xyz"
if point_transform_matrix:
scene["point_transform_matrix"] = point_transform_matrix
if calib:
scene["calib"] = calib
if image:
scene["image"] = image
return scene
def read_annotations(scene, frame):
filename = os.path.join(root_dir, scene, "label", frame+".json")
if (os.path.isfile(filename)):
with open(filename,"r") as f:
ann=json.load(f)
#print(ann)
return ann
else:
return []
def save_annotations(scene, frame, anno):
filename = os.path.join(root_dir, scene, "label", frame+".json")
with open(filename, 'w') as outfile:
json.dump(anno, outfile)
if __name__ == "__main__":
print(get_all_scenes())