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, train_txt, val_txt, test_txt, flag, ): data_abs_path = os.path.abspath(root) label_file_name = args.detLabelFileName if flag == "det" else 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) if flag == "det": image_path = os.path.join(data_abs_path, image_name) elif flag == "rec": 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) train_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) val_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) test_txt.write("{}\t{}".format(image_copy_path, image_label))删掉存在的文件
def removeFile(path): if os.path.exists(path): os.remove(path)
def genDetRecTrainVal(args): detAbsTrainRootPath = isCreateOrDeleteFolder(args.detRootPath, "train") detAbsValRootPath = isCreateOrDeleteFolder(args.detRootPath, "val") detAbsTestRootPath = isCreateOrDeleteFolder(args.detRootPath, "test") recAbsTrainRootPath = isCreateOrDeleteFolder(args.recRootPath, "train") recAbsValRootPath = isCreateOrDeleteFolder(args.recRootPath, "val") recAbsTestRootPath = isCreateOrDeleteFolder(args.recRootPath, "test")
removeFile(os.path.join(args.detRootPath, "train.txt")) removeFile(os.path.join(args.detRootPath, "val.txt")) removeFile(os.path.join(args.detRootPath, "test.txt")) removeFile(os.path.join(args.recRootPath, "train.txt")) removeFile(os.path.join(args.recRootPath, "val.txt")) removeFile(os.path.join(args.recRootPath, "test.txt")) detTrainTxt = open( os.path.join(args.detRootPath, "train.txt"), "a", encoding="UTF-8" ) detValTxt = open(os.path.join(args.detRootPath, "val.txt"), "a", encoding="UTF-8") detTestTxt = open(os.path.join(args.detRootPath, "test.txt"), "a", encoding="UTF-8") 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") splitTrainVal( args.datasetRootPath, detAbsTrainRootPath, detAbsValRootPath, detAbsTestRootPath, detTrainTxt, detValTxt, detTestTxt, "det", ) for root, dirs, files in os.walk(args.datasetRootPath): for dir in dirs: if dir == "crop_img": splitTrainVal( root, recAbsTrainRootPath, recAbsValRootPath, recAbsTestRootPath, recTrainTxt, recValTxt, recTestTxt, "rec", ) else: continue break - Assistant
- User
cmd line
- Assistant
To run the Python script from the command line with specific arguments, you need to use the
pythoncommand, followed by the script name and any additional argument flags and values you wish to specify. Here's an example command you can use, based on the script you provided:python your_script_name.py \ --trainValTestRatio "6:2:2" \ --datasetRootPath "../train_data/" \ --detRootPath "../train_data/det" \ --recRootPath "../train_data/rec" \ --detLabelFileName "Label.txt" \ --recLabelFileName "rec_gt.txt" \ --recImageDirName "crop_img" - User
python your_script_name.py
--trainValTestRatio "6:2:2"
--datasetRootPath "\10.0.0.2\dataset\cosai_dataset\OCR\1111"
--detRootPath "\10.0.0.2\dataset\cosai_dataset\OCR\1111"
--recRootPath "\10.0.0.2\dataset\cosai_dataset\OCR\1111
--detLabelFileName "\10.0.0.2\dataset\cosai_dataset\OCR\1111\Label.txt"
--recLabelFileName "\10.0.0.2\dataset\cosai_dataset\OCR\1111\Label.txt"
--recImageDirName "crop_img" correct this - Assistant
It looks like there are issues with how you're specifying paths and missing quotation marks, especially since you're trying to access network paths on a Windows system. Here is a corrected version of your command line, making sure that all paths are correctly enclosed in quotes and all parameters are properly specified:
python your_script_name.py \ --trainValTestRatio "6:2:2" \ --datasetRootPath "\\\\10.0.0.2\\dataset\\cosai_dataset\\OCR\\1111" \ --detRootPath "\\\\10.0.0.2\\dataset\\cosai_dataset\\OCR\\1111" \ --recRootPath "\\\\10.0.0.2\\dataset\\cosai_dataset\\OCR\\1111" \ --detLabelFileName "Label.txt" \ --recLabelFileName "rec_gt.txt" \ --recImageDirName "crop_img" - User
python your_script_name.py \ --trainValTestRatio "6:2:2" \ --datasetRootPath "\\10.0.0.2\dataset\cosai_dataset\OCR\1111" \ --detRootPath "\\10.0.0.2\dataset\cosai_dataset\OCR\1111" \ --recRootPath "\\10.0.0.2\dataset\cosai_dataset\OCR\1111" \ --detLabelFileName "Label.txt" \ --recLabelFileName "rec_gt.txt" \ --recImageDirName "crop_img"
- Assistant
The command you posted is for setting up and running a Python script using command-line arguments. It seems okay, but please note that this kind of syntax with backslashes as line continuation only works correctly in Unix-like systems (such as Linux and macOS). In Windows, the line continuation character should be the caret (
^), not the backslash (\). Here’s how you can write it to work on both types of systems properly:For Unix-like Systems (Linux/macOS):