fix -f传不了相对路径

dev_lsy
liusiyang 6 days ago
parent de04f149f8
commit d13a6d5f39

@ -2287,25 +2287,10 @@ int deal::LoadImgPath_JCSN(std::string strImgPath)
{ {
std::string path = img_paths[i]; std::string path = img_paths[i];
// std::cout << img_paths[i] << std::endl; // 解析出 SN 及一套图目录(保留路径的相对/绝对属性)
// 提取所有文件夹名称
std::vector<std::string> allDirectories = extractAllDirectories(path);
std::string strSN = ""; std::string strSN = "";
// 输出所有文件夹名称 std::string strAllPath = "";
std::string strAllPath = "/"; GetProductDirFromImgPath(path, strSN, strAllPath);
for (const auto &dir : allDirectories)
{
strAllPath += dir;
if (isValidString(dir))
{
strSN = dir;
// std::cout << strSN << std::endl;
break;
}
strAllPath += "/";
}
bool bh = false; bool bh = false;
for (int j = 0; j < m_OffImageSNPathList.size(); j++) for (int j = 0; j < m_OffImageSNPathList.size(); j++)
@ -2351,6 +2336,51 @@ bool deal::isValidString(const std::string &str)
// 字符串满足条件 // 字符串满足条件
return true; 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) int deal::LoadProductID(std::string strImgPath)
{ {
m_product_ID_List.erase(m_product_ID_List.begin(), m_product_ID_List.end()); 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::string path = img_paths[i];
// 提取所有文件夹名称 // 解析出 SN 及一套图目录(保留路径的相对/绝对属性, 避免拼成 /../data/... 这类非法路径)
std::vector<std::string> allDirectories = extractAllDirectories(path);
std::string strSN = ""; std::string strSN = "";
// 输出所有文件夹名称 std::string strAllPath = "";
std::string strAllPath = "/"; GetProductDirFromImgPath(path, strSN, strAllPath);
for (const auto &dir : allDirectories)
{
// std::cout << dir << std::endl;
strAllPath += dir;
if (isValidString(dir))
{
strSN = dir;
break;
}
strAllPath += "/";
}
bool bh = false; bool bh = false;
for (int j = 0; j < m_product_ID_List.size(); j++) for (int j = 0; j < m_product_ID_List.size(); j++)

@ -575,6 +575,10 @@ private:
int LoadImgPath_JCSN(std::string strImgPath); int LoadImgPath_JCSN(std::string strImgPath);
bool isValidString(const std::string &str); 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 LoadProductID(std::string strImgPath);
int set_cpu_id(const std::vector<int> &cpu_set_vec); int set_cpu_id(const std::vector<int> &cpu_set_vec);

Loading…
Cancel
Save