import numpy as np
path_txt_data = 'C:/Users/51102/Desktop/my_yolo/data/box/train.txt'
def input_data(path_txt_data): # 读取文件,其中文件格式为2.bmp 69,172,270,330,2 150,141,229,284,2 285,201,327,331,3 258,198,297,329,1
input_X_all = []
with open(path_txt_data, 'r') as f:
box_txt = f.readlines()
read_data_txt = [line.strip() for line in box_txt if len(line.strip().split()[1:]) != 0]
box_len = len(read_data_txt)
for j in range(box_len):
box_txt_one = read_data_txt[j].split()
box_txt_one = box_txt_one[1:]
box_txt_one_len = len(box_txt_one)
for i in range(box_txt_one_len):
box_txt_one_temp = np.array(list(map(int, box_txt_one[i].split(','))))[:4]
input_X_all.append(box_txt_one_temp)
input_X_all = np.array(input_X_all) # 将所有box变成二维矩阵,number * [x1,y1,x2,y2]
number_X, _ = input_X_all.shape
input_X_wh = np.zeros((number_X, 2))
input_X_wh[:, 0] = abs(input_X_all[:,0]-input_X_all[:, 2])
input_X_wh[:, 1] = abs(input_X_all[:, 1] - input_X_all[:, 3])
return input_X_wh
input=input_data(path_txt_data)
print(input)