上下翻转图像并且修改标记文件python程序
因为训练的需要,我需要把图片进行上下翻转然后保存,同时也要修改标记文件,主要的任务就这样,这个操作过程还是不难,程序注解也比较详尽在此不赘述,直接见代码吧,因为实在sublime写的程序,不知怎么中文注解,写的时候使用chinese English 简单注解了下,有点英语基础的都能看的懂。#!/usr/bin/python# -*- coding: UTF-8 -*-# ...
·
因为训练的需要,我需要把图片进行上下翻转然后保存,同时也要修改标记文件,主要的任务就这样,这个操作过程还是不难,程序注解也比较详尽在此不赘述,直接见代码吧,因为实在sublime写的程序,不知怎么中文注解,写的时候使用chinese English 简单注解了下,有点英语基础的都能看的懂。
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 2018/07/11 by DQ
import cv2
import os
try:
import xml.etree.cElementTree as ET
except ImportError:
import xml.etree.ElementTree as ET
ImSize=[640,480]
PreImNum=10000
fileIdLen=6
ImExpName='.jpg'
AnotExpName='.xml'
# up-down flip image
def GetUDFlipLoc(ImSize,BndBox):
H=ImSize[1]
xmin=BndBox[0]
ymin=BndBox[1]
xmax=BndBox[2]
ymax=BndBox[3]
NewXmin=xmin
NewYmin=H-ymax
NewXmax=xmax
NewYmax=H-ymin
NewBndBox=[NewXmin,NewYmin,NewXmax,NewYmax]
return NewBndBox
#draw all bndbox on image
def DrawObjectBox(Im,BndBoxSet,BoxColor):
BoxNum=len(BndBoxSet)
for i in range(BoxNum):
BndBox=BndBoxSet[i]
cv2.rectangle(Im,(BndBox[0],BndBox[1]),(BndBox[2],BndBox[3]),BoxColor,2)
#get flip image and xml file
def GetImAndXml(ImFolder,ImName,AnotFolder):
BndBoxSet=[]
NewBndBoxSet=[]
ImPath=os.path.join(ImFolder,ImName)
if os.path.isfile(ImPath):
TempStr=ImName.split('.')
AnotName=TempStr[0]+AnotExpName;
AnotPath=os.path.join(AnotFolder,AnotName)
#open xml
tree = ET.ElementTree(file=AnotPath)
root = tree.getroot()
#reset file name for <filename>xxx.jpg</filename>
filenameNode=root.find('filename')
filename=filenameNode.text;
#print filename
SplitStr=filename.split('.')
fileId=int(SplitStr[0])
NewfileId=(str(fileId+PreImNum)).zfill(fileIdLen)
#print fileId
NewImName=NewfileId+'.jpg'
filenameNode.text=NewImName
#save up-down flip image
Im = cv2.imread(ImPath,1)
UDFlipIm=cv2.flip(Im,0)
NewImPath=os.path.join(ImFolder,NewImName)
cv2.imwrite(NewImPath,UDFlipIm)
#reset bndbox for all object
for Child in root.iter('bndbox'):
#print(Child.tag,":", Child.attrib)
BndBox=[]
for SubChild in Child:
#print(SubChild.tag, ":", SubChild.attrib,SubChild.text)
BndBox.append(int(SubChild.text))
#print BndBox
NewBndBox=GetUDFlipLoc(ImSize,BndBox)
#print NewBndBox
i=0
SubChild=None
for SubChild in Child:
SubChild.text=str(NewBndBox[i])
i+=1
BndBoxSet.append(BndBox)
NewBndBoxSet.append(NewBndBox)
#save the fix file
NewAnotName=NewfileId+AnotExpName
NewAnotPath=os.path.join(AnotFolder,NewAnotName)
tree.write(NewAnotPath)
elif os.path.isdir(ImPath):
print 'this is document'
else:
print 'unknown'
return BndBoxSet,NewBndBoxSet
#data file
MainFolder='/home/Ok/Data/Fan'
ImFolder=os.path.join(MainFolder,'JPEGImages')
AnotFolder=os.path.join(MainFolder,'Annotations')
IsShowBoxIm=False
#ImNameSet=os.listdir(ImFolder)
#for ImName in ImNameSet:
ImNum=PreImNum
for Id in range(ImNum):
ImName=(str(Id+1)).zfill(fileIdLen)+ImExpName
print ImName
BndBoxSet,NewBndBoxSet=GetImAndXml(ImFolder,ImName,AnotFolder)
if IsShowBoxIm:
#imshow these image and box
#print len(BndBoxSet)
ImPath=os.path.join(ImFolder,ImName)
Im = cv2.imread(ImPath,1)
UDFlipIm=cv2.flip(Im,0)
#original box
DrawObjectBox(Im,BndBoxSet,(255,0,0))
cv2.imshow('OriginIm',Im)
SplitStr=ImName.split('.')
OriginBoxName=SplitStr[0]+'_OrigBox.'+SplitStr[1]
ImPath=os.path.join(MainFolder,'OriginBox',OriginBoxName)
cv2.imwrite(ImPath,Im)
#up-down flip box
DrawObjectBox(UDFlipIm,NewBndBoxSet,(0,255,0))
cv2.imshow('UDFlipIm',UDFlipIm)
NewBoxName=SplitStr[0]+'_NewBox.'+SplitStr[1]
ImPath=os.path.join(MainFolder,'NewBox',NewBoxName)
cv2.imwrite(ImPath,UDFlipIm)
cv2.waitKey(200)#这两句在sublime下不管用,不知道为啥?
cv2.destroyAllWindows()
更多推荐
已为社区贡献6条内容
所有评论(0)