1 Star 0 Fork 0

chenxuankai/daily_exercises

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
XML文件操作.py 4.65 KB
一键复制 编辑 原始数据 按行查看 历史
10613644@qq.com 提交于 2021-03-19 10:59 . 整合完毕
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 把voc xml格式转换为txt格式. 自己部门标注的人脸数据,需要转换
# 目录结构如下
# subDir
# --annotations
# --Images
import os,cv2
import sys
import time
import random
import argparse
import shutil
from multiprocessing.dummy import Pool as ThreadPool
try:
import xml.etree.cElementTree as ET
except ImportError:
import xml.etree.ElementTree as ET
classes = {'cycle':0,'truck':0,'bus':0,'car':0}
def view_bar(num, total): # 控制台上显示进度
rate = float(num) / total
rate_num = int(rate * 100)+1
r = '\r[%s%s]%d%% (%d/%d)' % ("#"*rate_num, " " *
(100-rate_num), rate_num, num, total)
sys.stdout.write(r)
sys.stdout.flush()
def GetSubDir(parDir):
dirlist = []
filelist = os.listdir(parDir)
for filename in filelist:
filepath = os.path.join(parDir, filename)
if os.path.isdir(filepath):
dirlist.append(filepath)
return dirlist
def GetBoxInfo(xmlfile):
global count
# print(xmlfile)
tree = ET.parse(xmlfile)
root = tree.getroot()
size = root.find('size')
w = int(size.find('width').text)
h = int(size.find('height').text)
if w==0 or h==0:
print(xmlfile)
# im0='Y://img_and_video_data//img_data//person_in_cycle//net_cycle_37.jpg'
# im0=cv2.imread(im0)
# print(im0.shape[0])
depth = int(size.find('depth').text)
bbox = []
# a=root.find('object')
# if a:
# pass
# else:
# print(xmlfile)
# # jpgfile='Y://img_and_video_data//img_data//from_open_img//'+xmlfile.split('//')[-1][:-4]+'.jpg'
# # os.remove(xmlfile)
# # os.remove(jpgfile)
# # print('ok')
for obj in root.iter('object'):
cls = obj.find('name').text
classes[cls]+=1
if cls not in classes:
print('warning: {} have object {}'.format(xmlfile, cls))
# continue
xmlbox = obj.find('bndbox')
if xmlbox:
# print(xmlbox)
pass
else:
print('该文件没有标注:%s'%xmlfile)
xmin = int(xmlbox.find('xmin').text)
ymin = int(xmlbox.find('ymin').text)
xmax = int(xmlbox.find('xmax').text)
ymax = int(xmlbox.find('ymax').text)
bbox.append([xmin, ymin, xmax, ymax])
# print(xmlfile)
# if xmin == 0:
# xmlbox.find('xmin').text = str(1)
# print(xmlfile)
# if ymin == 0:
# print(xmlfile)
# xmlbox.find('ymin').text = str(1)
# count+=1
# if xmin < 0 or xmax > (w-1) or ymin < 0 or ymax > (h-1) or xmin > xmax or ymin > ymax:
# count+=1
# xmin = int(xmlbox.find('xmin').text)
# ymin = int(xmlbox.find('ymin').text)
# xmax = int(xmlbox.find('xmax').text)
# ymax = int(xmlbox.find('ymax').text)
# print('宽:%s,高:%s' % (w, h))
# print(xmlfile)
# print([xmin, ymin, xmax, ymax])
# new_name='C:/Users/Administrator/Desktop/xml_test/%s'%xmlfile.split('//')[1]
# print(new_name)
# print('\n')
# shutil.copyfile(xmlfile,new_name)
flat_bbox = [item for sublist in bbox for item in sublist]
# tree.write(xmlfile)
return [flat_bbox, len(bbox), depth]
def ProcessXML(dir):
global count
count=0
files=[]
for i in os.listdir(dir):
if '.xml' in i:
files.append(i)
sample_num = 0
image_num = 0
for i in files:
xml_files = [dir+i]
# print(xml_files)
for k in range(len(xml_files)):
annfile = xml_files[k]
boxinfo = GetBoxInfo(annfile)
# print(annfile,boxinfo[0],boxinfo[1])
if boxinfo[1] != 0 and boxinfo[2] != 1:
sample_num = sample_num + boxinfo[1]
image_num = image_num + 1
# # else:
# # os.remove(annfile)
print("样本总个数 = ", sample_num, " 有效图片个数 = ", image_num)
print(classes)
global count
dir = 'Y://img_and_video_data//LJ//label//'
ProcessXML(dir)
# CXK={'cycle': 14532, 'truck': 1484, 'bus': 2067, 'car': 21339,'img':8202}
# LH={'cycle': 16298, 'truck': 1735, 'bus': 6518, 'car': 37562,'img':8914}
# LJ={'cycle': 16049, 'truck': 2574, 'bus': 4293, 'car': 28110,'img':8428}
# YLC={'cycle': 14602, 'truck': 2448, 'bus': 4533, 'car': 22709,'img':11164}
# cycle_sum=CXK['cycle']+LH['cycle']+LJ['cycle']+YLC['cycle']
# car_sum=CXK['car']+LH['car']+LJ['car']+YLC['car']
# +CXK['truck']+LH['truck']+LJ['truck']+YLC['truck']
# +CXK['bus']+LH['bus']+LJ['bus']+YLC['bus']
# print(cycle_sum/car_sum)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/chen-xuankai/daily_exercises.git
git@gitee.com:chen-xuankai/daily_exercises.git
chen-xuankai
daily_exercises
daily_exercises
master

搜索帮助