分离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)
相关推荐
程序员Forlan1 分钟前
fiddler+手机或模拟器进行APP抓包
前端·智能手机·fiddler
I疯子3 分钟前
2026-04-08 打卡第 5 天
开发语言·windows·python
aidou13144 分钟前
Vue3自定义实现日期选择器(可单选或多选)
前端·javascript·vue.js·日期选择器·transition
C+++Python16 分钟前
Python MCP Server 最简实现
开发语言·python
zhuhezhang25 分钟前
一个用python开发的文本对比工具
python·文本对比工具
绝世唐门三哥26 分钟前
OpenClaw 安装 + 手动配置 DeepSeek 模型(2026.4.5 版)
前端·oepn claw
智算菩萨26 分钟前
【Python图像处理】5 Pillow图像处理与格式转换
图像处理·python·pillow
来一颗砂糖橘27 分钟前
吃透 ES6 扩展运算符(...):从表面语法到底层逻辑,避开所有坑
前端·javascript·es6·扩展运算符·前端进阶
前端小D32 分钟前
JS模块化
开发语言·前端·javascript
人工干智能35 分钟前
科普:%%matplotlib inline:魔法命令 (Cell Magic)
python·matplotlib