LabelMe使用

原文鏈接:https://blog.csdn.net/qq_31347869/article/details/91180129

LabelMe 可用於實例分割,語義分割,目標檢測,分類任務的數據集標註工作。

在線標註版本:http://labelme2.csail.mit.edu/Release3.0/index.php?message=1
python 版本:https://github.com/wkentaro/labelme

官方文檔:

分類標註:Classification
目標檢測標註:Object Detection
語義分割標註:Semantic Segmentation
實例分割標註:Instance Segmentation
視頻標註:Video Annotation
其他形式標註:LabelMe Primitives

python版本使用

使用環境:Anaconda
python版本:2.7 / 3.6

1. 安裝

# python2.7版本安裝
conda create --name=labelme python=2.7
source activate labelme
# conda install -c conda-forge pyside2
conda install pyqt
pip install labelme
# if you'd like to use the latest version. run below:
# pip install git+https://github.com/wkentaro/labelme.git

# python3.6版本安裝
conda create --name=labelme python=3.6
source activate labelme
# conda install -c conda-forge pyside2
# conda install pyqt
pip install pyqt5 # pyqt5 can be installed via pip on python3
pip install labelme

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

2. 使用

# 查看labelme使用方法
labelme --help

(labelme) @supermicro:~$ labelme --help
usage: labelme [-h] [--version] [--reset-config]
[--logger-level {debug,info,warning,fatal,error}]
[--output OUTPUT] [--config CONFIG_FILE] [--nodata]
[--autosave] [--nosortlabels] [--flags FLAGS]
[--labelflags LABEL_FLAGS] [--labels LABELS]
[--validatelabel {exact,instance}] [--keep-prev]
[--epsilon EPSILON]
[filename]

positional arguments:
filename image or label filename

optional arguments:
-h, --help show this help message and exit
--version, -V show version
--reset-config reset qt config
--logger-level {debug,info,warning,fatal,error}
logger level
--output OUTPUT, -O OUTPUT, -o OUTPUT
output file or directory (if it ends with .json it is
recognized as file, else as directory)
--config CONFIG_FILE config file (default: /home/zyy/.labelmerc)
--nodata stop storing image data to JSON file
--autosave auto save
--nosortlabels stop sorting labels
--flags FLAGS comma separated list of flags OR file containing flags
--labelflags LABEL_FLAGS
yaml string of label specific flags OR file containing
json string of label specific flags (ex. {person-\d+:
[male, tall], dog-\d+: [black, brown, white], .*:
[occluded]})
--labels LABELS comma separated list of labels OR file containing
labels
--validatelabel {exact,instance}
label validation types
--keep-prev keep annotation of previous frame
--epsilon EPSILON epsilon to find nearest vertex on canvas

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • -h–help 顯示幫助信息
  • -V--version 顯示 labelme 版本號
  • --output:指定輸出標註文件的保存路徑,如果路徑以 .json 結尾,則保存爲一個 .json 文件,否則默認保存爲文件夾形式
  • --labels:用於指定標籤名稱,可以是用逗號分隔的 label list,也可以是包含標籤的 txt 文件
  • --nodata:不保存圖像到 JSON 文件

3. 分割任務標註示例

終端直接輸入:

# 直接打開labelme
labelme
  • 1
  • 2
  • open:打開某一張圖片
  • openDir:打開某一文件夾,加載其目錄下的所有圖片

通過 open 讀取圖片,選擇 create polygons 手動進行勾畫,全部完成後保存爲 json 文件(在當前目錄下):
在這裏插入圖片描述
右鍵單擊可以選擇不同的標註方式,比如 polygons 用於分割,rectangle 用於檢測。

如果是實例分割,一個圖像中有多隻貓,標籤的命名規則爲:cat1、cat2 …,如果是語義分割就不用區分了。
在這裏插入圖片描述
labelme 可以進行多類別標註,Label List 顯示當前已有的類別,Polygon Labels 顯示當前已標註的區域,通過勾選 Polygon Labels 前面的 “√”,可以選擇顯示特定的分割區域:
在這裏插入圖片描述
要得到 label 文件,需要將 json 轉換爲單通道的 image,終端輸入命令:

