当前位置:   article > 正文

如何使用labelme中的AI多边形(AI-polygon)标注_labelme 自动标注

labelme 自动标注

1.创建labelme虚拟环境

(1)创建基础环境并激活

conda create -n labelme python=3.8
conda activate labelme
  • 1
  • 2

(2)安装labelme

pip install labelme -i https://pypi.tuna.tsinghua.edu.cn/simple/ numpy
  • 1

(3)使用labelme启动
在这里插入图片描述

如果是第一次装labelme,打开图像路径,右键图像后选择Create AI-Polygon,软件会自动下载并安装AI标注模型,我的下载速度太慢,导致第一次下载失败,最后选择了手动安装。
在这里插入图片描述

2.下载AI标注模型

可以选择在官网上下载AI自动标注模型下载地址
在这里插入图片描述
如果连不到外网,可以通过迅雷网盘或者百度网盘提取模型

迅雷网盘链接:https://pan.xunlei.com/s/VNkyiDkG9ORZRr7Mhx4ru3I8A1#
提取码:2dbf

百度网盘链接:https://pan.baidu.com/s/11xrWH4p_auHl-cKYjZ899Q?pwd=lg1j
提取码:lg1j

在anaconda虚拟环境中找到E:\programFiles\anaconda3\envs\labelme\Lib\site-packages\labelme此路径,将下载好的文件放入此文件夹下。
在这里插入图片描述

3.修改配置文件

(1)找到"E:\programFiles\anaconda3\envs\labelme\Lib\site-packages\labelme\ai\__init__.py"文件,并修改里面的模型路径。

# flake8: noqa

import logging
import sys

from qtpy import QT_VERSION


__appname__ = "labelme"

# Semantic Versioning 2.0.0: https://semver.org/
# 1. MAJOR version when you make incompatible API changes;
# 2. MINOR version when you add functionality in a backwards-compatible manner;
# 3. PATCH version when you make backwards-compatible bug fixes.
# e.g., 1.0.0a0, 1.0.0a1, 1.0.0b0, 1.0.0rc0, 1.0.0, 1.0.0.post0
__version__ = "5.4.0a0"

QT4 = QT_VERSION[0] == "4"
QT5 = QT_VERSION[0] == "5"
del QT_VERSION

PY2 = sys.version[0] == "2"
PY3 = sys.version[0] == "3"
del sys

from labelme.label_file import LabelFile
from labelme import testing
from labelme import utils
import collections

from .models.segment_anything import SegmentAnythingModel  # NOQA


Model = collections.namedtuple(
    "Model", ["name", "encoder_weight", "decoder_weight"]
)

Weight = collections.namedtuple("Weight", ["url", "md5"])

