CVAT半自动化标注服务搭建

katekate 发布于 2 天前 2 次阅读


首先按照官方教程安装CVAT:https://docs.cvat.ai/docs/administration/basics/installation/,建议使用Ubuntu22.04版本,较为稳定。

CVAT使用无服务器函数进行自动标注模型的部署。此外,当需要使用自动标注模型的时候,不应直接使用docker compose up -d直接启动服务,而应该执行如下命令:

docker compose -f docker-compose.yml -f components/serverless/docker-compose.serverless.yml up -d

如果之前使用这个命令运行CVAT,需要进行停止:

docker compose -f docker-compose.yml -f components/serverless/docker-compose.serverless.yml down

安装nuctl(使用版本1.13.0):

wget https://github.com/nuclio/nuclio/releases/download/1.13.0/nuctl-1.13.0-linux-amd64

给予权限和软连接:

sudo chmod +x nuctl-1.13.0-linux-amd64
sudo ln -sf $(pwd)/nuctl-1.13.0-linux-amd64 /usr/local/bin/nuctl

部署无服务器函数(当且仅当CVAT启动完毕之后,再执行如下命令):

./serverless/deploy_cpu.sh serverless/openvino/dextr
./serverless/deploy_cpu.sh serverless/openvino/omz/public/yolo-v3-tf

使用GPU,需要对函数进行修改,并使用./serverless/depoly_gpu.sh。

示例:部署一个yolov8训练的权重的配置文件:

serverless目录下创建自定义函数目录

mkdir -p serverless/pytorch/ultralytics/yolov8/nuclio

创建镜像构建文件function-gpu.yaml

metadata:
  name: emdetector
  namespace: cvat
  annotations:
    name: emdetect
    type: detector
    framework: pytorch
    spec: |
      [
        { "id": 0, "name": "EM14", "type": "rectangle" },
        { "id": 1, "name": "EM18", "type": "rectangle" },
        { "id": 2, "name": "EM17", "type": "rectangle" },
      ]      

spec:
  description: 工位检测
  runtime: "python:3.11"
  handler: main:handler
  eventTimeout: 30s

  build:
    image: cvat.pth.yolo11.emdetector:latest-gpu
    baseImage: python:3.9
    directives:
      preCopy:
        - kind: ENV
          value: DEBIAN_FRONTEND=noninteractive
        - kind: RUN
          value: apt-get update && apt-get install -y libgl1 libglib2.0-0 && apt-get clean
        - kind: RUN
          value: pip install ultralytics torch torchvision opencv-python-headless && pip cache purge

  triggers:
    myHttpTrigger:
      numWorkers: 1
      kind: 'http'
      workerAvailabilityTimeoutMilliseconds: 10000
      attributes:
        # Set value from the calculation of tracking of 100 objects at the same time on a 4k image
        maxRequestBodySize: 268435456 # 256MB

  volumes:
    - volume:
        name: model-volume
        hostPath:
          path: /path/to/models/emdetection/best.pt  # 可选:如果使用HostPath挂载模型
      volumeMount:
        name: model-volume
        mountPath: /opt/nuclio/best.pt

  resources:
    limits:
      nvidia.com/gpu: 1

  platform:
    attributes:
      restartPolicy:
        name: always
        maximumRetryCount: 3
      mountMode: volume

创建函数入口文件main.py

import json
import base64
# from PIL import Image
import io
from model_handler import ModelHandler

def init_context(context):
    context.logger.info("Init context...  0%")

    context.logger.info("Initializing EMDetection model...")
    context.user_data.model_handler = ModelHandler()

    context.logger.info("Init context...100%")

def handler(context, event):
    context.logger.info("Run EMDetection model")
    data = event.body
    image_data = base64.b64decode(data["image"])
    threshold = float(data.get("threshold", 0.5))

    results = context.user_data.model_handler.infer(image_data, threshold)

    return context.Response(body=json.dumps(results), headers={},
        content_type='application/json', status_code=200)

创建推理函数文件model_handler.py

import io
from PIL import Image
from ultralytics import YOLO


class ModelHandler:
    def __init__(self):
        """加载 YOLOv11 模型"""
        self.model = YOLO("/opt/nuclio/best.pt")  # 确保路径正确

    def infer(self, image_data, threshold=0.3):
        """
        执行推理
        :param image_data: 图片的二进制数据
        :param threshold: 置信度阈值(默认0.3)
        :return: 符合阈值的检测结果
        """
        image = Image.open(io.BytesIO(image_data))
        results = self.model(image)

        detections = []
        for result in results:
            for box in result.boxes.data.tolist():
                x1, y1, x2, y2, score, class_id = box
                if score >= threshold:  # 过滤低置信度目标
                    detections.append({
                        "confidence": score,
                        "label": self.model.names[int(class_id)],
                        "points": [x1, y1, x2, y2],
                        "type": "rectangle",
                    })

        return detections

将这些文件上传至服务器指定目录,并运行GPU部署命令。即可完成部署。

此作者没有提供个人介绍
最后更新于 2025-10-30