# 進入json文件保存目錄
cd /path/to/your/jsonfile
# 轉換
labelme_json_to_dataset <文件名>.json
# 比如
labelme_json_to_dataset cat.1.json
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

將在當前目錄下得到一個文件夾 cat_1_json,包括四個文件:

  • img.png:原始圖像
  • label.png:標籤,uint8
  • label_viz.png:可視化的帶標籤圖像
  • label_names.txt:記錄了標籤的名稱

img.png
img.png
label.png:
label.png
label_viz.png:
label_viz.png

4. 其他說明

(1)啓動 labelme 的方式:

# 直接打開labelme
labelme

# 打開某個文件夾,加載該文件夾下及其子文件夾下的所有圖片
labelme path/to/imgfile/

# 直接打開指定的圖片
labelme cat.1.jpg

# 標註保存爲json文件同時自動關閉gui窗口
labelme cat.1.jpg -O cat.1.jpg.json

# 指定label list
labelme cat.1.jpg
--labels cat,eye
# 或者傳入文件形式的label list
--labels labels.txt

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

(2)將 JSON 文件轉換爲 image 和 label

# 在當前目錄下生成一個文件夾cat_1_json
labelme_json_to_dataset cat.1.json

# 指定生成文件夾的名字爲cat1
labelme_json_to_dataset cat.1.json -o cat1

  • 1
  • 2
  • 3
  • 4
  • 5

(3)可視化 json 文件:

# 終端輸入
labelme_draw_json cat.1.json
  • 1
  • 2

在這裏插入圖片描述

5. 加載標籤png

label.png 用 scipy.misc.imread 或者 skimage.io.imread 讀取可能會出錯,推薦用 PIL.Image.open 讀取:

>>> import numpy as np
>>> import PIL.Image

>>> label_png = ‘imgs/cat1/label.png’ # 設置標籤文件路徑
>>> lbl = np.asarray(PIL.Image.open(label_png))
>>> print(lbl.dtype)
dtype(‘uint8’)
>>> np.unique(lbl)
array([0, 1, 2], dtype=uint8)
>>> lbl.shape
(280, 300)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

查看 label.png:

# 終端輸入
labelme_draw_label_png imgs/cat1/label.png
  • 1
  • 2

在這裏插入圖片描述

6. 生成VOC格式的標籤數據

在下載的 labelme 的 zip 包裏,路徑 labelme/examples/semantic_segmentation 下,data_annotated 是原圖和對應的 JSON 文件,data_dataset_voc 是 voc 格式的輸出結果,labelme2voc.py 是轉換的主函數,labels.txt 是標籤類別。

1)文件組織形式如下:

  • *_annotated 存放原圖和已經生成的對應 JSON 文件
  • 將 labelme 工程文件下的 labelme2voc.py 複製過來
  • 自己寫一個 *.txt 文件,內容是分割的標籤,最前面加上 __ignore___background_
    labels.txt 文件內容
    文件組織格式
    文件組織格式

2)轉換爲voc數據格式:

# 終端輸入
./labelme2voc.py [圖像路徑] [voc文件夾名稱] --labels [label list]

# 比如
./labelme2voc.py cat_annotated cat_dataset_voc --labels labels.txt

  • 1
  • 2
  • 3
  • 4
  • 5

在當前目錄下自動生成 cat_dataset_voc 文件夾(轉換前確保不要有重名文件夾,否則會報錯)
在這裏插入圖片描述
在這裏插入圖片描述

cat_dataset_voc 文件夾內容:
在這裏插入圖片描述
轉換爲 COCO 數據格式,同樣的套路:

./labelme2coco.py data_annotated data_dataset_coco --labels labels.txt
  • 1
                                </div>
            <link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-e44c3c0e64.css" rel="stylesheet">
                </div>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章