首页 > 其他分享 >41、labelimg数据集转dota以及rolabelimg、labelimg数据集旋转水平镜像 垂直镜像 和水平垂直镜像

41、labelimg数据集转dota以及rolabelimg、labelimg数据集旋转水平镜像 垂直镜像 和水平垂直镜像

时间:2022-09-28 11:38:07浏览次数:58  
标签:appendChild doc labelimg createElement 垂直 text 镜像 path find


基本思想:为了给BBAVectors-Oriented-Object-Detection 提供数据,所以记录一下操作,搞了旋转,但是旋转影响图片大小,所以先写个镜像方法,进行训练和使扩充数据集

一、labelimg转dota数据集 ,并做了顺时针排序处理 

import json
import os
from glob import glob
import argparse
import numpy as np
import shutil
from PIL import Image


# convert labelme json to DOTA txt format
# convert DOTA json to lableme txt format
def custombasename(fullname):
return os.path.basename(os.path.splitext(fullname)[0])


def order_points_new(pts): # clock -https://zhuanlan.zhihu.com/p/10643062
# sort the points based on their x-coordinates
xSorted = pts[np.argsort(pts[:, 0]), :]

# grab the left-most and right-most points from the sorted
# x-roodinate points
leftMost = xSorted[:2, :]
rightMost = xSorted[2:, :]
if leftMost[0,1]!=leftMost[1,1]:
leftMost=leftMost[np.argsort(leftMost[:,1]),:]
else:
leftMost=leftMost[np.argsort(leftMost[:,0])[::-1],:]
(tl, bl) = leftMost
if rightMost[0,1]!=rightMost[1,1]:
rightMost=rightMost[np.argsort(rightMost[:,1]),:]
else:
rightMost=rightMost[np.argsort(rightMost[:,0])[::-1],:]
(tr,br)=rightMost
# return the coordinates in top-left, top-right,
# bottom-right, and bottom-left order
return np.array([tl, tr, br, bl], dtype="float32")


parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument('--input_dir', default=r'C:\Users\PHILIPS\Desktop\source',type=str,help='input annotated directory')
parser.add_argument('--output_dir', default=r'C:\Users\PHILIPS\Desktop\dest', type=str, help='output dataset directory')
parser.add_argument('--verify_dir', default=r'C:\Users\PHILIPS\Desktop\verify', type=str, help='verify dataset directory')
parser.add_argument('--verify', default=True,type=bool,help='verify')
parser.add_argument('--labels', default=r'labels.txt',type=str, help='labels annotated directory')
args = parser.parse_args()

if not os.path.exists(args.output_dir):
os.makedirs(args.output_dir)

print('Creating dataset:', args.output_dir)


file_list = glob(os.path.join(args.input_dir , ".".join(["*","json"])))

for i in range(len(file_list)):
with open(file_list[i]) as f:
label_str = f.read()
label_dict = json.loads(label_str) # json文件读入dict

# 输出 txt 文件的路径
out_file = os.path.join(args.output_dir ,".".join([ custombasename(file_list[i]), 'txt']))
# 写入 poly 四点坐标 和 label
fout = open(out_file, 'w')
out_str = ''
#np.array(box, dtype="int")
for shape_dict in label_dict['shapes']:
points = shape_dict['points']
item_points = []
for p in points:
item_points.append([p[0],p[1]])
item_points=order_points_new(np.array(item_points, dtype="float"))
for p in item_points.tolist():
out_str += (str(p[0]) + ' ' + str(p[1]) + ' ')
out_str += shape_dict['label'] + ' 0\n'
fout.write(out_str)
fout.close()
print('%d/%d' % (i + 1, len(file_list)))
print("labelme2dota...")
if args.verify:
if not os.path.exists(args.verify_dir):
os.makedirs(args.verify_dir)
txt_list = glob(os.path.join(args.output_dir, ".".join(["*", "txt"])))
for i in range(len(txt_list)):
(filepath, tempfilename) = os.path.split(txt_list[i])
(filename, extension) = os.path.splitext(tempfilename)
sourcePath=None
image_filename=None
if os.path.exists(os.path.join(args.input_dir, ".".join([filename, "jpg"]))):
sourcePath = os.path.join(args.input_dir, ".".join([filename, "jpg"]))
image_filename=".".join([filename, "jpg"])
elif os.path.exists(os.path.join(args.input_dir, ".".join([filename, "png"]))):
sourcePath = os.path.join(args.input_dir, ".".join([filename, "png"]))
image_filename = ".".join([filename, "png"])
if sourcePath is None:
print("check photo type")
continue
targetPath = args.verify_dir
shutil.copy(sourcePath, targetPath)
img= Image.open(sourcePath)
imgSize = img.size # 大小/尺寸
w = img.width # 图片的宽
h = img.height # 图片的高

