当前位置: 首页 > 工具软件 > Foggy > 使用案例 >

整理Cityscapes转换成foggy_cityscapes数据集所有使用的脚本合集

陆昂然
2023-12-01

前文:Cityscapes数据集转换成COCO类型和VOC类型_cityscapes转voc_努力学习放弃摆烂的小弟的博客-CSDN博客

①:

import json
import os
import os.path
from PIL import Image

#####################
#cityscapes数据集转换成VOC格式 按城市文件名分开 存入新文件夹中
#####################

def position(pos):
    # 该函数用来找出xmin,ymin,xmax,ymax即bbox包围框
    x = []
    y = []
    nums = len(pos)
    for i in range(nums):
        x.append(pos[i][0])
        y.append(pos[i][1])
    x_max = max(x)
    x_min = min(x)
    y_max = max(y)
    y_min = min(y)
    b = (float(x_min), float(y_min), float(x_max), float(y_max))
    return b

def convert_annotation(image_id):

        load_f = open(rootdir + '/' + image_id + "_gtFine_polygons.json", 'r')  # 导入json标签的路径
        load_dict = json.load(load_f)
        out_file = open(rootdir + '/' + '%s_leftImg8bit.txt' % (image_id), 'w')  # 输出标签的路径
        objects = load_dict['objects']
        nums = len(objects)

        cls_id = ''
        for i in range(0, nums):
            labels = objects[i]['label']
            if (labels in ['person']): #这里我需要用到的类别是这8类
                print(labels)
                pos = objects[i]['polygon']
                bb = position(pos)
                cls_id = labels
                out_file.write(cls_id + " " + " ".join([str(a) for a in bb]) + '\n')
        if cls_id == '':
            print('no label json:', "%s_gtFine_polygons.json" % (image_id))


def images_id(orgin_picture_dir):  # 获取训练集每个图像的名称  (orgin_picture_dir训练集图像的路径)
    a = []
    for parent, dirnames, filenames in os.walk(orgin_picture_dir):
        for filename in filenames:
            print(filename)
            filename = filename.split('_leftImg8bit.png')[0]
            a.append(filename)
    return a


def xml_newdir(orgin_picture_dir, rootdir, new_rootdir):
    img_basenames = os.listdir(orgin_picture_dir)
    img_names = []
    for item in img_basenames:
        temp1, temp2 = os.path.splitext(item)  # 分别提取图片名称和图片后缀名称(具有_leftImg8bit)
        img_names.append(temp1)
        print(img_names)
    for img in img_names:  # img是没有后缀的pic名称
        im = Image.open((orgin_picture_dir + img + '.png'))
        width, height = im.size

        # open the crospronding txt file
        gt = open(rootdir + '/' + img + '.txt').read().splitlines()

        # write in xml file
        xml_file = open((new_rootdir + '/' + img + '.xml'), 'w')
        xml_file.write('<annotation>\n')
        xml_file.write('    <folder>CITYSCAPE</folder>\n')
        xml_file.write('    <filename>' + str(img) + '.png' + '</filename>\n')
        xml_file.write('    <size>\n')
        xml_file.write('        <width>' + str(width) + '</width>\n')
        xml_file.write('        <height>' + str(height) + '</height>\n')
        xml_file.write('        <depth>3</depth>\n')
        xml_file.write('    </size>\n')

        # write the region of image on xml file
        for img_each_label in gt:
            spt = img_each_label.split(' ')  # 这里如果txt里面是以逗号‘,’隔开的,那么就改为spt = img_each_label.split(',')。
            xml_file.write('    <object>\n')
            xml_file.write('        <name>' + str(spt[0]) + '</name>\n')
            xml_file.write('        <pose>Unspecified</pose>\n')
            xml_file.write('        <truncated>0</truncated>\n')
            xml_file.write('        <difficult>0</difficult>\n')
            xml_file.write('        <bndbox>\n')
            xml_file.write('            <xmin>' + str(spt[1]) + '</xmin>\n')
            xml_file.write('            <ymin>' + str(spt[2]) + '</ymin>\n')
            xml_file.write('            <xmax>' + str(spt[3]) + '</xmax>\n')
            xml_file.write('            <ymax>' + str(spt[4]) + '</ymax>\n')
            xml_file.write('        </bndbox>\n')
            xml_file.write('    </object>\n')
        xml_file.write('</annotation>')


