forked from wandrzej/objectroom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.py
158 lines (109 loc) · 6.12 KB
/
script.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import json
import numpy as np
import sys
from scipy import misc
import glob
import imageio
import os.path
## I added this to defend myself from missing that it can change
spawn_area_x = str(sys.argv[1])
spawn_area_y = str(sys.argv[2])
spawn_distance_z = str(sys.argv[2])
def get_absolute_depth_image(image,near = 0.8, far = 15):
k1 = (far + near) / (far - near)
k2 = (- 2.0 * far * near) / (far - near)
divisor = np.max(image[:,:,0]) if np.max(image[:,:,0]) != 0 else 127.0
res = k2 / ((image[:,:,0] / divisor) - k1)
res = far + near - res #to inverse depth map
return res
def rgb_to_grayscale(image_to_convert):
rgb_weights = [0.2989, 0.5870, 0.1140]
grayscale_image = np.dot(image_to_convert[...,:3], rgb_weights)
return grayscale_image[:,:,None] #increase dimension
sample_frames = {'rgb_in' : [], 'rgb_gt' : [], 'depth_gt' : [], 'fore_msk_gt' : [], 'fore_z_extr_gt' : [], 'back_msk_gt' : [], 'back_z_extr_gt' : []}
path = '.'
with open(f'{path}/logs/objects_relative_to_cam.json') as f:
data = json.load(f)
frame_count = len(data['contentList'])
for current_i_frame in range(1,frame_count-1):
# if(type_dataset == 'test'):
# if(current_i_frame < 11001 or current_i_frame > 12001):
# continue
# if(type_dataset == 'val'):
# if(current_i_frame < 9001 or current_i_frame > 11001):
# continue
# if(type_dataset == 'train'):
# if(current_i_frame > 9000):
# break
full_frame_index = data['contentList'][current_i_frame]['index']
foreground_len = len(data['contentList'][current_i_frame]['foregroundObjects'])
background_len = len(data['contentList'][current_i_frame]['backgroundObjects'])
#Reading full RGB image
rgb_image = imageio.imread(f'{path}/rgb/rgb_{full_frame_index + 1}.png')
#Goal : rgb_in & rgb_gt : [B, 1, 64, 64, 3]
#print(rgb_image[None,:,:,0:3].shape)
sample_frames['rgb_in'].append(rgb_image[None,:,:,0:3])
sample_frames['rgb_gt'].append(rgb_image[None,:,:,0:3])
#Reading depth map
depth_image = imageio.imread(f'{path}/depth_map/aov_image_' + format(full_frame_index-2, '04') + ".png")
absolute_depth_image = get_absolute_depth_image(depth_image).astype('float32')
#Goal : depth_gt [B, 1, 64, 64]
sample_frames['depth_gt'].append(absolute_depth_image[None,:,:,None])
foreground_mask_instances = []
foreground_latent_instances = []
for current_object in range(foreground_len):
single_mask_instance = imageio.imread(f'{path}/instances/Instance_{full_frame_index+2+current_object}.png')
single_mask_instance = rgb_to_grayscale(single_mask_instance)
restructured_single_mask_instance = (single_mask_instance[:,:,:] > 0).astype('bool')
foreground_mask_instances.append(restructured_single_mask_instance)
orientation = 1/np.pi * np.deg2rad(data['contentList'][current_i_frame]['foregroundObjects'][current_object]['orientation'])
scale = 1.25 * data['contentList'][current_i_frame]['foregroundObjects'][current_object]['scale']
object_latent_variable = np.array([scale] + data['contentList'][current_i_frame]['foregroundObjects'][current_object]['position'] + [orientation])
foreground_latent_instances.append(object_latent_variable)
#Goal 'fore_msk_gt' [B, N, 1, 64, 64]
sample_frames['fore_msk_gt'].append(np.array(foreground_mask_instances)[:,None,:,:,0])
#Goal 'fore_z_extr_gt' [B,N,1,5]
sample_frames['fore_z_extr_gt'].append(np.array(foreground_latent_instances)[:,None,:])
background_mask_instances = []
background_latent_instances = []
for current_object in range(background_len):
single_mask_instance = imageio.imread(f'{path}/instances/Instance_{full_frame_index+2+foreground_len + current_object}.png')
single_mask_instance = rgb_to_grayscale(single_mask_instance)
restructured_single_mask_instance = (single_mask_instance[:,:,:] > 0).astype('bool')
background_mask_instances.append(restructured_single_mask_instance)
orientation = data['contentList'][current_i_frame]['backgroundObjects'][current_object]['orientation']
scale = data['contentList'][current_i_frame]['backgroundObjects'][current_object]['scale']
object_latent_variable = np.array(data['contentList'][current_i_frame]['backgroundObjects'][current_object]['position'] + [orientation, scale])
background_latent_instances.append(object_latent_variable)
#Goal 'back_msk_gt' [B, N, 1, 64, 64]
sample_frames['back_msk_gt'].append(np.array(background_mask_instances)[:,None,:,:,0])
#Goal 'back_z_extr_gt' [B,N,1,5]
sample_frames['back_z_extr_gt'].append(np.array(background_latent_instances)[:,None,:])
if(current_i_frame % 100 == 0):
print(f'{current_i_frame}/12000, {current_i_frame/12000*100}%')
#Goal : rgb_in & rgb_gt : [B, 1, 64, 64, 3]
print(np.array(sample_frames['rgb_in']).shape)
#Goal : depth_gt [B, 1, 64, 64]
print(np.array(sample_frames['depth_gt']).shape)
#Goal 'fore_msk_gt' [B, N, 1, 64, 64]
print(np.array(sample_frames['fore_msk_gt']).shape)
#Goal 'fore_z_extr_gt' [B,N,1,5]
print(np.array(sample_frames['fore_z_extr_gt']).shape)
#Goal 'back_msk_gt' [B, N, 1, 64, 64]
# print(np.array(sample_frames['back_msk_gt']).shape)
print(np.array(sample_frames['back_msk_gt']).shape)
#Goal 'back_z_extr_gt' [B,N,1,5]
print(np.array(sample_frames['back_z_extr_gt']).shape)
mask = np.concatenate([sample_frames['fore_msk_gt'], sample_frames['back_msk_gt']], axis = 1)
obj_extrs = np.concatenate([sample_frames['fore_z_extr_gt'], sample_frames['back_z_extr_gt']], axis = 1)
sample_frames['mask'] = mask
sample_frames['obj_extrs'] = obj_extrs
## Saving them
split_ranges = {'train' : (0,9000),'val' : (9000,10000), 'test' : (10000,12500)}
for key,value in sample_frames.items():
for split_type, split_range in split_ranges.items():
with open(f'{key}_{split_type}.npy', 'wb') as f:
save_value = np.array(value[split_range[0]:split_range[1]])
print(save_value.shape)
np.save(key + '_' + split_type, save_value)
{"mode":"full","isActive":False}