SVG格式转json文件

最近要做物体分割的任务,但在网上获取的标注图片以SVG格式存储,所以要通过SVG文件画出mask,再把格式转化为json

目录

SVG格式简介

数据集

用Python解析SVG文件

提取XML文档中关键字

1.观察svg文件层级关系

2.解析文件

3.保存polygon

4.画出轮廓

写入python字典转json


SVG格式简介

图像格式即图像文件存放的格式,svg为可缩放矢量图(Scalable Vector Graphics),基于XML格式描述图像,存储点和线,而位图图像都是存储像素点,图像文件比较大。

数据集

建筑平面图 : CVC-FP 

 标注文档形式:

用Python解析SVG文件

提取XML文档中关键字

1.观察svg文件层级关系

<polygon>中的points是我们要提取的部分

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
<polygon class="Wall" fill="#AFD8F8" id="0" transcription="" points="485.5,195.5 510,200 506.5,692.5 482.5,694.5 " />
<polygon class="Wall" fill="#AFD8F8" id="1" transcription="" points="913.5,697 481.5,695 481.5,672 912.5,675 " />
<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 " />
<polygon class="Wall" fill="#AFD8F8" id="3" transcription="" points="893.5,472 914.5,473 914.5,550 892.5,548 " />

2.解析文件

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 '}

3.保存polygon

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)

4.画出轮廓

        I = cv2.polylines(I,[pts],True,(100,255,255),2)
        cv2.imshow("img", I)
        cv2.waitKey(0)
        cv2.destroyAllWindows()

轮廓的结果: 

 

写入python字典转json

使用 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格式的过程,如果有更简便的方法欢迎留言交流呀~

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值