智算多多联系我们


关注我们

公众号

视频号
隐私协议用户协议
◎ 2025 北京智算多多科技有限公司版权所有京ICP备 2025150592号-1
优秀的自动化工具应该像瑞士军刀一样兼具功能完备和易用性。我们设计的脚本需要同时解决三个关键问题:
def main():
# 1. 初始化路径和参数
config = load_config()
# 2. 数据清洗阶段
clean_data(config)
# 3. 格式转换阶段
convert_labelme_to_yolo(config)
# 4. 数据集划分
split_dataset(config)
脚本设计采用模块化结构,每个功能独立成函数,方便后期维护和功能扩展。
原始数据中常混入未标注的图片,传统方法是人工筛选,我们的脚本通过自动配对机制解决:
def clean_data(config):
img_files = glob.glob(f"{config.input_dir}/*.{config.img_ext}")
for img_file in img_files:
json_file = img_file.replace(f".{config.img_ext}", ".json")
if os.path.exists(json_file): # 只有配对的图片才会被保留
shutil.copy(img_file, config.clean_dir)
shutil.copy(json_file, config.clean_dir)
文件处理流程:
Labelme使用像素坐标,而YOLO需要归一化的中心坐标,转换算法需要考虑边界情况:
def convert_bbox(size, box):
"""
将[xmin, xmax, ymin, ymax]转换为[x_center, y_center, width, height]
"""
dw, dh = 1. / size[0], 1. / size[1]
x = (box[0] + box[1]) / 2.0
y = (box[2] + box[3]) / 2.0
w = box[1] - box[0]
h = box[3] - box[2]
return (x * dw, y * dh, w * dw, h * dh)
注意:转换后的坐标值必须在0-1之间,脚本会自动处理越界情况。