#include "Read_Image.h" #include // 转小写 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 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 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(); } std::shared_ptr 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(); } std::shared_ptr Extract_Name_Base::GetCamera(std::shared_ptr 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(); } // 判断目录部分是否包含某些关键词(不区分大小写) 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 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(); 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 pCamera = GetCamera(pProduct, strCameraName); if (pCamera.get() == nullptr) { printf("pCamera %s is NULL \n", strCameraName.c_str()); pCamera = std::make_shared(); 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 pImage = std::make_shared(); 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(); // 文件名包含 BCA/BTA/DCA/DTA/CA/TA 即判定对应相机 if (filename.find("BCA") != std::string::npos) return "BCA"; else if (filename.find("DCA") != std::string::npos) return "DCA"; else if (filename.find("BTA") != std::string::npos) return "BTA"; else if (filename.find("DTA") != std::string::npos) return "DTA"; 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}_{Camera}{AB}, 如 A_BCAA, M_DTAB, A_BCAB // 通道名 = 前缀 (A/M), 表示不同打光方式 // 支持4相机: BCAA, BCAB, BTAA, BTAB, DCAA, DCAB, DTAA, DTAB size_t underscorePos = filename.find('_'); if (underscorePos != std::string::npos) { std::string prefix = filename.substr(0, underscorePos); std::string rest = filename.substr(underscorePos + 1); // 4相机格式 if (rest == "BCAA" || rest == "BCAB" || rest == "BTAA" || rest == "BTAB" || rest == "DCAA" || rest == "DCAB" || rest == "DTAA" || rest == "DTAB" ) { return prefix; // "A" or "M" } } // 旧格式兼容: CA_xxx.ytimage, TA_xxx.ytimage // 4相机格式兼容: BCA_xxx, BTA_xxx, DCA_xxx, DTA_xxx if (filename.find("BCAA") != std::string::npos || filename.find("BCAB") != std::string::npos) strchannelName = "BCA"; else if (filename.find("BTAA") != std::string::npos || filename.find("BTAB") != std::string::npos) strchannelName = "BTA"; else if (filename.find("DCAA") != std::string::npos || filename.find("DCAB") != std::string::npos) strchannelName = "DCA"; else if (filename.find("DTAA") != std::string::npos || filename.find("DTAB") != std::string::npos) strchannelName = "DTA"; else if (filename.find("BCA") != std::string::npos) strchannelName = "BCA"; else if (filename.find("BTA") != std::string::npos) strchannelName = "BTA"; else if (filename.find("DCA") != std::string::npos) strchannelName = "DCA"; else if (filename.find("DTA") != std::string::npos) strchannelName = "DTA"; return strchannelName; } int Extract_ALL::Read_Image_List(std::string strPath, std::string strproduct) { std::string strSearchImgPath = strPath + "/*.jpg"; 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 + "/*.jpg"; Read_Camera_Product_Image(strSearchImgPath, 0, strproduct); strSearchImgPath = strPath_right + "/*.jpg"; Read_Camera_Product_Image(strSearchImgPath, 0, strproduct); // strSearchImgPath = strPath + "/*.jpg"; // Read_Camera_Product_Image(strSearchImgPath, 1); return 0; }