使用标注工具如labelimg对图片物体进行voc类型标注,会生成xml文件,如何判断别人的数据集做的好不好,可以用以下代码进行目标还原。
import xml.etree.cElementTree as ET
import cv2
import os
import glob
def GetAnnotBoxLoc(AnotPath):
tree = ET.ElementTree(file=AnotPath)
root = tree.getroot()
ObjectSet=root.findall('object')
ObjBndBoxSet={}
for Object in ObjectSet:
ObjName=Object.find('name').text
BndBox=Object.find('bndbox')
x1 = int(BndBox.find('xmin').text)
y1 = int(BndBox.find('ymin').text)
x2 = int(BndBox.find('xmax').text)
y2 = int(BndBox.find('ymax').text)
BndBoxLoc=[x1,y1,x2,y2]
if ObjName in ObjBndBoxSet:
ObjBndBoxSet[ObjName].append(BndBoxLoc)
else:
ObjBndBoxSet[ObjName]=[BndBoxLoc]
return ObjBndBoxSet
def GetAnnotName(AnotPath):
tree = ET.ElementTree(file=AnotPath)
root = tree.getroot()
path=root.find('path').text
return path
def Drawpic(xml_path,result_path):
n = 0
xmls = glob.glob(os.path.join(xml_path, '*.xml'))
for xml in xmls:
n = n + 1
box=GetAnnotBoxLoc(xml)
path=GetAnnotName(xml)
img = cv2.imread(path)
for classes in list(box.keys()):
for boxes in box[classes]:
if classes == "bad1":
cv2.rectangle(img,(int(boxes[0]),int(boxes[1])),(int(boxes[2]),int(boxes[3])),(255,0,0),3) #blue
if classes == "bad2":
cv2.rectangle(img,(int(boxes[0]),int(boxes[1])),(int(boxes[2]),int(boxes[3])),(0,255,0),3) #green
if classes == "bad3":
cv2.rectangle(img,(int(boxes[0]),int(boxes[1])),(int(boxes[2]),int(boxes[3])),(0,0,255),3) #red
cv2.imwrite(result_path+"/"+str(n)+"_result.jpg", img)
print(path,"还原成功")
Drawpic("/home/wxy/Dashboard/dataset/VOCdevkit/VOC2012/Annotations","/home/wxy/Dashboard/dataset/VOCdevkit/VOC2012/test")
使用labelimg对图像进行标注,folder目录需要修改一下
import xml.etree.ElementTree as ET
import os
for i in os.listdir('/home/wxy/Dashboard/dataset/VOCdevkit/VOC2012/Annotations'):
tree = ET.parse('/home/wxy/Dashboard/dataset/VOCdevkit/VOC2012/Annotations'+'/'+i)
root = tree.getroot()
print(root.find('folder').text)
root.find('folder').text = 'VOC2012'
print(root.find('folder').text)
tree.write('/home/wxy/Dashboard/dataset/VOCdevkit/VOC2012/Annotations'+'/'+i)
批量裁剪特定类,xml.dom.minidom好像比xml.etree.cElementTree好用啊。
#coding=utf-8
import xml.dom.minidom
import cv2
import os
for name in os.listdir("./Annotations/"):
dom=xml.dom.minidom.parse("./Annotations/"+name)
root=dom.documentElement
object_name=root.getElementsByTagName('name')
if(object_name[0].firstChild.data == "normal"):
print(name)
xmin=root.getElementsByTagName('xmin')
ymin=root.getElementsByTagName('ymin')
xmax=root.getElementsByTagName('xmax')
ymax=root.getElementsByTagName('ymax')
x_min = int(xmin[0].firstChild.data)
y_min = int(ymin[0].firstChild.data)
x_max = int(xmax[0].firstChild.data)
y_max = int(ymax[0].firstChild.data)
img=cv2.imread("./JPEGImages/"+name[:-4]+".jpg")
cropped=img[y_min:y_max,x_min:x_max]
cv2.imwrite("./cut_jpg/"+name[:-4]+".jpg", cropped)