小弟最近寫了一個增強YOLO訓練集的CODE(左右翻轉)
但在翻轉完後的座標要存回XML檔內卻修改不了
請大大們幫忙看一下~~
import numpy as np
import random
import cv2
import glob
import os
import xml.etree.cElementTree as ET
def random_horizontal_flip(img, bboxes, p=0.5):
if random.random() < p:
_, w_img, _ = img.shape
img = img[:, ::-1, :]
bboxes[:, [0, 2]] = w_img - bboxes[:, [2, 0]]
return img, bboxes
def readAnnotations(xml_path):
et = ET.parse(xml_path)
element = et.getroot()
element_objs = element.findall('object')
results = []
for element_obj in element_objs:
result = []
obj_bbox = element_obj.find('bndbox')
x1 = int(round(float(obj_bbox.find('xmin').text)))
y1 = int(round(float(obj_bbox.find('ymin').text)))
x2 = int(round(float(obj_bbox.find('xmax').text)))
y2 = int(round(float(obj_bbox.find('ymax').text)))
result.append(int(x1))
result.append(int(y1))
result.append(int(x2))
result.append(int(y2))
results.append(result)
return results
if __name__ == "__main__":
img_list = glob.glob("E:/Opencv/dataip/test/*.jpg")
for image_path in img_list:
imgpath =image_path[:-4] + ".xml"
img_org = cv2.imread(image_path)
img = img_org
bboxes = readAnnotations(image_path[:-4] + ".xml")
print("img: {}, box: {}".format(image_path, bboxes))
img, bboxes = random_horizontal_flip(img, np.array(bboxes), 1)
#img, bboxes = random_vertical_flip(img, np.array(bboxes), 1)
#img, bboxes = random_rot90_1(img, np.array(bboxes), 1)
# img, bboxes = random_translate(img, np.array(bboxes), 1)
# img, bboxes = random_crop(img, np.array(bboxes), 1)
# img, bboxes = random_bright(img, np.array(bboxes), 1)
# img, bboxes = random_swap(img, np.array(bboxes), 1)
# img, bboxes = random_saturation(img, np.array(bboxes), 1)
# img, bboxes = random_hue(img, np.array(bboxes), 1)
img = np.array(img)
print(bboxes)
for i,box in enumerate(bboxes):
cv2.rectangle(img, (box[0], box[1]), (box[2], box[3]), (0, 255, 0), 2)
print(box[0], box[1], box[2], box[3])
et = ET.parse(imgpath)
element = et.getroot()
bndbox = element.findall("./object/bndbox")
print("修改前:"+bndbox[i].find('xmin').text,bndbox[i].find('xmax').text )
bndbox[i].find('xmin').text = str(box[0])
bndbox[i].find('ymin').text = str(box[1])
bndbox[i].find('xmax').text = str(box[2])
bndbox[i].find('ymax').text = str(box[3])
print("修改後:"+bndbox[i].find('xmin').text,bndbox[i].find('xmax').text )
et.write('test/new/'+'leftright'+imgpath[22:-4]+'.xml',encoding='UTF-8')
cv2.imwrite('test/new/'+'leftright'+image_path[22:-4]+'.jpg', img)
# cv2.imshow(image_path[22:-4]+'da', img)
img_rotate = 0
cv2.waitKey()
cv2.destroyAllWindows()
存回xml的部分Code附上:
這是print出修改前xml內bndbox的座標::
修改後的bndbox座標:
修改前、修改後的xmin、xmax值:
有用cv2去試畫過座標點 是沒問題的:
產出的xml檔長這樣: