Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
axis_orthogonal = fig.add_subplot(1, 2, 2)
ims = []
scene = build_scene(
floor_textures,
wall_textures,
fix_light_position=args.fix_light_position)
place_dice(
scene,
mnist_images,
discrete_position=args.discrete_position,
rotate_dice=args.rotate_dice)
camera_distance = 5
perspective_camera = PerspectiveCamera(yfov=math.pi / 4)
perspective_camera_node = Node(
camera=perspective_camera, translation=np.array([0, 1, 1]))
orthographic_camera = OrthographicCamera(xmag=3, ymag=3)
orthographic_camera_node = Node(camera=orthographic_camera)
rad_step = math.pi / 36
total_frames = int(math.pi * 2 / rad_step)
current_rad = 0
for _ in range(total_frames):
scene.add_node(perspective_camera_node)
# Perspective camera
camera_xz = camera_distance * np.array(
(math.sin(current_rad), math.cos(current_rad)))
# Compute yaw and pitch
camera_direction = np.array([camera_xz[0], 0, camera_xz[1]])
yaw, pitch = compute_yaw_and_pitch(camera_direction)
def build_scene(floor_textures, wall_textures, fix_light_position=False):
scene = Scene(
bg_color=np.array([153 / 255, 226 / 255, 249 / 255]),
ambient_light=np.array([0.5, 0.5, 0.5, 1.0]))
floor_trimesh = trimesh.load("{}/floor.obj".format(object_directory))
mesh = Mesh.from_trimesh(floor_trimesh, smooth=False)
node = Node(
mesh=mesh,
rotation=pyrender.quaternion.from_pitch(-math.pi / 2),
translation=np.array([0, 0, 0]))
texture_path = random.choice(floor_textures)
set_random_texture(node, texture_path)
scene.add_node(node)
texture_path = random.choice(wall_textures)
wall_trimesh = trimesh.load("{}/wall.obj".format(object_directory))
mesh = Mesh.from_trimesh(wall_trimesh, smooth=False)
node = Node(mesh=mesh, translation=np.array([0, 1.15, -3.5]))
set_random_texture(node, texture_path)
scene.add_node(node)
mesh = Mesh.from_trimesh(wall_trimesh, smooth=False)
# Place objects
directions = [-1.0, 0.0, 1.0]
available_positions = []
for z in directions:
for x in directions:
available_positions.append((x, z))
available_positions = np.array(available_positions)
num_objects = random.choice(range(args.max_num_objects)) + 1
indices = np.random.choice(
np.arange(len(available_positions)), replace=False, size=num_objects)
for xz in available_positions[indices]:
node = random.choice(objects)()
node.mesh.primitives[0].color_0 = random.choice(colors)
if args.discrete_position == False:
xz += np.random.uniform(-0.25, 0.25, size=xz.shape)
parent = Node(children=[node], translation=np.array([xz[0], 0, xz[1]]))
scene.add_node(parent)
return scene
fix_light_position=args.fix_light_position)
place_objects(
scene,
colors,
objects,
min_num_objects=args.num_objects,
max_num_objects=args.num_objects,
discrete_position=args.discrete_position,
rotate_object=args.rotate_object)
camera_distance = 5
perspective_camera = PerspectiveCamera(yfov=math.pi / 4)
perspective_camera_node = Node(
camera=perspective_camera, translation=np.array([0, 1, 1]))
orthographic_camera = OrthographicCamera(xmag=3, ymag=3)
orthographic_camera_node = Node(camera=orthographic_camera)
rad_step = math.pi / 36
total_frames = int(math.pi * 2 / rad_step)
current_rad = 0
for _ in range(total_frames):
scene.add_node(perspective_camera_node)
# Perspective camera
camera_xz = camera_distance * np.array(
(math.sin(current_rad), math.cos(current_rad)))
# Compute yaw and pitch
camera_direction = np.array([camera_xz[0], 0, camera_xz[1]])
yaw, pitch = compute_yaw_and_pitch(camera_direction)
perspective_camera_node.rotation = genearte_camera_quaternion(
yaw, pitch)
mesh=mesh,
rotation=pyrender.quaternion.from_yaw(math.pi),
translation=np.array([0, 1.15, 3.5]))
set_random_texture(node, texture_path)
scene.add_node(node)
mesh = Mesh.from_trimesh(wall_trimesh, smooth=False)
node = Node(
mesh=mesh,
rotation=pyrender.quaternion.from_yaw(-math.pi / 2),
translation=np.array([3.5, 1.15, 0]))
set_random_texture(node, texture_path)
scene.add_node(node)
mesh = Mesh.from_trimesh(wall_trimesh, smooth=False)
node = Node(
mesh=mesh,
rotation=pyrender.quaternion.from_yaw(math.pi / 2),
translation=np.array([-3.5, 1.15, 0]))
set_random_texture(node, texture_path)
scene.add_node(node)
light = DirectionalLight(color=np.ones(3), intensity=10)
if fix_light_position == True:
translation = np.array([1, 1, 1])
else:
xz = np.random.uniform(-1, 1, size=2)
translation = np.array([xz[0], 1, xz[1]])
yaw, pitch = compute_yaw_and_pitch(translation)
node = Node(
light=light,
rotation=genearte_camera_quaternion(yaw, pitch),
floor_trimesh = trimesh.load("objects/floor.obj")
mesh = Mesh.from_trimesh(floor_trimesh)
node = Node(
mesh=mesh,
rotation=pyrender.quaternion.from_pitch(-math.pi / 2),
translation=np.array([0, 0, 0]))
texture_path = random.choice(floor_textures)
set_random_texture(node, texture_path, intensity=0.8)
scene.add_node(node)
texture_path = random.choice(wall_textures)
wall_trimesh = trimesh.load("objects/wall.obj")
mesh = Mesh.from_trimesh(wall_trimesh)
node = Node(mesh=mesh, translation=np.array([0, 1.15, -3.5]))
set_random_texture(node, texture_path)
scene.add_node(node)
mesh = Mesh.from_trimesh(wall_trimesh)
node = Node(
mesh=mesh,
rotation=pyrender.quaternion.from_yaw(math.pi),
translation=np.array([0, 1.15, 3.5]))
set_random_texture(node, texture_path)
scene.add_node(node)
mesh = Mesh.from_trimesh(wall_trimesh)
node = Node(
mesh=mesh,
rotation=pyrender.quaternion.from_yaw(-math.pi / 2),
translation=np.array([3.5, 1.15, 0]))
for scene_index in tqdm(range(total_scenes_to_render)):
scene = build_scene(
floor_textures,
wall_textures,
fix_light_position=args.fix_light_position)
place_objects(
scene,
colors,
objects,
max_num_objects=args.max_num_objects,
discrete_position=args.discrete_position,
rotate_object=args.rotate_object)
camera_distance = 4.5
camera = PerspectiveCamera(yfov=math.pi / 4)
camera_node = Node(camera=camera, translation=np.array([0, 1, 1]))
scene.add_node(camera_node)
scene_data = SceneData((args.image_size, args.image_size),
args.num_observations_per_scene)
for observation_index in range(args.num_observations_per_scene):
rand_position_xz = np.random.normal(size=2)
rand_position_xz = camera_distance * rand_position_xz / np.linalg.norm(
rand_position_xz)
# Compute yaw and pitch
camera_direction = np.array(
[rand_position_xz[0], 0, rand_position_xz[1]])
yaw, pitch = compute_yaw_and_pitch(camera_direction)
camera_node.rotation = genearte_camera_quaternion(yaw, pitch)
camera_position = np.array(
[rand_position_xz[0], 1, rand_position_xz[1]])
camera_node.translation = camera_position
def Box():
sphere = trimesh.creation.box(extents=np.array([0.5, 0.5, 0.5]))
mesh = Mesh.from_trimesh(sphere, smooth=False)
node = Node(mesh=mesh, translation=np.array([0, 0.25, 0]))
return node
rotation=pyrender.quaternion.from_yaw(math.pi / 2),
translation=np.array([-3.5, 1.15, 0]))
set_random_texture(node, texture_path)
scene.add_node(node)
# light = PointLight(color=np.ones(3), intensity=200.0)
# node = Node(
# light=light,
# translation=np.array([0, 5, 5]))
# scene.add_node(node)
light = DirectionalLight(color=np.ones(3), intensity=10)
position = np.array([0, 1, 1])
position = position / np.linalg.norm(position)
yaw, pitch = compute_yaw_and_pitch(position)
node = Node(
light=light,
rotation=genearte_camera_quaternion(yaw, pitch),
translation=np.array([0, 1, 1]))
scene.add_node(node)
# Place objects
directions = [-1.0, 0.0, 1.0]
available_positions = []
for z in directions:
for x in directions:
available_positions.append((x, z))
available_positions = np.array(available_positions)
num_objects = random.choice(range(args.max_num_objects)) + 1
indices = np.random.choice(
np.arange(len(available_positions)), replace=False, size=num_objects)
for xz in available_positions[indices]:
#==============================================================================
scene = Scene(ambient_light=np.array([0.02, 0.02, 0.02, 1.0]))
#==============================================================================
# Adding objects to the scene
#==============================================================================
#------------------------------------------------------------------------------
# By manually creating nodes
#------------------------------------------------------------------------------
fuze_node = Node(mesh=fuze_mesh, translation=np.array([0.1, 0.15, -np.min(fuze_trimesh.vertices[:,2])]))
scene.add_node(fuze_node)
boxv_node = Node(mesh=boxv_mesh, translation=np.array([-0.1, 0.10, 0.05]))
scene.add_node(boxv_node)
boxf_node = Node(mesh=boxf_mesh, translation=np.array([-0.1, -0.10, 0.05]))
scene.add_node(boxf_node)
#------------------------------------------------------------------------------
# By using the add() utility function
#------------------------------------------------------------------------------
drill_node = scene.add(drill_mesh, pose=drill_pose)
bottle_node = scene.add(bottle_mesh, pose=bottle_pose)
wood_node = scene.add(wood_mesh)
direc_l_node = scene.add(direc_l, pose=cam_pose)
spot_l_node = scene.add(spot_l, pose=cam_pose)
#==============================================================================
# Using the viewer with a default camera
#==============================================================================
v = Viewer(scene, shadows=True)