def run(self): self.start() depth_pass = "******" # Create an empty room. # Set the screen size. commands = [TDWUtils.create_empty_room(12, 12), {"$type": "set_screen_size", "width": 512, "height": 512}] # Add the avatar. commands.extend(TDWUtils.create_avatar(position={"x": 1.57, "y": 3, "z": 3.56}, look_at=TDWUtils.VECTOR3_ZERO)) # Add an object. # Request images and camera matrices. commands.extend([self.get_add_object("trunck", object_id=0), {"$type": "set_pass_masks", "pass_masks": [depth_pass]}, {"$type": "send_images"}, {"$type": "send_camera_matrices"}]) resp = self.communicate(commands) depth_image = None camera_matrix = None images = None for i in range(len(resp) - 1): r_id = OutputData.get_data_type_id(resp[i]) # Get the image. if r_id == "imag": images = Images(resp[i]) for j in range(images.get_num_passes()): if images.get_pass_mask(j) == depth_pass: depth_image = images.get_image(j) # Get the camera matrix. elif r_id == "cama": camera_matrix = CameraMatrices(resp[i]).get_camera_matrix() # Save the image. TDWUtils.save_images(images=images, output_directory="D:/depth_shader", filename="0", append_pass=True) # Get the depth values of each pixel. depth = TDWUtils.get_depth_values(image=depth_image, width=images.get_width(), height=images.get_height()) print(np.min(depth), np.max(depth)) print(depth) np.save("depth", depth) np.save("camera_matrix", camera_matrix) # Get a point cloud and write it to disk. point_cloud_filename = "point_cloud.txt" print(f"Point cloud saved to: {Path(point_cloud_filename)}") TDWUtils.get_point_cloud(depth=depth, filename=point_cloud_filename, camera_matrix=camera_matrix) # Show the depth values. plt.imshow(depth) plt.show() self.communicate({"$type": "terminate"})
def get_shaped_depth_pass(images: Images, index: int) -> np.array: """ The `_depth` and `_depth_simple` passes are a 1D array of RGB values, as oppposed to a png or jpg like every other pass. This function reshapes the array into a 2D array of RGB values. :param images: The `Images` output data. :param index: The index in `Images` of the depth pass. See: `Images.get_pass_mask()`. :return: A reshaped depth pass. Shape is: `(height, width, 3)`. """ return np.flip( np.reshape(images.get_image(index), (images.get_height(), images.get_width(), 3)), 0)