目录
python生成voc xml文件
python生成voc xml文件_jacke121的专栏-CSDN博客
lxml windows安装
pip install lxml可能失败,
一种可行方法,在这里下载对应版本whl,然后安装,亲测OK。
读取示例
import os
import cv2
import numpy as np
import xml.etree.ElementTree as ET
CLASSES = ('m',)
_class_to_ind=dict(list(zip(CLASSES, list(range(len(CLASSES))))))
def get_label(label_path,shape, flip=False):
h, w, c = shape
annotations = np.zeros((0, 5),dtype=np.float32)
if os.path.exists(label_path):
tree = ET.parse(label_path)
objs = tree.findall('object')
for ix, obj in enumerate(objs):
if obj.find('name').text.lower().strip() not in _class_to_ind:
# logging.info("label not select: {}".format(obj.find('name').text.lower().strip()))
continue
bbox = obj.find('bndbox')
x1 = max(float(bbox.find('xmin').text), 1) # - 1
y1 = max(float(bbox.find('ymin').text), 1) # - 1
x2 = min(float(bbox.find('xmax').text), 10000) # - 1
y2 = min(float(bbox.find('ymax').text), 10000) # - 1
if obj.find("difficult") is not None and obj.find("difficult").text == '1':
if (x2-x1) * (y2-y1)< 10*10:
continue
可视化
import os
import cv2
import re
import xml.etree.ElementTree as ET
CLASSES = ('car',)
_class_to_ind=dict(list(zip(CLASSES, list(range(len(CLASSES))))))
if __name__ == '__main__':
image_dir = r'G:\img'
xml_dir = r'G:\xml'
image_list = os.listdir(image_dir)
cnt = 0
for i in image_list:
image_path = os.path.join(image_dir ,i)
xml_path = os.path.join(xml_dir ,os.path.splitext(i)[0 ] +'.xml')
tree = ET.parse(xml_path)
objs = tree.findall('object')
image = cv2.imread(image_path)
for ix, obj in enumerate(objs):
if obj.find('name').text.lower().strip() not in _class_to_ind:
# logging.info("label not select: {}".format(obj.find('name').text.lower().strip()))
continue
bbox = obj.find('bndbox')
x1 = max(float(bbox.find('xmin').text), 1) # - 1
y1 = max(float(bbox.find('ymin').text), 1) # - 1
x2 = min(float(bbox.find('xmax').text), 10000) # - 1
y2 = min(float(bbox.find('ymax').text), 10000) # - 1
cv2.rectangle(image, (int(x1), int(y1)), (int(x2), int(y2)), (255, 0, 0), thickness=2)
# cv2.putText(image, info[0], (info[1], info[2]), cv2.FONT_HERSHEY_PLAIN, 0.5, (0, 0, 255), 1)
cv2.imshow("image",image)
cv2.waitKey()
voc转换为json格式:
python生成coco格式_AI视觉网奇的博客-CSDN博客_coco python
生成示例
上面是代码,下面有调用示例
api调用代码,其实只有几行:
anno = GEN_Annotations(pic_path)
anno.set_size(new_width, new_height, 3)
for (x, y, w, h,name) in bbox_list:
anno.add_pic_attr(name, x, y, w, h)
save_xml_path=xml_path.replace(base_path,save_pic_path)
os.makedirs(os.path.dirname(save_xml_path), exist_ok=True)
anno.savefile(save_xml_path)
# -*- coding: utf-8 -*-
import os
from lxml import etree
class GEN_Annotations:
def __init__(self, filename):
self.root = etree.Element("annotation")
child1 = etree.SubElement(self.root, "folder")
child1.text = "VOC2007"
child2 = etree.SubElement(self.root, "filename")
child2.text = filename
child3 = etree.SubElement(self.root, "source")
# child2.set("database", "The VOC2007 Database")
child4 = etree.SubElement(child3, "annotation")
child4.text = "PASCAL VOC2007"
child5 = etree.SubElement(child3, "database")
child6 = etree.SubElement(child3, "image")
child6.text = "flickr"
child7 = etree.SubElement(child3, "flickrid")
child7.text = "35435"
# root.append( etree.Element("child1") )
# root.append( etree.Element("child1", interesting="totally"))
# child2 = etree.SubElement(root, "child2")
# child3 = etree.SubElement(root, "child3")
# root.insert(0, etree.Element("child0"))
def set_size(self,witdh,height,channel):
size = etree.SubElement(self.root, "size")
widthn = etree.SubElement(size, "width")
widthn.text = str(witdh)
heightn = etree.SubElement(size, "height")
heightn.text = str(height)
channeln = etree.SubElement(size, "channel")
channeln.text = str(channel)
def savefile(self,filename):
tree = etree.ElementTree(self.root)
tree.write(filename, pretty_print=True, xml_declaration=False, encoding='utf-8')
def add_pic_attr(self,label,x,y,w,h,diffi_=0):
object = etree.SubElement(self.root, "object")
namen = etree.SubElement(object, "name")
namen.text = label
diffi = etree.SubElement(object, "difficult")
diffi.text = str(diffi_)
truncated_ = etree.SubElement(object, "truncated")
truncated_.text = str(0)
bndbox = etree.SubElement(object, "bndbox")
xminn = etree.SubElement(bndbox, "xmin")
xminn.text = str(x)
yminn = etree.SubElement(bndbox, "ymin")
yminn.text = str(y)
xmaxn = etree.SubElement(bndbox, "xmax")
xmaxn.text = str(x+w)
ymaxn = etree.SubElement(bndbox, "ymax")
ymaxn.text = str(y+h)
调用:
import cv2
import xml.etree.ElementTree as ET
def change_size(pic_path, xml_path,base_path,save_pic_path):
img = cv2.imread(pic_path)
if img is None:
print('img is null', pic_path)
return
if img.shape[1] < 1 or img.shape[0] < 1:
print('img is null', pic_path)
return
height,width=img.shape[:2]
new_width = width
new_height = height
if height>700 and width>1200:
new_width = int(width * 0.8)
new_height = int(height * 0.8)
img=cv2.resize(img,(new_width,new_height))
if new_height!=576 or new_width!=1024:
print(new_height,new_width,pic_path)
pic_path = pic_path.replace(base_path, save_pic_path)
os.makedirs(os.path.dirname(pic_path), exist_ok=True)
cv2.imwrite(pic_path, img)
if os.path.exists(xml_path):
tree = ET.parse(xml_path)
# file_objs = tree.findall('filename')
objs = tree.findall('object')
objs_diffi = tree.findall('object/difficult')
if len(objs_diffi)==0:
print(xml_path, objs_diffi, len(objs_diffi))
bbox_list = []
for ix, obj in enumerate(objs):
name = obj.find('name').text
bbox = obj.find('bndbox')
x1 = int(int(bbox.find('xmin').text)*0.8)
y1 = int(int(bbox.find('ymin').text)*0.8)
x2 = int(int(bbox.find('xmax').text)*0.8)
y2 = int(int(bbox.find('ymax').text)*0.8)
bbox_list.append((x1,y1,(x2-x1),(y2-y1),name))
anno = GEN_Annotations(pic_path)
anno.set_size(new_width, new_height, 3)
for (x, y, w, h,name) in bbox_list:
anno.add_pic_attr(name, x, y, w, h)
save_xml_path=xml_path.replace(base_path,save_pic_path)
os.makedirs(os.path.dirname(save_xml_path), exist_ok=True)
anno.savefile(save_xml_path)
if __name__ == '__main__':
# save file path
source_base_path = r'D:\Team-CV\dataset\chumao_train/'
save_base_path = r'D:\Team-CV\dataset\chumao_train_1024/'
os.makedirs(save_base_path, exist_ok=True)
# file path
g = os.walk(source_base_path)
pic_files = ['%s\\%s' % (i[0], j) for i in g if i[0].endswith('JPEGImages') for j in i[-1] if j.endswith('jpg')]
for i,pic_file in enumerate(pic_files):
if 'JPEGImages' in pic_file:
xml_file = pic_file.replace('JPEGImages','Annotations').replace('.jpg','.xml')
change_size(pic_file, xml_file,source_base_path,save_base_path)
print(i,pic_file)