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
标注信息图如下
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
运行脚本执行转换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)
也可以用以下代码,查看类别对应的rgb颜色表
import imgviz
cm = imgviz.label_colormap()
print(f'len= {len(cm)}')
print(cm)
[[ 0 0 0]
[128 0 0]
[ 0 128 0]
[128 128 0]
[ 0 0 128]
[128 0 128]
[ 0 128 128]
[128 128 128]
[ 64 0 0]
[192 0 0]
[ 64 128 0]
[192 128 0]
[ 64 0 128]
[192 0 128]
[ 64 128 128]
[192 128 128]
[ 0 64 0]
[128 64 0]
[ 0 192 0]
[128 192 0]
[ 0 64 128]
[128 64 128]
[ 0 192 128]
[128 192 128]
[ 64 64 0]
[192 64 0]
[ 64 192 0]
[192 192 0]
[ 64 64 128]
[192 64 128]
[ 64 192 128]
[192 192 128]
[ 0 0 64]
[128 0 64]
[ 0 128 64]
[128 128 64]
[ 0 0 192]
[128 0 192]
[ 0 128 192]
[128 128 192]
[ 64 0 64]
[192 0 64]
[ 64 128 64]
[192 128 64]
[ 64 0 192]
[192 0 192]
[ 64 128 192]
[192 128 192]
[ 0 64 64]
[128 64 64]
[ 0 192 64]
[128 192 64]
[ 0 64 192]
[128 64 192]
[ 0 192 192]
[128 192 192]
[ 64 64 64]
[192 64 64]
[ 64 192 64]
[192 192 64]
[ 64 64 192]
[192 64 192]
[ 64 192 192]
[192 192 192]
[ 32 0 0]
[160 0 0]
[ 32 128 0]
[160 128 0]
[ 32 0 128]
[160 0 128]
[ 32 128 128]
[160 128 128]
[ 96 0 0]
[224 0 0]
[ 96 128 0]
[224 128 0]
[ 96 0 128]
[224 0 128]
[ 96 128 128]
[224 128 128]
[ 32 64 0]
[160 64 0]
[ 32 192 0]
[160 192 0]
[ 32 64 128]
[160 64 128]
[ 32 192 128]
[160 192 128]
[ 96 64 0]
[224 64 0]
[ 96 192 0]
[224 192 0]
[ 96 64 128]
[224 64 128]
[ 96 192 128]
[224 192 128]
[ 32 0 64]
[160 0 64]
[ 32 128 64]
[160 128 64]
[ 32 0 192]
[160 0 192]
[ 32 128 192]
[160 128 192]
[ 96 0 64]
[224 0 64]
[ 96 128 64]
[224 128 64]
[ 96 0 192]
[224 0 192]
[ 96 128 192]
[224 128 192]
[ 32 64 64]
[160 64 64]
[ 32 192 64]
[160 192 64]
[ 32 64 192]
[160 64 192]
[ 32 192 192]
[160 192 192]
[ 96 64 64]
[224 64 64]
[ 96 192 64]
[224 192 64]
[ 96 64 192]
[224 64 192]
[ 96 192 192]
[224 192 192]
[ 0 32 0]
[128 32 0]
[ 0 160 0]
[128 160 0]
[ 0 32 128]
[128 32 128]
[ 0 160 128]
[128 160 128]
[ 64 32 0]
[192 32 0]
[ 64 160 0]
[192 160 0]
[ 64 32 128]
[192 32 128]
[ 64 160 128]
[192 160 128]
[ 0 96 0]
[128 96 0]
[ 0 224 0]
[128 224 0]
[ 0 96 128]
[128 96 128]
[ 0 224 128]
[128 224 128]
[ 64 96 0]
[192 96 0]
[ 64 224 0]
[192 224 0]
[ 64 96 128]
[192 96 128]
[ 64 224 128]
[192 224 128]
[ 0 32 64]
[128 32 64]
[ 0 160 64]
[128 160 64]
[ 0 32 192]
[128 32 192]
[ 0 160 192]
[128 160 192]
[ 64 32 64]
[192 32 64]
[ 64 160 64]
[192 160 64]
[ 64 32 192]
[192 32 192]
[ 64 160 192]
[192 160 192]
[ 0 96 64]
[128 96 64]
[ 0 224 64]
[128 224 64]
[ 0 96 192]
[128 96 192]
[ 0 224 192]
[128 224 192]
[ 64 96 64]
[192 96 64]
[ 64 224 64]
[192 224 64]
[ 64 96 192]
[192 96 192]
[ 64 224 192]
[192 224 192]
[ 32 32 0]
[160 32 0]
[ 32 160 0]
[160 160 0]
[ 32 32 128]
[160 32 128]
[ 32 160 128]
[160 160 128]
[ 96 32 0]
[224 32 0]
[ 96 160 0]
[224 160 0]
[ 96 32 128]
[224 32 128]
[ 96 160 128]
[224 160 128]
[ 32 96 0]
[160 96 0]
[ 32 224 0]
[160 224 0]
[ 32 96 128]
[160 96 128]
[ 32 224 128]
[160 224 128]
[ 96 96 0]
[224 96 0]
[ 96 224 0]
[224 224 0]
[ 96 96 128]
[224 96 128]
[ 96 224 128]
[224 224 128]
[ 32 32 64]
[160 32 64]
[ 32 160 64]
[160 160 64]
[ 32 32 192]
[160 32 192]
[ 32 160 192]
[160 160 192]
[ 96 32 64]
[224 32 64]
[ 96 160 64]
[224 160 64]
[ 96 32 192]
[224 32 192]
[ 96 160 192]
[224 160 192]
[ 32 96 64]
[160 96 64]
[ 32 224 64]
[160 224 64]
[ 32 96 192]
[160 96 192]
[ 32 224 192]
[160 224 192]
[ 96 96 64]
[224 96 64]
[ 96 224 64]
[224 224 64]
[ 96 96 192]
[224 96 192]
[ 96 224 192]
[224 224 192]]
3:把数据整理为images,segmentations对应的文件夹
脚本如下: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)
至此数据集的制作完成。
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)