目标识别数据集扩充_旋转扩充

扩充方法

原图

标签格式转化

标签xml转txt格式

1
2
3
4
5
6
7
8
9
10
def xml_to_txt(self,dw,dh,xmin,ymin ,xmax ,ymax):
x = (xmin+ xmax) / 2.0
y = (ymax + ymin) / 2.0
w = xmax- xmin
h = ymax - ymin
x = x /dw
w = w / dw
y = y / dh
h = h / dh
return (x, y, w, h)

txt转xml格式

其实就是个数学计算而已

1
2
3
4
5
def txt_to_xml(self,Pwidth,Pheight,x,y , wp, hp):
xmin = int((x * Pwidth + 1) - wp * 0.5 * Pwidth)
ymin = int((y * Pheight + 1) - hp * 0.5 * Pheight)
xmax = int((x* Pwidth + 1) + wp * 0.5 * Pwidth)
ymax = int((y * Pheight + 1) + hp * 0.5 * Pheight)

txt文件

txt文件的读写

1
2
3
4
5
6
7
8
9
def saveFlipLabel(txtpath,txt_save_path):
with open(txt_save_path, "w") as outfile:
with open(txtpath, "r") as infile:
for line in infile.readlines():
words = line.split(" ")
horizontal_coord = float(words[1])
outfile.write(
words[0] + " " + str(format(1 - horizontal_coord, ".6f")) + " " + words[2] + " " + words[3] + " " +
words[4])

txt文件的拷贝

1
2
def copyLabel(txtpath,txt_save_path):
shutil.copyfile(txtpath, txt_save_path)

main

1
2
3
4
5
6
7
8
if __name__ == '__main__':
img_aug = ImgAugemention()
imgs_path = './img/'
txt_path = './label/'
img_save_path = './images/'
txt_save_path = './labels/'
angle_list = [0,30,60, 90, 120, 150, 180, 270,330]
img_aug.process_img2(imgs_path, txt_path, img_save_path, txt_save_path)

主要作用是实现对数据的扩展,对整个文件夹内所有文件进行读取,一键扩展

1
2
3
4
5
6
7
8
9
10
11
12
13
#!/usr/bin/env python

import cv2
import math
import numpy as np
import os
import pdb
import xml.etree.ElementTree as ET
from Image_expansion import *

class ImgAugemention():
def __init__(self):
self.angle = 90

主要由ImgAugemention类和Image_expansion构成

数据扩展结果

源码

如果需源码请联系本人,以上传至git私人仓库。