把txt文件里的浮点数(float)四舍五入转换成整数(int)形式

本文介绍了如何在Yolov5_obb的旋转目标检测任务中,由于原始标注文件是浮点数格式,需要将用标注工具导出的数据进行四舍五入并转换为整数格式,以便与模型要求相匹配。
摘要由CSDN通过智能技术生成

       在做yolov5_obb旋转目标检测时,用到的txt文件里的数据是整数型,用标注工具导出来的数据是浮点数,所以我们需要把导出来的数据进行四舍五入换成整数(int)形式。

import os

# 获取文件夹中所有的txt文件
folder_path = "/home/zhang/file/yolov5_obb_master/train_bottle/train/labels"
txt_files = [file for file in os.listdir(folder_path) if file.endswith(".txt")]

# 遍历每个txt文件
for file in txt_files:
    file_path = os.path.join(folder_path, file)

    # 打开文件并读取内容
    with open(file_path, "r") as f:
        lines = f.readlines()

    # 对每行的数值进行四舍五入,确保可以转换为浮点数的部分
    rounded_lines = [str(round(float(coord.split()[0]))) + " " +
                     str(round(float(coord.split()[1]))) + " " +
                     str(round(float(coord.split()[2]))) + " " +
                     str(round(float(coord.split()[3]))) + " " +
                     str(round(float(coord.split()[4]))) + " " +
                     str(round(float(coord.split()[5]))) + " " +
                     str(round(float(coord.split()[6]))) + " " +
                     str(round(float(coord.split()[7]))) + " " +
                     coord.split()[8] + " " +
                     coord.split()[9] for coord in lines]

    # 将四舍五入后的内容写回文件
    with open(file_path, "w") as f:
        f.writelines('\n'.join(rounded_lines))

  • 11
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值