当前位置:网站首页>【目标检测】小脚本:提取训练集图片与标签并更新索引
【目标检测】小脚本:提取训练集图片与标签并更新索引
2022-08-10 12:37:00 【zstar-_】
问题场景
在做目标检测任务时,我想提取训练集的图片单独进行外部数据增强。因此,需要根据划分出的train.txt
来提取训练集图片与标签。
需求实现
我使用VOC数据集进行测试,实现比较简单。
import shutil
if __name__ == '__main__':
img_src = r"D:\Dataset\VOC2007\images"
xml_src = r"D:\Dataset\VOC2007\Annotations"
img_out = "image_out/"
xml_out = "xml_out/"
txt_path = r"D:\Dataset\VOC2007\ImageSets\Segmentation\train.txt"
# 读取txt文件
with open(txt_path, 'r') as f:
line_list = f.readlines()
for line in line_list:
line_new = line.replace('\n', '') # 将换行符替换为空('')
shutil.copy(img_src + '/' + line_new + ".jpg", img_out)
shutil.copy(xml_src + '/' + line_new + ".xml", xml_out)
效果:
更新训练集索引
使用数据增强之后,把生成的图片和标签丢到VOC里面,混在一起。
然后再写个脚本,将生成好的图片名称添加到train.txt
文件中。
import os
if __name__ == '__main__':
xml_src = r"C:\Users\xy\Desktop\read_train\xml_out_af"
txt_path = r"D:\Dataset\VOC2007\ImageSets\Segmentation\train.txt"
for name in os.listdir(xml_src):
with open(txt_path, 'a') as f:
f.write(name[:-4] + "\n")
效果:
最后,再运行之前在VOC博文里面写过的xml2txt脚本:
import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import join
sets = ['train', 'test', 'val']
Imgpath = r'D:\Dataset\VOC2007\images' # 图片文件夹
xmlfilepath = r'D:\Dataset\VOC2007\Annotations' # xml文件存放地址
ImageSets_path = r'D:\Dataset\VOC2007\ImageSets\Segmentation'
Label_path = r'D:\Dataset\VOC2007'
classes = ['aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog',
'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor']
def convert(size, box):
dw = 1. / size[0]
dh = 1. / size[1]
x = (box[0] + box[1]) / 2.0
y = (box[2] + box[3]) / 2.0
w = box[1] - box[0]
h = box[3] - box[2]
x = x * dw
w = w * dw
y = y * dh
h = h * dh
return (x, y, w, h)
def convert_annotation(image_id):
in_file = open(xmlfilepath + '/%s.xml' % (image_id))
out_file = open(Label_path + '/labels/%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').text
cls = obj.find('name').text
if cls not in classes or int(difficult) == 1:
continue
cls_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')
for image_set in sets:
if not os.path.exists(Label_path + 'labels/'):
os.makedirs(Label_path + 'labels/')
image_ids = open(ImageSets_path + '/%s.txt' % (image_set)).read().strip().split()
list_file = open(Label_path + '%s.txt' % (image_set), 'w')
for image_id in image_ids:
# print(image_id) # DJI_0013_00360
list_file.write(Imgpath + '/%s.jpg\n' % (image_id))
convert_annotation(image_id)
list_file.close()
运行之后,就可以看到生成的数据增强样本被完美得添加到了原始数据集中。
边栏推荐
- 【百度统计】用户行为分析
- 10 款更先进的开源命令行工具
- 【黑马早报】雷军称低谷期曾想转行开酒吧;拜登正式签署芯片法案;软银二季度巨亏230亿美元;北京市消协约谈每日优鲜...
- Alibaba Cloud Jia Zhaohui: Cloud XR platform supports Bizhen Technology to present a virtual concert of national style sci-fi
- odps sql 不支持 unsupported feature CREATE TEMPORARY
- jenkins数据迁移和备份
- 想通这点,治好 AI 打工人的精神内耗
- G1和CMS的三色标记法及漏标问题
- 讯飞创意组别 全国选拔赛成绩公布说明
- Real-time data warehouse practice of Baidu user product flow and batch integration
猜你喜欢
Chapter9 : De Novo Molecular Design with Chemical Language Models
接口自动化测试基础篇
来看Prada大秀吗?在元宇宙里那种!
神经网络学习-正则化
线代 | 秒杀方法与技巧
shell:常用小工具(sort、uniq、tr、cut)
Open Office XML 格式里如何描述多段具有不同字体设置的段落
「网络架构」网络代理第一部分: 代理概述
ArcMAP has a problem of -15 and cannot be accessed [Provide your license server administrator with the following information:Err-15]
ABAP 里文件操作涉及到中文字符集的问题和解决方案试读版
随机推荐
DNS欺骗-教程详解
啥?他一个人写了个价值100万的软件,却用来开源了!
Codeforces Round #276 (Div. 1) D. Kindergarten
Polygon zkEVM工具——PIL和CIRCOM
47Haproxy Cluster
Codeforces Round #276 (Div. 1) B. Maximum Value
专有云ABC Stack,真正的实力派!
shell:正则表达式及三剑客grep命令
娄底干细胞制备实验室建设须知要求
ASP.NET Core依赖注入系统学习教程:ServiceDescriptor(服务注册描述类型)
虚拟机桥接模式不能上网
iTextSharp操作PDF
YTU 2295: KMP模式匹配 一(串)
Overseas media publicity. What problems should domestic media pay attention to?
10 款更先进的开源命令行工具
C# 当前上下文中不存在InitializeComponent()
Efficient and Robust 2D-to-BEV Representation Learning via Geometry-guided Kernel Transformer 论文笔记
[Advanced Digital IC Verification] Difference and focus analysis between SoC system verification and IP module verification
把相亲角搬到海外,不愧是咱爸妈
ArcMAP出现-15的问题无法访问[Provide your license server administrator with the following information:Err-15]