功能:根據給定點座標集合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)