【学习笔记】使用python批量读取并修改xml文件(4)

在大老板的安排下最近在某公司实习,实习期间要求实现一个图像识别模块的封装。无奈基础太薄弱,只能将任务细分,单独学习来实现。以此为背景……


代码更新V4.0版本,通过openCV实现xml中bounding box在原始图像和尺寸调整后的图片中的可视化(part three部分)。方便对比尺寸调整前后是否存在误差。

# coding:utf-8
#############################################################################
# version:4.0 @author:KangShiFu                                             #
# 思路:1.首先统一resize图片-,图片尺寸用户自行输入2.读取标注原图的xml文件的长、宽值#
# 3.计算变化后的xml的相应坐标值,并写入xml文件中                                 #
#############################################################################
# 改进方向:1.分母是否为0的判断。(低优先级)
# 2.通过界面显示原始的bnb_box,以及更改后的bnb_box×(已实现)
# 3.将识别后的bnb_box坐标用来辅助标注
import os
import os.path
import cv2
import xml.dom.minidom

#---------------------------part one-----------------------------------#
imgw = input("请输入图像尺寸变化后的宽度:\n")
imgw = int(imgw)
imgh = input("请输入图像尺寸变化后的高度:\n")
imgh = int(imgh)

# 地址也可以改成交互式???
path_img='/home/kanghao/learning_something/about_xml/yibiao512/JPEGImages/'
path_rszimg='/home/kanghao/learning_something/about_xml/yibiao512/resizedJPEGImages/'
# sorted可以让os.listdir顺序遍历文件夹中的文件
files_img=sorted(os.listdir(path_img))
files_rszimg=sorted(os.listdir(path_rszimg))

for imgfile in files_img:
	img_dir = os.path.join(path_img,imgfile)
	print(img_dir)
	raw_img = cv2.imread(img_dir)
	raw_size = raw_img.shape
	raw_imgh = float(raw_size[0])
	raw_imgw = float(raw_size[1])
	resize_img = cv2.resize(raw_img, (imgw, imgh), interpolation=cv2.INTER_CUBIC)
	# ~ cv2.imshow('rezied img', resize_img) 
	# ~ cv2.waitKey(0)
	# ~ cv2.destroyAllWindows()
	rszimg_dir=os.path.join(path_rszimg,imgfile)
	# ~ cv2.imwrite(rszimg_dir, resize_img)

print("图片尺寸转换已完成!")
#--------------------------part one over-------------------------------#
#---------------------------part two-----------------------------------#
path_xml='/home/kanghao/learning_something/about_xml/yibiao512/Annotations'
files_xml=sorted(os.listdir(path_xml)) #得到文件夹下所有文件名称
for xmlFile in files_xml:
	if not os.path.isdir(xmlFile): #判断是否是文件夹,不是文件夹才打开
		print(xmlFile)
			
		dom=xml.dom.minidom.parse(os.path.join(path_xml,xmlFile))
		root=dom.documentElement
		
		# ~ width=root.getElementsByTagName('width')
		# ~ height=root.getElementsByTagName('height')
		xmin=root.getElementsByTagName('xmin')
		ymin=root.getElementsByTagName('ymin')
		xmax=root.getElementsByTagName('xmax')
		ymax=root.getElementsByTagName('ymax')
		filename=root.getElementsByTagName('filename')
		
		# 修改<xmin>	
		for i in range(len(xmin)):
			print("原始的xmin坐标值为 " + str(xmin[i].firstChild.data))
			xmin_raw = unicode.encode(xmin[i].firstChild.data)
			xmin[i].firstChild.data=float(xmin_raw)*(imgw/raw_imgw)
			rsz_xmin = xmin[i].firstChild.data
			print("修改后的xmin坐标值为 " + str(xmin[i].firstChild.data))
			
		# 修改<xmax>	
		for i in range(len(xmax)):
			print("原始的xmax坐标值为 " + str(xmax[i].firstChild.data))
			xmax_raw = unicode.encode(xmax[i].firstChild.data)
			xmax[i].firstChild.data=float(xmax_raw)*(imgw/raw_imgw)
			rsz_xmax = xmax[i].firstChild.data
			print("修改后的xmax坐标值为 " + str(xmax[i].firstChild.data))

		# 修改<ymin>	
		for i in range(len(ymin)):
			print("原始的ymin坐标值为 " + str(ymin[i].firstChild.data))
			ymin_raw = unicode.encode(ymin[i].firstChild.data)
			ymin[i].firstChild.data=float(ymin_raw)*(imgh/raw_imgh)
			rsz_ymin = ymin[i].firstChild.data
			print("修改后的ymin坐标值为 " + str(ymin[i].firstChild.data))
			
		# 修改<ymax>	
		for i in range(len(ymax)):
			print("原始的ymax坐标值为 " + str(ymax[i].firstChild.data))
			ymax_raw = unicode.encode(ymax[i].firstChild.data)
			ymax[i].firstChild.data=float(ymax_raw)*(imgh/raw_imgh)
			rsz_ymax = ymax[i].firstChild.data
			print("修改后的ymax坐标值为 " + str(ymax[i].firstChild.data))			
#--------------------------part two over-------------------------------#
		#保存修改到xml文件中
		# ~ with open(os.path.join(path_xml,xmlFile),'w') as fh:
			# ~ dom.writexml(fh)
			# ~ print('恭喜,写入xml文件成功!')
#---------------------------part three---------------------------------#
		for i in range(len(filename)): 
			print(filename[i].firstChild.data)
			img_name=filename[i].firstChild.data
			raw_img_dir = os.path.join(path_img,img_name)
			raw_img = cv2.imread(raw_img_dir)
			rsz_img_dir = os.path.join(path_rszimg,img_name)
			rsz_img = cv2.imread(rsz_img_dir)
			cv2.rectangle(raw_img, (int(float(xmin_raw)),int(float(ymin_raw))), (int(float(xmax_raw)),int(float(ymax_raw))), (0,191,255), 5)
			cv2.rectangle(rsz_img, (int(float(rsz_xmin)),int(float(rsz_ymin))), (int(float(rsz_xmax)),int(float(rsz_ymax))), (238,44,44), 5)
			cv2.imshow("原始图片",raw_img)
			cv2.imshow("尺寸变更后的图片",rsz_img)
			
			cv2.waitKey(0)
			cv2.destroyAllWindows()
#--------------------------part three over-----------------------------#

猜你喜欢

转载自blog.csdn.net/yourgreatfather/article/details/84256624
今日推荐