FT - Write Jsonl file

This commit is contained in:
JB Briant
2025-05-07 20:04:41 +07:00
parent e130c08975
commit 08d6570ea3
5 changed files with 49 additions and 9 deletions

View File

@ -243,7 +243,7 @@ bool ULuckyDataTransferSubsystem::WriteImageToDisk(const double Timestamp, const
);
// TODO This logs 1, it doesn't work?
UE_LOG(LogTemp, Warning, TEXT("Logged pixels: %d"), OutPixels.Num())
// UE_LOG(LogTemp, Warning, TEXT("Logged pixels: %d"), OutPixels.Num())
FImageWriteTask* ImageTask = new FImageWriteTask();
@ -270,7 +270,7 @@ bool ULuckyDataTransferSubsystem::WriteImageToDisk(const double Timestamp, const
UE_LOG(LogTemp, VeryVerbose, TEXT("FileName %s"), *Filename);
ImageTask->Format = EImageFormat::PNG;
ImageTask->Format = EImageFormat::JPEG;
ImageTask->Filename = Filename;
ImageTask->PixelData = MakeUnique<TImagePixelData<FColor>>(FIntPoint(Sensor->RenderTarget->GetSurfaceWidth(), Sensor->RenderTarget->GetSurfaceHeight()), TArray64<FColor>(OutPixels));
ImageTask->bOverwriteFile = true;

View File

@ -12,6 +12,7 @@
#include "Dom/JsonObject.h"
#include "Serialization/JsonWriter.h"
#include "Serialization/JsonSerializer.h"
#include "_Utils/FileUtils.h"
UEpisodeSubSystem::UEpisodeSubSystem()
{
@ -57,9 +58,11 @@ void UEpisodeSubSystem::Tick(float DeltaTime)
if (DataTransfer) DataTransfer->WriteImageToDisk(CurrentRobot->PhysicsSceneProxy->GetMujocoData().time);
EpisodeFrames++;
if (bIsEpisodeCompleted && CapturedEpisodes <= EpisodesToCapture)
{
if (!bIsEpisodeCompleted) return;
EndEpisode();
if (CapturedEpisodes < EpisodesToCapture)
{
StartEpisode();
}
else
@ -122,6 +125,7 @@ void UEpisodeSubSystem::StartTraining(const int32 EpisodesCountIn, FString BaseI
void UEpisodeSubSystem::EndTraining()
{
StopTicking();
CreateEpisodesStatsJsonFile();
// Create jsonl files
}
@ -138,9 +142,6 @@ void UEpisodeSubSystem::StartEpisode()
RobotTransform.GetLocation() + RobotTransform.GetRotation().GetForwardVector() * HardCodedRewardDistanceFromRobotPivot * (FMath::RandBool() ? 1 : -1)
};
// DrawDebugLine(this->GetWorld(), EpisodeRewardZone.GetLocation() + FVector::UpVector * 70, EpisodeRewardZone.GetLocation(), FColor::Red, true);
// DrawDebugLine(this->GetWorld(), RobotTransform.GetLocation() + FVector::UpVector * 70, RobotTransform.GetLocation(), FColor::Blue, true);
// Ask the bot to give a reachable location for the Training Object Transform
EpisodeObjectBaseTransform = CurrentRobot->RobotPilotComponent->GetReachableTransform();
@ -276,7 +277,7 @@ void UEpisodeSubSystem::CreateEpisodeStatJsonLine(const FTrainingEpisodeData& Tr
// Serialize into FString
FString Output;
const TSharedRef<TJsonWriter<>> Writer = TJsonWriterFactory<>::Create(&Output);
const TSharedRef< TJsonWriter< TCHAR, TCondensedJsonPrintPolicy<TCHAR> > > Writer = TJsonWriterFactory< TCHAR, TCondensedJsonPrintPolicy<TCHAR> >::Create(&Output);
FJsonSerializer::Serialize(Root.ToSharedRef(), Writer);
EpisodeStatLines.Add(Output);
}
@ -294,6 +295,7 @@ void UEpisodeSubSystem::ConvertImagesToVideo()
void UEpisodeSubSystem::CreateEpisodesStatsJsonFile()
{
// TODO Do not use FJsonObject - simply concat the FStrings into a file
UFileUtils::WriteJsonlFile(EpisodeStatLines, FPaths::ProjectSavedDir(), FString("episodes_stats"));
// Create a jsonl file and store in the correct directory
// concat TArray<FString> EpisodeStatLines into a single file

View File

@ -0,0 +1,24 @@
#include "_Utils/FileUtils.h"
#include "Misc/FileHelper.h"
#include "Misc/Paths.h"
UFileUtils::UFileUtils()
{
}
bool UFileUtils::WriteJsonlFile(const TArray<FString>& JsonLines, const FString& BasePath, const FString& FileName)
{
// Ensure the directory exists
IFileManager::Get().MakeDirectory(*BasePath, true);
// Construct the full file path
const FString FullFilePath = FPaths::Combine(BasePath, FileName + TEXT(".jsonl"));
// Join the array into one string with line breaks
const FString FileContent = FString::Join(JsonLines, TEXT(""));
// Write to file
return FFileHelper::SaveStringToFile(FileContent, *FullFilePath);
}

View File

@ -0,0 +1,14 @@
#pragma once
#include "CoreMinimal.h"
#include "FileUtils.generated.h"
UCLASS()
class LUCKYWORLDV2_API UFileUtils : public UObject
{
GENERATED_BODY()
public:
UFileUtils();
static bool WriteJsonlFile(const TArray<FString>& JsonLines, const FString& BasePath, const FString& FileName);
};