xml标签可视化
标图之后⽣成了xml⽂件,但是只是⽂本格式,不⽅便查看。
python脚本如下
# coding=UTF-8
from xml.dom.minidom import parse
import matplotlib.pyplot as plt
import xml.dom.minidom
import os,shutil
import matplotlib
import numpy as np
import cv2
from PIL import Image, ImageDraw
>>>>>>>>>>>###python处理xml文件
root='/media/lab522/d67894e6-0574-4ac5-b9f7-b773a22de8ec/ubuntu/project_bdf/comparations/data/'
#only need to change these
>>>>>>>>>>>###
#annroot=root+'2/'
#picroot=root+'1/'
#annroot=root+'xml/'
#picroot=root+'img/'
annroot='/media/lab522/d67894e6-0574-4ac5-b9f7-b773a22de8ec/ubuntu/project_bdf/SSD_data_augment/Annotations/'
picroot='/media/lab522/d67894e6-0574-4ac5-b9f7-b773a22de8ec/ubuntu/project_bdf/SSD_data_augment/JPEGImages/'
anns=os.listdir(annroot)
imgs=os.listdir(picroot)
labelmap=["person","car"]
colormap=["red" , "green", "blue" , "yellow", "pink" , "olive" , "deeppink" , "darkorange", "purple", "cyan","red" , "green", "blue" , "yellow", "pink" , "olive" , "deeppink"
def mkdir(path):
folder = ists(path)
if not folder:
os.makedirs(path)
number = 0
nn=0
for ann in anns:
number += 1
print (number)
print (ann)
annpath=annroot+ann
picpath=place("xml","jpg")
im = Image.open(picpath)
img = cv2.imread(picpath)
draw = ImageDraw.Draw(im)
DOMTree = xml.dom.minidom.parse(annpath)
collection = DOMTree.documentElement
objects = ElementsByTagName("object")
labelsss = ""
for object_ in objects:
#print (object_)
a=object_.getElementsByTagName("name")[0].childNodes[0].nodeValue
k=a.split('.',1)
kk=k[0]
b=str(kk)
for i in range(0,len(labelmap)):
label = labelmap[i]
print (label)
if b == label:
nn+= 1
if label not in labelsss:
labelsss+= label +"_"
bndboxs = object_.getElementsByTagName("bndbox")
for bndbox in bndboxs:
xmin = ElementsByTagName('xmin')[0].childNodes[0].nodeValue ymin = ElementsByTagName('ymin')[0].childNodes[0].nodeValue xmax = ElementsByTagName('xmax')[0].childNodes[0].nodeValue ymax = ElementsByTagName('ymax')[0].childNodes[0].nodeValue xtmp1=xmin.split('.',1)
xmin1=xtmp1[0]
xtmp2= xmax.split('.', 1)
xmax1 = xtmp2[0]
xtmp3=ymin.split('.',1)
ymin1=xtmp3[0]
xtmp4=ymax.split('.',1)
ymax1=xtmp4[0]
xmin = int(xmin1)
ymin = int(ymin1)
xmax = int(xmax1)
ymax = int(ymax1)
if xmin<0:
xmin=0
if ymin<0:
ymin=0
sp=img.shape
if xmax>sp[1]:
xmax=sp[1]
if ymax>sp[0]:
ymax=sp[0]
roiimg=img[ymin: ymax, xmin:xmax]
save_op = root+'check/'+label +"_"+"/"
mkdir(save_op)
saveopath = save_op+str(nn)+"_"+place("xml","jpg")
cv2.imwrite(saveopath,roiimg)
break
label_has=0
for label in labelmap:
if b != label:
label_has = 1
if not label_has:
print (ann+"======"+b+"============================")
save_p = root+'check/'+labelsss+"/"
savepath = save_place("xml","jpg")
mkdir(save_p)
im.save(savepath)
#cv2.imwrite(savepath,roiimg)
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论