Semantic Segmentation: Corresponding naming and batch modification of png pictures and mask pictures after data enhancement using the Augmentor module

Foreword
When performing semantic segmentation but the amount of data is not very large, data enhancement is a good choice

Augmentor can be used for data enhancement in semantic segmentation (I won’t introduce Augmentor here, you can search on the Internet, and many will get started soon)

After enhancement, Augmentor will output the manipulated image and the mask image in the same position, and name them regularly as shown in the
figure:
insert image description here

Name the png image and the mask image correspondingly after data enhancement

import os
import cv2
import sys

def chang_filename(name,i):
	#掩码的文件夹路径
    folder_path = "C:/Users/Administrator/Desktop/data_enhancement/png/output/"
	#_groundtruth_(1)_img的img会随着文件夹的名字变化而变化 
	#注意观察output文件夹掩码图片与实物图片命名的规律
    mask = cv2.imread(folder_path + "_groundtruth_(1)_img"+name)
    if mask is  None:
        print("图片为空")
        sys.exit()
        
    cv2.imwrite("C:/Users/Administrator/Desktop/data_enhancement/output_mask/" + i + ".png",mask)



def remake_filename():
	#待批量重命名的文件夹  png文件夹存放的是原图png 
	#数据增强后会在png文件夹产生一个output文件夹 里面包含了增强后的掩码以及增强后的图片
    src_folder= "C:/Users/Administrator/Desktop/data_enhancement/png/output"
    dst_folder = "C:/Users/Administrator/Desktop/data_enhancement/output_png"
    #class_name = ".json"  # 重命名后的文件名后缀
    class_name = ".png"     #重命名后的文件名后缀

    file_in = os.listdir(path_in)        #返回文件夹包含的所有文件名
    num_file_in = len(file_in)          #获取文件数目
    print(file_in , num_file_in)         #输出修改前的文件名

    for i in range(0,num_file_in):
        t = str(i +1 + 0)

        img_name = file_in[i]
		
		# 如果存放原图的文件夹叫img 这里需要改成img_original
		if "png_original" in img_name:
	        name = img_name.split('img_original')[1]
	        print(img_name)
	        #重命名文件名 并输出到指定文件夹
	        os.rename(src_folder+"/"+file_in[i],dst_folder +"/"+t+class_name)        
	        chang_filename(name,t)


    file_out = os.listdir(path_in)
    print(file_out)         #输出修改后的结果

remake_filename()

This code will name the picture A and the mask A corresponding to the picture A at the same time to generate 1.png 2.png and so on

Modify picture names in batches

When the data is enhanced and the enhanced image and mask image are separated and named in digital form,
or there are other needs, the original number of the image is changed to another number for naming

import os

def remake_filename()
     path_in = "C:/Users/Administrator/Desktop/data_enhancement/mask_output"#待批量重命名的文件夹
     #class_name = ".json"  # 重命名后的文件名后缀
     class_name = ".png"     #重命名后的文件名后缀

     file_in = os.listdir(path_in)        #返回文件夹包含的所有文件名
     num_file_in = len(file_in)          #获取文件数目
     print(file_in , num_file_in)         #输出修改前的文件名

     #获取图片名字 包含文件类型在内
     num_list = []
     for i in range(0, num_file_in):
          img_name = file_in[i]
          # 获取文件类型前的名字 例如1.jpg 则num = 1
          num = img_name.split(class_name)[0]
          num_list.append(num)

     #获取最小值
     min_num = min(num_list)
     min_num = int(min_num) - 1
     print(min_num)

     # 输出的基础值 相当于0 输出图片会以此为0 第一张图片则命名为 output_minimum +1
     output_minimum = 1000

     for i in range(0,num_file_in):

          img_name = file_in[i]
          num = int(img_name.split('.png')[0])

          diff = num - min_num

          output_num = str(output_minimum + diff)
          new_name = os.rename(path_in+"/"+file_in[i],path_in+"/"+ output_num + class_name)        #重命名文件名

     file_out = os.listdir(path_in)
     print(file_out)         #输出修改后的结果

remake_filename()

Guess you like

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