data = {}
data['imagePath'] = image_filename
data['flags'] = {}
data['imageWidth'] = w
data['imageHeight'] = h
data['imageData'] = None
data['version'] = "5.0.1"
data["shapes"] = []

with open(txt_list[i]) as f:
label_str = f.readlines()
for label_item in label_str:
line_char = label_item.split("\n")[0].split(' ')
points = [[eval(line_char[0]), eval(line_char[1])], [eval(line_char[2]), eval(line_char[3])],[eval(line_char[4]), eval(line_char[5])],[eval(line_char[6]), eval(line_char[7])]]
itemData = {'points': []}
itemData['points'].extend(points)
itemData["flag"] = {}
itemData["group_id"] = None
itemData["shape_type"] = "polygon"
itemData["label"] = line_char[-2]
data["shapes"].append(itemData)

jsonName = ".".join([filename, "json"])
jsonPath = os.path.join(targetPath, jsonName)
with open(jsonPath, "w") as f:
json.dump(data, f)
print(jsonName)
print("dota2labelme...")

robelimg数据集扩充

import numpy as np
import os
import cv2
import math
from xml.dom.minidom import Document
from xml.etree.ElementTree import parse, Element

dir_path = r'G:\1\high_black_svg'
sv_path = r"G:\1\black_total" # 修改后的xml文件存放路径
if not os.path.exists(sv_path):
os.mkdir(sv_path)
list_mirror=[1,0,-1] #水平镜像 垂直镜像 水平垂直翻转 0
file_ext="jpg"
for mirror in list_mirror:
for File in os.listdir(dir_path):
if File.endswith('.xml'):
print(File)
xml_path = os.path.join(dir_path, File)
dom = parse(xml_path) ###最核心的部分,路径拼接,输入的是具体路径
root = dom.getroot()
folder_text=root.find("folder").text
filename_text = root.find("filename").text
filename_text,_=os.path.splitext(filename_text)
xml_name_=".".join([filename_text+str(mirror),"xml"])
save_path=os.path.join(sv_path,xml_name_)
img_name_=".".join([filename_text+str(mirror) , file_ext])
save_image = os.path.join(sv_path,img_name_ )
path_text = root.find("path").text
source_text = root.find("source").find("database").text
width_text = root.find("size").find("width").text
height_text = root.find("size").find("height").text
depth_text = root.find("size").find("depth").text
segmented_text=root.find("segmented").text
(filename, extension) = os.path.splitext(File)
img_jpg = ".".join([filename,file_ext])
img_path = os.path.join(dir_path, img_jpg)
image=cv2.imread(img_path)
doc = Document() # 创建DOM文档对象
DOCUMENT = doc.createElement('annotation') # 创建根元素

folder = doc.createElement('folder') ##建立自己的开头
folder_text = doc.createTextNode(folder_text) ##建立自己的文本信息
folder.appendChild(folder_text) ##自己的内容
DOCUMENT.appendChild(folder)
doc.appendChild(DOCUMENT)

filename = doc.createElement('filename')
filename_text = doc.createTextNode(filename_text+str(mirror))
filename.appendChild(filename_text)
DOCUMENT.appendChild(filename)
doc.appendChild(DOCUMENT)

path_ = doc.createElement('path')
path_text = doc.createTextNode(img_name_)
path_.appendChild(path_text)
DOCUMENT.appendChild(path_)
doc.appendChild(DOCUMENT)

