diff --git a/example/LoadImage.cpp b/example/LoadImage.cpp index b744c4d..dc19023 100644 --- a/example/LoadImage.cpp +++ b/example/LoadImage.cpp @@ -9,6 +9,8 @@ namespace { constexpr const char *LEFT_DIR_NAME = "left"; constexpr const char *RIGHT_DIR_NAME = "right"; + constexpr const char *LEFT2_DIR_NAME = "0"; + constexpr const char *RIGHT2_DIR_NAME = "1"; constexpr const char *PRODUCT_DIR_PREFIX = ""; } @@ -34,7 +36,7 @@ static void findCameraDirectories( const std::string &dir_name = entry.path().filename().string(); // 找到 left/right 目录 - if (dir_name == LEFT_DIR_NAME || dir_name == RIGHT_DIR_NAME) + if (dir_name == LEFT_DIR_NAME || dir_name == RIGHT_DIR_NAME || dir_name == LEFT2_DIR_NAME || dir_name == RIGHT2_DIR_NAME) { camera_dirs.push_back(entry.path()); continue; @@ -295,8 +297,9 @@ std::vector LoadImage::LoadALLImg( { // 判断是左图还是右图目录 const std::string camera_name = camera_dir.filename().string(); - const bool is_left = (camera_name == LEFT_DIR_NAME); - const bool is_right = (camera_name == RIGHT_DIR_NAME); + const bool is_left = (camera_name == LEFT_DIR_NAME || camera_name == LEFT2_DIR_NAME); + const bool is_right = (camera_name == RIGHT_DIR_NAME || camera_name == RIGHT2_DIR_NAME); + // 如果不是 left 或 right,跳过(理论上不会发生) if (!is_left && !is_right) @@ -306,92 +309,98 @@ std::vector LoadImage::LoadALLImg( try { - // 遍历相机目录下的产品目录 - for (const auto &product_entry : fs::directory_iterator(camera_dir)) + // 【关键修改】获取产品目录路径:即 camera_dir 的父目录 + // 例如: /.../6LPW614019B9DCA/0 -> /.../6LPW614019B9DCA + fs::path product_dir_path = camera_dir.parent_path(); + + // 提取产品 ID (从父目录名中提取) + const std::string product_id = extractProductId(product_dir_path.filename().string()); + + if (product_id.empty()) + { + // 如果父目录名不符合产品ID格式,跳过 + continue; + } + + // 如果指定了目标产品 ID,只加载该产品 + if (!target_product_id.empty() && product_id != target_product_id) + { + continue; + } + + // 获取或创建产品数据容器 + auto &product = product_map[product_id]; + + // 【关键修改】直接遍历 camera_dir (即 "0" 或 "1" 目录) 下的图片文件 + // 因为图片直接在 /.../6LPW614019B9DCA/0/ 下面 + for (const auto &file_entry : fs::directory_iterator(camera_dir)) { - if (!product_entry.is_directory()) + if (!file_entry.is_regular_file()) continue; - const std::string product_id = extractProductId(product_entry.path().filename().string()); - if (product_id.empty()) + const std::string filename = file_entry.path().filename().string(); + if (!matchesExtension(filename)) continue; - // 如果指定了目标产品 ID,只加载该产品 - if (!target_product_id.empty() && product_id != target_product_id) - { + // 提取原始通道名称 + std::string original_channel = extractChannelName(filename); + if (original_channel.empty()) continue; - } - auto &product = product_map[product_id]; + std::string channel = original_channel; - // 遍历产品目录下的图片文件 - for (const auto &file_entry : fs::directory_iterator(product_entry.path())) + // 使用 m_image_ReadChannel 进行通道过滤和名称映射 + if (pImageReadChannel != nullptr) { - if (!file_entry.is_regular_file()) - continue; - - const std::string filename = file_entry.path().filename().string(); - if (!matchesExtension(filename)) - continue; - - // 提取原始通道名称 - std::string original_channel = extractChannelName(filename); - if (original_channel.empty()) - continue; - - std::string channel = original_channel; + Image_ReadChannel *pChannel = static_cast(pImageReadChannel); + std::string mapped_channel = pChannel->strDetName(channel); - // 使用 m_image_ReadChannel 进行通道过滤和名称映射 - if (pImageReadChannel != nullptr) + if (mapped_channel.empty()) { - Image_ReadChannel *pChannel = static_cast(pImageReadChannel); - std::string mapped_channel = pChannel->strDetName(channel); - - // 如果映射结果为空,说明该通道不在检测列表中,过滤掉 - if (mapped_channel.empty()) - { - printf("[LoadImage] 跳过未配置的通道:%s (文件:%s)\n", - original_channel.c_str(), filename.c_str()); - continue; - } - - channel = mapped_channel; + printf("[LoadImage] 跳过未配置的通道:%s (文件:%s)\n", + original_channel.c_str(), filename.c_str()); + continue; } + channel = mapped_channel; + } - // 根据目录类型设置左图或右图路径 - auto &img_pair = product.images[filename + camera_name]; - img_pair.original_channel = original_channel; - img_pair.channel = channel; + // 根据目录类型设置左图或右图路径 + // 注意:这里的 key 需要确保唯一性。 + // 如果同一个产品下,不同相机目录(0和1)可能有同名文件,需要区分 + auto &img_pair = product.images[filename]; + + // 更新通道信息(如果多个相机目录都有同名文件,以后遍历到的为准,或者需要合并逻辑) + img_pair.original_channel = original_channel; + img_pair.channel = channel; - if (is_left) - { - img_pair.left_path = file_entry.path().string(); - } - else if (is_right) - { - img_pair.right_path = file_entry.path().string(); - } - - // 设置相机方位(在都设置了之后判断) - if (!img_pair.left_path.empty() && !img_pair.right_path.empty()) - { - img_pair.camera_side = "merge"; - } - else if (!img_pair.left_path.empty()) - { - img_pair.camera_side = "left"; - } - else if (!img_pair.right_path.empty()) - { - img_pair.camera_side = "right"; - } - else - { - img_pair.camera_side = "unknown"; - } + if (is_left) + { + img_pair.left_path = file_entry.path().string(); + } + else if (is_right) + { + img_pair.right_path = file_entry.path().string(); + } - total_loaded_.fetch_add(1, std::memory_order_relaxed); + // 设置相机方位 + if (!img_pair.left_path.empty() && !img_pair.right_path.empty()) + { + img_pair.camera_side = "merge"; + } + else if (!img_pair.left_path.empty()) + { + img_pair.camera_side = "left"; } + else if (!img_pair.right_path.empty()) + { + img_pair.camera_side = "right"; + } + else + { + img_pair.camera_side = "unknown"; + } + + total_loaded_.fetch_add(1, std::memory_order_relaxed); } } catch (const std::exception &e)