自制数据集处理

1. 视频抽帧

每90帧抽一帧

import cv2
import os
# 处理视频为图片

def extract_frames(video_path, output_folder, frame_interval=30):
    # 打开视频文件
    video_capture = cv2.VideoCapture(video_path)
    # 确保视频文件已成功打开
    if not video_capture.isOpened():
        print("Error: Unable to open video file.")
        return

    # 确保输出文件夹存在
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)

    frame_count = 0
    while True:
        # 读取一帧
        ret, frame = video_capture.read()
        if not ret:
            break

        # 在每个指定的帧间隔保存帧
        if frame_count % frame_interval == 0:
            # 保存当前帧为图像文件
            frame_filename = os.path.join(output_folder, f"frame_{frame_count}.jpg")
            cv2.imwrite(frame_filename, frame)

        frame_count += 1

    # 释放视频捕获对象
    video_capture.release()


# 视频文件路径
video_path = "maize_tassel.mp4"
# 输出文件夹路径
output_folder = ("maize_tassel")
# 每30帧抽取一张图片
extract_frames(video_path, output_folder, frame_interval=90)

2. 图像裁剪

裁剪5120*2700图像为8份1280*1280图像

import os
from PIL import Image

def crop_images(input_folder, output_folder, crop_width, crop_height):
    # 确保输出文件夹存在
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)

    # 获取输入文件夹中的所有图像文件
    images = [f for f in os.listdir(input_folder) if f.endswith(('jpg', 'jpeg', 'png', 'bmp', 'tiff', 'png'))]

    for image_file in images:
        # 打开图像
        with Image.open(os.path.join(input_folder, image_file)) as img:
            img_width, img_height = img.size

            # 确保图像尺寸为5120x2700
            if img_width != 5120 or img_height != 2700:
                print(f"图像 {image_file} 尺寸不符合要求,跳过裁剪。")
                continue

            # 裁剪并保存图像
            for i in range(2):  # 高度方向裁剪2个块
                for j in range(4):  # 宽度方向裁剪4个块
                    left = j * crop_width
                    top = i * crop_height
                    right = left + crop_width
                    bottom = top + crop_height

                    cropped_img = img.crop((left, top, right, bottom))
                    cropped_img_name = f"{os.path.splitext(image_file)[0]}_crop_{i}_{j}.png"
                    cropped_img.save(os.path.join(output_folder, cropped_img_name))
                    print(f"保存裁剪图像: {cropped_img_name}")

# 示例用法
input_folder = './maize1'  # 输入文件夹路径
output_folder = './maize_split_8'  # 输出文件夹路径
crop_width = 1280  # 裁剪宽度
crop_height = 1280  # 裁剪高度

crop_images(input_folder, output_folder, crop_width, crop_height)

3. 图像画框

将一个voc格式的xml文件中的框画在一个图像中。

import os
import cv2
import xml.etree.ElementTree as ET


def draw_bounding_boxes(image_path, xml_path, output_path):
    # 读取图像
    image = cv2.imread(image_path)
    if image is None:
        print(f"Error: Unable to open image file {image_path}")
        return

    # 解析XML文件
    tree = ET.parse(xml_path)
    root = tree.getroot()

    # 获取所有object标签
    for obj in root.findall('object'):
        # 获取bndbox标签
        bndbox = obj.find('bndbox')
        if bndbox is None:
            continue

        # 提取边框坐标
        xmin = int(bndbox.find('xmin').text)
        ymin = int(bndbox.find('ymin').text)
        xmax = int(bndbox.find('xmax').text)
        ymax = int(bndbox.find('ymax').text)

        # 在图像上绘制红色边框,线条宽度为2
        cv2.rectangle(image, (xmin, ymin), (xmax, ymax), (0, 0, 255), 10)

    # 保存结果图像
    cv2.imwrite(output_path, image)
    print(f"Output saved to {output_path}")


def process_directories(xml_dir, img_dir, output_dir):
    # 确保输出目录存在
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    # 遍历XML目录中的所有文件
    for xml_file in os.listdir(xml_dir):
        if xml_file.endswith('.xml'):
            xml_path = os.path.join(xml_dir, xml_file)
            img_filename = xml_file.replace('.xml', '.jpg')
            image_path = os.path.join(img_dir, img_filename)

            if not os.path.exists(image_path):
                img_filename = xml_file.replace('.xml', '.png')
                image_path = os.path.join(img_dir, img_filename)

            if os.path.exists(image_path):
                output_path = os.path.join(output_dir, img_filename)
                draw_bounding_boxes(image_path, xml_path, output_path)
            else:
                print(f"Warning: No image file found for {xml_file}")


# 示例用法
xml_dir = './outputs'  # 替换为包含XML标签文件的目录路径
img_dir = './images'  # 替换为包含图像文件的目录路径
output_dir = './end'  # 替换为输出图像的保存目录路径

process_directories(xml_dir, img_dir, output_dir)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用PyTorch Geometric (pyg)创建自制数据集时,你可以继承`InMemoryDataset`类,并重写一些必要的方法来实现数据集的下载、处理和加载。首先,你需要定义一个类,例如`MyDataset`,并将其继承自`InMemoryDataset`。在构造函数中,你可以调用父类的构造函数,并在其中加载数据集。你可以使用`torch.load`函数加载数据,并将其赋值给`self.data`和`self.slices`。同时,你可以重写`raw_file_names`和`processed_file_names`方法来指定原始文件和处理后的文件的名称。在`download`方法中,你可以实现数据集的下载逻辑。在`process`方法中,你可以对数据集进行处理,并将处理后的数据保存到指定的路径。最后,在`collate`函数中,你可以将一个python列表形式的数据转换为`InMemoryDataset`内部存储数据的格式。你可以使用`data_list\[0\].__class__()`来创建一个空的数据对象,然后将列表中的每个数据对象转换为该类型。\[2\]\[3\] #### 引用[.reference_title] - *1* [在PyG上构建自己的数据集](https://blog.csdn.net/qq_32113189/article/details/126663738)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [【PyG入门学习】四:构建自己的数据集](https://blog.csdn.net/TwT520Ly/article/details/105633847)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值