You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Python中导入GoPro_large数据集的所有清晰图像

遍历GoPro_large数据集并导入清晰图像的Python实现

核心思路

遍历数据集根目录下的所有场景文件夹(如GOPR0884*_11_XX),进入每个文件夹内的sharp子目录,读取其中所有清晰图像文件。

代码实现(基于os和PIL)

import os
from PIL import Image

# 替换为你的数据集根目录路径
dataset_root = "/path/to/your/GoPro_large"
sharp_images = []

# 遍历根目录下的所有场景文件夹
for scene_folder in os.listdir(dataset_root):
    scene_path = os.path.join(dataset_root, scene_folder)
    # 跳过非文件夹或不符合命名规则的目录
    if not os.path.isdir(scene_path):
        continue
    if not scene_folder.startswith("GOPR0884"):
        continue
    
    # 定位sharp子文件夹
    sharp_dir = os.path.join(scene_path, "sharp")
    if not os.path.exists(sharp_dir):
        print(f"Warning: sharp文件夹未找到于 {scene_folder}")
        continue
    
    # 遍历sharp文件夹内的图像文件
    for img_filename in os.listdir(sharp_dir):
        img_path = os.path.join(sharp_dir, img_filename)
        # 过滤非图像文件(可根据实际格式调整)
        if img_filename.lower().endswith(('.png', '.jpg', '.jpeg')):
            try:
                # 导入图像
                img = Image.open(img_path)
                sharp_images.append(img)
                # 可在此添加后续处理逻辑,比如记录路径、预处理等
                # print(f"已加载: {img_path}")
            except Exception as e:
                print(f"加载失败 {img_path}: {str(e)}")

print(f"共加载清晰图像数量: {len(sharp_images)}")

代码实现(基于pathlib,更简洁)

from pathlib import Path
from PIL import Image

dataset_root = Path("/path/to/your/GoPro_large")
sharp_images = []

# 匹配所有符合命名格式的场景文件夹
scene_folders = dataset_root.glob("GOPR0884*")  # 通用匹配规则,可按需调整
for scene_folder in scene_folders:
    if not scene_folder.is_dir():
        continue
    sharp_dir = scene_folder / "sharp"
    if not sharp_dir.exists():
        print(f"Warning: sharp文件夹未找到于 {scene_folder.name}")
        continue
    
    # 遍历所有png格式图像(可改为*.jpg或*.[png,jpg,jpeg])
    for img_path in sharp_dir.glob("*.png"):
        try:
            img = Image.open(img_path)
            sharp_images.append(img)
        except Exception as e:
            print(f"加载失败 {img_path}: {str(e)}")

print(f"共加载清晰图像数量: {len(sharp_images)}")

关键说明

  • 务必将dataset_root替换为你本地数据集的实际路径。
  • 图像导入用了PIL库,若习惯使用OpenCV,可替换为cv2.imread(str(img_path))。
  • 可根据数据集实际图像格式调整文件过滤规则。
  • 代码加入了异常处理,避免单个损坏图像导致程序中断。

内容的提问来源于stack exchange,提问作者mahtab shahrivand

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 13:12:03