坐标x1y1x2y2转yolo标注格式

# -*- coding:utf-8 -*- 
#作用:
#将图片标注文件转化成yolo格式的txt标注文件
#
#
import sys 
import os 
import cv2
import random
data_base_dir = "/root/dyg/test_dir/annots/"
# data_base_dir = "/root/dyg/disc_data/annots" 
file_list = []

for file in os.listdir(data_base_dir): 
    if file.endswith(".txt"):
        # print(file)
        img_name = file[:-4]
        print(file) 
        # print(file[:-4])    #得到图片名,不带后缀
        imginfo = cv2.imread('/root/dyg/disc_data/imgs/'+  img_name + '.jpg').shape
        #h = shape[0]  w = shape[1]

        raw_file = open(data_base_dir+file)                    # 返回一个文件对象 
        print('raw_file is '+ data_base_dir +file)  
        new_file = open('/root/dyg/test_dir/result/'+file,'a+')
        line = raw_file.readline()               # 调用文件的 readline()方法   
        while line:   
            print(line)
            line = line.split(" ")
            print(line[1])
            # line[0] = float(line[0])
            x1 = float(line[1])
            print(x1)
            y1 = float(line[2])
            x2 = float(line[3])
            y2 = float(line[4])
            h = imginfo[0]
            w = imginfo[1]
            print('h== '+str(h))
            print('w== '+str(w))

            new_x = str((x1 + x2)/(2*w))
            new_y = str((y1 + y2)/(2*h))

            new_w = str((x2 - x1)/w)
            new_h = str((y2 - y1)/h)

            new_file.write(line[0]+' '+new_x+' '+new_y+' '+new_w+' '+new_h+'\n')
            print(line[0]+' '+new_x+' '+new_y+' '+new_w+' '+new_h+'\n')
            
            # print line                  # 后面跟 ',' 将忽略换行符   
            #print(line, end = '')       # 在 Python 3 中使用   
            line = raw_file.readline()      
new_file.close()
raw_file.close()
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值