source = doc.createElement('source')
database = doc.createElement('database')
database_text = doc.createTextNode(source_text) # 元素内容写入
database.appendChild(database_text)
source.appendChild(database)
DOCUMENT.appendChild(source)
doc.appendChild(DOCUMENT)

size = doc.createElement('size')
width = doc.createElement('width')
width_text_ = doc.createTextNode(width_text) # 元素内容写入
width.appendChild(width_text_)
size.appendChild(width)

height = doc.createElement('height')
height_text_ = doc.createTextNode(height_text)
height.appendChild(height_text_)
size.appendChild(height)

depth = doc.createElement('depth')
depth_text = doc.createTextNode(depth_text)
depth.appendChild(depth_text)
size.appendChild(depth)

DOCUMENT.appendChild(size)

segmented = doc.createElement('segmented')
segmented_text = doc.createTextNode(segmented_text)
segmented.appendChild(segmented_text)
DOCUMENT.appendChild(segmented)
doc.appendChild(DOCUMENT)


for ano in root.iter('annotation'): # 获取object节点中的name子节点

for obj in ano.iter("object"):
type_text=obj.find('type').text
name_text = obj.find('name').text
pose_text = obj.find('pose').text
truncated_text = obj.find('truncated').text
difficult_text = obj.find('difficult').text

object = doc.createElement('object')
type = doc.createElement('type')
type_text = doc.createTextNode(type_text)
type.appendChild(type_text)
object.appendChild(type)

name = doc.createElement('name')
name_text = doc.createTextNode(name_text)
name.appendChild(name_text)
object.appendChild(name)

pose = doc.createElement('pose')
pose_text = doc.createTextNode(pose_text)
pose.appendChild(pose_text)
object.appendChild(pose)

truncated = doc.createElement('truncated')
truncated_text = doc.createTextNode(truncated_text)
truncated.appendChild(truncated_text)
object.appendChild(truncated)

difficult = doc.createElement('difficult')
difficult_text = doc.createTextNode(difficult_text)
difficult.appendChild(difficult_text)
object.appendChild(difficult)
robndbox = doc.createElement('robndbox')
cx_text = obj.find('robndbox').find('cx').text
cy_text = obj.find('robndbox').find('cy').text
w_text = obj.find('robndbox').find('w').text
h_text = obj.find('robndbox').find('h').text
angle_text = obj.find('robndbox').find('angle').text

image = cv2.flip(image, mirror) # 水平翻转
#image2 = cv2.flip(image, 0) # 垂直翻转
#image3 = cv2.flip(image, -1) # 水平垂直翻转

#https://github.com/ChenCongGit/RoLabelImg_Transform/blob/master/txt_to_xml.py
if mirror==1 or mirror==-1:
cx_ = doc.createElement('cx')
cx_text = doc.createTextNode(str(eval(width_text)-eval(cx_text)))
cx_.appendChild(cx_text)
robndbox.appendChild(cx_)
else:
cx_ = doc.createElement('cx')
cx_text = doc.createTextNode(str(cx_text))
cx_.appendChild(cx_text)
robndbox.appendChild(cx_)
if mirror == 0 or mirror==-1:
cy_ = doc.createElement('cy')
cy_text = doc.createTextNode(str(eval(height_text)-eval(cy_text)))
cy_.appendChild(cy_text)
robndbox.appendChild(cy_)
else:
cy_ = doc.createElement('cy')
cy_text = doc.createTextNode(str(cy_text))
cy_.appendChild(cy_text)
robndbox.appendChild(cy_)

w_ = doc.createElement('w')
w_text = doc.createTextNode(str(w_text))
w_.appendChild(w_text)
robndbox.appendChild(w_)

h_ = doc.createElement('h')
h_text = doc.createTextNode(str(h_text))
h_.appendChild(h_text)
robndbox.appendChild(h_)

