python 批量生成xml標記文件(連通域坐標分割)
阿新 • • 發佈:2019-05-15
odi 寫入 spa 文件 報錯 ffi glob npr 所有
#!/usr/bin/python # -*- coding=utf-8 -*- # author : Manuel # date: 2019-05-15 from xml.etree import ElementTree as ET import numpy as np from skimage import data,filters,segmentation,measure,morphology,color from scipy.misc import imread import os from os import getcwd IMAGES_LIST=os.listdir(‘ls‘)#圖片路徑 #連通域分割,返回連通域坐標列表 def connected_domain_position_get(image): coordinates_list=[]#創建坐標列表 thresh = filters.threshold_otsu(image) # 閾值分割,自動返回一個閾值 bw = morphology.closing(image > thresh, morphology.square(3)) # (image > thresh, morphology.square(3)) #閉運算#將0,1轉換成boolcleared = bw.copy() # 復制 segmentation.clear_border(cleared) # 清除與邊界相連的目標物 label_image = measure.label(cleared) # 連通區域標記 borders = np.logical_xor(bw, cleared) # 邏輯異或 label_image[borders] = -1 # ? # image_label_overlay = color.label2rgb(label_image, image=image) # 不同標記用不同顏色顯示for region in measure.regionprops(label_image): # 循環得到每一個連通區域屬性集 # 忽略小區域 if region.area < 1000: continue # print(region.bbox) # 繪制外包矩形 minr, minc, maxr, maxc = region.bbox # rect = mpatches.Rectangle((minc - 10, minr - 10), maxc - minc + 20, maxr - minr + 20, # fill=False, edgecolor=‘red‘, # linewidth=2) # mpatches.Rectangle(矩形左上頂點坐標(x,y), width, height) left = minc - 10 upper = minr - 10 right = maxc + 10 lower = maxr + 10 coordinates_list.append([left,upper,right,lower])#將每組連通域坐標添加進坐標列表 return coordinates_list#返回連通域坐標列表 #創建一級分支object def create_object(root,xi,yi,xa,ya):#參數依次,樹根,xmin,ymin,xmax,ymax #創建一級分支object _object=ET.SubElement(root,‘object‘) #創建二級分支 name=ET.SubElement(_object,‘name‘) name.text=‘AreaMissing‘ pose=ET.SubElement(_object,‘pose‘) pose.text=‘Unspecified‘ truncated=ET.SubElement(_object,‘truncated‘) truncated.text=‘0‘ difficult=ET.SubElement(_object,‘difficult‘) difficult.text=‘0‘ #創建bndbox bndbox=ET.SubElement(_object,‘bndbox‘) xmin=ET.SubElement(bndbox,‘xmin‘) xmin.text=‘%s‘%xi ymin = ET.SubElement(bndbox, ‘ymin‘) ymin.text = ‘%s‘%yi xmax = ET.SubElement(bndbox, ‘xmax‘) xmax.text = ‘%s‘%xa ymax = ET.SubElement(bndbox, ‘ymax‘) ymax.text = ‘%s‘%ya #創建xml文件 def create_tree(image_name): global annotation # 創建樹根annotation annotation = ET.Element(‘annotation‘) #創建一級分支folder folder = ET.SubElement(annotation,‘folder‘) #添加folder標簽內容 folder.text=(‘ls‘) #創建一級分支filename filename=ET.SubElement(annotation,‘filename‘) filename.text=image_name.strip(‘.jpg‘) #創建一級分支path path=ET.SubElement(annotation,‘path‘) path.text=getcwd()+‘/ls/%s‘%image_name#用於返回當前工作目錄 #創建一級分支source source=ET.SubElement(annotation,‘source‘) #創建source下的二級分支database database=ET.SubElement(source,‘database‘) database.text=‘Unknown‘ #創建一級分支size size=ET.SubElement(annotation,‘size‘) #創建size下的二級分支圖像的寬、高及depth width=ET.SubElement(size,‘width‘) width.text=‘512‘ height=ET.SubElement(size,‘height‘) height.text=‘384‘ depth = ET.SubElement(size,‘depth‘) depth.text = ‘3‘ #創建一級分支segmented segmented = ET.SubElement(annotation,‘segmented‘) segmented.text = ‘0‘ def main(): for image_name in IMAGES_LIST: #只處理jpg文件 if image_name.endswith(‘jpg‘): #將圖像通過連通域分割,得到連通域坐標列表,該列表的形式[[a,b,c,d],[e,f,g,h]...,] image = color.rgb2gray(imread(os.path.join(r‘./ls‘, image_name))) coordinates_list = connected_domain_position_get(image) create_tree(image_name) for coordinate_list in coordinates_list: create_object(annotation, coordinate_list[0], coordinate_list[1], coordinate_list[2], coordinate_list[3]) # if coordinates_list==[]: # break # 將樹模型寫入xml文件 tree = ET.ElementTree(annotation) tree.write(‘ls/%s.xml‘ % image_name.strip(‘.jpg‘)) if __name__ == ‘__main__‘: main()
註:xml中所有值必須是字符串,否則報錯
python 批量生成xml標記文件(連通域坐標分割)