可视化coco格式标注的mask(.json 分割)

目标检测、分割任务中,常用的标注格式是PASCAL VOC或coco数据集格式,其中coco的标签是以,json文件定义的。

以下的代码段可以帮您可视化数据的mask掩码(文末附完整代码)。

key words:语义分割  coco  .json 掩码mask 可视化


先上结果:(支持单个或多个目标可视化)

可视化coco格式标注的mask(.json 分割)_第1张图片

 

可视化coco格式标注的mask(.json 分割)_第2张图片

 

可视化coco格式标注的mask(.json 分割)_第3张图片

#  首先导入必要的库

import json
import numpy as np
import skimage.io
import cv2
import matplotlib.pyplot as plt
import PIL.Image
import PIL.ImageDraw
import numpy

#  加载图片数据,可使用相对路径,直接使用图片名;也可以使用绝对路径加载图片

data=json.load(open('1.json'))

# img_path=data['imagePath'].split('/')[-1]
img_path = '1.png'
img=skimage.io.imread(img_path)

#  提供了两种polygon2mask的函数

def polygons_to_mask(img_shape, polygons):

    mask = np.zeros(img_shape, dtype=np.uint8)
    mask = PIL.Image.fromarray(mask)
    xy = list(map(tuple, polygons))
    PIL.ImageDraw.Draw(mask).polygon(xy=xy, outline=1, fill=1)
    mask = np.array(mask, dtype=bool)
    return mask

def polygons_to_mask2(img_shape, polygons):

    mask = np.zeros(img_shape, dtype=np.uint8)
    polygons = np.asarray([polygons], np.int32) # 这里必须是int32,其他类型使用fillPoly会报错
    # cv2.fillPoly(mask, polygons, 1) # 非int32 会报错
    cv2.fillConvexPoly(mask, polygons, 1)  # 非int32 会报错
    return mask

# 批量读取标签,转化代码

points=[]
labels=[]

annotations = data['annotations']
len_ann = len(annotations)

half_mask=[]
final_mask=[]

for ann in annotations:
    seg1 = ann['segmentation']
    seg = seg1[0]
    for j in range(0,len(seg),2):
        x = seg[j]
        y = seg[j+1]
        mas = [x,y]
        half_mask.append(mas)
    final_mask.append(half_mask)
    half_mask=[]

mask0=polygons_to_mask(img.shape[:2],final_mask[0])

if len_ann>2:
    for i in range(1,len_ann):
        print(i)
        maskany=polygons_to_mask(img.shape[:2],final_mask[i])
        print(mask0)
        print(maskany)
        mask0 += maskany
else:
    maskany=polygons_to_mask(img.shape[:2],final_mask[len_ann-1])
    print(mask0)
    print(maskany)
    mask0 += maskany

# 可视化结果

plt.subplot(121)
plt.imshow(img)
plt.axis('off')
plt.title('original')

plt.subplot(122)
plt.imshow(mask0.astype(np.uint8),'gray')
plt.axis('off')
plt.show()

完整代码如下:

import json
import numpy as np
import skimage.io
import cv2
import matplotlib.pyplot as plt
import PIL.Image
import PIL.ImageDraw
import numpy

def polygons_to_mask(img_shape, polygons):

    mask = np.zeros(img_shape, dtype=np.uint8)
    mask = PIL.Image.fromarray(mask)
    xy = list(map(tuple, polygons))
    PIL.ImageDraw.Draw(mask).polygon(xy=xy, outline=1, fill=1)
    mask = np.array(mask, dtype=bool)
    return mask

def polygons_to_mask2(img_shape, polygons):

    mask = np.zeros(img_shape, dtype=np.uint8)
    polygons = np.asarray([polygons], np.int32) # 这里必须是int32,其他类型使用fillPoly会报错
    # cv2.fillPoly(mask, polygons, 1) # 非int32 会报错
    cv2.fillConvexPoly(mask, polygons, 1)  # 非int32 会报错
    return mask

points=[]
labels=[]

annotations = data['annotations']
len_ann = len(annotations)

half_mask=[]
final_mask=[]

for ann in annotations:
    seg1 = ann['segmentation']
    seg = seg1[0]
    for j in range(0,len(seg),2):
        x = seg[j]
        y = seg[j+1]
        mas = [x,y]
        half_mask.append(mas)
    final_mask.append(half_mask)
    half_mask=[]

mask0=polygons_to_mask(img.shape[:2],final_mask[0])

if len_ann>2:
    for i in range(1,len_ann):
        print(i)
        maskany=polygons_to_mask(img.shape[:2],final_mask[i])
        print(mask0)
        print(maskany)
        mask0 += maskany
else:
    maskany=polygons_to_mask(img.shape[:2],final_mask[len_ann-1])
    print(mask0)
    print(maskany)
    mask0 += maskany

plt.subplot(121)
plt.imshow(img)
plt.axis('off')
plt.title('original')

plt.subplot(122)
plt.imshow(mask0.astype(np.uint8),'gray')
plt.axis('off')
plt.show()

 

你可能感兴趣的:(可视化coco格式标注的mask(.json 分割))