if mirror == 0 or mirror == 1:
angle_ = doc.createElement('angle')
angle_text = doc.createTextNode(str(np.pi-eval(angle_text)))
angle_.appendChild(angle_text)
robndbox.appendChild(angle_)
object.appendChild(robndbox)
else:
angle_ = doc.createElement('angle')
angle_text = doc.createTextNode(str(np.pi + eval(angle_text)))
angle_.appendChild(angle_text)
robndbox.appendChild(angle_)
object.appendChild(robndbox)


DOCUMENT.appendChild(object)


########### 将DOM对象doc写入文件
with open(save_path, 'w') as f:
doc.writexml(f, indent='\t', newl='\n', addindent='\t', encoding='utf-8')
cv2.imwrite(save_image,image)


labelimg数据的水平、垂直、水平垂直镜像翻转

import numpy as np
import os
import cv2
import math
from xml.dom.minidom import Document
from xml.etree.ElementTree import parse, Element

dir_path = r'G:\k\test'
sv_path = r"G:\k\mirror" # 修改后的xml文件存放路径
if not os.path.exists(sv_path):
os.mkdir(sv_path)
list_mirror = [1, 0, -1] # 水平镜像 垂直镜像 水平垂直翻转 0
file_ext = "jpg"
for mirror in list_mirror:
for File in os.listdir(dir_path):
if File.endswith('.xml'):
print(File)
xml_path = os.path.join(dir_path, File)
dom = parse(xml_path) ###最核心的部分,路径拼接,输入的是具体路径
root = dom.getroot()
folder_text = root.find("folder").text
filename_text = root.find("filename").text
filename_text, _ = os.path.splitext(filename_text)
if mirror==1:
label_ext='mirror1'
elif mirror==0:
label_ext = 'mirror0'
else:
label_ext = 'mirror_1'
xml_name_ = ".".join([filename_text + label_ext, "xml"])
save_path = os.path.join(sv_path, xml_name_)
img_name_ = ".".join([filename_text + label_ext, file_ext])
save_image = os.path.join(sv_path, img_name_)
path_text = root.find("path").text
source_text = root.find("source").find("database").text
width_text = root.find("size").find("width").text
height_text = root.find("size").find("height").text
depth_text = root.find("size").find("depth").text
segmented_text = root.find("segmented").text
(filename, extension) = os.path.splitext(File)
img_jpg = ".".join([filename, file_ext])
img_path = os.path.join(dir_path, img_jpg)
image = cv2.imread(img_path)
doc = Document() # 创建DOM文档对象
DOCUMENT = doc.createElement('annotation') # 创建根元素

folder = doc.createElement('folder') ##建立自己的开头
folder_text = doc.createTextNode(folder_text) ##建立自己的文本信息
folder.appendChild(folder_text) ##自己的内容
DOCUMENT.appendChild(folder)
doc.appendChild(DOCUMENT)

filename = doc.createElement('filename')
filename_text = doc.createTextNode(filename_text +label_ext)
filename.appendChild(filename_text)
DOCUMENT.appendChild(filename)
doc.appendChild(DOCUMENT)

path_ = doc.createElement('path')
path_text = doc.createTextNode(img_name_)
path_.appendChild(path_text)
DOCUMENT.appendChild(path_)
doc.appendChild(DOCUMENT)

source = doc.createElement('source')
database = doc.createElement('database')
database_text = doc.createTextNode(source_text) # 元素内容写入
database.appendChild(database_text)
source.appendChild(database)
DOCUMENT.appendChild(source)
doc.appendChild(DOCUMENT)

size = doc.createElement('size')
width = doc.createElement('width')
width_text_ = doc.createTextNode(width_text) # 元素内容写入
width.appendChild(width_text_)
size.appendChild(width)

height = doc.createElement('height')
height_text_ = doc.createTextNode(height_text)
height.appendChild(height_text_)
size.appendChild(height)

depth = doc.createElement('depth')
depth_text = doc.createTextNode(depth_text)
depth.appendChild(depth_text)
size.appendChild(depth)

DOCUMENT.appendChild(size)

segmented = doc.createElement('segmented')
segmented_text = doc.createTextNode(segmented_text)
segmented.appendChild(segmented_text)
DOCUMENT.appendChild(segmented)
doc.appendChild(DOCUMENT)

