C++处理YouTuBe Faces数据集(删除无用标注文件)

youtube faces数据集中,下载下来,发现frame_images_DB文件中的图片文件和TxT文本标注文件数量不一致,经查看,出现了许多空白表注文件;

1.将所有的图像文件复制到工程目录下的video文件夹下;

2.将所有的txt文件复制到工程目录下的1文件夹下;

3.读取.\\video文件夹下所有一级目录的名称存到hashset中;

4.读取.\\1文件夹中的所有txt文件名称,且只取第一个.号前的字符;

               如:Aaron_Eckhart.labeled_faces;取:Aaron_Eckhart;

5.查询取得的字符是否存在于hashset中,若不存在,则删除该txt文件;

6.统计所有的video文件夹下的一级目录的数量;统计所有的txt文件数量对比是否一致;

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

// Function to read directory names from a given path
void ReadDirectory(const std::string& path, std::unordered_set<std::string>& dirNames)
{
    WIN32_FIND_DATAA fileData;
    HANDLE hFind = INVALID_HANDLE_VALUE;

    std::string searchPath = path + "\\*";
    hFind = FindFirstFileA(searchPath.c_str(), &fileData);

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

                // Exclude "." and ".." directories
                if (dirName != "." && dirName != "..")
                {
                    dirNames.insert(dirName);
                }
            }
        } while (FindNextFileA(hFind, &fileData) != 0);

        FindClose(hFind);
    }
}

// Function to delete a file from a given path
bool DeleteFile(const std::string& filePath)
{
    return DeleteFileA(filePath.c_str()) != 0;
}

int main()
{
    std::string videoPath = ".\\video";
    std::string txtPath = ".\\1";
    std::unordered_set<std::string> dirNames;

    // Step 1: Read directory names from the video path
    ReadDirectory(videoPath, dirNames);

    // Step 2: Read txt file names from the txt path and delete if not found in the directory names set
    WIN32_FIND_DATAA fileData;
    HANDLE hFind = INVALID_HANDLE_VALUE;

    std::string searchPath = txtPath + "\\*.txt";
    hFind = FindFirstFileA(searchPath.c_str(), &fileData);

    if (hFind != INVALID_HANDLE_VALUE)
    {
        do
        {
            std::string fileName = fileData.cFileName;
            std::size_t dotPos = fileName.find_first_of('.');

            if (dotPos != std::string::npos)
            {
                std::string nameBeforeDot = fileName.substr(0, dotPos);

                // Compare with directory names set
                if (dirNames.find(nameBeforeDot) == dirNames.end())
                {
                    std::string filePath = txtPath + "\\" + fileName;

                    // Step 3: Delete the file
                    if (DeleteFile(filePath))
                    {
                        std::cout << "Deleted file: " << filePath << std::endl;
                    }
                    else
                    {
                        std::cout << "Failed to delete file: " << filePath << std::endl;
                    }
                }
            }
        } while (FindNextFileA(hFind, &fileData) != 0);

        FindClose(hFind);
    }

    return 0;
}

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

// Function to count the number of subdirectories in a given path
int CountSubdirectories(const std::wstring& path)
{
    int count = 0;

    WIN32_FIND_DATAW fileData;
    HANDLE hFind = INVALID_HANDLE_VALUE;

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

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

                // Exclude "." and ".." directories
                if (dirName != L"." && dirName != L"..")
                {
                    count++;
                }
            }
        } while (FindNextFileW(hFind, &fileData) != 0);

        FindClose(hFind);
    }

    return count;
}

// Function to count the number of files in a given path with the specified extension
int CountFiles(const std::wstring& path, const std::wstring& extension)
{
    int count = 0;

    WIN32_FIND_DATAW fileData;
    HANDLE hFind = INVALID_HANDLE_VALUE;

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

    if (hFind != INVALID_HANDLE_VALUE)
    {
        do
        {
            if (!(fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
            {
                count++;
            }
        } while (FindNextFileW(hFind, &fileData) != 0);

        FindClose(hFind);
    }

    return count;
}

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

    // Step 2: Count the number of subdirectories in the video path
    int subdirectoryCount = CountSubdirectories(videoPath);
    std::cout << "Number of subdirectories in .\\video: " << subdirectoryCount << std::endl;

    // Step 3: Count the number of txt files in the txt path
    int txtFileCount = CountFiles(txtPath, L".txt");
    std::cout << "Number of txt files in .\\1: " << txtFileCount << std::endl;

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值