(image_dir,txt_dir,image_save_dir, xml_save_dir,n)
:param image_dir:
    :param txt_dir:
    :param image_save_dir:
    :param xml_save_dir:
    :param n:增强次数
    :return:
:param image_dir:
    :param txt_dir:
    :param image_save_dir:
    :param xml_save_dir:
    :param n:增强次数
    :return:
: param image_dir : : param txt_dir : : param image_save_dir : : param xml_save_dir : : param n : 增强次数 : return :
def aug_data(image_dir,txt_dir,image_save_dir, xml_save_dir,n):
    """
    :param image_dir:
    :param txt_dir:
    :param image_save_dir:
    :param xml_save_dir:
    :param n:增强次数
    :return:
    """
    files = getFiles(txt_dir)  # 所有txt
    files = [file for file in files if file.endswith('ellipseList.txt')]  # 过滤出annotation文件
    for file in files:
        print(file)
        txt_path = os.path.join(txt_dir, file)
        with open(txt_path, 'r') as f:
            while True:
                line = f.readline()
                if line == '':
                    break
                image_dir_tmp = line.strip()
                print(">>>{} is being processed...".format(image_dir_tmp))
                new_name = '_'.join(image_dir_tmp.split('/'))
                image_path = os.path.join(image_dir, image_dir_tmp + '.jpg')
                im = Image.open(image_path)
                (w, h) = im.size
                d = 3
                center = (w//2,h//2)
                line = f.readline().strip()
                num = int(line)
                box_list = []
                for i in range(num):
                    line = f.readline().strip()
                    data = [float(val) for val in line.split(' ') if val != '']
                    coord = convert_coord(data)
                    box_list.append(coord)
                ii = 0
                while ii < n:
                    angle = random.randint(1,359)
                    rotate_matrix = np.array([
                        [np.cos(angle * np.pi / 180), np.sin(angle * np.pi / 180)],
                        [-np.sin(angle * np.pi / 180), np.cos(angle * np.pi / 180)]
                    ])
                    box_list_new = rotateBox(box_list,rotate_matrix,h,w)
                    if len(box_list_new) == 0:
                        continue
                    ii += 1
                    new_im = im.rotate(angle, center=center)
                    new_im.save(os.path.join(image_save_dir,'{}_{}.jpg'.format(new_name,angle)))
                    WriterXMLFiles('{}_{}.xml'.format(new_name,angle), xml_save_dir, box_list_new, w, h, d)
def aug_data ( image_dir , txt_dir , image_save_dir , xml_save_dir , n ) : files = getFiles ( txt_dir ) # 所有txt files = [ file for file in files if file . endswith ( 'ellipseList.txt' ) ] # 过滤出annotation文件 for file in files : print ( file ) txt_path = os . path . join ( txt_dir , file ) with open ( txt_path , 'r' ) as f : while True : line = f . readline ( ) if line == '' : break image_dir_tmp = line . strip ( ) print ( ">>>{} is being processed..." . format ( image_dir_tmp ) ) new_name = '_' . join ( image_dir_tmp . split ( '/' ) ) image_path = os . path . join ( image_dir , image_dir_tmp + '.jpg' ) im = Image . open ( image_path ) ( w , h ) = im . size d = 3 center = ( w // 2 , h // 2 ) line = f . readline ( ) . strip ( ) num = int ( line ) box_list = [ ] for i in range ( num ) : line = f . readline ( ) . strip ( ) data = [ float ( val ) for val in line . split ( ' ' ) if val != '' ] coord = convert_coord ( data ) box_list . append ( coord ) ii = 0 while ii < n : angle = random . randint ( 1 , 359 ) rotate_matrix = np . array ( [ [ np . cos ( angle * np . pi / 180 ) , np . sin ( angle * np . pi / 180 ) ] , [ - np . sin ( angle * np . pi / 180 ) , np . cos ( angle * np . pi / 180 ) ] ] ) box_list_new = rotateBox ( box_list , rotate_matrix , h , w ) if len ( box_list_new ) == 0 : continue ii += 1 new_im = im . rotate ( angle , center = center ) new_im . save ( os . path . join ( image_save_dir , '{}_{}.jpg' . format ( new_name , angle ) ) ) WriterXMLFiles ( '{}_{}.xml' . format ( new_name , angle ) , xml_save_dir , box_list_new , w , h , d )