- 自定义制作样本数据集,包括图像增强旋转等。
- crsamples.py
- 北京世鼎科技发展有限公司出品
import os,time,sys,cv2
import numpy as np
from math import *
# from xml.etree.cElementTree import *
import xml.etree.ElementTree as ET
samples_path='data/dahuoji/samples'
txt_path='data/dahuoji'
# labels_path='data/dahuoji/labels'
# if not os.path.exists(labels_path):os.makedirs(labels_path)
classes = ["dahuoji"] # 我们只是检测细胞,因此只有一个类别
val_split = 0.1
xml_list=[]
def crexml(fileName,name='dahuoji',w=100,h=100,depth=3,xmin=1,ymin=1,xmax=100,ymax=100):
with open(fileName,encoding='utf-8',mode='w') as xml_file:
xml_file.write('<annotation>')
xml_file.write('<filename>'+fileName+'</filename>')
xml_file.write('<size><width>'+str(w)+'</width><height>'+str(h)+'</height><depth>'+str(depth)+'</depth></size>')
xml_file.write('<object><name>'+name+'</name><bndbox><xmin>'+str(xmin)+'</xmin><ymin>'+str(ymin)+'</ymin><xmax>'+str(xmax)+'</xmax><ymax>'+str(ymax)+'</ymax></bndbox></object>')
xml_file.write('</annotation>')
def exRotation(img,degree = 45):
height, width = img.shape[:2]
# 旋转后的尺寸
heightNew = int(width * fabs(sin(radians(degree))) + height * fabs(cos(radians(degree))))
widthNew = int(height * fabs(sin(radians(degree))) + width * fabs(cos(radians(degree))))
matRotation = cv2.getRotationMatrix2D((width / 2, height / 2), degree, 1)
matRotation[0, 2] += (widthNew - width) / 2 # 重点在这步,目前不懂为什么加这步
matRotation[1, 2] += (heightNew - height) / 2 # 重点在这步
imgRotation = cv2.warpAffine(img, matRotation, (widthNew, heightNew), borderValue=(255, 255, 255))
return imgRotation
def convert_annotation(image_id):
in_file = open(os.path.join(samples_path,image_id+'.xml'))
img_path=os.path.join(samples_path,image_id+'.jpg')
src=cv2.imread(img_path)
tree = ET.parse(in_file)
root = tree.getroot()
size = root.find('size')
w = int(size.find('width').text)
h = int(size.find('height').text)
i=0
for obj in root.iter('object'):
i+=1
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))
#######################################################################################################################################
img=src[int(b[2]):int(b[3]),int(b[0]):int(b[1])]
for c in range(45,360,45):
img_name='cinding-'+image_id+'-'+str(i)+'-'+str(c)+'.jpg'
exImage=exRotation(img,c)
cv2.imwrite(os.path.join(samples_path,img_name),exImage)
crexml(fileName=os.path.join(samples_path,'cinding-'+image_id+'-'+str(i)+'-'+str(c)+'.xml'),name=cls,w=exImage.shape[1],h=exImage.shape[0],xmax=exImage.shape[1],ymax=exImage.shape[0])
cb=(0.0,float(exImage.shape[1]),0.0,float(exImage.shape[0]))
cbb=convert((exImage.shape[1],exImage.shape[0]),cb)
with open(os.path.join(samples_path,img_name.replace('.jpg','.txt')),encoding='utf-8',mode='w') as out_file:
out_file.write(str(cls_id) + " " + " ".join([str(a) for a in cbb]) + '\n')
#######################################################################################################################################
bb = convert((w, h), b)
with open(os.path.join(samples_path,image_id+'.txt'),encoding='utf-8',mode='a') as out_file:
out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
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)
for name in os.listdir(samples_path):
if name.endswith('.txt'):
if os.path.exists(os.path.join(samples_path,name)):os.remove(os.path.join(samples_path,name))
if name.startswith('cinding-'):
if os.path.exists(os.path.join(samples_path,name)):os.remove(os.path.join(samples_path,name))
for name in os.listdir(samples_path):
if not name.endswith('.xml'):
continue
convert_annotation(name.replace('.xml',''))
print('create convert_annotation is ',name)
for name in os.listdir(samples_path):
if not name.endswith('.xml'):
continue
xml_list.append(os.path.join(samples_path, name.replace('.xml', '.jpg')))
# np.random.seed(10101)
np.random.shuffle(xml_list)
num_val = int(len(xml_list) * val_split)
num_train = len(xml_list) - num_val
train_data=xml_list[:num_train]
val_data=xml_list[num_train:]
if os.path.exists(os.path.join(txt_path,'train.txt')):os.remove(os.path.join(txt_path,'train.txt'))
if os.path.exists(os.path.join(txt_path,'val.txt')):os.remove(os.path.join(txt_path,'val.txt'))
for line in train_data:
with open(os.path.join(txt_path,'train.txt'),encoding='utf-8',mode='a') as train_txt:
train_txt.write(line+'\n')
for line in val_data:
with open(os.path.join(txt_path,'val.txt'),encoding='utf-8',mode='a') as val_txt:
val_txt.write(line+'\n')
print('samples lens is',len(xml_list),'train_data lens is ',len(train_data),'val_data lens is',len(val_data))
网友评论