-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcygnus_interpolated_keypoints.py
244 lines (193 loc) · 8.93 KB
/
cygnus_interpolated_keypoints.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import numpy as np
import bpy
import starfish
from starfish import Frame, Sequence
import math
import starfish.annotation
from mathutils import Euler
import sys
import json
import time
import os
import boto3
import shortuuid
import subprocess
import tqdm
def enable_gpus(device_type, use_cpus=False):
preferences = bpy.context.preferences
cycles_preferences = preferences.addons["cycles"].preferences
cuda_devices, opencl_devices = cycles_preferences.get_devices()
if device_type == "CUDA":
devices = cuda_devices
elif device_type == "OPENCL":
devices = opencl_devices
else:
raise RuntimeError("Unsupported device type")
activated_gpus = []
for device in devices:
if device.type == "CPU":
device.use = use_cpus
else:
device.use = True
activated_gpus.append(device.name)
cycles_preferences.compute_device_type = device_type
for scene in bpy.data.scenes:
scene.cycles.device = 'GPU'
return activated_gpus
enable_gpus("CUDA", True)
sys.stdout = sys.stderr
BACKGROUND_COLOR = (0, 0, 0)
LABEL_MAP_FULL = {
'barrel': (206, 0, 0),
'panel_right': (206, 206, 0),
'panel_left': (0, 0, 206),
'orbitrak_logo': (0, 206, 206),
'cygnus_logo': (206, 0, 206)
}
LABEL_MAP_SINGLE = {'cygnus': list(LABEL_MAP_FULL.values())}
OG_KEYPOINTS = {
'barrel_center': (0.0, 0.0, 0.0),
'panel_left': (0.0, -3.3931, -3.4995),
'panel_right': (0.0, 3.3931, -3.4995),
'cygnus_logo': (1.524, 0.0, 0.8718),
'orbitrak_logo': (1.524, 0.0, 0.2359),
'barrel_bottom': (0, 0, -3.6295),
'barrel_top': (0, 0, 3.18566)
}
def reset_filter_nodes(node_tree):
"""
resets filters nodes to default values that will not modify final image
"""
if 'Glare' in node_tree.nodes.keys():
node_tree.nodes['Glare'].mix = -1
node_tree.nodes['Glare'].threshold = 8
if 'Blur' in node_tree.nodes.keys():
node_tree.nodes['Blur'].size_x = 0
node_tree.nodes['Blur'].size_y = 0
def generate(ds_name):
start_time = time.time()
# check if folder exists in render, if not, create folder
try:
os.mkdir(os.path.join("render", ds_name))
except Exception:
pass
data_storage_path = os.path.join(os.getcwd(), "render", ds_name)
output_node = bpy.data.scenes["Render"].node_tree.nodes["File Output"]
output_node.base_path = data_storage_path
# remove all animation
for scene in bpy.data.scenes:
for obj in scene.objects:
obj.animation_data_clear()
bpy.context.scene.frame_set(0)
shortuuid.set_alphabet('12345678abcdefghijklmnopqrstwxyz')
def E(*args):
return Euler([-math.pi, 0, 0]).to_quaternion() @ Euler(*args).to_quaternion()
waypoints = [
Frame(pose=E((math.radians(-45), math.radians(-35), math.radians(0)), 'XYZ'), distance=50, offset=(0.35, 0.35), background=Euler((math.radians(0), math.radians(0), math.radians(0)), 'XYZ')),
Frame(pose=E((math.radians(-30), math.radians(-40), math.radians(-10)), 'XYZ'), distance=45, offset=(0.35, 0.5), background=Euler((math.radians(20), math.radians(0), math.radians(0)), 'XYZ')),
Frame(pose=E((math.radians(0), math.radians(-50), math.radians(20)), 'XYZ'), distance=40, offset=(0.5, 0.5), background=Euler((math.radians(44), math.radians(0), math.radians(-5)), 'XYZ')),
Frame(pose=E((math.radians(30), math.radians(-50), math.radians(-10)), 'XYZ'), distance=37, offset=(0.55, 0.55), background=Euler((math.radians(70), math.radians(0), math.radians(-20)), 'XYZ')),
Frame(pose=E((math.radians(60), math.radians(-55), math.radians(-10)), 'XYZ'), distance=35, offset=(0.55, 0.6), background=Euler((math.radians(90), math.radians(0), math.radians(-40)), 'XYZ')),
]
counts = [120, 120, 120, 120]
sequence = starfish.Sequence.interpolated(waypoints, counts)
keypoints = starfish.annotation.generate_keypoints(bpy.data.objects['Cygnus_Real'], 128, seed=4)
with open(os.path.abspath(__file__), 'r') as f:
code = f.read()
metadata = {
'keypoints': keypoints,
'og_keypoints': OG_KEYPOINTS,
'label_map': LABEL_MAP_FULL
}
with open(os.path.join(data_storage_path, 'metadata.json'), 'w') as f:
json.dump(metadata, f)
with open(os.path.join(data_storage_path, 'gen_code.py'), 'w') as f:
f.write(code)
node_tree = bpy.data.scenes["Render"].node_tree
reset_filter_nodes(node_tree)
for i, frame in enumerate(tqdm.tqdm(sequence)):
frame.setup(bpy.data.scenes['Real'], bpy.data.objects["Cygnus_Real"], bpy.data.objects["Camera_Real"], bpy.data.objects["Sun"])
frame.setup(bpy.data.scenes['Mask_ID'], bpy.data.objects["Cygnus_MaskID"], bpy.data.objects["Camera_MaskID"], bpy.data.objects["Sun"])
# create name for the current image (unique to that image)
name = str(i).zfill(5)
output_node.file_slots[0].path = "image_#" + str(name)
output_node.file_slots[1].path = "mask_#" + str(name)
# render
bpy.ops.render.render(scene="Render")
# mask/bbox stuff
mask = starfish.annotation.normalize_mask_colors(os.path.join(data_storage_path, f'mask_0{name}.png'),
list(LABEL_MAP_SINGLE.values())[0] + [BACKGROUND_COLOR])
frame.bboxes = starfish.annotation.get_bounding_boxes_from_mask(mask, LABEL_MAP_SINGLE)
frame.centroids = starfish.annotation.get_centroids_from_mask(mask, LABEL_MAP_SINGLE)
frame.keypoints = starfish.annotation.project_keypoints_onto_image(keypoints, bpy.data.scenes['Real'],
bpy.data.objects['Cygnus_Real'], bpy.data.objects['Camera_Real'])
og_keypoints = starfish.annotation.project_keypoints_onto_image(OG_KEYPOINTS.values(), bpy.data.scenes['Real'],
bpy.data.objects['Cygnus_Real'], bpy.data.objects['Camera_Real'])
frame.og_keypoints = {k: v for k, v in zip(OG_KEYPOINTS.keys(), og_keypoints)}
frame.sequence_name = ds_name
# dump data to json
with open(os.path.join(output_node.base_path, "meta_0" + str(name)+ ".json"), "w") as f:
f.write(frame.dumps())
print("===========================================" + "\r")
time_taken = time.time() - start_time
print("------Time Taken: %s seconds----------" % (time_taken) + "\r")
print("Number of images generated: " + str(i) + "\r")
print("Average time per image: " + str(time_taken / i))
print("Data stored at: " + data_storage_path)
bpy.ops.wm.quit_blender()
def upload(ds_name, bucket_name):
print("\n\n______________STARTING UPLOAD_________")
subprocess.run(['aws', 's3', 'sync', os.path.join('render', ds_name), f's3://{bucket_name}/{ds_name}'])
"""
# Create an S3 client
s3 = boto3.client('s3')
print("...begining upload to %s..." % bucket_name)
try:
files = next(os.walk(os.path.join(os.getcwd(), "render", ds_name)))[2]
except Exception:
print("...No data set named " + ds_name + " found in starfish/render. Please generate images with that folder name or move existing folder into render folder")
exit()
# count number of files
num_files = 0
# For every file in directory
for file in files:
# ignore hidden files
if not file.startswith('.'):
# upload to s3
print("uploading...")
sys.stdout.write("\033[F")
local_file = os.path.join(os.getcwd(), "render", ds_name, file)
s3.upload_file(local_file, bucket_name, os.path.join(ds_name, file)
num_files = num_files + 1
print("...finished uploading...%d files uploaded..." % num_files)
"""
def validate_bucket_name(bucket_name):
s3t = boto3.resource('s3')
# check if bucket exits. If not return false
if s3t.Bucket(bucket_name).creation_date is None:
print("...Bucket does not exist, enter valid bucket name...")
return False
else:
# if exists, return true
print("...bucket exists....")
return True
def main():
try:
os.mkdir("render")
except Exception:
pass
yes = {'y', 'Y', 'yes'}
runUpload = input("*> Would you like to upload these images to AWS? [y/n]: ")
if runUpload in yes:
bucket_name = input("*> Enter Bucket name: ")
# check if bucket name valid
while not validate_bucket_name(bucket_name):
bucket_name = input("*> Enter Bucket name: ")
dataset_name = input("*> Enter name for dataset/folder: ")
print(" Note: rendered images will be stored in a directory called 'render' in the same local directory this script is located under the directory name you specify.")
generate(dataset_name)
if runUpload in yes:
upload(dataset_name, bucket_name)
print("______________DONE EXECUTING______________")
if __name__ == "__main__":
main()