# MODELS = [
#     Model(
#         name="Segment-Anything (speed)",
#         encoder_weight=Weight(
#             url="https://github.com/wkentaro/labelme/releases/download/sam-20230416/sam_vit_b_01ec64.quantized.encoder.onnx",  # NOQA
#             md5="80fd8d0ab6c6ae8cb7b3bd5f368a752c",
#         ),
#         decoder_weight=Weight(
#             url="https://github.com/wkentaro/labelme/releases/download/sam-20230416/sam_vit_b_01ec64.quantized.decoder.onnx",  # NOQA
#             md5="4253558be238c15fc265a7a876aaec82",
#         ),
#     ),
#     Model(
#         name="Segment-Anything (balanced)",
#         encoder_weight=Weight(
#             url="https://github.com/wkentaro/labelme/releases/download/sam-20230416/sam_vit_l_0b3195.quantized.encoder.onnx",  # NOQA
#             md5="080004dc9992724d360a49399d1ee24b",
#         ),
#         decoder_weight=Weight(
#             url="https://github.com/wkentaro/labelme/releases/download/sam-20230416/sam_vit_l_0b3195.quantized.decoder.onnx",  # NOQA
#             md5="851b7faac91e8e23940ee1294231d5c7",
#         ),
#     ),
#     Model(
#         name="Segment-Anything (accuracy)",
#         encoder_weight=Weight(
#             url="https://github.com/wkentaro/labelme/releases/download/sam-20230416/sam_vit_h_4b8939.quantized.encoder.onnx",  # NOQA
#             md5="958b5710d25b198d765fb6b94798f49e",
#         ),
#         decoder_weight=Weight(
#             url="https://github.com/wkentaro/labelme/releases/download/sam-20230416/sam_vit_h_4b8939.quantized.decoder.onnx",  # NOQA
#             md5="a997a408347aa081b17a3ffff9f42a80",
#         ),
#     ),
# ]
MODELS = [
    Model(
        name="Segment-Anything (speed)",
        encoder_weight=Weight(
            url="E:\programFiles\\anaconda3\envs\labelme\Lib\site-packages\labelme\model_file\sam_vit_b_01ec64.quantized.encoder.onnx",  # NOQA
            md5="80fd8d0ab6c6ae8cb7b3bd5f368a752c",
        ),
        decoder_weight=Weight(
            url="E:\programFiles\\anaconda3\envs\labelme\Lib\site-packages\labelme\model_file\sam_vit_b_01ec64.quantized.decoder.onnx",  # NOQA
            md5="4253558be238c15fc265a7a876aaec82",
        ),
    ),
    Model(
        name="Segment-Anything (balanced)",
        encoder_weight=Weight(
            url="E:\\programFiles\\anaconda3\\envs\\labelme\\Lib\\site-packages\\labelme\\model_file\\sam_vit_l_0b3195.quantized.encoder.onnx",  # NOQA
            md5="080004dc9992724d360a49399d1ee24b",
        ),
        decoder_weight=Weight(
            url="E:\\programFiles\\anaconda3\\envs\\labelme\\Lib\\site-packages\\labelme\\model_file\\sam_vit_l_0b3195.quantized.decoder.onnx",  # NOQA
            md5="851b7faac91e8e23940ee1294231d5c7",
        ),
    ),
    Model(
        name="Segment-Anything (accuracy)",
        encoder_weight=Weight(
            url="E:\\programFiles\\anaconda3\\envs\\labelme\\Lib\\site-packages\\labelme\\model_file\\sam_vit_h_4b8939.quantized.decoder.onnx",  # NOQA
            md5="958b5710d25b198d765fb6b94798f49e",
        ),
        decoder_weight=Weight(
            url="E:\\programFiles\\anaconda3\\envs\\labelme\\Lib\\site-packages\\labelme\\model_file\\sam_vit_h_4b8939.quantized.encoder.onnx",  # NOQA
            md5="a997a408347aa081b17a3ffff9f42a80",
        ),
    ),
]

  • 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
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110

(2)找到E:\programFiles\anaconda3\envs\labelme\Lib\site-packages\labelme\widgets\canvas.py文件夹并修改initializeAiModel方法

    def initializeAiModel(self, name):
        if name not in [model.name for model in labelme.ai.MODELS]:
            raise ValueError("Unsupported ai model: %s" % name)
        model = [model for model in labelme.ai.MODELS if model.name == name][0]

        if self._ai_model is not None and self._ai_model.name == model.name:
            logger.debug("AI model is already initialized: %r" % model.name)
        else:
            logger.debug("Initializing AI model: %r" % model.name)
            self._ai_model = labelme.ai.SegmentAnythingModel(
                name=model.name,
                # encoder_path=gdown.cached_download(
                #     url=model.encoder_weight.url,
                #     md5=model.encoder_weight.md5,
                # ),
                # decoder_path=gdown.cached_download(
                #     url=model.decoder_weight.url,
                #     md5=model.decoder_weight.md5,
                # ),
                encoder_path=model.encoder_weight.url,
                decoder_path=model.decoder_weight.url,
            )

        self._ai_model.set_image(
            image=labelme.utils.img_qt_to_arr(self.pixmap.toImage())
        )

  • 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

4.愉快地使用labelme的AI标注工具

这样再激活虚拟环境,使用labelme命令打开标注工具,右键选择AI标注,双击标注完成。
在这里插入图片描述

参考链接:labelme加载AI模型

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/码创造者/article/detail/749833
推荐阅读
相关标签
  

闽ICP备14008679号