最近要做物体分割的任务,但在网上获取的标注图片以SVG格式存储,所以要通过SVG文件画出mask,再把格式转化为json
目录
SVG格式简介
数据集
用Python解析SVG文件
提取XML文档中关键字
1.观察svg文件层级关系
2.解析文件
3.保存polygon
4.画出轮廓
写入python字典转json
图像格式即图像文件存放的格式,svg为可缩放矢量图(Scalable Vector Graphics),基于XML格式描述图像,存储点和线,而位图图像都是存储像素点,图像文件比较大。
建筑平面图 : CVC-FP
标注文档形式:
wind = etree.parse('xxx/CVC-FP/ImagesGT/1_gt_14.svg')
root = wind.getroot()
nodes = wind.findall("{http://www.w3.org/2000/svg}svg")
for child in root:
print(child.tag,child.attrib)
部分打印出来的结果
{http://www.w3.org/2000/svg}width {}
{http://www.w3.org/2000/svg}height {}
{http://www.w3.org/2000/svg}nclasslabel {}
{http://www.w3.org/2000/svg}nclassappear {}
{http://www.w3.org/2000/svg}class {}
{http://www.w3.org/2000/svg}class {}
{http://www.w3.org/2000/svg}class {}
{http://www.w3.org/2000/svg}class {}
{http://www.w3.org/2000/svg}class {}
{http://www.w3.org/2000/svg}class {}
{http://www.w3.org/2000/svg}class {}
{http://www.w3.org/2000/svg}class {}
{http://www.w3.org/2000/svg}class {}
{http://www.w3.org/2000/svg}class {}
{http://www.w3.org/2000/svg}polygon {'class': 'Wall', 'fill': '#AFD8F8', 'id': '0', 'transcription': '', 'points': '485.5,195.5 510,200 506.5,692.5 482.5,694.5 '}
{http://www.w3.org/2000/svg}polygon {'class': 'Wall', 'fill': '#AFD8F8', 'id': '1', 'transcription': '', 'points': '913.5,697 481.5,695 481.5,672 912.5,675 '}
{http://www.w3.org/2000/svg}polygon {'class': 'Wall', 'fill': '#AFD8F8', 'id': '2', 'transcription': '', 'points': '893,592 915,592 914.5,644.5 914,697 892,697 892.5,644.5 '}
{http://www.w3.org/2000/svg}polygon {'class': 'Wall', 'fill': '#AFD8F8', 'id': '3', 'transcription': '', 'points': '893.5,472 914.5,473 914.5,550 892.5,548 '}
{http://www.w3.org/2000/svg}polygon {'class': 'Wall', 'fill': '#AFD8F8', 'id': '4', 'transcription': '', 'points': '893,429.5 916,428.5 916,308.5 893,308.5 '}
{http://www.w3.org/2000/svg}polygon {'class': 'Wall', 'fill': '#AFD8F8', 'id': '5', 'transcription': '', 'points': '903,227.5 916,227.5 916,309.5 903,308.5 '}
{http://www.w3.org/2000/svg}polygon {'class': 'Wall', 'fill': '#AFD8F8', 'id': '6', 'transcription': '', 'points': '750.5,307 728.5,307 728.5,199 751.5,197 '}
{http://www.w3.org/2000/svg}polygon {'class': 'Wall', 'fill': '#AFD8F8', 'id': '7', 'transcription': '', 'points': '658,295 694.5,295.5 731,296 731,302 658,301 '}
{http://www.w3.org/2000/svg}polygon {'class': 'Wall', 'fill': '#AFD8F8', 'id': '8', 'transcription': '', 'points': '751,318 896,319 897,313 751,312 '}
{http://www.w3.org/2000/svg}polygon {'class': 'Wall', 'fill': '#AFD8F8', 'id': '9', 'transcription': '', 'points': '751,338 751,325 751,312 744,314 744,338 '}
{http://www.w3.org/2000/svg}polygon {'class': 'Wall', 'fill': '#AFD8F8', 'id': '10', 'transcription': '', 'points': '655,416.875 661,415.875 661,433.875 655,433.875 655,425.375 655,421.125 '}
{http://www.w3.org/2000/svg}polygon {'class': 'Wall', 'fill': '#AFD8F8', 'id': '11', 'transcription': '', 'points': '710,430 684.5,430 659,430 659,424 685,424 711,425 '}
{http://www.w3.org/2000/svg}polygon {'class': 'Wall', 'fill': '#AFD8F8', 'id': '12', 'transcription': '', 'points': '710.5,338 711.5,431 704.5,430 705,384 705.5,338 '}
{http://www.w3.org/2000/svg}polygon {'class': 'Wall', 'fill': '#AFD8F8', 'id': '13', 'transcription': '', 'points': '662,338 746,338 746,333 662,332 '}
points = []
for child in root:
if child.tag == "{http://www.w3.org/2000/svg}polygon":#筛选label
I = np.zeros((1094, 905), dtype=np.uint8)
pts = []
p = child.attrib['points'].split()
for i in p:
j = i.split(',')
pts.append([float(j[0]),float(j[1])])
pts = np.array(pts,np.int32)
points.append(pts)
I = cv2.polylines(I,[pts],True,(100,255,255),2)
cv2.imshow("img", I)
cv2.waitKey(0)
cv2.destroyAllWindows()
轮廓的结果:
使用 collections.defaultdict 新建字典
anno = tree()
anno["info"] = "info"
anno["licenses"] = "license"
anno["annotations"] = []
总代码:
import xml.etree.ElementTree as etree
import numpy as np
import cv2
import collections
import json
wind = etree.parse('xxx/ImagesGT/1_gt_14.svg')
root = wind.getroot()
nodes = wind.findall("{http://www.w3.org/2000/svg}svg")
tree = lambda: collections.defaultdict(tree)
anno = tree()
points = []
anno["info"] = "info"
anno["licenses"] = "license"
anno["annotations"] = []
for child in root:
if child.tag == "{http://www.w3.org/2000/svg}polygon":
I = np.zeros((1094, 905), dtype=np.uint8)
pts = []
p = child.attrib['points'].split()
for i in p:
j = i.split(',')
pts.append([float(j[0]),float(j[1])])
annotation = tree()
annotation["label"] = child.attrib['class']
annotation["segmentation"] = pts
pts = np.array(pts,np.int32)
points.append(pts)
anno["annotations"].append(annotation)
a = json.dumps(anno,indent=4)
f2 = open('dict.json', 'w')
f2.write(a)
f2.close()
I = cv2.polylines(I,points,True,(100,255,255),2)
cv2.imwrite('I.PNG',I)
cv2.imshow("img", I)
cv2.waitKey(0)
cv2.destroyAllWindows()
json结果:
以上就是svg文件转json格式的过程,如果有更简便的方法欢迎留言交流呀~