json转voc数据集,eiseg json转voc

此案例是用eiseg 保存的json转成voc掩码图片,如果是labelme 只需修改部分代码

主要核心函数:cv2.fillPoly

不懂的自行百度吧, 需要批量的自行添加

# -*- coding: utf-8 -*-
import json
import cv2
import numpy as np

def cvt_one(json_path, img_path, save_path, label_color):
    # 加载json 和 图片原图
    data = json.load(open(json_path, encoding='gbk'))
    img = cv2.imread(img_path)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)  # 图片二值化

    # get background data
    img_h = img.shape[0]
    img_w = img.shape[1]
    # 生产图片大小图片
    points_bg = [(0, 0), (0, img_h), (img_w, img_h), (img_w, 0)]
    img = cv2.fillPoly(img, [np.array(points_bg)], 0)
    # 将多边形坐标绘制到图片中
    for item in data:
        className = item.get("name")
        points = item.get("points")
        img = cv2.fillPoly(img, [np.array(points, dtype=int)], label_color[className])
    cv2.imwrite(save_path, img)



if __name__ == '__main__':
    json_path = r"D:\CodeBase\dataset_tool\imgAll\0000001.json"
    img_path = r"D:\CodeBase\dataset_tool\imgAll\0000001.jpg"
    save_path = r"D:\CodeBase\dataset_tool\imgAll\0000001.png"
    label_color = {
        'cat': 1,
        'god': 2,
    }

    cvt_one(json_path, img_path, save_path, label_color)


你可能感兴趣的:(json,numpy,python)