分离image和txt json等

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

需要分离一下

c 复制代码
# 分离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)
相关推荐
qq_424098562 分钟前
HTML5中解决数据库版本号管理混乱的规范化建议
jvm·数据库·python
小小码农Come on4 分钟前
QPainter双缓冲区实现一个简单画图软件
linux·服务器·前端
nunumaymax6 分钟前
【第三章-react 应用(基于 react 脚手架)】
前端·react.js·前端框架
Sherry Wangs6 分钟前
flash-attn安装指南
pytorch·python·flash-attn
Irene19918 分钟前
Python下载第三方库:requests、oracledb,连接 Oracle 数据库,测试数据输出(切记不要操作或删除系统表)
数据库·python·oracledb
空中海8 分钟前
第一章:Vue 基础与模板语法
前端·javascript·vue.js
weixin_441003648 分钟前
python编程从入门到实践第三版pdf免费下载
python·pdf
四维迁跃12 分钟前
HTML5中SVG利用Javascript实现图形拖拽与缩放
jvm·数据库·python
每天吃饭的羊22 分钟前
水平,垂直居中
前端·javascript·html
深度红薯27 分钟前
SAM3:开放式分割,太强了(后面有SAM3权重下载方式)(单图测试、视频测试、实时跟踪)
图像处理·人工智能·python·深度学习·毕业设计·毕设·sam3