參考:圖像語義分割標註工具labelme製作自己的數據集用於mask-rcnn訓練
1.安裝
labelme(標註mask數據集用的)github地址
python3環境
pip install pyqt5
pip install labelme
建議使用國內鏡像:
pip install pyqt5 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install labelme -i https://pypi.tuna.tsinghua.edu.cn/simple
2.使用
(1)打開軟件
終端執行:
labelme
(2)標註圖片
點擊Open Dir
,選擇文件夾;點擊Create Polygons
開始標註;按字母D
完成標註跳往下一張,這事會彈出保存界面,默認保存在圖片所在文件夾。右鍵可選擇取消當前標註點選項(ctrl+z
)。
相同類的多個對象需要在類名後加數字。
(3) 標註文件處理
標註完成後會產生一個對應的圖片名.json
文件
此時需要進入圖片所指的文件夾,執行下面的程序生成下面的標註圖:
json_to_dataset <文件名>.json
沒有可以取github複製個文件過來,可以直接使用。
https://github.com/wkentaro/labelme/blob/master/labelme/cli/json_to_dataset.py
(這裏我的版本缺少了.yaml文件,不過不影響,可以參考:labelme:缺少生成"info.yaml"文件)
但是批量處理圖片需要寫腳本。
可使用下面的程序:唯一輸入是隻帶.json文件的文件夾
參考:關於json_to_dataset時出現 module ‘labelme.utils’ has no attribute 'draw_label’的解決
# labelme_json_to_dataset_batch.py
import argparse
import base64
import json
import os
import os.path as osp
import imgviz
import PIL.Image
from labelme.logger import logger
from labelme import utils
import cv2
from math import *
import numpy as np
import random
def main():
# 唯一輸入:
# 給出只帶.json文件的文件夾
label_path = r"E:/pics/json/"
list_path = os.listdir(label_path)
for i in range(0, len(list_path)):
logger.warning('This script is aimed to demonstrate how to convert the'
'JSON file to a single image dataset, and not to handle'
'multiple JSON files to generate a real-use dataset.')
parser = argparse.ArgumentParser()
parser.add_argument('--json_file')
parser.add_argument('-o', '--out', default=None)
args = parser.parse_args()
json_file = label_path + list_path[i]
print(list_path[i])
if args.out is None:
out_dir = osp.basename(json_file).replace('.', '_') # 返回文件名
out_dir = osp.join(osp.dirname(json_file), out_dir) # 把目錄和文件名合成一個路徑
else:
out_dir = args.out
if not osp.exists(out_dir):
os.mkdir(out_dir) # 用於以數字權限模式創建目錄
data = json.load(open(json_file))
imageData = data.get('imageData')
if not imageData:
imagePath = os.path.join(os.path.dirname(json_file), data['imagePath']) # os.path.dirname返回文件路徑
with open(imagePath, 'rb') as f:
imageData = f.read()
imageData = base64.b64encode(imageData).decode('utf-8')
img = utils.img_b64_to_arr(imageData)
label_name_to_value = {'_background_': 0}
for shape in sorted(data['shapes'], key=lambda x: x['label']):
label_name = shape['label']
if label_name in label_name_to_value:
label_value = label_name_to_value[label_name]
else:
label_value = len(label_name_to_value)
label_name_to_value[label_name] = label_value
lbl, _ = utils.shapes_to_label(
img.shape, data['shapes'], label_name_to_value
)
label_names = [None] * (max(label_name_to_value.values()) + 1)
for name, value in label_name_to_value.items():
label_names[value] = name
lbl_viz = imgviz.label2rgb(
label=lbl, img=imgviz.asgray(img), label_names=label_names, loc='rb'
)
PIL.Image.fromarray(img).save(osp.join(out_dir, 'img.png'))
utils.lblsave(osp.join(out_dir, 'label.png'), lbl)
PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, 'label_viz.png'))
with open(osp.join(out_dir, 'label_names.txt'), 'w') as f:
for lbl_name in label_names:
f.write(lbl_name + '\n')
logger.info('Saved to: {}'.format(out_dir))
if __name__ == '__main__':
main()