读取json文件
json_path = r'C:\Users\Administrator\Desktop\pytorch-UNet-master\data\image\000799.json'
json_data = json.load(open(json_path , js), 'r'))
json格式
image.pngshape = json_data['shapes']
image.png
points = shape[1]['points'] #标注点的位置[[1,1], ....]
label = shape[1]['label'] #标注标签person
points = tuple(tuple(i) for i in points) #把标注标签变成 ((1,1),.....),用于mask标注
mask = Image.new('P', Image.open(os.path.join(image_dir, js.replace('json', 'png'))).size) #生成mask,大小是图片长宽
from PIL import Image, ImageDraw
mask_draw = ImageDraw.Draw(mask) #申明需要对mask进行标注
mask_draw.polygon(points, fill=CLASS_NAMES.index(label) + 1) #将区域点标注成标签,其中points必须为元组((),()..)
img = Image.open(img_dir)
temp = max(img.size)
mask = Image.new('RGB', (temp, temp))
mask.paste(img, (0, 0)) #左上角为坐标原点,把img贴图上mask
mask = mask.resize(size) # 修改图片尺寸
img = mask.crop((0,0,95,63))
image.png
网友评论