if __name__ == '__main__':
    city_name = os.listdir('H:/gtFine_trainvaltest/gtFine/train/')
    print(city_name)
    for city in city_name:
        rootdir = 'H:/gtFine_trainvaltest/gtFine/train/' + city + '/'  # json所在路径
        orgin_picture_dir = 'H:/leftImg8bit_trainvaltest/leftImg8bit/train/' + city + '/'  # cityscape的train所在路径
        names = images_id(orgin_picture_dir)
        for image_id in names:
            print(image_id)
            convert_annotation(image_id)
        # xml_generator(orgin_picture_dir, rootdir)

        new_rootdir = 'H:/new_label_images/train/'  # 新路径
        if os.path.exists(new_rootdir + city):
            pass
        else:
            os.makedirs(new_rootdir + city)
        xml_newdir(orgin_picture_dir, rootdir, new_rootdir + city)


②:

import os
import shutil


def CreateDir(path):
    isExists=os.path.exists(path)
    # 判断结果
    if not isExists:
        # 如果不存在则创建目录
        os.makedirs(path)
        print(path+' 目录创建成功')
    else:
        # 如果目录存在则不创建,并提示目录已存在
        print(path+' 目录已存在')


def CopyFile(filepath, newPath):
    # 获取当前路径下的文件名,返回List
    fileNames = os.listdir(filepath)
    for file in fileNames:
        # 将文件命加入到当前文件路径后面
        newDir = filepath + '/' + file
        for i in os.listdir(newDir):
            path=newDir+"/"+str(i)
            new_path=newPath+'/'+str(i)
            shutil.copyfile(path, new_path)

if __name__=="__main__":
    path='H:/new_label_images/train/'
    new_path_3='H:/new_label_images_foggy/'
    CreateDir(new_path_3)
    CopyFile(path,new_path_3)

③:

import os
import shutil

# 设置原始文件夹和目标文件夹的路径
src_folder = 'H:/new_label_images/foggy_train_xml/'
dst_folder = 'H:/new_label_images/0.005_xml/'
if not os.path.exists(dst_folder):
    os.makedirs(dst_folder)
# 获取原始文件夹中以“0.02.txt”结尾的文件列表
file_list = [os.path.join(src_folder, f) for f in os.listdir(src_folder) if f.endswith('0.005.xml')]

# 将符合条件的文件复制到目标文件夹中
for file_path in file_list:
    shutil.copy(file_path, dst_folder)

④:

import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import join


def convert(size, box):
    x_center = (box[0] + box[1]) / 2.0
    y_center = (box[2] + box[3]) / 2.0
    x = x_center / size[0]
    y = y_center / size[1]
    w = (box[1] - box[0]) / size[0]
    h = (box[3] - box[2]) / size[1]
    return (x, y, w, h)


def convert_annotation(xml_files_path, save_txt_files_path, classes):
    xml_files = os.listdir(xml_files_path)
    print(xml_files)
    for xml_name in xml_files:
        print(xml_name)
        xml_file = os.path.join(xml_files_path, xml_name)
        out_txt_path = os.path.join(save_txt_files_path, xml_name.split('.')[0] + '.txt')
        out_txt_f = open(out_txt_path, 'w')
        tree = ET.parse(xml_file)
        root = tree.getroot()
        size = root.find('size')
        w = int(size.find('width').text)
        h = int(size.find('height').text)

        for obj in root.iter('object'):
            difficult = obj.find('difficult').text
            cls = obj.find('name').text
            if cls not in classes or int(difficult) == 1:
                continue
            cls_id = classes.index(cls)
            xmlbox = obj.find('bndbox')
            b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),
                 float(xmlbox.find('ymax').text))
            # b=(xmin, xmax, ymin, ymax)
            print(w, h, b)
            bb = convert((w, h), b)
            out_txt_f.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')


