labelme标注结果可视化(持续补充)

        图像数据常用的标注工具是labelme,标注的格式是json。labelme标注结果可视化,是将标注结果绘制到原始图像上,以方便查看标注结果。

1 json文件读取与保存

        由于labelme标注的保存格式为json,所以必须掌握json文件的读取与保存。

import json
#json文件读取
anno= json.load(open("test.json", "r", encoding="utf-8"))
#json文件保存
with open('test.json', 'w') as f:
    json.dump(ann, f)

2 opencv中文路径读写

        opencv默认的cv2.imwrite()和cv2.imread()函数是不支持中文路径的。

import os
import cv2
from pathlib import Path

#支持中文路径读取图片
def cv_imread(filePath):
    cv_img=cv2.imdecode(np.fromfile(filePath,dtype=np.uint8),-1)
    cv_img = cv2.cvtColor(cv_img, cv2.COLOR_RGB2BGR)
    return cv_img

#支持中文路径存储图片
cv2.imencode('.png', image)[1].tofile(save_dir+image_path)

3 矩形rectangle目标可视化

        定义线line目标可视化函数为vis_labelme_line_label。label_dir为labelme标注文件和原始图片存储目录,save_dir为可视化图片保存的文件目录。

import os
import cv2
from pathlib import Path

#支持中文路径
def cv_imread(filePath):
    cv_img=cv2.imdecode(np.fromfile(filePath,dtype=np.uint8),-1)
    cv_img = cv2.cvtColor(cv_img, cv2.COLOR_RGB2BGR)
    return cv_img

def vis_labelme_rect_label(label_dir, save_dir='res/'):
    label_dir = str(Path(label_dir)) + '/'
    save_dir  = str(Path(save_dir))  + '/'
    if not os.path.exists(save_dir):
        os.makedirs(save_dir)
    label_files = glob(label_dir+'*.json')
    for label_file in label_files:
        anno= json.load(open(label_file, "r", encoding="utf-8"))
        image_path = anno['imagePath']
        image = cv_imread(label_dir+image_path)
        shapes = anno['shapes']
        for s in shapes:
            pts = np.array(s['points']).astype(np.int)
            x1  = min(pts[:, 0])
            y1  = min(pts[:, 1])
            x2  = max(pts[:, 0])
            y2  = max(pts[:, 1])
            cv2.rectangle(image, (x1, y1), (x2, y2), (255, 0, 255), 1)
        cv2.imencode('.png', image)[1].tofile(save_dir+image_path)

4 线line目标可视化

        定义线line目标可视化函数为vis_labelme_line_label。label_dir为labelme标注文件和原始图片存储目录,save_dir为可视化图片保存的文件目录。

import os
import cv2
from pathlib import Path

#支持中文路径
def cv_imread(filePath):
    cv_img=cv2.imdecode(np.fromfile(filePath,dtype=np.uint8),-1)
    cv_img = cv2.cvtColor(cv_img, cv2.COLOR_RGB2BGR)
    return cv_img

def vis_labelme_line_label(label_dir, save_dir='res/'):
    label_dir = str(Path(label_dir)) + '/'
    save_dir  = str(Path(save_dir))  + '/'
    if not os.path.exists(save_dir):
        os.makedirs(save_dir)
    label_files = glob(label_dir+'*.json')
    for label_file in label_files:
        anno= json.load(open(label_file, "r", encoding="utf-8"))
        image_path = anno['imagePath']
        image = cv_imread(label_dir+image_path)
        shapes = anno['shapes']
        for s in shapes:
            pts = s['points']
            x1, y1 = pts[0]
            x2, y2 = pts[1]
            cv2.line(image, (int(x1), int(y1)), (int(x2), int(y2)), (255, 0, 255), 1)
        cv2.imencode('.png', image)[1].tofile(save_dir+image_path)

【python三维深度学习】python三维点云从基础到深度学习_Coding的叶子的博客-CSDN博客_三维点云深度学习

更多三维、二维感知算法和金融量化分析算法请关注“乐乐感知学堂”微信公众号,并将持续进行更新。

猜你喜欢

转载自blog.csdn.net/suiyingy/article/details/125622064