根据已知点生成图像对应的语义标签文件json

功能:根据给定点座标集合points,生成图像对应的语义标签文件json。

测试过了,生成json文件,并打开labelme进行标注没有问题。


from PIL import Image
import json
import base64
 
filename = r'xxx\1.jpg'
name = filename[23:-4]  #这里之后要根据filename路径长度发生变化
im = Image.open(filename)
 
imageWidth = im.size[0]
imageHeight = im.size[1]  #获取宽、高
imagePath = filename
with open(imagePath, 'rb') as f:
    imageData = f.read()
    imageData = base64.b64encode(imageData).decode('utf-8')  
 
__version__ = '4.2.9'
label = 'road'
shape_type = 'polygon'
group_id = None  
flags = {}
#边角点赋给points
points=[[670.2970297029702,718.8118811881188],[1570,1079],[1794,1079],[927.7227722772277,750.4950495049505],[667.3267326732673,639.6039603960396]]


shapes = [dict(label=label,points= points,shape_type= shape_type,flags= flags,group_id= group_id)]

data = dict(
    version = __version__,
    flags=flags,
    shapes=shapes,
    imagePath = imagePath,
    imageData = imageData,
    imageHeight = imageHeight,
    imageWidth = imageWidth,
)

json_name = name + '.json'
 
with open(json_name,'w') as f:
    json.dump(data, f, ensure_ascii=False, indent=2)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章