目录地址需要放在:

D:\data\code\pythonProject

生成前的目录

生成后

代码如下:

import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import join
import random
from shutil import copyfileclasses = ["people", "head"]
# classes=["ball"]TRAIN_RATIO = 80def clear_hidden_files(path):dir_list = os.listdir(path)for i in dir_list:abspath = os.path.join(os.path.abspath(path), i)if os.path.isfile(abspath):if i.startswith("._"):os.remove(abspath)else:clear_hidden_files(abspath)def convert(size, box):dw = 1. / size[0]dh = 1. / size[1]x = (box[0] + box[1]) / 2.0y = (box[2] + box[3]) / 2.0w = box[1] - box[0]h = box[3] - box[2]x = x * dww = w * dwy = y * dhh = h * dhreturn (x, y, w, h)def convert_annotation(image_id):in_file = open('D:/data/code/pythonProject/VOCdevkit/VOC2007/Annotations/%s.xml' % image_id)out_file = open('D:/data/code/pythonProject/VOCdevkit/VOC2007/YOLOLabels/%s.txt' % image_id, 'w')tree = ET.parse(in_file)root = tree.getroot()size = root.find('size')w = int(size.find('width').text)h = int(size.find('height').text)for obj in root.iter('object'):difficult = obj.find('difficult').textcls = obj.find('name').textif cls not in classes or int(difficult) == 1:continuecls_id = classes.index(cls)xmlbox = obj.find('bndbox')b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),float(xmlbox.find('ymax').text))bb = convert((w, h), b)out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')in_file.close()out_file.close()wd = os.getcwd()
wd = os.getcwd()
data_base_dir = os.path.join(wd, "./")
if not os.path.isdir(data_base_dir):os.mkdir(data_base_dir)
work_sapce_dir = os.path.join(data_base_dir, "VOC2007/")
if not os.path.isdir(work_sapce_dir):os.mkdir(work_sapce_dir)
annotation_dir = os.path.join(work_sapce_dir, "Annotations/")
if not os.path.isdir(annotation_dir):os.mkdir(annotation_dir)
clear_hidden_files(annotation_dir)
image_dir = os.path.join(work_sapce_dir, "JPEGImages/")
if not os.path.isdir(image_dir):os.mkdir(image_dir)
clear_hidden_files(image_dir)
yolo_labels_dir = os.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_images_dir = os.path.join(data_base_dir, "images/")
if not os.path.isdir(yolov5_images_dir):os.mkdir(yolov5_images_dir)
clear_hidden_files(yolov5_images_dir)
yolov5_labels_dir = os.path.join(data_base_dir, "labels/")
if not os.path.isdir(yolov5_labels_dir):os.mkdir(yolov5_labels_dir)
clear_hidden_files(yolov5_labels_dir)
yolov5_images_train_dir = os.path.join(yolov5_images_dir, "train/")
if not os.path.isdir(yolov5_images_train_dir):os.mkdir(yolov5_images_train_dir)
clear_hidden_files(yolov5_images_train_dir)
yolov5_images_test_dir = os.path.join(yolov5_images_dir, "val/")
if not os.path.isdir(yolov5_images_test_dir):os.mkdir(yolov5_images_test_dir)
clear_hidden_files(yolov5_images_test_dir)
yolov5_labels_train_dir = os.path.join(yolov5_labels_dir, "train/")
if not os.path.isdir(yolov5_labels_train_dir):os.mkdir(yolov5_labels_train_dir)
clear_hidden_files(yolov5_labels_train_dir)
yolov5_labels_test_dir = os.path.join(yolov5_labels_dir, "val/")
if not os.path.isdir(yolov5_labels_test_dir):os.mkdir(yolov5_labels_test_dir)
clear_hidden_files(yolov5_labels_test_dir)train_file = open(os.path.join(wd, "VOC2007/yolov5_train.txt"), 'w')
test_file = open(os.path.join(wd, "VOC2007/yolov5_val.txt"), 'w')
train_file.close()
test_file.close()
train_file = open(os.path.join(wd, "VOC2007/yolov5_train.txt"), 'a')
test_file = open(os.path.join(wd, "VOC2007/yolov5_val.txt"), 'a')
list_imgs = os.listdir(image_dir)  # list image files
prob = random.randint(1, 100)
print("Probability: %d" % prob)
for i in range(0, len(list_imgs)):path = os.path.join(image_dir, list_imgs[i])if os.path.isfile(path):image_path = image_dir + list_imgs[i]voc_path = list_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_name = nameWithoutExtention + '.xml'annotation_path = os.path.join(annotation_dir, annotation_name)label_name = nameWithoutExtention + '.txt'label_path = os.path.join(yolo_labels_dir, label_name)prob = random.randint(1, 100)print("Probability: %d" % prob)if (prob < TRAIN_RATIO):  # train datasetif os.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 datasetif os.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()

