import os def replace_category(file_path, target_category, new_category): lines = [] with open(file_path, 'r') as file: lines = file.readlines() new_lines = [] for line in lines: parts = line.split(' ') category = int(parts[0]) if category == target_category: parts[0] = str(new_category) new_line = ' '.join(parts) new_lines.append(new_line) with open(file_path, 'w') as file: file.writelines(new_lines) def modify_yolo_txt_files(folder_path, target_category, new_category): for filename in os.listdir(folder_path): if filename.endswith('.txt'): file_path = os.path.join(folder_path, filename) replace_category(file_path, target_category, new_category) folder_path = r'E:\cigarette_face-detection.v3i.yolov8\test\labels' target_category = 0 new_category = 2 modify_yolo_txt_files(folder_path, target_category, new_category)
修改class编号
最新推荐文章于 2025-04-03 19:17:28 发布