You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

301 lines
8.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include "Read_Image.h"
#include <filesystem>
// 转小写
std::string toLower(const std::string &str)
{
std::string lower = str;
std::transform(lower.begin(), lower.end(), lower.begin(), ::tolower);
return lower;
}
int Extract_Name_Base::Read_Product_Image(std::string strPath)
{
return 0;
}
int Extract_Name_Base::Read_Camera_Product_Image(std::string strPath, int userflag, std::string strproduct)
{
std::vector<cv::String> img_paths;
std::cout << strPath << std::endl;
cv::glob(strPath, img_paths, true);
for (int i = 0; i < img_paths.size(); i++)
{
string strImagePath = img_paths[i];
Extract_Info(strImagePath, strproduct, userflag);
}
return 0;
}
int Extract_Name_Base::Read_Product_Camera_Image(std::string strPath)
{
return 0;
}
std::shared_ptr<Product_File_Info> Extract_Name_Base::GetProduct(std::string strProductID)
{
for (int i = 0; i < m_product_Camera_List.size(); i++)
{
if (m_product_Camera_List[i]->strProductID == strProductID)
{
return m_product_Camera_List[i];
}
}
return std::shared_ptr<Product_File_Info>();
}
std::shared_ptr<Camera_File_Info> Extract_Name_Base::GetCamera(std::string strProductID, std::string strCamName)
{
for (int i = 0; i < m_product_Camera_List.size(); i++)
{
if (m_product_Camera_List[i]->strProductID == strProductID)
{
for (int j = 0; j < m_product_Camera_List[i]->camera_list.size(); j++)
{
if (m_product_Camera_List[i]->camera_list[j]->strCamName == strCamName)
{
return m_product_Camera_List[i]->camera_list[j];
}
}
}
}
return std::shared_ptr<Camera_File_Info>();
}
std::shared_ptr<Camera_File_Info> Extract_Name_Base::GetCamera(std::shared_ptr<Product_File_Info> product, std::string strCamName)
{
for (int i = 0; i < product->camera_list.size(); i++)
{
if (product->camera_list[i]->strCamName == strCamName)
{
return product->camera_list[i];
}
}
return std::shared_ptr<Camera_File_Info>();
}
// 判断目录部分是否包含某些关键词(不区分大小写)
bool Extract_Name_Base::dirPathContains(const fs::path &fullPath, const std::string KeyName)
{
fs::path parentDir = fullPath.parent_path();
std::string lowKeyName = toLower(KeyName);
for (const auto &dir : parentDir)
{
std::string dirName = toLower(dir.string());
if (dirName == lowKeyName)
{
return true;
}
}
return false;
}
Read_Image::Read_Image(/* args */)
{
}
Read_Image::~Read_Image()
{
}
int Read_Image::Read_Image_List(std::string strPath, std::string strproduct)
{
m_extract.pimage_ReadChannel = pimage_ReadChannel;
m_extract.m_product_Camera_List.clear();
int re = m_extract.Read_Image_List(strPath, strproduct);
if (re != 0)
{
return re;
}
m_product_Camera_List.clear();
m_product_Camera_List.assign(m_extract.m_product_Camera_List.begin(), m_extract.m_product_Camera_List.end());
for (int i = 0; i < m_extract.m_product_Camera_List.size(); i++)
{
m_extract.m_product_Camera_List[i]->print();
}
return 0;
}
int Read_Image::Read_Image_List(std::string strPath_left, std::string strPath_right, std::string strproduct)
{
m_extract.pimage_ReadChannel = pimage_ReadChannel;
m_extract.m_product_Camera_List.clear();
int re = m_extract.Read_Image_List(strPath_left, strPath_right, strproduct);
if (re != 0)
{
return re;
}
m_product_Camera_List.clear();
m_product_Camera_List.assign(m_extract.m_product_Camera_List.begin(), m_extract.m_product_Camera_List.end());
for (int i = 0; i < m_extract.m_product_Camera_List.size(); i++)
{
m_extract.m_product_Camera_List[i]->print();
}
return 0;
}
int Extract_ALL::Extract_Info(std::string strPath, std::string strproduct, int userflag)
{
std::cout << strPath << std::endl;
// 1、获取 产品名称
std::string strProductName = Extract_Product_Name(strPath, userflag);
if (strProductName.empty())
{
return -1;
}
std::shared_ptr<Product_File_Info> pProduct = GetProduct(strProductName);
if (strproduct != "" && strproduct != strProductName)
{
return -2;
}
if (pProduct.get() == nullptr)
{
printf("pProduct %s is NULL \n", strProductName.c_str());
pProduct = std::make_shared<Product_File_Info>();
pProduct->strProductID = strProductName;
m_product_Camera_List.push_back(pProduct);
}
// 2、获取 相机名称
std::string strCameraName = Extract_Camera_Name(strPath, userflag);
if (strCameraName.empty())
{
printf("strCameraName is error \n");
return -1;
}
std::shared_ptr<Camera_File_Info> pCamera = GetCamera(pProduct, strCameraName);
if (pCamera.get() == nullptr)
{
printf("pCamera %s is NULL \n", strCameraName.c_str());
pCamera = std::make_shared<Camera_File_Info>();
pCamera->strCamName = strCameraName;
pProduct->camera_list.push_back(pCamera);
}
// 3、获取 通道
std::string strChannelName = Extract_Channel_Name(strPath, userflag);
if (strChannelName.empty())
{
printf("strChannelName is NULL \n");
return -1;
}
fs::path p = fs::path(strPath);
std::string filenameWithoutExt = p.stem().string();
std::shared_ptr<Image_Info> pImage = std::make_shared<Image_Info>();
pImage->strProductID = strProductName;
pImage->strCamID = strCameraName;
pImage->strchannelName = strChannelName;
pImage->strName = filenameWithoutExt;
pImage->strPath = strPath;
pCamera->image_list.push_back(pImage);
return 0;
}
std::string Extract_ALL::Extract_Camera_Name(std::string strPath, int userflag)
{
fs::path p = fs::path(strPath);
std::string filename = p.stem();
// 文件名包含 CA 或 TA 即判定对应相机
// 支持旧格式: CA_20250508074237.ytimage
// 支持新格式: A_CAA.png, M_CAB.png (A_/M_ 仅是打光方式同属一个camera)
if (filename.find("TA") != std::string::npos)
return "TA";
else if (filename.find("CA") != std::string::npos)
return "CA";
return "";
}
std::string Extract_ALL::Extract_Product_Name(std::string strPath, int userflag)
{
// std::cout << strPath << std::endl;
fs::path p = fs::path(strPath);
fs::path lastDir = p.parent_path().filename();
std::string name = lastDir.string();
if (name.length()<7)
{
lastDir = p.parent_path().parent_path().filename();
name = lastDir.string();
}
// std::cout << name << std::endl;
if (name.empty())
{
return "";
}
return name;
}
std::string Extract_ALL::Extract_Channel_Name(std::string strPath, int userflag)
{
std::string strchannelName = "";
fs::path p = fs::path(strPath);
std::string filename = p.stem();
// 新格式: {Prefix}_{Type}{AB}, 如 A_CAA, M_TAB
// 通道名 = 前缀 (A/M), 表示不同打光方式
size_t underscorePos = filename.find('_');
if (underscorePos != std::string::npos)
{
std::string prefix = filename.substr(0, underscorePos);
std::string rest = filename.substr(underscorePos + 1);
if (rest == "CAA" || rest == "CAB" || rest == "TAA" || rest == "TAB")
{
return prefix; // "A" or "M"
}
}
// 旧格式兼容: CA_xxx.ytimage, TA_xxx.ytimage
if (filename.find("TAA") != std::string::npos)
strchannelName = "TA";
else if (filename.find("TAB") != std::string::npos)
strchannelName = "TA";
else if (filename.find("CAA") != std::string::npos)
strchannelName = "CA";
else if (filename.find("CAB") != std::string::npos)
strchannelName = "CA";
else if (filename.find("CA") != std::string::npos)
strchannelName = "CA";
else if (filename.find("TA") != std::string::npos)
strchannelName = "TA";
return strchannelName;
}
int Extract_ALL::Read_Image_List(std::string strPath, std::string strproduct)
{
std::string strSearchImgPath = strPath + "/*.png";
Read_Camera_Product_Image(strSearchImgPath, 0, strproduct);
strSearchImgPath = strPath + "/*.ytimage";
Read_Camera_Product_Image(strSearchImgPath, 0, strproduct);
return 0;
}
int Extract_ALL::Read_Image_List(std::string strPath_left, std::string strPath_right, std::string strproduct)
{
std::string strSearchImgPath = strPath_left + "/*.png";
Read_Camera_Product_Image(strSearchImgPath, 0, strproduct);
strSearchImgPath = strPath_right + "/*.png";
Read_Camera_Product_Image(strSearchImgPath, 0, strproduct);
// strSearchImgPath = strPath + "/*.png";
// Read_Camera_Product_Image(strSearchImgPath, 1);
return 0;
}