一:解决数据集命名重复的问题
把第一位的0改成c
jpg版本
import os
def rename_files_in_directory(directory):
# 遍历目录下所有文件
for filename in os.listdir(directory):
if filename.endswith('.jpg'):
filepath = os.path.join(directory, filename)
# 获取文件名
file_name, file_extension = os.path.splitext(filename)
# 如果文件名以数字0开头,则进行替换
if file_name[0] == '0':
new_file_name = 'c' + file_name[1:] + file_extension
# 重命名文件
os.rename(filepath, os.path.join(directory, new_file_name))
# 指定目录
directory = 'D:/Desktop/datasets_car/images/val'
# 调用函数
rename_files_in_directory(directory)
txt版本
import os
def rename_files_in_directory(directory):
# 遍历目录下所有文件
for filename in os.listdir(directory):
if filename.endswith('.txt'):
filepath = os.path.join(directory, filename)
# 获取文件名
file_name, file_extension = os.path.splitext(filename)
# 如果文件名以数字0开头,则进行替换
if file_name[0] == '0':
new_file_name = 'c' + file_name[1:] + file_extension
# 重命名文件
os.rename(filepath, os.path.join(directory, new_file_name))
# 指定目录
directory = 'D:/Desktop/datasets_car/labels/val'
# 调用函数
rename_files_in_directory(directory)
二: 在原有的数据集上添加新的类别
原来是只有一类0,加在新的数据集上的12类之后,改成12类。
import os
def replace_first_zero_with_12_in_txt_files(directory):
# 遍历目录下所有文件
for filename in os.listdir(directory):
if filename.endswith('.txt'):
filepath = os.path.join(directory, filename)
# 读取文件内容
with open(filepath, 'r') as file:
lines = file.readlines()
# 修改文件内容
with open(filepath, 'w') as file:
for line in lines:
# 利用split方法分割每行字符串,取第一个元素进行替换
parts = line.strip().split(' ')
if parts[0] == '0':
parts[0] = '12'
# 重新将修改后的行写入文件
file.write(' '.join(parts) + '\n')
# 指定目录
directory = 'D:/Desktop/datasets_car/labels/val'
# 调用函数
replace_first_zero_with_12_in_txt_files(directory)
三:文件后缀.txt转换成.c文件
import os
import chardet
def detect_encoding(file_path):
with open(file_path, 'rb') as f:
result = chardet.detect(f.read())
return result['encoding']
def txt_to_c(txt_file, c_file):
encoding = detect_encoding(txt_file)
with open(txt_file, 'r', encoding=encoding) as f:
txt_content = f.read()
with open(c_file, 'w', encoding='utf-8') as f:
f.write(txt_content)
def batch_convert(txt_folder, c_folder):
if not os.path.exists(c_folder):
os.makedirs(c_folder)
for filename in os.listdir(txt_folder):
if filename.endswith('.txt'):
txt_file = os.path.join(txt_folder, filename)
c_file = os.path.join(c_folder, filename.replace('.txt', '.c'))
txt_to_c(txt_file, c_file)
if __name__ == "__main__":
txt_folder = "D:/Desktop/input_directory" # 原始txt文件所在的文件夹
c_folder = "D:/Desktop/output_directory" # 保存转换后c文件的文件夹
batch_convert(txt_folder, c_folder)