制作自己的CTPN训练集
使用labelimg工具制作YOLO格式再将其转为CTPN中需要的8个坐标
1.标注框

2.代码生成坐标并保存
import cv2
import os
def change_labelimage_to_cptn_data(pictures_file_path, txt_file_path, cptn_data_labels_path):list = os.listdir(txt_file_path)for i in range(1, len(list)+1):txt_path = txt_file_path + str(i).zfill(6) + '.txt' #原数据集(label-image标注的)对应的txt标签存放路径dir = open(txt_path)lines = dir.readlines()lists = [] # 直接用一个数组存起来就好了for line in lines:lists.append(line.split())print(lists)im_path = pictures_file_path+str(i).zfill(6)+'.jpg' #原数据集图片存放路径picture = cv2.imread(im_path)for j in range(0, len(lists)):a = lists[j][1] #宽b = lists[j][2] #高c = lists[j][3] #宽度d = lists[j][4] #高度e = int((float(a) * picture.shape[1]) - (float(c) *int(picture.shape[1])/2))#x1f = int((float(b) * picture.shape[0]) - (float(d) * picture.shape[0]/2))#y1q = int((float(a) * picture.shape[1]) + (float(c) * picture.shape[1]/2))#x4s = int((float(b) * picture.shape[0]) + (float(d) * picture.shape[0]/2))#y4print(a,b,c,d,e,f,q,s)with open(cptn_data_labels_path + str(i).zfill(6)+'.txt', 'a+') as p:x2 = qy2 = fx3 = ey3 = sp.write(str(e) + ',' + str(f) + ',' + str(x2) + ',' + str(y2) + ',' + str(x3) + ',' + str(y3) + ',' + str(q) + ',' + str(s) + '\n')
if __name__ == '__main__':pictures_path = r'C:/Users/User/Desktop/2/' #图片路径txt_path = r'C:/Users/User/Desktop/2save/' #label-image标注的标签路径ctpn_labels = r'C:/Users/User/Desktop/CTPNSJJ/' #转换后,结果保存路径change_labelimage_to_cptn_data(pictures_path, txt_path, ctpn_labels)
3.结果













