diff --git a/example/deal.cpp b/example/deal.cpp index 63032d6..80a67ac 100644 --- a/example/deal.cpp +++ b/example/deal.cpp @@ -2287,25 +2287,10 @@ int deal::LoadImgPath_JCSN(std::string strImgPath) { std::string path = img_paths[i]; - // std::cout << img_paths[i] << std::endl; - - // 提取所有文件夹名称 - std::vector allDirectories = extractAllDirectories(path); + // 解析出 SN 及一套图目录(保留路径的相对/绝对属性) std::string strSN = ""; - // 输出所有文件夹名称 - std::string strAllPath = "/"; - - for (const auto &dir : allDirectories) - { - strAllPath += dir; - if (isValidString(dir)) - { - strSN = dir; - // std::cout << strSN << std::endl; - break; - } - strAllPath += "/"; - } + std::string strAllPath = ""; + GetProductDirFromImgPath(path, strSN, strAllPath); bool bh = false; for (int j = 0; j < m_OffImageSNPathList.size(); j++) @@ -2351,6 +2336,51 @@ bool deal::isValidString(const std::string &str) // 字符串满足条件 return true; } + +// 从单张图片路径解析出 SN 及 SN 所在目录(即一套图的目录) +// 说明: 该目录作为后续 glob 的根路径, 必须保留输入路径的相对/绝对属性。 +// 原先用 "/" 作为前缀拼接目录名, 输入相对路径时会得到 "/../data/..." 这类非法路径, 导致 glob 抛异常。 +// 返回: true 表示找到符合 SN 规则的目录 +bool deal::GetProductDirFromImgPath(const std::string &strImgPath, std::string &strSN, std::string &strProductDir) +{ + strSN = ""; + strProductDir = ""; + + fs::path pathImg(strImgPath); + if (pathImg.empty()) + { + strProductDir = "."; + return false; + } + + // 逐级向下拼接目录分量, 取第一个符合 SN 规则的目录作为一套图的目录 + // 首个分量可能是根 "/" 或相对路径的起点(如 ".."), 需要原样保留 + fs::path pathDir; + bool bHasDir = false; + for (const auto &part : pathImg.parent_path()) + { + if (!bHasDir) + { + pathDir = part; + bHasDir = true; + } + else + { + pathDir /= part; + } + + if (isValidString(part.string())) + { + strSN = part.string(); + strProductDir = pathDir.string(); + return true; + } + } + + // 没有找到 SN 规则的目录(如路径中不含 SN), 退化为图片所在目录 + strProductDir = bHasDir ? pathDir.string() : "."; + return false; +} int deal::LoadProductID(std::string strImgPath) { m_product_ID_List.erase(m_product_ID_List.begin(), m_product_ID_List.end()); @@ -2370,23 +2400,10 @@ int deal::LoadProductID(std::string strImgPath) { std::string path = img_paths[i]; - // 提取所有文件夹名称 - std::vector allDirectories = extractAllDirectories(path); + // 解析出 SN 及一套图目录(保留路径的相对/绝对属性, 避免拼成 /../data/... 这类非法路径) std::string strSN = ""; - // 输出所有文件夹名称 - std::string strAllPath = "/"; - - for (const auto &dir : allDirectories) - { - // std::cout << dir << std::endl; - strAllPath += dir; - if (isValidString(dir)) - { - strSN = dir; - break; - } - strAllPath += "/"; - } + std::string strAllPath = ""; + GetProductDirFromImgPath(path, strSN, strAllPath); bool bh = false; for (int j = 0; j < m_product_ID_List.size(); j++) diff --git a/example/deal.h b/example/deal.h index 66db572..3d8a11b 100644 --- a/example/deal.h +++ b/example/deal.h @@ -575,6 +575,10 @@ private: int LoadImgPath_JCSN(std::string strImgPath); bool isValidString(const std::string &str); + // 从图片路径解析出 SN 及其所在目录(一套图的目录) + // 注意: 返回的目录必须保留输入路径的相对/绝对属性, 不能直接拼根目录 "/" + bool GetProductDirFromImgPath(const std::string &strImgPath, std::string &strSN, std::string &strProductDir); + int LoadProductID(std::string strImgPath); int set_cpu_id(const std::vector &cpu_set_vec);