iT邦幫忙

0

利用Python 寫資料增強的Code後修改不了xml檔(以貼code圖和附Code)

小弟最近寫了一個增強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附上:
https://ithelp.ithome.com.tw/upload/images/20200615/2012679581BCXZyr1e.jpg
這是print出修改前xml內bndbox的座標::
https://ithelp.ithome.com.tw/upload/images/20200615/20126795hB8awXHJmq.jpg
修改後的bndbox座標:
https://ithelp.ithome.com.tw/upload/images/20200615/20126795ypRjaGQJsm.jpg
修改前、修改後的xmin、xmax值:
https://ithelp.ithome.com.tw/upload/images/20200615/20126795DPfuxf1mmO.jpg
有用cv2去試畫過座標點 是沒問題的:
https://ithelp.ithome.com.tw/upload/images/20200615/20126795NS59ys5gDv.jpg
產出的xml檔長這樣:
https://ithelp.ithome.com.tw/upload/images/20200615/20126795mL3ShV5BQP.jpg

看更多先前的討論...收起先前的討論...
貼圖,不叫附code. 叫做附 code的圖.
你是要幫友先寫程式來辨識你的code嗎?
謝謝提醒喔 我已改了
有沒有可能是路徑的問題
回listennn08:
到寫回xml為止的路徑檢查過都沒有問題 write 寫成xml 路徑那邊也有試過但結果也是一樣 所以應該不是那個問題~~
https://stackoverflow.com/questions/37713184/python-element-tree-writing-to-new-file
試試這個
回listennn08:
有參考你的網址去修改過了~~ 但問題依舊沒有改善 謝謝你~~

尚未有邦友回答

立即登入回答