分离image和txt json等

Labelme 这个垃圾软件会把图像和 标签放到一个文件夹,十分不方便。
需要分离一下

# 分离jpg txt和json文件


import os.path
import xml.etree.ElementTree as ET
from xml.dom.minidom import Document
import os
import os.path
from PIL import Image

import os
import shutil

class_name = './class.txt'

class_new_name = './class_new.txt'
Class_Name = []
Class_new_Name = []
# img_path  = r'/media/xxx/2/diyun/11_h264_data/苏州申通5/images/'    #图片文件夹
# xml_path  = r'/media/xxx/2/diyun/11_h264_data/苏州申通5/xml/'  #xml文件存放地址
# ann_path  = r'/media/xxx/2/diyun/11_h264_data/苏州申通5/0/'# yolov3标注.txt文件夹

source_folder="/media/xxx/2/diyun/11_h264_data/traffic_light_simulation/suzhou_yunda3/crossroad_10/images/"

dst_folder="/media/xxx/2/diyun/11_h264_data/traffic_light_simulation/suzhou_yunda3/crossroad_10"


jpg_destination_folder=dst_folder + "/images"  # 源文件夹路径
txt_destination_folder = dst_folder + "/labels"  # 目标文件夹路径
json_destination_folder= dst_folder + "/json"  # 目标文件夹路径

is_delete_src=1



def copy_txt_files(source_folder, destination_folder,is_delete_src):
    # 遍历源文件夹中的所有文件和目录
    for root, dirs, files in os.walk(source_folder):
        for file in files:
            # 检查文件扩展名是否为 .txt
            if file.endswith(".txt"):
                # 构建源文件的完整路径
                source_path = os.path.join(root, file)
                # 构建目标文件的完整路径
                destination_path = os.path.join(destination_folder, file)
                # 复制文件到目标文件夹
                shutil.copy(source_path, destination_path)
                if is_delete_src:
                    os.remove(source_path)

def copy_json_files(source_folder, destination_folder,is_delete_src):
    # 遍历源文件夹中的所有文件和目录
    for root, dirs, files in os.walk(source_folder):
        for file in files:
            # 检查文件扩展名是否为 .txt
            if file.endswith(".json"):
                # 构建源文件的完整路径
                source_path = os.path.join(root, file)
                # 构建目标文件的完整路径
                destination_path = os.path.join(destination_folder, file)
                # 复制文件到目标文件夹
                shutil.copy(source_path, destination_path)
                if is_delete_src:
                    os.remove(source_path)

def copy_jpg_files(source_folder, destination_folder):
    # 遍历源文件夹中的所有文件和目录
    for root, dirs, files in os.walk(source_folder):
        for file in files:
            # 检查文件扩展名是否为 .txt
            if file.endswith(".jpg"):
                # 构建源文件的完整路径
                source_path = os.path.join(root, file)
                # 构建目标文件的完整路径
                destination_path = os.path.join(destination_folder, file)
                # 复制文件到目标文件夹
                shutil.copy(source_path, destination_path)




if not os.path.exists(txt_destination_folder):
    os.mkdir(txt_destination_folder)
if not os.path.exists(json_destination_folder):
    os.mkdir(json_destination_folder)
if not os.path.exists(jpg_destination_folder):
    os.mkdir(jpg_destination_folder)


copy_txt_files(source_folder, txt_destination_folder,is_delete_src)
copy_json_files(source_folder, json_destination_folder,is_delete_src)

#copy_jpg_files(source_folder, jpg_destination_folder)



你可能感兴趣的:(工具类,json,python,前端)