目标检测_训练数据处理

1、训练xml中的矩形框到图上并保存:

# coding=utf-8
import os
import xml.dom.minidom
import cv2 as cv
  
ImgPath = './JPEGImages/'
AnnoPath = './Annotations_new/' 
save_path = './save_JPEGImages/'
def draw_anchor(ImgPath,AnnoPath,save_path):
  imagelist = os.listdir(ImgPath)
  for image in imagelist:  
    image_pre, ext = os.path.splitext(image)
    imgfile = ImgPath + image
    xmlfile = AnnoPath + image_pre + '.xml'
    DOMTree = xml.dom.minidom.parse(xmlfile)
    collection = DOMTree.documentElement
    img = cv.imread(imgfile)
  
    filenamelist = collection.getElementsByTagName("filename")
    filename = filenamelist[0].childNodes[0].data
    print(filename)
    objectlist = collection.getElementsByTagName("object")  
    for objects in objectlist:
      namelist = objects.getElementsByTagName('name')
      objectname = namelist[0].childNodes[0].data  
      bndbox = objects.getElementsByTagName('bndbox')
      for box in bndbox:
        x1_list = box.getElementsByTagName('xmin')
        x1 = int(x1_list[0].childNodes[0].data)
        y1_list = box.getElementsByTagName('ymin')
        y1 = int(y1_list[0].childNodes[0].data)
        x2_list = box.getElementsByTagName('xmax')  
        x2 = int(x2_list[0].childNodes[0].data)
        y2_list = box.getElementsByTagName('ymax')
        y2 = int(y2_list[0].childNodes[0].data)
        cv.rectangle(img, (x1, y1), (x2, y2), (255, 255, 255), thickness=2)
        cv.putText(img, objectname, (x1, y1), cv.FONT_HERSHEY_COMPLEX, 0.7, (0, 255, 0),
              thickness=2)
        cv.imwrite(save_path+'/'+filename+'.jpg', img)  
draw_anchor(ImgPath,AnnoPath,save_path)

 2、xml转txt并保存(符合yoloV*训练):

# coding=utf-8
import os
import xml.dom.minidom
import xml.etree.ElementTree as ET

ImgPath = './JPEGImages/'
AnnoPath = './Annotations/' 
save_path = './Labels/'

def convert(size, box):
    dw = 1./size[0]
    dh = 1./size[1]
    x = (box[0] + box[1])/2.0
    y = (box[2] + box[3])/2.0
    w = box[1] - box[0]
    h = box[3] - box[2]
    x = x*dw
    w = w*dw
    y = y*dh
    h = h*dh
    return (x,y,w,h)

def draw_anchor(ImgPath,AnnoPath,save_path):
  imagelist = os.listdir(ImgPath)
  for image in imagelist:
    image_pre, ext = os.path.splitext(image)
    imgfile = ImgPath + image
    xmlfile = AnnoPath + image_pre + '.xml'
    DOMTree = xml.dom.minidom.parse(xmlfile)
    collection = DOMTree.documentElement
    #img = cv.imread(imgfile)
    
    filenamelist = collection.getElementsByTagName("filename")
    filename = filenamelist[0].childNodes[0].data
    print(filename)
    cls_id = 0

    tree=ET.parse(xmlfile)
    root = tree.getroot()
    size = root.find('size')
    w = int(size.find('width').text)
    h = int(size.find('height').text)
    
    out_file = open(save_path+filename+'.txt', 'w')

    objectlist = collection.getElementsByTagName("object")
    for objects in objectlist:
      namelist = objects.getElementsByTagName('name')
      objectname = namelist[0].childNodes[0].data
  
      bndbox = objects.getElementsByTagName('bndbox')
      for box in bndbox:
        x1_list = box.getElementsByTagName('xmin')
        x1 = int(x1_list[0].childNodes[0].data)
        y1_list = box.getElementsByTagName('ymin')
        y1 = int(y1_list[0].childNodes[0].data)
        x2_list = box.getElementsByTagName('xmax')  
        x2 = int(x2_list[0].childNodes[0].data)
        y2_list = box.getElementsByTagName('ymax')
        y2 = int(y2_list[0].childNodes[0].data)
        
        b = (float(x1), float(x2), float(y1), float(y2))
        bb = convert((w,h), b)
        out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')

draw_anchor(ImgPath,AnnoPath,save_path)

 

posted @ 2020-12-14 14:14  皮卡皮卡妞  阅读(227)  评论(0编辑  收藏  举报