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

2023-12-13 08:33:00

1.创建labelme虚拟环境

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

conda create -n labelme python=3.8
conda activate labelme

(2)安装labelme

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

(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",
        ),
    ),
]

(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())
        )

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

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

参考链接:labelme加载AI模型

文章来源:https://blog.csdn.net/m0_49519243/article/details/134827020
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。