C++处理YTB数据集(合并所有图片,处理标注信息)

该程序涉及图片管理,包括从子目录递归合并图片到指定目录,并删除源子目录。同时,它处理标注信息txt文件,提取关键数据并删除不必要的部分。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.先将所有图片放到一个文件夹下;

2.读取所有的txt中的标注信息到一个txt文件中;

        原:Aaron_Eckhart\2\2.1732.jpg,0,165,80,32,32,0.0,1

        现:2.1732.jpg,0,165,80,32,32,0.0,1

3.处理每行的txt文本信息;

        原:2.1732.jpg,0,165,80,32,32,0.0,1

        现:2.1732.jpg,165,80,32,32

#include <iostream>
#include <string>
#include <windows.h>

// Function to recursively merge images from subdirectories to a specified directory
void MergeImagesRecursive(const std::wstring& srcDir)
{
    WIN32_FIND_DATAW fileData;
    HANDLE hFind = INVALID_HANDLE_VALUE;

    std::wstring searchPath = srcDir + L"\\*";
    hFind = FindFirstFileW(searchPath.c_str(), &fileData);

    if (hFind != INVALID_HANDLE_VALUE)
    {
        do
        {
            if (fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
            {
                std::wstring subDirName = fileData.cFileName;

                // Exclude "." and ".." directories
                if (subDirName != L"." && subDirName != L"..")
                {
                    std::wstring subDirPath = srcDir + L"\\" + subDirName;

                    // Merge images from subdirectories to the subdirectory
                    MergeImagesRecursive(subDirPath);

                    // Merge images from subdirectory to the current directory
                    std::wstring subSearchPath = subDirPath + L"\\*.jpg"; // Assuming the images are JPG files
                    WIN32_FIND_DATAW subFileData;
                    HANDLE subHFind = FindFirstFileW(subSearchPath.c_str(), &subFileData);

                    if (subHFind != INVALID_HANDLE_VALUE)
                    {
                        do
                        {
                            std::wstring subFilePath = subDirPath + L"\\" + subFileData.cFileName;
                            std::wstring destFilePath = srcDir + L"\\" + subFileData.cFileName;

                            // Move the image file to the current directory
                            if (MoveFileW(subFilePath.c_str(), destFilePath.c_str()))
                            {
                                std::wcout << L"Moved file: " << subFilePath << L" to " << destFilePath << std::endl;
                            }
                            else
                            {
                                std::wcout << L"Failed to move file: " << subFilePath << std::endl;
                            }
                        } while (FindNextFileW(subHFind, &subFileData) != 0);

                        FindClose(subHFind);
                    }

                    // Delete the subdirectory
                    if (RemoveDirectoryW(subDirPath.c_str()))
                    {
                        std::wcout << L"Deleted directory: " << subDirPath << std::endl;
                    }
                    else
                    {
                        std::wcout << L"Failed to delete directory: " << subDirPath << std::endl;
                    }
                }
            }
        } while (FindNextFileW(hFind, &fileData) != 0);

        FindClose(hFind);
    }
}

int main()
{
    std::wstring videoPath = L".\\video";

    // Step 2: Merge images from subdirectories' subdirectories to the subdirectories and delete the subdirectories' subdirectories
    MergeImagesRecursive(videoPath);

    return 0;
}

  



//将.\2文件夹下的的标注文件合并读取到.\3\label.txt文件中
//删除每行标注文件中第二个\前的内容
//原内容Aaron_Eckhart\0\0.555.jpg,0,237,137,84,84,0.0,1
//读取后0.555.jpg,0,237,137,84,84,0.0,1

#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>

void RemoveCharacter(std::wstring& str, wchar_t ch)
{
    std::size_t pos = str.find(ch);
    if (pos != std::wstring::npos)
    {
        std::size_t nextPos = str.find(ch, pos + 1);
        if (nextPos != std::wstring::npos)
        {
            str.erase(0, nextPos + 1);
        }
    }
}

void ProcessTextFile(const std::wstring& filePath, const std::wstring& outputFile)
{
    std::wifstream inputFile(filePath);
    std::wofstream outputFileStream(outputFile, std::ios::app); // Open the output file in append mode

    if (!inputFile)
    {
        std::wcerr << L"Failed to open input file: " << filePath << std::endl;
        return;
    }

    if (!outputFileStream)
    {
        std::wcerr << L"Failed to open output file: " << outputFile << std::endl;
        inputFile.close();
        return;
    }

    std::wstring line;
    while (std::getline(inputFile, line))
    {
        RemoveCharacter(line, L'\\');
        outputFileStream << line << std::endl;
    }

    inputFile.close();
    outputFileStream.close();

    std::wcout << L"Processed file: " << filePath << std::endl;
}

int main()
{
    std::wstring directory = L".\\2";
    std::wstring outputFile = L".\\3\\label.txt";

    WIN32_FIND_DATAW fileData;
    HANDLE hFind = INVALID_HANDLE_VALUE;

    std::wstring searchPath = directory + L"\\*.txt";
    hFind = FindFirstFileW(searchPath.c_str(), &fileData);

    if (hFind != INVALID_HANDLE_VALUE)
    {
        do
        {
            std::wstring filePath = directory + L"\\" + fileData.cFileName;
            ProcessTextFile(filePath, outputFile);
        } while (FindNextFileW(hFind, &fileData) != 0);

        FindClose(hFind);
    }

    return 0;
}
//0.555.jpg,0,237,137,84,84,0.0,1
//0.555.jpg,237,137,84,84
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>

void ProcessTextFile(const std::wstring& filePath)
{
    std::wifstream inputFile(filePath);
    std::wstring tempFilePath = filePath + L".temp";
    std::wofstream tempFileStream(tempFilePath);

    if (!inputFile)
    {
        std::wcerr << L"Failed to open input file: " << filePath << std::endl;
        return;
    }

    if (!tempFileStream)
    {
        std::wcerr << L"Failed to open temporary file: " << tempFilePath << std::endl;
        inputFile.close();
        return;
    }

    std::wstring line;
    while (std::getline(inputFile, line))
    {
        if (!line.empty())
        {
            // Remove the first comma and the next character
            std::size_t commaPos = line.find(L',');
            if (commaPos != std::wstring::npos)
            {
                line.erase(commaPos, 2);
            }

            // Remove the last two characters and two commas
            if (line.size() >= 2)
            {
                line = line.substr(0, line.size() - 6);
            }
        }

        tempFileStream << line << std::endl;
    }

    inputFile.close();
    tempFileStream.close();

    // Replace the original file with the temporary file
    if (MoveFileExW(tempFilePath.c_str(), filePath.c_str(), MOVEFILE_REPLACE_EXISTING) == 0)
    {
        std::wcerr << L"Failed to replace file: " << filePath << std::endl;
    }
    else
    {
        std::wcout << L"Processed file: " << filePath << std::endl;
    }
}

int main()
{
    std::wstring filePath = L".\\3\\label.txt";

    // Step 2: Process the label.txt file
    ProcessTextFile(filePath);

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值