语义分割数据集(多分类)制作

1:使用labelme标注工具

直接在命令行安装或者在anaconda下面新建虚拟环境安装(避免污染环境,不用的时候可以直接delete该环境)
直接命令行(base)安装

pip install labelme
labelme

创建虚拟环境安装,python版本选择3.6.x,打开Anaconda Prompt

conda create -n labelme python=3.6
conda activate labelme
pip install labelme
labelme

当前自动安装的版本为labelme-4.5.6
标注信息图如下


woman.png

plane.png

computer.png

train.png

2:使用提供的脚本json_to_dataset.py把json转换为png形式,对于多分类的数据标注,需要在下面路径找到json_to_dataset.py修改:

虚拟环境下的路径为
C:\Anaconda3\envs\labelme\Lib\site-packages\labelme\cli
cmd命令行(base)的路径为
C:\Anaconda3\Lib\site-packages\labelme\cli


需要修改的地方.png

运行脚本执行转换json操作,以下代码名为labelme_json_to_png.py

# -*- coding: utf-8 -*- 

import os

json_folder = r"C:\Users\eadhaw\Desktop\0120test"
#  获取文件夹内的文件名
FileNameList = os.listdir(json_folder)
for i in range(len(FileNameList)):
    #  判断当前文件是否为json文件
    if(os.path.splitext(FileNameList[i])[1] == ".json"):
        json_file = json_folder + "\\" + FileNameList[i]
        #  将该json文件转为png
        os.system("labelme_json_to_dataset " + json_file)

运行脚本.png

生成文件夹.png

验证类别对应.png

类别1红色的rgb值.png

类别2绿色的rgb值.png

类别3黄色的rgb值.png

类别4蓝色的rgb值.png

3:把数据整理为images,segmentations对应的文件夹

整理文件夹.png

脚本如下:spilit_labelme_dataset.py

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

def find_dir_path(path, keyword_name, dir_list):
    files = os.listdir(path)
    for file_name in files:
        file_path = os.path.join(path, file_name)
        if os.path.isdir(file_path) and keyword_name not in file_path:
            find_dir_path(file_path, keyword_name, dir_list)
        elif os.path.isdir(file_path) and keyword_name in file_path:
            dir_list.append(file_path)
            

all_result_path = []
src_path = r'C:\Users\eadhaw\Desktop\0120test'
label_save_path = r'C:\Users\eadhaw\Desktop\0120test\segmentations'
image_save_path = r'C:\Users\eadhaw\Desktop\0120test\images'
find_dir_path(src_path, '_json', all_result_path)              # 找出所有带着关键词(_json)的所有目标文件夹
#print(all_result_path)   


for dir_path in all_result_path:
    # print(dir_path)
    file_name = dir_path.split('\\')[-1]
    key_word = file_name[:-5]
    # print(key_word)
    label_file = dir_path + "\\" + "label.png"
    new_label_save_path = label_save_path + "\\" + key_word + ".png"      # 复制生成的label.png到新的文件夹segmentations
    #print(new_label_save_path)
    shutil.copyfile(label_file, new_label_save_path)

    img_dir = os.path.dirname(dir_path)                         # 复制原图到新的文件夹images
    img_file = img_dir + "\\" + key_word + ".jpg"
    new_img_save_path = image_save_path + "\\" + key_word + ".jpg"
    shutil.copyfile(img_file, new_img_save_path)

4: 把png转换为label图([128,0,0]对应类别1,其他依次类推)

代码如下:

# -*- coding: utf-8 -*-
import os
import cv2
import numpy as np
import matplotlib.pyplot as plt
import skimage.io as io
import skimage
print("skimage:", skimage.__version__)    #  '0.14.2'

color2index = {
    (0,0,0) : 0,
    (128,0,0) : 1,
    (0,128,0) : 2,
    (128,128,0) : 3,
    (0,0,128) : 4,
}
def rgb2mask(img):

    assert len(img.shape) == 3
    height, width, ch = img.shape
    assert ch == 3

    W = np.power(256, [[0],[1],[2]])

    img_id = img.dot(W).squeeze(-1) 
    values = np.unique(img_id)
    mask = np.zeros(img_id.shape)

    for i, c in enumerate(values):
        try:
            mask[img_id==c] = color2index[tuple(img[img_id==c][0])] 
        except:
            pass
    return mask

path = "C:/Users/eadhaw/Desktop/0120test/segmentations/"
new_path = "C:/Users/eadhaw/Desktop/0120test/labels/"
files = os.listdir(path)

for filename in files:
    f_path = path + filename
    print(f_path)
    img = io.imread(f_path)
    print(img.shape)
    #判断一下读取的图像是否为三通道格式,rgba则要转换
    if img.shape[2] == 4:
        img = cv2.cvtColor(img, cv2.COLOR_BGRA2BGR)
    mask = rgb2mask(img)
    mask = mask.astype(np.uint8)
    print(mask.shape)
    f_new_path = new_path + filename
    io.imsave(f_new_path,mask)

labels结果图.png

至此数据集的制作完成。

PS:bmp,png,jpg的相互转换

path = "C:/Users/eadhaw/Desktop/0120test/images/"
new_path = "C:/Users/eadhaw/Desktop/0120test/png/"
files = os.listdir(path)
#files.sort()

for filename in files:
    f_path = path + filename
    print(f_path)
    img = cv2.imread(f_path)
    print(img.shape)
    
    newname=filename.split(".")[0]+".png"  # 此处可更换想要转换的类型
    print(newname)
    f_new_path = new_path + newname
    cv2.imwrite(f_new_path,img)

你可能感兴趣的:(语义分割数据集(多分类)制作)