carla/PythonAPI/examples/tutorial_gbuffer.py

128 lines
6.2 KiB
Python
Raw Normal View History

Merge GBuffer features. (#5960) * Add Misc/GBuffer * Add EnqueueRenderSceneImmediateWithGBuffer. * Modified ASceneCaptureSensor to allow gbuffer recording. * Minor GBuffer progress. * More GBuffer changes. * Removed unnecessary files. * Add FRHITexture* functions to FPixelReader + temporarily disabled non-gbuffer rendering in ASceneCaptureSensor. * Add missing FPixelReader functions. * Minor changes (Switching to Windows). * Remove FRHITexture functions from FPixelReader, added the file ImageUtil. * Remove FRHITexture functions from FPixelReader, added the file ImageUtil. (2) * Added API to listen GBuffer data. * Added gbuffer serializer classes * Temporarily remove ViewRect hack. * Add USceneCaptureComponent* derived classes. * Disable USceneCaptureComponent*_CARLA and add initial FRHIGPUTextureReadBack-based code. * Fix and re-enable custom SceneCaptureComponents. * Fully switch to FRHIGPUTextureReadback. * Remove unnecessary call to FlushRenderingCommands. * Minor API changes. * Add support for PF_DepthStencil in ImageUtil. * More API progress... * More API progress... (2) * Removed testing code. * Minor changes for testing. * GBuffer API fixes. * Improve GBuffer capture code. * Fixed SceneDepth transfer issues and added SceneStencil, CustomDepth and CustomStencil to the GBuffer capture. * Fix compilation error due to the usage of C++17 features. * Removed major memory leak and added manual_control_gbuffer.py. * Fixed a silly mistake. * Minor changes to manual_control_gbuffer and SceneCaptureSensor. * Fix compilation error on some versions of Ubuntu. * Disable TAA when reading GBuffers to avoid jitter. * Improve memory usage. * Progress towards automatically detecting when a GBuffer stream is unused. * Fix includes in SceneCaptureSensor + minor change in manual_control_gbuffer.py * Progress on automatically detecting which GBuffers aren't needed. * Remove unneeded __declspec. * Revert ASensor changes + fix tutorial_gbuffer.py * Update CHANGELOG.md * Apply requested changes for the PR, add gitignore for the file OptionalModules.ini and add a GBufferTextureID enum to the Python API. * Remove OptionalModules.ini. * Fix indentation. * Fix indentation (2). * Fix indentation (3). * Add documentation and more indentation fixes. * Remove commented includes. * Add missing line break. * Fix memory leak + remove unneeded files. * Add .uproject again, fix EngineAssociation. * Remove unneeded ENQUEUE_RENDER_COMMAND. * Fix manual_control_gbuffer.py. * Add `stop_gbuffer` to the Python API. * Minor fixes. * Fix performance bug. Previously, when a client requested a gbuffer that is unused it would stay open, even after stopping it explicitly. This commit fixes this issue. * Fix indentation. * Add missing braces, more indentation fixes and simplify some of the code. * Update sensor.yml docs. * Update docs. * Remove unnecessary UE_Log + changed one verbosity level. Co-authored-by: Axel <axellopez92@outlook.com> Co-authored-by: Axel1092 <35765780+Axel1092@users.noreply.github.com>
2022-11-29 18:24:26 +08:00
#!/usr/bin/env python
# Copyright (c) 2019 Computer Vision Center (CVC) at the Universitat Autonoma de
# Barcelona (UAB).
#
# This work is licensed under the terms of the MIT license.
# For a copy, see <https://opensource.org/licenses/MIT>.
import glob
import os
import sys
try:
sys.path.append(glob.glob('../carla/dist/carla-*%d.%d-%s.egg' % (
sys.version_info.major,
sys.version_info.minor,
'win-amd64' if os.name == 'nt' else 'linux-x86_64'))[0])
except IndexError:
pass
import carla
import random
import time
def main():
actor_list = []
# In this tutorial script, we are going to add a vehicle to the simulation
# and let it drive in autopilot. We will also create a camera attached to
# that vehicle, and save all the images generated by the camera to disk.
# Additionally, we will save all of the gbuffer textures for each frame.
try:
# First of all, we need to create the client that will send the requests
# to the simulator. Here we'll assume the simulator is accepting
# requests in the localhost at port 2000.
client = carla.Client('127.0.0.1', 2000)
client.set_timeout(2.0)
# Once we have a client we can retrieve the world that is currently
# running.
world = client.get_world()
# The world contains the list blueprints that we can use for adding new
# actors into the simulation.
blueprint_library = world.get_blueprint_library()
# Now let's filter all the blueprints of type 'vehicle' and choose one
# at random.
bp = random.choice(blueprint_library.filter('vehicle'))
# A blueprint contains the list of attributes that define a vehicle's
# instance, we can read them and modify some of them. For instance,
# let's randomize its color.
if bp.has_attribute('color'):
color = random.choice(bp.get_attribute('color').recommended_values)
bp.set_attribute('color', color)
# Now we need to give an initial transform to the vehicle. We choose a
# random transform from the list of recommended spawn points of the map.
transform = world.get_map().get_spawn_points()[0]
# So let's tell the world to spawn the vehicle.
vehicle = world.spawn_actor(bp, transform)
# It is important to note that the actors we create won't be destroyed
# unless we call their "destroy" function. If we fail to call "destroy"
# they will stay in the simulation even after we quit the Python script.
# For that reason, we are storing all the actors we create so we can
# destroy them afterwards.
actor_list.append(vehicle)
print('created %s' % vehicle.type_id)
# Let's put the vehicle to drive around.
vehicle.set_autopilot(True)
# Let's add now a "rgb" camera attached to the vehicle. Note that the
# transform we give here is now relative to the vehicle.
camera_bp = blueprint_library.find('sensor.camera.rgb')
camera_bp.set_attribute('image_size_x', '1920')
camera_bp.set_attribute('image_size_y', '1080')
camera_transform = carla.Transform(carla.Location(x=1.5, z=2.4))
camera = world.spawn_actor(camera_bp, camera_transform, attach_to=vehicle)
actor_list.append(camera)
print('created %s' % camera.type_id)
# Register a callback for whenever a new frame is available. This step is
# currently required to correctly receive the gbuffer textures, as it is
# used to determine whether the sensor is active.
camera.listen(lambda image: image.save_to_disk('_out/FinalColor-%06d.png' % image.frame))
# Here we will register the callbacks for each gbuffer texture.
# The function "listen_to_gbuffer" behaves like the regular listen function,
# but you must first pass it the ID of the desired gbuffer texture.
camera.listen_to_gbuffer(carla.GBufferTextureID.SceneColor, lambda image: image.save_to_disk('_out/SceneColor-%06d.png' % image.frame))
camera.listen_to_gbuffer(carla.GBufferTextureID.SceneDepth, lambda image: image.save_to_disk('_out/SceneDepth-%06d.png' % image.frame))
camera.listen_to_gbuffer(carla.GBufferTextureID.SceneStencil, lambda image: image.save_to_disk('_out/SceneStencil-%06d.png' % image.frame))
camera.listen_to_gbuffer(carla.GBufferTextureID.GBufferA, lambda image: image.save_to_disk('_out/GBufferA-%06d.png' % image.frame))
camera.listen_to_gbuffer(carla.GBufferTextureID.GBufferB, lambda image: image.save_to_disk('_out/GBufferB-%06d.png' % image.frame))
camera.listen_to_gbuffer(carla.GBufferTextureID.GBufferC, lambda image: image.save_to_disk('_out/GBufferC-%06d.png' % image.frame))
camera.listen_to_gbuffer(carla.GBufferTextureID.GBufferD, lambda image: image.save_to_disk('_out/GBufferD-%06d.png' % image.frame))
# Note that some gbuffer textures may not be available for a particular scene.
# For example, the textures E and F are likely unavailable in this example,
# which will result in them being sent as black images.
camera.listen_to_gbuffer(carla.GBufferTextureID.GBufferE, lambda image: image.save_to_disk('_out/GBufferE-%06d.png' % image.frame))
camera.listen_to_gbuffer(carla.GBufferTextureID.GBufferF, lambda image: image.save_to_disk('_out/GBufferF-%06d.png' % image.frame))
camera.listen_to_gbuffer(carla.GBufferTextureID.Velocity, lambda image: image.save_to_disk('_out/Velocity-%06d.png' % image.frame))
camera.listen_to_gbuffer(carla.GBufferTextureID.SSAO, lambda image: image.save_to_disk('_out/SSAO-%06d.png' % image.frame))
camera.listen_to_gbuffer(carla.GBufferTextureID.CustomDepth, lambda image: image.save_to_disk('_out/CustomDepth-%06d.png' % image.frame))
camera.listen_to_gbuffer(carla.GBufferTextureID.CustomStencil, lambda image: image.save_to_disk('_out/CustomStencil-%06d.png' % image.frame))
time.sleep(10)
finally:
print('destroying actors')
camera.destroy()
client.apply_batch([carla.command.DestroyActor(x) for x in actor_list])
print('done.')
if __name__ == '__main__':
main()