labelme在win下可视化json文件

首先要安装配好labelme(上篇)
根据该readme文档尝试
https://github.com/wkentaro/labelme/tree/master/examples/tutorial
改动labelme_draw_json
这里写图片描述
把scripts里的labelme_draw_json移动到该目录下,并加.py 后缀
这里写图片描述
把想可视化的json放在同一目录
shift+右键打开命令行

python labelme_draw_json.py bird.json

这里写图片描述
之前标定的bird和默认背景分别显示
labelme.json显示

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


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

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

def polygons_to_mask(img_shape, polygons):
    '''
    边界点生成mask
    :param img_shape: [h,w]
    :param polygons: labelme JSON中的边界点格式 [[x1,y1],[x2,y2],[x3,y3],...[xn,yn]]
    :return: 
    '''
    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
    :param img_shape: [h,w]
    :param polygons: labelme JSON中的边界点格式 [[x1,y1],[x2,y2],[x3,y3],...[xn,yn]]
    :return: 
    '''
    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=[]
for shapes in data['shapes']:
    points.append(shapes['points'])
    labels.append(shapes['label'])

mask0=polygons_to_mask(img.shape[:2],points[0])
#mask1=polygons_to_mask(img.shape[:2],points[1])

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.title('SegmentationObject:\n'+labels[0])
plt.show()

这里写图片描述
reference
json文件格式:
https://blog.csdn.net/wc781708249/article/details/79603522#%E9%A6%96%E5%85%88%E6%98%AF%E8%A6%81labelme%E5%81%9A%E5%A5%BD%E5%9B%BE%E7%89%87%E6%A0%87%E6%B3%A8

可视化 LabelMe 的 JSON 标注文件,可以按照以下步骤进行操作: 1. 首先,安装 LabelMe 工具包。你可以通过以下命令使用 pip 安装: ``` pip install labelme ``` 2. 导入 labelme 包: ```python import labelme ``` 3. 使用 `labelme.LabelFile` 类加载 JSON 文件: ```python json_file = 'path/to/your/json/file.json' label_data = labelme.LabelFile(json_file) ``` 4. 获取图像数据和标注信息: ```python image_data = label_data.imageData annotations = label_data.shapes ``` 5. 可以使用 OpenCV 或其他图像处理库加载图像数据并显示: ```python import cv2 import numpy as np image = np.frombuffer(image_data, dtype=np.uint8) image = cv2.imdecode(image, cv2.IMREAD_COLOR) cv2.imshow("Image", image) cv2.waitKey(0) cv2.destroyAllWindows() ``` 6. 可以使用 OpenCV 图像绘制函数绘制标注的边界框或多边形等形状: ```python for annotation in annotations: shape_type = annotation['shape_type'] points = annotation['points'] if shape_type == 'rectangle': x, y, w, h = cv2.boundingRect(np.array(points)) cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2) elif shape_type == 'polygon': pts = np.array(points, np.int32) cv2.polylines(image, [pts], True, (0, 255, 0), 2) ``` 7. 最后,显示带有标注的图像: ```python cv2.imshow("Annotated Image", image) cv2.waitKey(0) cv2.destroyAllWindows() ``` 通过以上步骤,你可以将 LabelMe 的 JSON 文件中的图像和标注信息可视化显示出来。请确保已正确安装所需的包,并将代码中的文件路径替换为实际的 JSON 文件路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值