1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【画像関係】Labelmeの導入

Last updated at Posted at 2024-02-21

はじめに

LabelMeは画像にラベリングを行うツールで、MITの研究所?で開発、管理されているツールのようです。
画像にポリゴンによる領域指定をマウスクリックで行い、ラベルをJSONファイルに保存できます。
そして、ソフトを何とPIPでインストールできたりします。便利そうだけど、後述のようにQT関係で対応が必要でした。

Windows11 WSL2 で環境を用意したのでメモしておきます。poetry での動作確認の環境はこちらにコピーしています。

設定

インストール

PIPで入るのですが、依存ライブラリとか環境変数の設定が必要でした。
自分は、poetry で管理しているのでその過程をメモっておきます。

とりあえず下記を行いました。labelme だけだとQTが無いと怒られるので、入れます。

$ poetry init
$ poetry install
$ poetry add labelme pyqt5-tools
$ export LIBGL_ALWAYS_SOFTWARE=1
$ peotry run labelme
Traceback (most recent call last):
  File "/home/x77/labelme_setup/.venv/bin/labelme", line 5, in <module>
    from labelme.__main__ import main
  File "/home/x77/labelme_setup/.venv/lib/python3.10/site-packages/labelme/__init__.py", line 6, in <module>
    from qtpy import QT_VERSION
  File "/home/x77/labelme_setup/.venv/lib/python3.10/site-packages/qtpy/__init__.py", line 287, in <module>
    raise QtBindingsNotFoundError from None
qtpy.QtBindingsNotFoundError: No Qt bindings could be found

それでもライブラリが無いと怒られてます。詳しく見てみます。

$ export QT_DEBUG_PLUGINS=1
$ labelme

...

QFactoryLoader::QFactoryLoader() checking directory path "/home/x77/.pyenv/versions/3.10.6/bin/platforms" ...
Cannot load library /home/x77/labelme_setup/.venv/lib/python3.10/site-packages/PyQt5/Qt5/plugins/platforms/libqxcb.so: (libxcb-icccm.so.4: cannot open shared object file: No such file or directory)
QLibraryPrivate::loadPlugin failed on "/home/x77/labelme_setup/.venv/lib/python3.10/site-packages/PyQt5/Qt5/plugins/platforms/libqxcb.so" : "Cannot load library /home/x77/labelme_setup/.venv/lib/python3.10/site-packages/PyQt5/Qt5/plugins/platforms/libqxcb.so: (libxcb-icccm.so.4: cannot open shared object file: No such file or directory)"
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found.

と言われます。libqxcb.so で足りないものを見つけてインストールしました。

$ ldd .venv/lib/python3.10/site-packages/PyQt5/Qt5/plugins/platforms/libqxcb.so | grep "not found"
        libxcb-icccm.so.4 => not found
        libxcb-image.so.0 => not found
        libxcb-util.so.1 => not found
        libxcb-keysyms.so.1 => not found
        libxcb-render-util.so.0 => not found
        libxcb-xinerama.so.0 => not found
        libxcb-xkb.so.1 => not found
        libxkbcommon-x11.so.0 => not found
        libxcb-icccm.so.4 => not found
        libxcb-image.so.0 => not found
        libxcb-util.so.1 => not found
        libxcb-keysyms.so.1 => not found
        libxcb-render-util.so.0 => not found
        libxcb-xinerama.so.0 => not found
        libxcb-xkb.so.1 => not found
        libxkbcommon-x11.so.0 => not found
$ sudo apt -y install libxcb-icccm4
$ sudo apt -y install libxcb-image0
$ sudo apt -y install libxcb-keysyms1
$ sudo apt -y install libxcb-render-util0
$ sudo apt -y install libxcb-xinerama0
$ sudo apt -y install libxcb-xkb1
$ sudo apt -y install libxkbcommon-x11-0

apt で入れるものの名前の規則、分かってきました。^^;)
これらは、これを参考にしました。多謝。

libxcb.so のリンクが全て確認できて、無事に起動できた。(スクショは次節のAI mask を使っているところ)

$ labelme

image.png

使ってみると

使い方を語るほど使い込んでいませんが、画像ファイルをOpenで読み込み、Create Polygons で領域指定してLabel をつけ、File > Save で json ファイルに保存しました。

Image Data を保存しない

デフォルトだと、ラベルをjson で保存するとき、サムネイル画像も保存指定しサイズが大きくなります。起動時に

$ labelme --nodata 

とするとか、設定ファイル$HOME/.labelmerc)に

store_data: false

と書き換えればOKです。(初期設定ではここがtrue になっていました。)

json ファイル

ここでは下記のようになっていました。

{
  "version": "5.4.1",
  "flags": {},
  "shapes": [
    {
      "label": "bottle",
      "points": [
        [
          4207.142857142857,
          3032.142857142857
        ],
        ...
        [
          4342.857142857143,
          3146.428571428571
        ]
      ],
      "group_id": null,
      "description": "",
      "shape_type": "polygon",
      "flags": {},
      "mask": null
    }
  ],
  "imagePath": "DSC00033.JPG",
  "imageData": null,
  "imageHeight": 4000,
  "imageWidth": 6000
}

"shapes"が配列としてannotate されたlabel 情報を持っているようです。それぞれ、

AIの利用

その他、UIにある "Create AI polygon", "Create AI mask" を選択すると、裏で自動で

From: https://github.com/labelmeai/efficient-sam/releases/download/onnx-models-20231225/efficient_sam_vits_encoder.onnx
To: /home/x77/.cache/gdown/https-COLON--SLASH--SLASH-github.com-SLASH-labelmeai-SLASH-efficient-sam-SLASH-releases-SLASH-download-SLASH-onnx-models-20231225-SLASH-efficient_sam_vits_encoder.onnx

のようにダウンロードしてくれていました。

labelme2yolo というものを利用すると、学習用ラベルも作ってくれるらしく、いつか使ってみるかもです。

参考情報

意外と日本語の情報源が多かったです。みんな、使っているのかな。

まとめ

とりあえずラベル付けはできそうな気がしてきた。良かった。まだ数週間は生き延びることができそうな気がしてきた。神様に感謝。。。
(2024/2/22)

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?