python xml转voc格式(图形化操作)


#coding=utf-8

from tkinter import *
from tkinter.filedialog import askdirectory

from tkinter import *
import os,shutil
import xml.etree.ElementTree as ET
import pickle
import os
import threading



class WidgetsDemo:
	def __init__(self):
		
		window = Tk()                 # 创建一个窗口
		#设置屏幕居中
		sw=window.winfo_screenwidth()
		sh=window.winfo_screenheight()
		ww=400
		wh=130
		x = (sw-ww) / 2
		y = (sh-wh) / 2
		window.geometry("%dx%d+%d+%d" %(ww,wh,x,y))
		window.title("Widgets Demo")  # 设置标题
		
		
		frame1 = Frame(window)        # 创建一个框架
		frame1.pack(side=TOP)                 # 将框架frame1放置在window中
		self.label_1=Label(frame1,text = "XML路径:").grid(row = 0, column = 0)
		self.path_1=StringVar()
		self.entry_1=Entry(frame1, textvariable = self.path_1,width = 36)
		self.entry_1.grid(row = 0, column = 1)
		
		self.button_1=Button(frame1, text = "浏览", command = self.selectPath_1).grid(row = 0, column = 2)

		self.label_2=Label(frame1,text = "图片路径:").grid(row = 1, column = 0)
		self.path_2=StringVar()
		self.entry_2=Entry(frame1, textvariable = self.path_2,width = 36)
		self.entry_2.grid(row = 1, column = 1)
		self.button_2=Button(frame1, text = "浏览", command = self.selectPath_2).grid(row = 1, column = 2)
		
		self.label_3=Label(frame1,text = "保存路径:").grid(row = 2, column = 0)
		self.path_3=StringVar()
		self.entry_3=Entry(frame1, textvariable = self.path_3,width = 36)
		self.entry_3.grid(row = 2, column = 1)
		self.button_3=Button(frame1, text = "浏览", command = self.selectPath_3).grid(row = 2, column = 2)
		
		
		frame2 = Frame(window)        # 另外创建一个框架
		frame2.pack()                 # 将框架frame2放置在window中
		self.button_4=Button(frame2, text = "运行", width = 12,command = self.generate)
		self.button_4.pack(side=TOP)
		
		window.mainloop()
		
	#设置显示路径
	def selectPath_1(self):
		path_ = askdirectory()
		self.path_1.set(path_)
		
	def selectPath_2(self):
		path_ = askdirectory()
		self.path_2.set(path_)
		
	def selectPath_3(self):
		path_ = askdirectory()
		self.path_3.set(path_)
		
	#生成绝对路径
	
	def convert(self,size, box):
		dw = 1./size[0]
		dh = 1./size[1]
		x = (box[0] + box[1])/2.0
		y = (box[2] + box[3])/2.0
		w = box[1] - box[0]
		h = box[3] - box[2]
		x = x*dw
		w = w*dw
		y = y*dh
		h = h*dh
		return (x,y,w,h)
		
	
	def convert_annotation(self,xml,xml_path,image_path):
		in_file = open(xml_path+'/'+str(xml)+'.xml','rb')
		out_file = open(image_path+'/'+str(xml)+'.txt', 'w')
		tree=ET.parse(in_file)
		in_file.close()
		root = tree.getroot()
		size = root.find('size')
		w = int(size.find('width').text)
		h = int(size.find('height').text)

		for obj in root.iter('object'):
			difficult = obj.find('difficult').text
			cls = obj.find('name').text
			cls_id = 0
			#这里需要修改
			if cls == 'normal':
				cls_id = 0
			elif cls == 'play_mobile_phone':
				cls_id = 1
			else :
				cls_id = 2
			#这里需要修改
			xmlbox = obj.find('bndbox')
			b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text), float(xmlbox.find('ymax').text))
			bb = self.convert((w,h), b)
			out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
		out_file.close()
		
	def newthread(self):
		#获取文件名
		xml_path=self.entry_1.get()
		image_path=self.entry_2.get()
		total_xml = os.listdir(xml_path)
		#print(total_xml)
		for xml in total_xml:
			print(xml)
			self.convert_annotation(xml[:-4],xml_path,image_path)
		print('解析xml成功')
		
		#保存绝对路径
		train_path=self.entry_3.get()+'/train.txt'
		with open(train_path,'w') as f:
			ImgName = os.listdir(image_path)
			for img in ImgName:
				if img.endswith('.txt'):
					f.write(image_path+'/'+img[:-4]+'.jpg\n')
		print('获取绝对路径成功')
		
	def generate(self):
		th=threading.Thread(target=self.newthread)
		th.setDaemon(True)
		th.start()
		

		
WidgetsDemo()

python xml转voc格式(图形化操作)_第1张图片

 

你可能感兴趣的:(Python)