接上一篇来说,将真实的样本过一遍baseline模型后得到最初版的boundingbox信息的txt文件,又将这些txt文件转成xml文件进行纠正,纠正后使用IphotoDraw导出的还是xml文件,因此还需要将这些xml文件再次转换成txt文件。(是不是很晕?哈哈,晕就对了,多想几遍就好了)
以下是以IphotoDraw为标注软件,将纠正好的xml文件转成txt文件的代码:
#coding:utf-8
#@Time:2017/6/16 19:37
#@author: xxx
import os
import re
import math
import string
from tqdm import tqdm
# 提取xml文件中的矩形框四个角点跟标签
def get_new_coord(center_coord,ori_coord,rotate_angle):
x_new = (ori_coord[0]-center_coord[0])*math.cos((rotate_angle/180.)*math.pi)+(ori_coord[1]-center_coord[1])*math.sin((rotate_angle/180.)*math.pi)+center_coord[0]
y_new = (ori_coord[1]-center_coord[1])*math.cos((rotate_angle/180.)*math.pi)-(ori_coord[0]-center_coord[0])*math.sin((rotate_angle/180.)*math.pi)+center_coord[1]
return int(x_new),int(y_new)
def get_coord_range(item):
list_location = []
for i in range(len(item)):
if (item[i] == '"'):
list_location.append(i + 1)
X=item[list_location[0]:list_location[1]-1]
Y=item[list_location[2]:list_location[3]-1]
Width=item[list_location[4]:list_location[5]-1]
Height=item[list_location[6]:list_location[7]-1]
return X,Y,Width,Height
def GetItemLocation(xml_file):
fid=open(xml_file,"r",encoding="utf-8")
list=[]
for line in fid.readlines():
line=line.replace("\n","")
line=line.replace(" ","")
list.append(line)
str1 = "".join(list)
label_list=re.findall("<Text>.*?</Text>",str1)
position_list=re.findall("<Extent.*?/>",str1)
angle_list=re.findall("<Data.*?>",str1)
# 取出文本框标签
label_list_new = []
for label in label_list:
result_label = label[6:-7]
label_list_new.append(result_label)
# 取出文本框旋转角度
angle_list_new = []
for angle in angle_list:
result_angle = float(angle.split('"')[-2])
angle_list_new.append(result_angle)
#取出左上角点 w h
position_list_new = []
for position in position_list:
X, Y, Width, Height = get_coord_range(position)
position_list_new.append([X, Y, Width, Height])
# 得到标注框坐标
point_list = []
for i in range(len(position_list_new)):
value = position_list_new[i]
angle = angle_list_new[i]
x1 = int(float(value[0])) # 左上
y1 = int(float(value[1]))
x2 = int(float(value[0]) + float(value[2])) # 右上
y2 = int(float(value[1]))
x3 = int(float(value[0]) + float(value[2])) # 右下
y3 = int(float(value[1]) + float(value[3]))
x4 = int(float(value[0])) # 左下
y4 = int(float(value[1]) + float(value[3]))
if not angle == 0:
angle = -angle
center_x = (x1 + x2 + x3 + x4) / 4
center_y = (y1 + y2 + y3 + y4) / 4
x1, y1 = get_new_coord([center_x, center_y], [x1, y1], angle)
x2, y2 = get_new_coord([center_x, center_y], [x2, y2], angle)
x3, y3 = get_new_coord([center_x, center_y], [x3, y3], angle)
x4, y4 = get_new_coord([center_x, center_y], [x4, y4], angle)
if int(x1) < 0:
x1 = 0
if (int(y1) < 0):
y1 = 0
if (int(y2) < 0):
y2 = 0
if (int(x4) < 0):
x4 = 0
point = [[x1,y1],[x2,y2],[x3,y3],[x4,y4]]
point_list.append(point)
return label_list_new,point_list
if __name__=='__main__':
path=r'xxxxxx'
import glob
import traceback
jpg_files=glob.glob(os.path.join(path,'*.jpg'))
bar=tqdm(total=len(jpg_files))
for file in jpg_files:
bar.update(1)
try:
xml_file = file.replace('.jpg','_data.xml')
label_list, position_list = GetItemLocation(xml_file)
# 左上 左下 右下 右上
IMIMkey = file.split('\\')[-1].replace('.jpg', '')
with open(os.path.join(path, IMIMkey + '.txt'), 'w+', encoding='utf-8') as fid:
for i, label in enumerate(label_list):
position = position_list[i]
# flag = 0
# if len(label) > 0:
# for dd in d:
# if dd in label:
# flag = 1
# break
flag = 1
if flag == 1:
fid.writelines(str(position[0][0]) + ',' + str(position[0][1]) + ',' +
str(position[1][0]) + ',' + str(position[1][1]) + ',' +
str(position[2][0]) + ',' + str(position[2][1]) + ',' +
str(position[3][0]) + ',' + str(position[3][1]) +','+ label +'\n')
except:
print(file)
traceback.print_exc()