分离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)
相关推荐
哇哈哈&10 分钟前
安装wxWidgets3.2.0(编译高版本erlang的时候用,不如用rpm包),而且还需要高版本的gcc++19以上,已基本舍弃
linux·数据库·python
Kaze_story26 分钟前
Vue第四节:组件化、组件生命周期
前端·javascript·vue.js
yuzhiboyouye32 分钟前
web前端开发自测清单
前端
妮妮分享41 分钟前
H5获取定位的方式是什么?
java·前端·javascript
weixin_439930641 小时前
前端js日期计算跨月导致的错误
开发语言·前端·javascript
luod1 小时前
pymysql执行DDL语句
python
零一科技1 小时前
瑞吉外卖项目,前端源码(用户端)解析
前端
用户93051065822241 小时前
module federation,monorepo分不清楚?
前端·架构
柳安1 小时前
手写new操作符执行过程
前端·javascript
狗哥哥1 小时前
Vue 3 统一面包屑导航系统:从配置地狱到单一数据源
前端·vue.js·架构