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.

470 lines
12 KiB

#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::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, 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)
{
m_extract_CELL_ET.pimage_ReadChannel = pimage_ReadChannel;
m_extract_CELL_ET.m_product_Camera_List.clear();
int re = m_extract_CELL_ET.Read_Image_List(strPath);
if (re != 0)
{
return re;
}
m_product_Camera_List.clear();
m_product_Camera_List.assign(m_extract_CELL_ET.m_product_Camera_List.begin(), m_extract_CELL_ET.m_product_Camera_List.end());
for (int i = 0; i < m_extract_CELL_ET.m_product_Camera_List.size(); i++)
{
m_extract_CELL_ET.m_product_Camera_List[i]->print();
}
// m_extract_BD.m_product_Camera_List.clear();
// int re = m_extract_BD.Read_Image_List(strPath);
// if (re != 0)
// {
// return re;
// }
// m_product_Camera_List.clear();
// m_product_Camera_List.assign(m_extract_BD.m_product_Camera_List.begin(), m_extract_BD.m_product_Camera_List.end());
// for (int i = 0; i < m_extract_BD.m_product_Camera_List.size(); i++)
// {
// m_extract_BD.m_product_Camera_List[i]->print();
// }
return 0;
}
int Extract_CELL_ET::Extract_Info(std::string strPath, 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 (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_CELL_ET::Extract_Camera_Name(std::string strPath, int userflag)
{
if (userflag == 1)
{
std::string prefix = "_0_";
bool bleft = false;
size_t pos1 = strPath.rfind(prefix);
if (pos1 != std::string::npos)
{
bleft = true;
}
std::string prefix1 = "工位左";
bool bleft1 = false;
size_t pos2 = strPath.rfind(prefix1);
if (pos2 != std::string::npos)
{
bleft1 = true;
}
if (bleft || bleft1)
{
return "left";
}
else
{
std::string prefix = "_1_";
bool bright = false;
size_t pos1 = strPath.rfind(prefix);
if (pos1 != std::string::npos)
{
bright = true;
}
std::string prefix1 = "工位右";
bool bright1 = false;
size_t pos2 = strPath.rfind(prefix1);
if (pos2 != std::string::npos)
{
bright1 = true;
}
if (bright || bright1)
{
return "right";
}
}
}
else
{
fs::path p = fs::path(strPath);
bool bleft = dirPathContains(p, "left");
bool bleft1 = dirPathContains(p, "shard_left");
if (bleft || bleft1)
{
return "left";
}
else
{
bool bright = dirPathContains(p, "right");
bool bright1 = dirPathContains(p, "shard_right");
if (bright || bright1)
{
return "right";
}
}
}
return "";
}
std::string Extract_CELL_ET::Extract_Product_Name(std::string strPath, int userflag)
{
if (userflag == 0)
{
/* code */
// 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.empty())
{
return "";
}
std::string prefix = "__DB__";
if (name.rfind(prefix, 0) == 0)
{ // 判断是否以 "__DB__" 开头
std::string result = name.substr(prefix.length());
std::cout << "Product name: " << result << std::endl;
return result;
}
else
{
std::cout << "Product name error: " << name << std::endl;
}
}
else if (userflag == 1)
{
fs::path p = fs::path(strPath);
fs::path lastDir = p.parent_path().filename();
std::string name = lastDir.string();
if (name.empty() || name != "OriginalImage")
{
return "";
}
fs::path lastDir123 = p.parent_path().parent_path().filename();
std::string sproduct = lastDir123.string();
// std::cout << "Product name error: " << sproduct << std::endl;
size_t lastUnderscore = sproduct.find('_');
if (lastUnderscore == std::string::npos)
return "";
std::string strImageproduct = sproduct.substr(0, lastUnderscore);
// printf("strImageChannel %s\n", strImageproduct.c_str());
return strImageproduct;
}
// // 独特判断
// if (name.find("__DB__") != std::string::npos)
// {
// }
return "";
}
std::string Extract_CELL_ET::Extract_Channel_Name(std::string strPath, int userflag)
{
std::string strchannelName = "";
if (userflag == 1)
{
fs::path p = fs::path(strPath);
std::string filenameWithoutExt = p.stem().string();
std::cout << "提取的内容: " << filenameWithoutExt << std::endl;
if (pimage_ReadChannel)
{
strchannelName = pimage_ReadChannel->strDetName(filenameWithoutExt);
}
}
else
{
fs::path p = fs::path(strPath);
std::string filenameWithoutExt = p.stem().string();
// std::cout << "提取的内容: " << filenameWithoutExt << std::endl;
std::string strexName = "";
size_t pos = filenameWithoutExt.find('^');
if (pos != std::string::npos && pos + 1 < filenameWithoutExt.size())
{
strexName = filenameWithoutExt.substr(pos + 1);
std::cout << "提取到的字符串: " << strexName << std::endl;
}
if (pimage_ReadChannel)
{
strchannelName = pimage_ReadChannel->strDetName(strexName);
}
}
return strchannelName;
}
int Extract_CELL_ET::Read_Image_List(std::string strPath)
{
std::string strSearchImgPath = strPath + "/*.tif";
Read_Camera_Product_Image(strSearchImgPath, 0);
strSearchImgPath = strPath + "/*.png";
Read_Camera_Product_Image(strSearchImgPath, 1);
return 0;
}
int Extract_BD::Extract_Info(std::string strPath, int userflag)
{
std::cout << strPath << std::endl;
// 1、获取 产品名称
std::string strProductName = Extract_Product_Name(strPath);
if (strProductName.empty())
{
return -1;
}
std::shared_ptr<Product_File_Info> pProduct = GetProduct(strProductName);
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);
if (strCameraName.empty())
{
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);
if (strChannelName.empty())
{
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;
return 0;
}
std::string Extract_BD::Extract_Camera_Name(std::string strPath, int userflag)
{
fs::path p = fs::path(strPath);
return "";
}
std::string Extract_BD::Extract_Product_Name(std::string strPath, int userflag)
{
fs::path p = fs::path(strPath);
fs::path lastDir = p.parent_path().filename();
std::string name = lastDir.string();
if (name.empty())
{
return "";
}
size_t pos = name.rfind('-'); // 找最后一个 '-'
if (pos != std::string::npos && pos + 1 < name.size())
{
return name.substr(pos + 1);
}
return ""; // 没有找到 '-' 或后面没有字符
}
std::string Extract_BD::Extract_Channel_Name(std::string strPath, int userflag)
{
return "";
}
int Extract_BD::Read_Image_List(std::string strPath)
{
std::string strSearchImgPath = strPath + "/*.jpg";
Read_Camera_Product_Image(strSearchImgPath, 0);
return 0;
}