for ano in root.iter('annotation'): # 获取object节点中的name子节点

for obj in ano.iter("object"):
name_text = obj.find('name').text
pose_text = obj.find('pose').text
truncated_text = obj.find('truncated').text
difficult_text = obj.find('difficult').text

object = doc.createElement('object')
name = doc.createElement('name')
name_text = doc.createTextNode(name_text)
name.appendChild(name_text)
object.appendChild(name)

pose = doc.createElement('pose')
pose_text = doc.createTextNode(pose_text)
pose.appendChild(pose_text)
object.appendChild(pose)

truncated = doc.createElement('truncated')
truncated_text = doc.createTextNode(truncated_text)
truncated.appendChild(truncated_text)
object.appendChild(truncated)

difficult = doc.createElement('difficult')
difficult_text = doc.createTextNode(difficult_text)
difficult.appendChild(difficult_text)
object.appendChild(difficult)
bndbox = doc.createElement('bndbox')
xmin_text = obj.find('bndbox').find('xmin').text
ymin_text = obj.find('bndbox').find('ymin').text
xmax_text = obj.find('bndbox').find('xmax').text
ymax_text = obj.find('bndbox').find('ymax').text

image_flip = cv2.flip(image, mirror)
# image2 = cv2.flip(image, 1) # 水平翻转
# image2 = cv2.flip(image, 0) # 垂直翻转
# image3 = cv2.flip(image, -1) # 水平垂直翻转

# https://github.com/ChenCongGit/RoLabelImg_Transform/blob/master/txt_to_xml.py
if mirror == 1 or mirror == -1:
xmin_ = doc.createElement('xmin')
xmin_text_ = doc.createTextNode(str(min(eval(width_text) - eval(xmin_text),eval(width_text) - eval(xmax_text))))
xmin_.appendChild(xmin_text_)
bndbox.appendChild(xmin_)
xmax_ = doc.createElement('xmax')
xmax_text_ = doc.createTextNode(str(max(eval(width_text) - eval(xmin_text),eval(width_text) - eval(xmax_text))))
xmax_.appendChild(xmax_text_)
bndbox.appendChild(xmax_)
else:
xmin_ = doc.createElement('xmin')
xmin_text_ = doc.createTextNode(str(min(eval(xmin_text),eval(xmax_text))))
xmin_.appendChild(xmin_text_)
bndbox.appendChild(xmin_)
xmax_ = doc.createElement('xmax')
xmax_text_ = doc.createTextNode(str(max(eval(xmin_text),eval(xmax_text))))
xmax_.appendChild(xmax_text_)
bndbox.appendChild(xmax_)

if mirror == 0 or mirror == -1:
ymin_ = doc.createElement('ymin')
ymin_text_ = doc.createTextNode(str(min(eval(height_text) - eval(ymin_text),eval(height_text) - eval(ymax_text))))
ymin_.appendChild(ymin_text_)
bndbox.appendChild(ymin_)
ymax_ = doc.createElement('ymax')
ymax_text_ = doc.createTextNode(str(max(eval(height_text) - eval(ymin_text),eval(height_text) - eval(ymax_text))))
ymax_.appendChild(ymax_text_)
bndbox.appendChild(ymax_)
else:
ymin_ = doc.createElement('ymin')
ymin_text_ = doc.createTextNode(str(min(eval(ymin_text),eval(ymax_text))))
ymin_.appendChild(ymin_text_)
bndbox.appendChild(ymin_)
ymax_ = doc.createElement('ymax')
ymax_text_ = doc.createTextNode(str(max(eval(ymin_text),eval(ymax_text))))
ymax_.appendChild(ymax_text_)
bndbox.appendChild(ymax_)
object.appendChild(bndbox)

DOCUMENT.appendChild(object)

########### 将DOM对象doc写入文件
with open(save_path, 'w') as f:
doc.writexml(f, indent='\t', newl='\n', addindent='\t', encoding='utf-8')
cv2.imwrite(save_image, image_flip)

标签:appendChild,doc,labelimg,createElement,垂直,text,镜像,path,find
From: https://blog.51cto.com/u_12504263/5719065

相关文章