coding:utf8 import os import shutil import random import argparse…
- User
coding:utf8
import os import shutil import random import argparse
删除划分的训练集、验证集、测试集文件夹,重新创建一个空的文件夹
def isCreateOrDeleteFolder(path, flag): flagPath = os.path.join(path, flag)
if os.path.exists(flagPath): shutil.rmtree(flagPath) os.makedirs(flagPath) flagAbsPath = os.path.abspath(flagPath) return flagAbsPathdef splitTrainVal( root, abs_train_root_path, abs_val_root_path, abs_test_root_path, rec_txt, ): data_abs_path = os.path.abspath(root) label_file_name = args.recLabelFileName label_file_path = os.path.join(data_abs_path, label_file_name)
with open(label_file_path, "r", encoding="UTF-8") as label_file: label_file_content = label_file.readlines() random.shuffle(label_file_content) label_record_len = len(label_file_content) for index, label_record_info in enumerate(label_file_content): image_relative_path, image_label = label_record_info.split("\t") image_name = os.path.basename(image_relative_path) image_path = os.path.join(data_abs_path, args.recImageDirName, image_name) train_val_test_ratio = args.trainValTestRatio.split(":") train_ratio = eval(train_val_test_ratio[0]) / 10 val_ratio = train_ratio + eval(train_val_test_ratio[1]) / 10 cur_ratio = index / label_record_len if cur_ratio < train_ratio: image_copy_path = os.path.join(abs_train_root_path, image_name) shutil.copy(image_path, image_copy_path) rec_txt.write("{}\t{}".format(image_copy_path, image_label)) elif cur_ratio >= train_ratio and cur_ratio < val_ratio: image_copy_path = os.path.join(abs_val_root_path, image_name) shutil.copy(image_path, image_copy_path) rec_txt.write("{}\t{}".format(image_copy_path, image_label)) else: image_copy_path = os.path.join(abs_test_root_path, image_name) shutil.copy(image_path, image_copy_path) rec_txt.write("{}\t{}".format(image_copy_path, image_label))删掉存在的文件
def removeFile(path): if os.path.exists(path): os.remove(path)
def genRecTrainVal(args): recAbsTrainRootPath = isCreateOrDeleteFolder(args.recRootPath, "train") recAbsValRootPath = isCreateOrDeleteFolder(args.recRootPath, "val") recAbsTestRootPath = isCreateOrDeleteFolder(args.recRootPath, "test")
removeFile(os.path.join(args.recRootPath, "train.txt")) removeFile(os.path.join(args.recRootPath, "val.txt")) removeFile(os.path.join(args.recRootPath, "test.txt")) recTrainTxt = open(os.path.join(args.recRootPath, "train.txt"), "a", encoding="UTF-8") recValTxt = open(os.path.join(args.recRootPath, "val.txt"), "a", encoding="UTF-8") recTestTxt = open(os.path.join(args.recRootPath, "test.txt"), "a", encoding="UTF-8") for root, dirs, files in os.walk(args.datasetRootPath): for dir in dirs: if dir == args.recImageDirName: splitTrainVal( root, recAbsTrainRootPath, recAbsValRootPath, recAbsTestRootPath, recTrainTxt, ) else: continue break recTrainTxt.close() recValTxt.close() recTestTxt.close() - Assistant
", help="file name of the recognition label file", ) parser.add_argument( "--recImageDirName", type=str, default="images", help="directory name containing the images", ) args = parser.parse_args()
# Generate train, validation, and test datasets for recognition genRecTrainVal(args) - User
correct this python code
- Assistant
- User
cmd line
- Assistant
To run the corrected Python script from the command line, you first need to have Python installed on your system. Here are the steps and an example command for running the script:
Prerequisites:
-
Ensure that Python is installed on your system. You can verify this by running:
python --version -
Navigate to the directory where your script is located.
Command-Line Example:
Assuming your script is saved as
split_dataset.py, you can execute it using the following command: -
- User
output not working
- Assistant
- User
- Assistant