尧图建网站 尧图建网站 YAOTU WEB BUILD 免费咨询
ARTICLE DETAIL

资讯详情

深耕网站建设与建站编程的一线实战洞察。

数据集格式转化 xml转换txt xml转换txt 转换代码示例参考 VOC(xml)格式如何转换yolo(txt )格式 (1)

数据集格式转化 xml转换txt xml转换txt 转换代码示例参考 VOC(xml)格式如何转换yolo(txt )格式 (1) 如何三图 一套代码 来进行xml转换txt 转换代码示例参考。VOCxml格式如何转换yolotxt 格式 的转换首先你得有数据集吧。然后先进行数据集的标注在分类到训练集 测试集 验证集。最起码要搞个yaml,里面路径和yaml搞好对不。然后训练不会出现路径报错类似于这样的问题对不对答疑解惑同学经常问的问题另外VOC格式XML和YOLO(txt)格式可以互转,不要忘记了还有YOLO数据集通用YOLOV5-YOLOV12都可以。另外呢附一个代码让你知道如何XML格式转换TXT的yolo格式。这里以 帽子和人 CLASSES [“hat”, “person”] 两类数据集作为示例如何转换格式**第一步**第二步这里假设hat person 是smoke 和 fire第三步修改路径importxml.etree.ElementTree as ETimportpickleimportos from osimportlistdir, getcwd from os.pathimportjoinimportrandom from shutilimportcopyfile from PILimportImage只要改下面的CLASSES和PATH就可以了其他的不用改这个脚本会自动划分数据集生成YOLO格式的标签文件分类名称 这里改成数据集的分类名称一定要改请查看数据集目录下的txt文件CLASSES[hat,person]数据集目录 这里改成数据集的根目录根目录下有两个文件夹Annotations和JPEGImages一定要改PATHrC:\Users\87018\Desktop\xml2txt训练集占比80% 训练集:验证集8:2 这里划分数据集 不用改TRAIN_RATIO80def 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()
返回列表