supermachine--tomato-passio.../20240627test4/data_processing/name.py
TG 242bb9a71b feat:新增20240627test4为现场部署版本的测试版(包含传统方法、实例分割、目标检测、图像分类多个模型)
fix:修复在20240627test4中的classifier.py的analyze_tomato函数中white_defect的函数忘记传递两个阈值量的错误;修复analyze_tomato函数中的叶片实例分割存在的问题,解决由于变量污染引起的分割错误;
2024-07-21 22:17:46 +08:00

34 lines
1.3 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
import re
def natural_sort_key(s):
"""提取文本中的数字作为排序键"""
return [int(text) if text.isdigit() else text.lower() for text in re.split('(\d+)', s)]
def rename_bmp_images(folder_path, prefix, suffix):
# 获取文件夹中的所有文件
files = os.listdir(folder_path)
# 过滤出BMP图像文件并进行自然排序
bmp_files = sorted([f for f in files if f.lower().endswith('.bmp')], key=natural_sort_key)
# 对每个BMP图像文件进行重命名
for index, bmp_file in enumerate(bmp_files):
old_path = os.path.join(folder_path, bmp_file)
# 格式化新文件名例如1-1-1.bmp, 1-2-1.bmp, ...
new_name = f"{prefix}-{index + 1}-{suffix}.bmp"
new_path = os.path.join(folder_path, new_name)
# 重命名文件
os.rename(old_path, new_path)
print(f'Renamed {old_path} to {new_path}')
# 指定文件夹路径
folder_path = r'D:\project\20240714Actual_deployed\20240718test\T\bottom'
folder_path1 = r'D:\project\20240714Actual_deployed\20240718test\T\middle'
folder_path2 = r'D:\project\20240714Actual_deployed\20240718test\T\top'
num = '1'
# 调用函数进行重命名
rename_bmp_images(folder_path, prefix=num, suffix='1')
rename_bmp_images(folder_path1, prefix=num, suffix='2')
rename_bmp_images(folder_path2, prefix=num, suffix='3')