Python通过XML标注⽂件⽤框把标注物体出⽬录
实现1
from __future__ import division
import os
from PIL import Image
import xml.dom.minidom
import numpy as np
ImgPath = r'E:/正课/⼤⼆上/计算机⽹络/⽹络编程/Taidi/train/train'
AnnoPath = r'E:/正课/⼤⼆上/计算机⽹络/⽹络编程/Taidi/train/train'
ProcessedPath = r'E:/正课/⼤⼆上/计算机⽹络/⽹络编程/Taidi/train'
imagelist = os.listdir(ImgPath)
for image in imagelist:
image_pre, ext = os.path.splitext(image)
imgfile = ImgPath + image
# print(imgfile)
if not ists(AnnoPath + image_pre + '.xml' ):
continue
xmlfile = AnnoPath + image_pre + '.xml'
DomTree = xml.dom.minidom.parse(xmlfile)
annotation = DomTree.documentElement
filenamelist = ElementsByTagName('filename')
filename = filenamelist[0].childNodes[0].data
objectlist = ElementsByTagName('object')
i = 1
for objects in objectlist:
namelist = ElementsByTagName('name')
objectname = namelist[0].childNodes[0].data
savepath = ProcessedPath + objectname
if not ists(savepath):
os.makedirs(savepath)
bndbox = ElementsByTagName('bndbox')
cropboxes = []
for box in bndbox:
x1_list = ElementsByTagName('xmin')
x1 = int(x1_list[0].childNodes[0].data)
y1_list = ElementsByTagName('ymin')
y1 = int(y1_list[0].childNodes[0].data)
x2_list = ElementsByTagName('xmax')
x2 = int(x2_list[0].childNodes[0].data)
y2_list = ElementsByTagName('ymax')
y2 = int(y2_list[0].childNodes[0].data)
w = x2 - x1
h = y2 - y1
obj = np.array([x1,y1,x2,y2])
shift = np.array([[1,1,1,1]])
XYmatrix = np.tile(obj,(1,1))
cropboxes = XYmatrix * shift
img = Image.open(imgfile)
for cropbox in cropboxes:
cropedimg = p(cropbox)
python处理xml文件cropedimg.save(savepath + '/' + image_pre + '_' + str(i) + '.jpg')
print(savepath + '/' + image_pre + '_' + str(i) + '.jpg')
i += 1
实现2
import os
import xml.dom.minidom
import cv2 as cv
ImgPath = r"C:\Users\WalterJ726\Desktop\train\train"
AnnoPath = r"C:\Users\WalterJ726\Desktop\train\train" # xml⽂件地址
save_path = r"C:\Users\WalterJ726\Desktop\train"
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'
#print(image)
# 打开xml⽂档
DOMTree = xml.dom.minidom.parse(xmlfile)
# 得到⽂档元素对象
collection = DOMTree.documentElement
# 读取图⽚
img = cv.imread(imgfile)
filenamelist = ElementsByTagName("filename")
filename = filenamelist[0].childNodes[0].data
#print(filename)
# 得到标签名为object的信息
objectlist = ElementsByTagName("object")
for objects in objectlist:
# 每个object中得到⼦标签名为name的信息
namelist = ElementsByTagName('name')
# 通过此语句得到具体的某个name的值
objectname = namelist[0].childNodes[0].data
print(objectname)
bndbox = ElementsByTagName('bndbox')
# print(bndbox)
for box in bndbox:
x1_list = ElementsByTagName('xmin')
x1 = int(x1_list[0].childNodes[0].data)
y1_list = ElementsByTagName('ymin')
y1 = int(y1_list[0].childNodes[0].data)
x2_list = ElementsByTagName('xmax')  # 注意坐标,看是否需要转换
x2 = int(x2_list[0].childNodes[0].data)
y2_list = ElementsByTagName('ymax')
y2 = int(y2_list[0].childNodes[0].data)
cv.putText(img, objectname, (x1, y1), cv.FONT_HERSHEY_COMPLEX, 0.7, (0, 255, 0),                          thickness=2)
# cv.imshow('head', img)
cv.imwrite(save_path + '/' + filename, img)  # save picture
draw_anchor(ImgPath, AnnoPath, save_path)

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。