yolov5手动标注的xml转txt相关推荐

  1. 【Python 实操】labelImg标注的xml格式转换为yolo的txt文件

    [Python 实操]labelImg标注的xml格式转换为yolo的txt文件 背景 YOLO家族开枝散叶(尤其是YOLOv5)的广泛应用就涉及到图像的标注与文件格式转换,常用的标注工具有 Labe ...

  2. python批量实现labelImg标注的 xml格式数据转换成 txt格式保存

    labelImg标注的 xml格式数据如下: 单个xml文件数据打开如下: python实现labelImg标注的 xml格式数据转换成 txt格式数据的代码xml2txt.py如下: # -*- c ...

  3. VOC数据集 XML 和 txt标注文本的理解

    VOC数据集 XML 和 txt标注文本的理解 以VOC数据集中VOC2007中的000001.jpg图片为例,分析下其对应的Annotations文件夹下的000001.xml文件和labels文件 ...

  4. win10使用roLabelimg(可标注旋转矩形)保存带有汉字的label及xml转txt(含文件)

    win10使用roLabelImg保存带有汉字的label及xml转txt(含文件) 简介 roLabelimg可以标注旋转矩形,虽不太好用,但比不能标注强太多太多,[转载请注明出处].https:/ ...

  5. YOLO的XML和TXT标注文本解释

    XML和TXT标注文本解释 转换函数 xml:左上角坐标和右下角坐标 转换为txt txt:中心点坐标(比例).宽(比例).高(比例) (0到1之间) 转换函数

  6. 用python将xml文件转换为txt文件_python代码xml转txt实例

    为了训练深度学习模型,经常要整理大量的标注数据,需统一不同格式的标注数据,一般情况下习惯读取TXT格式的数据.但实际中经常遇到XML格式的标注数据,在此举例:1.读取XML标注数据:2.写入TXT文件 ...

  7. xml 转 txt (亲测有效)

    代码使用说明 下图中只有 train_annotation(保存xml) .train_labels(保存txt) 两个文件夹在代码中调用,这里只是对训练集的 xml 进行 txt 转换的示例,如果想 ...

  8. yolo图像检测数据集格式转换:xml 与 txt格式相互转换

    格式介绍 一图流介绍的比较详细,一般图像检测数据集格式为txt或者xml格式,在使用labelimg进行标注的时候,可以设置获得不同格式的数据集,以满足不同算法训练格式要求: 一般建议使用pascal ...

  9. httpf发送 json_Java用HttpClient3发送http/https协议get/post请求,发送map,json,xml,txt数据...

    使用的是httpclient 3.1, 使用"httpclient"4的写法相对简单点,百度:httpclient https post 当不需要使用任何证书访问https网页时, ...

最新文章

  1. 【CentOS】如何在线安装pcre?
  2. Android的ADB工具使用
  3. linux显卡内核修改,iTOP-4412开发板-驱动-linux显卡驱动的修改方法
  4. 无需写try/catch,也能正常处理异常
  5. Java回调函数使用
  6. 手把手带你阅读Mybatis源码(一)构造篇
  7. Jquery的基础学习
  8. C# 获得当前应用程序路径
  9. Python:内置类型
  10. 数据库:码 属性 候选码 主码的关系
  11. 小白学习java连通、操作mysql数据库小记(更新中)
  12. 认知科学、神经科学、和认知神经科学
  13. linux proftpd 关闭匿名用户,Linux ProFTPd安装与卸载详细介绍_Linux_脚本之家
  14. (五)UE4常用材质制作
  15. VS2010安装Visual Assist X
  16. 基于8266WIFI模块实现智能手机与51单片机的通信入门
  17. The 10 Most Important Linux Commands/10个最经常使用的命令行
  18. android Ble4.0蓝牙开发之搜索慢、startLeScan()过时,6.0以上不需要定位权限也能快速搜索到蓝牙设备
  19. python爬虫入门:搜索和批量下载图片
  20. 高中计算机手抄报图片大全集,精美的高中手抄报设计图大全

热门文章

  1. 5、frida进阶-Android逆向之旅---Hook神器家族的Frida工具使用详解
  2. Linux 系统中如何查看日志 (常用命令)
  3. python基于PHP+MySQL的物流配送管理系统平台
  4. js网页导出excel表格文件
  5. 《重来》与去”完美主义“
  6. 关于Windows XP sp2下金山词霸2005取词BUG,桌面重启修正方法
  7. Java学习笔记-线程中countDown()使用
  8. 从公务员转行网络安全工程师,铁饭碗也比不过“金饽饽”。
  9. Unity入门 ---- unity2D基础知识
  10. SpringBoot的easyui实现导入和导出功能到excel