xyxy 转 xywh
def xyxy2xywh(size, box):
dw = 1. / size[0]
dh = 1. / size[1]
x = (box[0] + box[2]) / 2 * dw
y = (box[1] + box[3]) / 2 * dh
w = (box[2] - box[0]) * dw
h = (box[3] - box[1]) * dh
return (x, y, w, h)
xywh 转 xyxy
def xywh2xyxy(size, box):
width = size[0]
height = size[1]
x1 = box[0]*width - 0.5*box[2]*width
x2 = box[0]*width + 0.5*box[2]*width
y1 = box[1]*height - 0.5*box[3]*height
y2 = box[1]*height + 0.5*box[3]*height
return (x1,y1,x2,y2)
读xml
import os
import xml.etree.ElementTree as ET
classes = [类别信息]
tree = ET.parse(label_file)
root = tree.getroot()
size = root.find('size')
w = int(size.find('width').text)
h = int(size.find('height').text)
for obj in root.iter('object'):
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)
bndbox = obj.find('bndbox')
box = [float(bndbox.find('xmin').text), float(bndbox.find('ymin').text), float(bndbox.find('xmax').text),
float(bndbox.find('ymax').text)]