xml转化yolo格式 txt 转换代码示例代码importxml.etree.ElementTree as ETimportpickleimportos from osimportlistdir, getcwd from os.pathimportjoinimportrandom from shutilimportcopyfile from PILimportImage只要改下面的CLASSES和PATH就可以了其他的不用改这个脚本会自动划分数据集生成YOLO格式的标签文件分类名称 这里改成数据集的分类名称一定要改请查看数据集目录下的txt文件CLASSES [“hat”, “person”]数据集目录 这里改成数据集的根目录根目录下有两个文件夹Annotations和JPEGImages一定要改PATH r’C:\Users\87018\Desktop\xml2txt’训练集占比80% 训练集:验证集8:2 这里划分数据集 不用改TRAIN_RATIO 80def clear_hidden_files(path): dir_listos.listdir(path)foriindir_list: abspathos.path.join(os.path.abspath(path), i)ifos.path.isfile(abspath):ifi.startswith(._): os.remove(abspath)else: clear_hidden_files(abspath)def convert(size, box): dw1. / size[0]dh1. / size[1]x(box[0] box[1])/2.0y(box[2] box[3])/2.0wbox[1]- box[0]hbox[3]- box[2]xx * dw ww * dw yy * dh hh * dhreturn(x, y, w, h)def convert_annotation(image_id):# Assuming the image format is jpgimage_pathos.path.join(image_dir, f{image_id}.jpg)imgImage.open(image_path)w, himg.size in_fileopen(PATH/Annotations/%s.xml% image_id,encodingutf-8)out_fileopen(PATH/YOLOLabels/%s.txt% image_id,w,encodingutf-8)treeET.parse(in_file)roottree.getroot()sizeroot.find(size)# w int(size.find(width).text)# h int(size.find(height).text)difficult0forobjinroot.iter(object):ifobj.find(difficult): difficultobj.find(difficult).text clsobj.find(name).textifcls notinCLASSES or int(difficult)1:continuecls_idCLASSES.index(cls)xmlboxobj.find(bndbox)b(float(xmlbox.find(xmin).text), float(xmlbox.find(xmax).text), float(xmlbox.find(ymin).text), float(xmlbox.find(ymax).text))bbconvert((w,h),b)out_file.write(str(cls_id) .join([str(a)for a in bb])\n)in_file.close()out_file.close()wdos.getcwd()wdos.getcwd()work_sapce_diros.path.join(wd,PATH/)annotation_diros.path.join(work_sapce_dir,Annotations/)if not os.path.isdir(annotation_dir):os.mkdir(annotation_dir)clear_hidden_files(annotation_dir)image_diros.path.join(work_sapce_dir,JPEGImages/)if not os.path.isdir(image_dir):os.mkdir(image_dir)clear_hidden_files(image_dir)yolo_labels_diros.path.join(work_sapce_dir,YOLOLabels/)if not os.path.isdir(yolo_labels_dir):os.mkdir(yolo_labels_dir)clear_hidden_files(yolo_labels_dir)yolov5_train_diros.path.join(work_sapce_dir,train/)if not os.path.isdir(yolov5_train_dir):os.mkdir(yolov5_train_dir)clear_hidden_files(yolov5_train_dir)yolov5_images_train_diros.path.join(yolov5_train_dir,images/)if not os.path.isdir(yolov5_images_train_dir):os.mkdir(yolov5_images_train_dir)clear_hidden_files(yolov5_images_train_dir)yolov5_labels_train_diros.path.join(yolov5_train_dir,labels/)if not os.path.isdir(yolov5_labels_train_dir):os.mkdir(yolov5_labels_train_dir)clear_hidden_files(yolov5_labels_train_dir)yolov5_test_diros.path.join(work_sapce_dir,val/)if not os.path.isdir(yolov5_test_dir):os.mkdir(yolov5_test_dir)clear_hidden_files(yolov5_test_dir)yolov5_images_test_diros.path.join(yolov5_test_dir,images/)if not os.path.isdir(yolov5_images_test_dir):os.mkdir(yolov5_images_test_dir)clear_hidden_files(yolov5_images_test_dir)yolov5_labels_test_diros.path.join(yolov5_test_dir,labels/)if not os.path.isdir(yolov5_labels_test_dir):os.mkdir(yolov5_labels_test_dir)clear_hidden_files(yolov5_labels_test_dir)train_fileopen(os.path.join(wd,yolov5_train.txt),w,encodingutf-8)test_fileopen(os.path.join(wd,yolov5_valid.txt),w,encodingutf-8)train_file.close()test_file.close()train_fileopen(os.path.join(wd,yolov5_train.txt),a,encodingutf-8)test_fileopen(os.path.join(wd,yolov5_valid.txt),a,encodingutf-8)list_imgsos.listdir(image_dir)# list image files probrandom.randint(1,100)print(数据集:%d个%len(list_imgs))foriinrange(0, len(list_imgs)): pathos.path.join(image_dir, list_imgs[i])ifos.path.isfile(path): image_pathimage_dir list_imgs[i]voc_pathlist_imgs[i](nameWithoutExtention, extention)os.path.splitext(os.path.basename(image_path))(voc_nameWithoutExtention, voc_extention)os.path.splitext(os.path.basename(voc_path))annotation_namenameWithoutExtention .xmlannotation_pathos.path.join(annotation_dir, annotation_name)label_namenameWithoutExtention .txtlabel_pathos.path.join(yolo_labels_dir, label_name)probrandom.randint(1,100)print(Probability: %d% prob, i, list_imgs[i])if(probTRAIN_RATIO):# train datasetifos.path.exists(annotation_path): train_file.write(image_path \n)convert_annotation(nameWithoutExtention)# convert labelcopyfile(image_path, yolov5_images_train_dir voc_path)copyfile(label_path, yolov5_labels_train_dir label_name)else:# test datasetifos.path.exists(annotation_path): test_file.write(image_path \n)convert_annotation(nameWithoutExtention)# convert labelcopyfile(image_path, yolov5_images_test_dir voc_path)copyfile(label_path, yolov5_labels_test_dir label_name)train_file.close()test_file.close()