if __name__ == "__main__":
    # 需要转换的类别,需要一一对应
    classes1 = ['person']
    # 2、voc格式的xml标签文件路径
    xml_files1 = 'H:/new_label_images/0.005_xml/'
    # 3、转化为yolo格式的txt标签文件存储路径
    save_txt_files1 = 'H:/new_label_images/0.005_txt/'

    convert_annotation(xml_files1, save_txt_files1, classes1)

⑤:

# 将cityscpe_xml转化为fog_cityscapes_xml:
import os
import xml.etree.ElementTree as ET
source_path = "H:/gtFine_trainvaltest/gtFine/train/"
city_names =os.listdir(source_path)
for city in city_names:
    cityscape_xml_path = "H:/new_label_images/train/" + city + '/'
    foggy_xml_path = "H:/new_label_images/foggy_train_xml/"
    betas = [0.01, 0.02, 0.005]
    city_xml_list = os.listdir(cityscape_xml_path)
    for file in city_xml_list:
        if file.endswith('.xml') or file.endswith('XML'):
            city_xml = file
            print(city_xml)
            tree = ET.parse(cityscape_xml_path + city_xml)
            root = tree.getroot()
            filename = root.find('filename').text  # 获取xml中的filename
            for beta in betas:
                filename = filename.split(".png")[0] + "_foggy_beta_" + str(beta) + ".png"
                root.find('filename').text = filename
                foggy_city_xml = city_xml.split(".xml")[0] + "_foggy_beta_" + str(beta) + ".xml"
                tree.write(foggy_xml_path + foggy_city_xml)

⑥:

# import os
# import shutil
#
# # 设置输入和输出文件夹路径
# input_folder = "H:/foggy_train_images"
# output_folder = "H:/foggy"
#
# # 设置需要分离的字符串
# split_string = "0.02"
#
# # 遍历输入文件夹中的所有文件
# for filename in os.listdir(input_folder):
#     # 跳过非图像文件
#     if not filename.endswith(".jpg") and not filename.endswith(".png"):
#         continue
#
#     # 检查文件名是否包含指定的分离字符串
#     if split_string not in filename:
#         continue
#
#     # 创建对应的输出文件夹
#     output_path = os.path.join(output_folder, split_string)
#     os.makedirs(output_path, exist_ok=True)
#
#     # 将图像文件复制到对应的输出文件夹中
#     image_path = os.path.join(input_folder, filename)
#     output_file = os.path.join(output_path, filename)
#     shutil.copy(image_path, output_file)

import os
import shutil


def CreateDir(path):
    isExists=os.path.exists(path)
    # 判断结果
    if not isExists:
        # 如果不存在则创建目录
        os.makedirs(path)
        print(path+' 目录创建成功')
    else:
        # 如果目录存在则不创建,并提示目录已存在
        print(path+' 目录已存在')


def CopyFile(filepath, newPath):
    # 获取当前路径下的文件名,返回List
    fileNames = os.listdir(filepath)
    for file in fileNames:
        # 将文件命加入到当前文件路径后面
        newDir = filepath + '/' + file
        for i in os.listdir(newDir):
            path=newDir+"/"+str(i)
            new_path=newPath+'/'+str(i)
            shutil.copyfile(path, new_path)

if __name__=="__main__":
    path='H:/leftImg8bit_trainvaltest_foggy/leftImg8bit_foggy/test/'
    new_path_3='H:/leftImg8bit_trainvaltest_foggy/test/'
    CreateDir(new_path_3)
    CopyFile(path,new_path_3)
 类似资料: