[数据集处理]数据增强2

在[数据集处理]数据增强1方法上的提升,灵感来自yolo4的马赛克图像拼接方法的数据增强,该数据增强方法带来了以下优点:

(1)变相增加了bach_size;

(2)增加了背景复杂度;

(3)获得了多尺度的目标;

。。。。。。

import cv2
import os
import sys
import re
import xml.etree.ElementTree as ET
from PIL import Image


imgreadpath = './img/'
imgwritepath  = './f_img/'

xmlreadpath = './xml/'
xmlwritepath = './f_xml/'

allimgreadpath = './allimg/'
ximgwritepath = './ximg/'

allxmlreadpath = './allxml/'
xxmlwritepath = './xxml/'


def flitimg(imgname):
	image = cv2.imread(imgname)
	name = imgname.split('/')[-1].split('.')[-2]
	image_f = cv2.flip(image, 1)  #1:水平翻转
	cv2.imwrite(imgwritepath + 'f' + name + '.jpg',image_f)

def flitxml(xmlname):

	bwidth = ''
	bheight = ''
	bdepth = ''

	text=open(xmlname).read()
	f_test = open(xmlwritepath + 'f' + xmlname.split("/")[-1].split('.')[-2] + '.xml', 'w')

	text=re.sub(u"[\x00-\x08\x0b-\x0c\x0e-\x1f]+",u"",text)
	root=ET.fromstring(text)

	if root.findtext('folder') == ' ':
		print("no folder filename:",filename)
		print('A')
	else:
		folder = root.findtext('folder')
	
	if root.findtext('filename') == ' ':
		print("no filename filename:",filename)
		print('B')
	else:
		filename = root.findtext('filename')

	# if root.findall('size') == []:
	# 	print("no size filename:",filename)
	# 	print('C')
	# else:
	# 	size = root.findall('size')
	# 	print(size)
	# 	size = size[0]
	# 	bwidth = size.findtext('width')
	# 	bheight = size.findtext('height')
	# 	bdepth = size.findtext('depth')
	img = Image.open(imgwritepath + 'f' + xmlname.split("/")[-1].split('.')[-2] + '.jpg')
	bwidth, bheight = img.size
	bdepth  =3

	f_test.write('\n')
	f_test.write('	' + folder + '\n')
	f_test.write('	' + filename + '\n')
	f_test.write('	\n')
	f_test.write('		' + str(bwidth) + '\n')
	f_test.write('		' + str(bheight) + '\n')
	f_test.write('		' + str(bdepth) + '\n')
	f_test.write('	\n')

	if root.findall('object') == []:
		print("no object filename:",filename)
	else:
		for object in root.findall('object'):
			label = object.findtext('name')
			x1 = object.findtext('bndbox/xmin')
			y1 = object.findtext('bndbox/ymin')
			x2 = object.findtext('bndbox/xmax')
			y2 = object.findtext('bndbox/ymax')

			f_test.write('	\n')
			f_test.write('		' + label + '\n')
			f_test.write('		\n')
			f_test.write('			' + str(int(bwidth) - int(x1) - (int(x2) - int(x1))) + '\n')
			f_test.write('			' + y1 + '\n')
			f_test.write('			' + str(int(bwidth) - int(x2) + (int(x2) - int(x1))) + '\n')
			f_test.write('			' + y2 + '\n')
			f_test.write('		\n')
			f_test.write('	\n')

	f_test.write('\n')
	f_test.close()


def img4to1(image_names):
	# image_names 是一个含有四个图片名的list
	to_image = Image.new('RGB', (IMAGE_COLUMN * IMAGE_SIZE, IMAGE_ROW * IMAGE_SIZE))
	for y in range(1, IMAGE_ROW + 1):
		for x in range(1, IMAGE_COLUMN + 1):
			from_image = Image.open(allimgreadpath + image_names[IMAGE_COLUMN * (y - 1) + x - 1]).resize((IMAGE_SIZE, IMAGE_SIZE),Image.ANTIALIAS)
			to_image.paste(from_image, ((x - 1) * IMAGE_SIZE, (y - 1) * IMAGE_SIZE))
	to_image.save(ximgwritepath + 'ximg' + str(NUM) + '.jpg')
	return 'ximg' + str(NUM) + '.jpg'


