Semantic Segmentation Training Dataset Extract the pictures in the folder and modify the picture properties in the folder

Preface
After the author uses labelme to mark the data and modify it to json_to_dataset.py file, execute json_to_dataset.exe to generate many folders, as shown in Figure 1
insert image description here

And each small folder contains the png file about the original image and the mask image label.png

insert image description here

What the author wants to generate is the VOCdevkit data set, which needs to put the original image into the JPEGImages file and the mask image into the SegmentationClass file.

Obviously, it is impossible to manually click on the folders to pull out the pictures, and then write a code to extract the pictures with the same name specified in these folders (although there should be quite a lot on the Internet hahaha, but I wrote it with my heart on hand)

Extract specified pictures (files) under multiple folders

code show as below:

import os

import shutil

# 定义源文件夹和目标文件夹路径

source_folder = 'C:/Users/Administrator/Desktop/output'

#输出的路径
target_folder = 'C:/Users/Administrator/Desktop/img'

# 如果目标文件夹不存在,则创建它

if not os.path.exists(target_folder):
    os.makedirs(target_folder)

# 遍历源文件夹中的所有子文件夹
#输入源文件夹中的子文件夹个数 +1  或者使用函数读取出包含的子文件个数+1
for i in range(1, 278):

    # 构建子文件夹名称和标签文件名
	#子文件夹名称 例如1_json
    subfolder_name = f'{
      
      i}_json'
    
	#输入想要提取的文件或者图片名称 
    label_file_name = 'label.png'

    label_file_path = os.path.join(source_folder, subfolder_name, label_file_name)

    # 如果标签文件存在,则读取它并重命名图片

    if os.path.exists(label_file_path):
        with open(label_file_path, 'rb') as f:
            content = f.read()

        # 将图片内容写入新的文件中,并重命名新文件为原文件名加上数字后缀的形式
		#如1.png 2.png等等
        new_file_name = f'{
      
      i}.png'

        new_file_path = os.path.join(target_folder, new_file_name)

        with open(new_file_path, 'wb') as f:
            f.write(content)

        # 删除原始标签文件 使用这个函数之后会删除源文件中的想要提取的图片或者文件 慎用!
        # os.remove(label_file_path)

print('Done!')

Extract all specified types of files or pictures under a single folder

Go directly to the code:

import os

import shutil



# 定义源文件夹和目标文件夹路径

source_folder = 'C:/Users/Administrator/Desktop/img'

target_folder = 'C:/Users/Administrator/Desktop/JPEGImages'



# 如果目标文件夹不存在,则创建它

if not os.path.exists(target_folder):

    os.makedirs(target_folder)



# 遍历源文件夹下的所有文件

for filename in os.listdir(source_folder):

    # 如果文件是。jpg格式,则将其复制到目标文件夹中

    if filename.endswith('.jpg'):

        old_file_path = os.path.join(source_folder, filename)

        new_file_path = os.path.join(target_folder, filename)

        shutil.copy(old_file_path, new_file_path)

Change png images to jpg images in a single folder

It can realize the conversion between png and jpg pictures.


import os

import cv2



# 定义文件夹路径和文件后缀名

folder_path = 'C:/Users/Administrator/Desktop/png'

file_suffix = '.png'



# 遍历文件夹下的所有文件

for filename in os.listdir(folder_path):

    # 如果文件是图片,则将其后缀名改为.jpg

    if filename.endswith(file_suffix):

        old_file_path = os.path.join(folder_path, filename)

        new_file_path = os.path.join(folder_path, filename[:-4] + '.jpg')
        #如果不知道filename[:-4] 输出的是啥 不妨print一下吧

        # 读取图片数据并保存为新的.jpg格式

        img = cv2.imread(old_file_path)

        cv2.imwrite(new_file_path, img)
        
		#删除png图片
        os.remove(old_file_path)

Guess you like

Origin blog.csdn.net/weixin_44598554/article/details/131455327