Added the option to save SceneCapture images to disk on SensorSpawnerActor. (#7152)

This commit is contained in:
Jose 2024-02-16 11:08:56 +01:00 committed by GitHub
parent f6fc4f998d
commit 0551fd458e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 84 additions and 9 deletions

View File

@ -4,14 +4,17 @@
#include "Util/SensorSpawnerActor.h"
#include "Carla/Game/CarlaEpisode.h"
#include "Game/CarlaGameModeBase.h"
#include "Sensor/SceneCaptureCamera.h"
#include "Sensor/Sensor.h"
DEFINE_LOG_CATEGORY_STATIC(LogSensorSpawnerActor, Verbose, All);
ASensorSpawnerActor::ASensorSpawnerActor()
{
PrimaryActorTick.bCanEverTick = false;
PrimaryActorTick.bCanEverTick = true;
PrimaryActorTick.bStartWithTickEnabled = false;
PrimaryActorTick.TickInterval = TickInterval;
SceneComp = CreateDefaultSubobject<USceneComponent>(TEXT("SceneComp"));
RootComponent = SceneComp;
}
@ -19,14 +22,19 @@ ASensorSpawnerActor::ASensorSpawnerActor()
void ASensorSpawnerActor::BeginPlay()
{
Super::BeginPlay();
// Wait for the CarlaEpisode initialisation. It is done on CarlaGameMode BeginPlay().
if(ACarlaGameModeBase* CarlaGameMode = Cast<ACarlaGameModeBase>(UGameplayStatics::GetGameMode(GetWorld())))
{
CarlaGameMode->OnEpisodeInitialisedDelegate.AddDynamic(this, &ASensorSpawnerActor::OnEpisodeInitialised);
}
SetActorTickInterval(TickInterval);
SaveImagePath = FPaths::ProjectSavedDir() + "/SensorSpawnerCaptures/" + FString::Printf(TEXT("%lld"), FDateTime::Now().ToUnixTimestamp());
}
void ASensorSpawnerActor::OnEpisodeInitialised(UCarlaEpisode* InitialisedEpisode)
{
if(IsValid(InitialisedEpisode))
@ -83,14 +91,19 @@ const FActorDefinition* ASensorSpawnerActor::GetActorDefinitionByClass(const TSu
return ActorDefinition;
}
void ASensorSpawnerActor::SpawnSensorActor(const FActorDescription& SensorDescription) const
void ASensorSpawnerActor::SpawnSensorActor(const FActorDescription& SensorDescription)
{
if(IsValid(CarlaEpisode))
{
FTransform Transform;
GetRandomTransform(Transform);
CarlaEpisode->SpawnActorWithInfo(Transform, SensorDescription);
const TPair<EActorSpawnResultStatus, FCarlaActor*> SpawnResult = CarlaEpisode->SpawnActorWithInfo(Transform, SensorDescription);
if(bSaveCameraToDisk && SpawnResult.Value)
{
StartSavingCapturesToDisk(SpawnResult.Value->GetActor());
}
}
}
@ -144,4 +157,41 @@ void ASensorSpawnerActor::SpawnSensorsDelayed()
{
SensorsToSpawnCopy.RemoveAt(0);
}
}
}
void ASensorSpawnerActor::Tick(float DeltaSeconds)
{
Super::Tick(DeltaSeconds);
for(const ASceneCaptureCamera* CaptureCamera : SceneCaptureCameras)
{
if(CaptureCamera)
{
CaptureCamera->SaveCaptureToDisk(SaveImagePath + "/" + CaptureCamera->GetName() + "/" + FString::Printf(TEXT("%lld"), FDateTime::Now().ToUnixTimestamp()) + ".png" );
}
}
}
void ASensorSpawnerActor::RemoveSceneCaptureCameras()
{
if(SceneCaptureCameras.IsValidIndex(0))
{
SceneCaptureCameras.RemoveAt(0);
}
if(SceneCaptureCameras.IsEmpty())
{
SetActorTickEnabled(false);
}
}
void ASensorSpawnerActor::StartSavingCapturesToDisk(const AActor* Actor)
{
if(const ASceneCaptureCamera* CaptureCamera = Cast<ASceneCaptureCamera>(Actor))
{
SceneCaptureCameras.Add(CaptureCamera);
SetActorTickEnabled(true);
FTimerHandle CaptureTimerHandle;
GetWorldTimerManager().SetTimer(CaptureTimerHandle, this, &ASensorSpawnerActor::RemoveSceneCaptureCameras, CaptureTime);
}
}

View File

@ -43,6 +43,8 @@ protected:
// Called when the game starts or when spawned.
virtual void BeginPlay() override;
virtual void Tick(float DeltaSeconds) override;
// Root
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Components")
USceneComponent* SceneComp;
@ -67,6 +69,18 @@ protected:
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Config")
FVector MinSpawnLocation {-120000.f, -110000.f, 9300.f};
// Enables that all SceneCaptures cameras save captures to /Saved/SensorSpawnerCaptures folder.
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Config|SceneCapture")
bool bSaveCameraToDisk = false;
// How often ticking is done and a SceneCapture image is taken.
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Config|SceneCapture")
float TickInterval = 1.f;
// Amount of time each camera takes captures.
UPROPERTY(EditAnywhere, BlueprintReadOnly, meta=(ClampMin=1.f), Category="Config|SceneCapture")
float CaptureTime = 30.f;
private:
UFUNCTION()
@ -74,13 +88,18 @@ private:
UFUNCTION()
void SpawnSensorsDelayed();
UFUNCTION()
void RemoveSceneCaptureCameras();
void StartSavingCapturesToDisk(const AActor* Actor);
void GenerateSensorActorDescription(const FActorDefinition* Definition, FActorDescription& SensorDescription) const;
// Gets a transform with a random location between MaxSpawnLocation and MinSpawnLocation.
void GetRandomTransform(FTransform &Transform) const;
void SpawnSensorActor(const FActorDescription& SensorDescription) const;
void SpawnSensorActor(const FActorDescription& SensorDescription);
const FActorDefinition* GetActorDefinitionByClass(const TSubclassOf<AActor> ActorClass) const;
@ -89,9 +108,15 @@ private:
UPROPERTY()
UCarlaEpisode* CarlaEpisode;
// Used for delayed spawn
// Used for delayed spawn.
FTimerHandle SpawnSensorsDelayedTimerHandle;
// Used for delayed spawn
// Used for delayed spawn. Track cameras taking pictures on tick.
TArray<FSensorTuple> SensorsToSpawnCopy;
// Track cameras saving pictures on tick.
TArray<const class ASceneCaptureCamera*> SceneCaptureCameras;
// Path to the /Saved/SensorSpawnerCaptures project folder.
FString SaveImagePath;
};