def xml4to1(image_names,xxmlname):
	f_test = open(xxmlwritepath + xxmlname.split('.')[-2] + '.xml', 'w')

	f_test.write('\n')
	f_test.write('	' + ximgwritepath + '\n')
	f_test.write('	' + xxmlname + '\n')
	f_test.write('	\n')
	f_test.write('		' + str(IMAGE_COLUMN*IMAGE_SIZE) + '\n')
	f_test.write('		' + str(IMAGE_ROW*IMAGE_SIZE) + '\n')
	f_test.write('		' + str(3) + '\n')
	f_test.write('	\n')

	for i in range(len(image_names)):
		if i == 0:
			img = Image.open(allimgreadpath + image_names[i])
			width, height = img.size
			scalewidth = IMAGE_SIZE/width
			scaleheight = IMAGE_SIZE/height

			text=open(allxmlreadpath + image_names[i].split('.')[-2] + '.xml').read()
			text=re.sub(u"[\x00-\x08\x0b-\x0c\x0e-\x1f]+",u"",text)
			root=ET.fromstring(text)

			if root.findall('object') == []:
				print("no object filename:",filename)
			else:
				for object in root.findall('object'):
					label = object.findtext('name')
					x1 = object.findtext('bndbox/xmin')
					y1 = object.findtext('bndbox/ymin')
					x2 = object.findtext('bndbox/xmax')
					y2 = object.findtext('bndbox/ymax')

					newx1 = int(int(x1)*scalewidth)
					newy1 = int(int(y1)*scaleheight)
					newx2 = int(int(x2)*scalewidth)
					newy2 = int(int(y2)*scaleheight)

					# if (newx2-newx1)>MINISCALE and (newy2-newy1)>MINISCALE:
					if (newy2-newy1)>MINISCALE:
						f_test.write('	\n')
						f_test.write('		' + label + '\n')
						f_test.write('		\n')
						f_test.write('			' + str(newx1) + '\n')
						f_test.write('			' + str(newy1) + '\n')
						f_test.write('			' + str(newx2) + '\n')
						f_test.write('			' + str(newy2) + '\n')
						f_test.write('		\n')
						f_test.write('	\n')
					else:
						continue

		elif i == 1:
			img = Image.open(allimgreadpath + image_names[i])
			width, height = img.size
			scalewidth = IMAGE_SIZE/width
			scaleheight = IMAGE_SIZE/height

			text=open(allxmlreadpath + image_names[i].split('.')[-2] + '.xml').read()
			text=re.sub(u"[\x00-\x08\x0b-\x0c\x0e-\x1f]+",u"",text)
			root=ET.fromstring(text)

			if root.findall('object') == []:
				print("no object filename:",filename)
			else:
				for object in root.findall('object'):
					label = object.findtext('name')
					x1 = object.findtext('bndbox/xmin')
					y1 = object.findtext('bndbox/ymin')
					x2 = object.findtext('bndbox/xmax')
					y2 = object.findtext('bndbox/ymax')

					newx1 = int(int(x1)*scalewidth)
					newy1 = int(int(y1)*scaleheight)
					newx2 = int(int(x2)*scalewidth)
					newy2 = int(int(y2)*scaleheight)

					# if (newx2-newx1)>MINISCALE and (newy2-newy1)>MINISCALE:
					if (newy2-newy1)>MINISCALE:
						f_test.write('	\n')
						f_test.write('		' + label + '\n')
						f_test.write('		\n')
						f_test.write('			' + str(IMAGE_SIZE + newx1) + '\n')
						f_test.write('			' + str(newy1) + '\n')
						f_test.write('			' + str(IMAGE_SIZE + newx2) + '\n')
						f_test.write('			' + str(newy2) + '\n')
						f_test.write('		\n')
						f_test.write('	\n')
					else:
						continue

		elif i == 2:
			img = Image.open(allimgreadpath + image_names[i])
			width, height = img.size
			scalewidth = IMAGE_SIZE/width
			scaleheight = IMAGE_SIZE/height

			text=open(allxmlreadpath + image_names[i].split('.')[-2] + '.xml').read()
			text=re.sub(u"[\x00-\x08\x0b-\x0c\x0e-\x1f]+",u"",text)
			root=ET.fromstring(text)

			if root.findall('object') == []:
				print("no object filename:",filename)
			else:
				for object in root.findall('object'):
					label = object.findtext('name')
					x1 = object.findtext('bndbox/xmin')
					y1 = object.findtext('bndbox/ymin')
					x2 = object.findtext('bndbox/xmax')
					y2 = object.findtext('bndbox/ymax')

					newx1 = int(int(x1)*scalewidth)
					newy1 = int(int(y1)*scaleheight)
					newx2 = int(int(x2)*scalewidth)
					newy2 = int(int(y2)*scaleheight)

					# if (newx2-newx1)>MINISCALE and (newy2-newy1)>MINISCALE:
					if (newy2-newy1)>MINISCALE:
						f_test.write('	\n')
						f_test.write('		' + label + '\n')
						f_test.write('		\n')
						f_test.write('			' + str(newx1) + '\n')
						f_test.write('			' + str(IMAGE_SIZE + newy1) + '\n')
						f_test.write('			' + str(newx2) + '\n')
						f_test.write('			' + str(IMAGE_SIZE + newy2) + '\n')
						f_test.write('		\n')
						f_test.write('	\n')
					else:
						continue

		elif i == 3:
			img = Image.open(allimgreadpath + image_names[i])
			width, height = img.size
			scalewidth = IMAGE_SIZE/width
			scaleheight = IMAGE_SIZE/height

			text=open(allxmlreadpath + image_names[i].split('.')[-2] + '.xml').read()
			text=re.sub(u"[\x00-\x08\x0b-\x0c\x0e-\x1f]+",u"",text)
			root=ET.fromstring(text)

			if root.findall('object') == []:
				print("no object filename:",filename)
			else:
				for object in root.findall('object'):
					label = object.findtext('name')
					x1 = object.findtext('bndbox/xmin')
					y1 = object.findtext('bndbox/ymin')
					x2 = object.findtext('bndbox/xmax')
					y2 = object.findtext('bndbox/ymax')

					newx1 = int(int(x1)*scalewidth)
					newy1 = int(int(y1)*scaleheight)
					newx2 = int(int(x2)*scalewidth)
					newy2 = int(int(y2)*scaleheight)

					# if (newx2-newx1)>MINISCALE and (newy2-newy1)>MINISCALE:
					if (newy2-newy1)>MINISCALE:
						f_test.write('	\n')
						f_test.write('		' + label + '\n')
						f_test.write('		\n')
						f_test.write('			' + str(IMAGE_SIZE + newx1) + '\n')
						f_test.write('			' + str(IMAGE_SIZE + newy1) + '\n')
						f_test.write('			' + str(IMAGE_SIZE + newx2) + '\n')
						f_test.write('			' + str(IMAGE_SIZE + newy2) + '\n')
						f_test.write('		\n')
						f_test.write('	\n')
					else:
						continue
	f_test.write('\n')
	f_test.close()

if __name__ == '__main__':

	# # 图片镜像翻转
	# imgnames = os.listdir(imgreadpath)
	# for imgname in imgnames:
	# 	imgname = imgreadpath + imgname
	# 	flitimg(imgname)

	# # 图片镜像翻转中对应的xml标注内容对应翻转
	# xmlnames = os.listdir(xmlreadpath)
	# for xmlname in xmlnames:
	# 	xmlname = xmlreadpath + xmlname
	# 	flitxml(xmlname)

	# # 图片4合1 对应的xml标注内容同时4合1
	allimgnames = os.listdir(allimgreadpath)
	NUM = 0
	IMAGE_ROW = 2
	IMAGE_COLUMN = 2
	IMAGE_SIZE = 320
	MINISCALE = 10
	for image_names in [allimgnames[i:i+4] for i in range(0,len(allimgnames),4)]:
		NUM += 1
		xxmlname = img4to1(image_names)
		print(xxmlname)
		xml4to1(image_names,xxmlname)

原创脚本,转载请注明出处

希望对你有所帮助

(•̀ᴗ•́)و ̑̑点赞

你可能感兴趣的:(pytorch)