C++处理YTB faces 标注信息

C++编写程序:
1.采用windows api;
2.创建结构体
struct Object {
    double o_x, o_y;  // 中心坐标
    double o_width, o_height;  // 长宽
};
3.创建上述结构体类型的数组;
4.读取.\Aaron_Eckhart文件夹下的txt文件,将文件中的字符按行处理:

        如:1.37.jpg,165,142,61,61
        每行的第一的逗号后到第二个逗号间的字符转成double传进o_x变量,
        第二个逗号到第三个逗号间的字符转成double传进o_y变量,
        第三个逗号到第四个逗号间的字符转成double传进o_width变量,
        第四个逗号后的字符转成double传进o_height变量

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

struct Object {
    double o_x, o_y;       // 中心坐标
    double o_width, o_height;  // 长宽
    double name;
};

std::vector<Object> ReadObjectsFromTxt(const std::wstring& filePath)
{
    std::wifstream inputFile(filePath);

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

    std::vector<Object> objects;
    std::wstring line;
    while (std::getline(inputFile, line))
    {
        Object obj;
        std::wstringstream ss(line);
        std::wstring token;

        // Parse the line using comma as the delimiter
        std::getline(ss, token, L',');

        double name = std::stod(token.substr(token.find(L',') + 1));

        std::getline(ss, token, L',');

        obj.o_x = std::stod(token.substr(token.find(L',') + 1));

        std::getline(ss, token, L',');
        obj.o_y = std::stod(token.substr(token.find(L',') + 1));

        std::getline(ss, token, L',');
        obj.o_width = std::stod(token.substr(token.find(L',') + 1));

        std::getline(ss, token, L',');
        obj.o_height = std::stod(token.substr(token.find(L',') + 1));

        objects.push_back(obj);
    }

    inputFile.close();

    return objects;
}

int main()
{
    std::wstring directory = L".\\Aaron_Eckhart";
    std::wstring searchPath = directory + L"\\*.txt";

    WIN32_FIND_DATAW fileData;
    HANDLE hFind = FindFirstFileW(searchPath.c_str(), &fileData);

    if (hFind != INVALID_HANDLE_VALUE)
    {
        do
        {
            std::wstring filePath = directory + L"\\" + fileData.cFileName;
            std::vector<Object> objects = ReadObjectsFromTxt(filePath);

            // Process the objects
            for (const auto& obj : objects)
            {
                // Do something with the object data
                std::cout << "Object: x=" << obj.o_x << ", y=" << obj.o_y
                    << ", width=" << obj.o_width << ", height=" << obj.o_height << std::endl;
            }
        } while (FindNextFileW(hFind, &fileData) != 0);

        FindClose(hFind);
    }

    return 0;
}

其中, double name = std::stod(token.substr(token.find(L',') + 1));

是为了去掉第一个逗号前的字符,为了更方便读取坐标;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值