路径+左上左边+右下坐标+类别--------类别+归一化之后的坐标+宽高
-- coding:utf-8 --
作用:
将图片标注文件转化成yolo格式的txt标注文件
import sys
import os
import cv2
import random
def text_create(name1, msg1): #msg1是写进文件内的内容
#desktop_path = ‘C:/Users/Admin/Desktop/STUDY/train/’
#full_path = desktop_path + name1 + ‘.txt’
full_path = ‘C:/Users/Admin/Desktop/STUDY/train/result/’ + name1 + ‘.txt’ #文件存放
# print(full_path)
file = open(full_path, ‘a+’)
#msg1 = msg1.strip("\n")
for point in msg1:
#print(point[-1])
point = point.split(",")
#(point[-1])
if point[-1] == ‘0’:
a_0 = 0
center_x_0 = ((int(point[0]) + int(point[2])) / 2) / 1920
center_y_0 = ((int(point[1]) + int(point[3])) / 2) / 1080
w_0 = (int(point[2]) - int(point[0])) / 1920
y_0 = (int(point[3]) - int(point[1])) / 1080
line_0 = str(a_0) + ’ ’ + str(center_x_0) + ’ ’ + str(center_y_0) + ’ ’ + str(w_0) + ’ ’ + str(y_0)
# print(line_0)
file.write(line_0 + '\n')
if point[-1] == '1':
a_1 = 1
center_x_1 = ((int(point[0]) + int(point[2])) / 2) / 1920
center_y_1 = ((int(point[1]) + int(point[3])) / 2) / 1080
w_1 = (int(point[2]) - int(point[0])) / 1920
y_1 = (int(point[3]) - int(point[1])) / 1080
line_1 = str(a_1) + ' ' + str(center_x_1) + ' ' + str(center_y_1) + ' ' + str(w_1) + ' ' + str(y_1)
file.write(line_1 + '\n')
if point[-1] == '2':
a_2 = 2
center_x_2 = ((int(point[0]) + int(point[2])) / 2) / 1920
center_y_2 = ((int(point[1]) + int(point[3])) / 2) / 1080
w_2 = (int(point[2]) - int(point[0])) / 1920
y_2 = (int(point[3]) - int(point[1])) / 1080
line_2 = str(a_2) + ' ' + str(center_x_2) + ' ' + str(center_y_2) + ' ' + str(w_2) + ' ' + str(y_2)
file.write(line_2 + '\n')
file.close()
def wirte_txt(in_path):
with open(in_path) as F1:
for line in F1:
# print(line)
# print(‘111’)
a_line = line.strip(’\n’)
# print(a_line)
a_line = a_line.split(" ")
# print(a_line)
name = a_line[0].split(’.’)[0]
print(name)
msg = a_line[1:]
#print(msg)
text_create(name, msg)
if name == “main”:
in_path = ‘C:/Users/Admin/Desktop/STUDY/train/traincorrect.txt’ #输入的文件
wirte_txt(in_path)