diff --git a/Content/Blueprint/RobotPawnActors/BP_mujokoSO_100.uasset b/Content/Blueprint/RobotPawnActors/BP_mujokoSO_100.uasset index 87a704d6..4d8a6bcb 100644 Binary files a/Content/Blueprint/RobotPawnActors/BP_mujokoSO_100.uasset and b/Content/Blueprint/RobotPawnActors/BP_mujokoSO_100.uasset differ diff --git a/Plugins/LuckyDataTransfer/Source/LuckyDataTransfer/Private/LuckyDataTransferSubsystem.cpp b/Plugins/LuckyDataTransfer/Source/LuckyDataTransfer/Private/LuckyDataTransferSubsystem.cpp index 68c5746b..ebe16c93 100644 --- a/Plugins/LuckyDataTransfer/Source/LuckyDataTransfer/Private/LuckyDataTransferSubsystem.cpp +++ b/Plugins/LuckyDataTransfer/Source/LuckyDataTransfer/Private/LuckyDataTransferSubsystem.cpp @@ -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>(FIntPoint(Sensor->RenderTarget->GetSurfaceWidth(), Sensor->RenderTarget->GetSurfaceHeight()), TArray64(OutPixels)); ImageTask->bOverwriteFile = true; diff --git a/Source/LuckyWorldV2/Private/Episode/EpisodeSubSystem.cpp b/Source/LuckyWorldV2/Private/Episode/EpisodeSubSystem.cpp index c5848e5a..41971e10 100644 --- a/Source/LuckyWorldV2/Private/Episode/EpisodeSubSystem.cpp +++ b/Source/LuckyWorldV2/Private/Episode/EpisodeSubSystem.cpp @@ -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) { - EndEpisode(); 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> Writer = TJsonWriterFactory<>::Create(&Output); + const TSharedRef< TJsonWriter< TCHAR, TCondensedJsonPrintPolicy > > Writer = TJsonWriterFactory< TCHAR, TCondensedJsonPrintPolicy >::Create(&Output); FJsonSerializer::Serialize(Root.ToSharedRef(), Writer); EpisodeStatLines.Add(Output); } @@ -294,7 +295,8 @@ 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 EpisodeStatLines into a single file // https://huggingface.co/datasets/youliangtan/so100_strawberry_grape/blob/main/meta/episodes_stats.jsonl diff --git a/Source/LuckyWorldV2/Private/_Utils/FileUtils.cpp b/Source/LuckyWorldV2/Private/_Utils/FileUtils.cpp new file mode 100644 index 00000000..d4bb9b0b --- /dev/null +++ b/Source/LuckyWorldV2/Private/_Utils/FileUtils.cpp @@ -0,0 +1,24 @@ +#include "_Utils/FileUtils.h" +#include "Misc/FileHelper.h" +#include "Misc/Paths.h" + +UFileUtils::UFileUtils() +{ +} + + + +bool UFileUtils::WriteJsonlFile(const TArray& 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); +} \ No newline at end of file diff --git a/Source/LuckyWorldV2/Public/_Utils/FileUtils.h b/Source/LuckyWorldV2/Public/_Utils/FileUtils.h new file mode 100644 index 00000000..e0a58af7 --- /dev/null +++ b/Source/LuckyWorldV2/Public/_Utils/FileUtils.h @@ -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& JsonLines, const FString& BasePath, const FString& FileName); +}; \ No newline at end of file