Files
MPKFNet/inf_all_box_voc.py
T
2021-09-15 11:33:58 +08:00

147 lines
5.8 KiB
Python
Executable File

from scipy.io import loadmat
import glob,os
import xml.dom.minidom
from tqdm import tqdm
from creat_xml_fordetection import make_xml_iccv
den_path = "../data_MPKFNet/data/VOC/VOCdevkit/VOCiccvtrain/ground_truth/"
voc_path = "../data_MPKFNet/data/VOC/VOCdevkit/VOCiccvtrain/Annotations/"
save_path = "../data_MPKFNet/data/VOC/VOCdevkit/VOCiccvtrain/Bird_detection/"
def get_mat_point (path):
mat_data = loadmat(path)
positions = mat_data['image_info'][0, 0][0, 0][0]
count = mat_data['image_info'][0][0][0][0][1][0][0]
return positions
def get_xml_box (path):
dom = xml.dom.minidom.parse(path)
objects = dom.getElementsByTagName("object")
all_boxes = []
for object in objects:
bndbox = object.getElementsByTagName('bndbox')[0]
xmin = bndbox.getElementsByTagName('xmin')[0]
ymin = bndbox.getElementsByTagName('ymin')[0]
xmax = bndbox.getElementsByTagName('xmax')[0]
ymax = bndbox.getElementsByTagName('ymax')[0]
xmin_data = int(xmin.childNodes[0].data)
ymin_data = int(ymin.childNodes[0].data)
xmax_data = int(xmax.childNodes[0].data)
ymax_data = int(ymax.childNodes[0].data)
all_boxes.append((xmin_data,ymin_data,xmax_data,ymax_data))
sizes = dom.getElementsByTagName("size")
for size in sizes:
w = size.getElementsByTagName('width')[0]
w_data = int(w.childNodes[0].data)
h = size.getElementsByTagName('height')[0]
h_data = int(h.childNodes[0].data)
return all_boxes ,w_data,h_data
def point_in_box (site,box):
flag = False
if (site[0]>=box[0]) and (site[0]<=box[2]) and (site[1]>=box[1]) and (site[1]<=box[3]):
flag = True
return flag
def ifsame_box(site1,site2,box):
centerx = (box[0]+box[2])/2
centery = (box[1]+box[3])/2
dis1 = ((site1[0]-centerx)*(site1[0]-centerx) +(site1[1]-centery)*(site1[1]-centery))**0.5
dis2 = ((site2[0] - centerx) * (site2[0] - centerx) + (site2[1] - centery) * (site2[1] - centery))**0.5
if dis1>dis2:
flag = True
else:
flag = False
return flag
def get_new(site,boxes,img_w,img_h):
min_dis = 10e10
min_index = 0
for i in range(len(boxes)):
centerx = (boxes[i][0] + boxes[i][2]) / 2
centery = (boxes[i][1] + boxes[i][3]) / 2
dis = ((site[0]-centerx)*(site[0]-centerx) +(site[1]-centery)*(site[1]-centery))**0.5
if dis < min_dis:
min_dis = dis
min_index = i
w = (boxes[min_index][2]-boxes[min_index][0])/2
h = (boxes[min_index][3]-boxes[min_index][1])/2
xmin = site[0]-w
if xmin <0:
xmin =0
xmax = site[0]+w
if xmax >img_w:
xmax = img_w-1
ymin = site[1]-h
if ymin<0:
ymin = 0
ymax = site[1]+h
if ymax >img_h:
ymax = img_h-1
return ((xmin,ymin,xmax,ymax))
if __name__ == '__main__':
xml_lists = os.listdir(voc_path)
for xml_path in tqdm(xml_lists) :
print(xml_path+"is doing")
mat_path = xml_path.replace(".xml",".mat").replace("HatIMG","GT_HatIMG")
ALL_points = get_mat_point(den_path+mat_path)
ALl_boxes, image_W,image_H = get_xml_box (voc_path+xml_path)
ALL_point_box = [] #点对应的边框的编号
No_Label_point = []
Yes_Label_point = []
Yes_Box_index = []
###############步1 完成对ALL_point的所有点的遍历,寻找每个点对应的box在ALL_boxes中的索引,最终得到一个list ALL_point_box 代表每个点对应的box的索引,没有的是100000
for i in range(len(ALL_points)):
ALL_point_box.append(100000)
for j in range(len(ALl_boxes)):
if point_in_box(ALL_points[i],ALl_boxes[j]): #判断是否在边框里面,是的话就将边框的编号放到点对应边框的数组里面,否则该点对应的边框为100000
ALL_point_box[i]=j
break
#########################################步2.1 筛选出来准标注的和未标注的
for i in range(len(ALL_point_box)):
if ALL_point_box[i]==100000:
No_Label_point.append(ALL_points[i])
else:
Yes_Label_point.append(ALL_points[i])
Yes_Box_index.append(ALL_point_box[i])
###################步2.2对已标记的点进行遍历,去除掉具有重复边框的点
for i in range(5):
for i in range(len(Yes_Label_point)):
if i >= len(Yes_Label_point):
break
else:
for j in range(i+1,len(Yes_Label_point)):
#print(len(Yes_Label_point))
if j >= len(Yes_Label_point):
break
else:
if Yes_Box_index[i] == Yes_Box_index[j]: #当前点和遍历点是一样的
if ifsame_box(Yes_Label_point[i],Yes_Label_point[j],ALl_boxes[Yes_Box_index[i]]):
No_Label_point.append(Yes_Label_point[j])
del Yes_Label_point[j]
Yes_Box_index.remove(Yes_Box_index[j])
else:
No_Label_point.append(Yes_Label_point[i])
del Yes_Label_point[i]
Yes_Box_index.remove(Yes_Box_index[i])
break
##########步3推理未标记的点的边框信息 对No_Laber_point进行遍历
final_boxes = ALl_boxes
for i in range(len(No_Label_point)):
final_boxes.append(get_new(No_Label_point[i],ALl_boxes,image_W,image_H))
#########步4存储为xml
make_xml_iccv(xml_path.replace(".xml",".jpg"),image_H,image_W,final_boxes,save_path+xml_path)
print(final_boxes)