YOLO视觉分析服务

通过Model Context Protocol (MCP)与Claude AI集成的YOLO计算机视觉服务,提供物体检测、分割、分类和实时摄像头分析功能。

  • by GongRzhe
  • Python
  • 本地部署
  • 38 星标
  • 更新于
在 GitHub 查看 GongRzhe/YOLO-MCP-Server

使用说明

YOLO MCP服务

强大的YOLO(You Only Look Once)计算机视觉服务,通过模型上下文协议(MCP)与Claude AI集成。该服务使Claude能够使用最先进的YOLO模型进行对象检测、分割、分类和实时相机分析。

特性

  • 目标检测、分割、分类和姿态估计
  • 实时摄像头集成用于活体检测
  • 支持模型训练、验证和导出
  • 结合多种模型的综合图像分析
  • 支持文件路径和base64编码图像
  • 与Claude AI无缝集成

安装说明

先决条件

  • Python 3.10或更高版本
  • Git(可选,用于克隆存储库)

环境设置

  1. 为项目创建一个目录并导航到该目录:

    mkdir yolo-mcp-service
    cd yolo-mcp-service
    
  2. 从存储库下载项目文件或克隆:

    # If you have the files, copy them to this directory
    # If using git:
    git clone https://github.com/GongRzhe/YOLO-MCP-Server.git .
    
  3. 创建虚拟环境:

    # On Windows
    python -m venv .venv
    
    # On macOS/Linux
    python3 -m venv .venv
    
  4. 激活虚拟环境:

    # On Windows
    .venv\Scripts\activate
    
    # On macOS/Linux
    source .venv/bin/activate
    
  5. 运行安装脚本:

    python setup.py
    

    安装脚本将:

    • 检查你的Python版本
    • 创建虚拟环境(如果尚未创建)
    • 安装所需的依赖项
    • 生成MCP配置文件(MCP-config.json)
    • 输出包括Claude在内的不同MCP客户端的配置信息
  6. 注意安装脚本的输出,它看起来类似于:

    MCP configuration has been written to: /path/to/mcp-config.json
    
    MCP configuration for Cursor:
    
    /path/to/.venv/bin/python /path/to/server.py
    
    MCP configuration for Windsurf/Claude Desktop:
    {
      "mcpServers": {
        "yolo-service": {
          "command": "/path/to/.venv/bin/python",
          "args": [
            "/path/to/server.py"
          ],
          "env": {
            "PYTHONPATH": "/path/to"
          }
        }
      }
    }
    
    To use with Claude Desktop, merge this configuration into: /path/to/claude_desktop_config.json
    

下载YOLO型号

在使用该服务之前,您需要下载YOLO型号。该服务在以下目录中查找模型:

  • 服务运行的当前目录
  • A. models 子目录
  • 在中配置的任何其他目录 CONFIG["model_dirs"] server.py中的变量

创建一个模型目录并下载一些常见模型:

# Create models directory
mkdir models

# Download YOLOv8n for basic object detection
curl -L https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8n.pt -o models/yolov8n.pt

# Download YOLOv8n-seg for segmentation
curl -L https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8n-seg.pt -o models/yolov8n-seg.pt

# Download YOLOv8n-cls for classification
curl -L https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8n-cls.pt -o models/yolov8n-cls.pt

# Download YOLOv8n-pose for pose estimation
curl -L https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8n-pose.pt -o models/yolov8n-pose.pt

对于Windows PowerShell用户:

# Create models directory
mkdir models

# Download models using Invoke-WebRequest
Invoke-WebRequest -Uri "https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8n.pt" -OutFile "models/yolov8n.pt"
Invoke-WebRequest -Uri "https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8n-seg.pt" -OutFile "models/yolov8n-seg.pt"
Invoke-WebRequest -Uri "https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8n-cls.pt" -OutFile "models/yolov8n-cls.pt"
Invoke-WebRequest -Uri "https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8n-pose.pt" -OutFile "models/yolov8n-pose.pt"

配置Claude

要与Claude一起使用此服务,请执行以下操作:

  1. 对于Claude web:在本地计算机上设置服务,并使用MCP客户端中设置脚本提供的配置。

  2. 对于Claude Desktop:

    • 运行安装脚本并记录配置输出
    • 找到您的Claude Desktop配置文件(路径在安装脚本输出中提供)
    • 将配置添加或合并到您的Claude Desktop配置文件中
    • 重新启动克劳德桌面

在Claude中使用YOLO工具

1.首先检查可用型号

始终先检查您的系统上有哪些型号可用:

I'd like to use the YOLO tools. Can you first check which models are available on my system?

<function_calls>
<invoke name="list_available_models">
</invoke>
</function_calls>

2.检测图像中的对象

要分析计算机上的图像文件:

Can you analyze this image file for objects?

<function_calls>
<invoke name="analyze_image_from_path">
<parameter name="image_path">/path/to/your/image.jpg</parameter>
<parameter name="confidence">0.3</parameter>
</invoke>
</function_calls>

您还可以指定其他模型:

Can you analyze this image using a different model?

<function_calls>
<invoke name="analyze_image_from_path">
<parameter name="image_path">/path/to/your/image.jpg</parameter>
<parameter name="model_name">yolov8n.pt</parameter>
<parameter name="confidence">0.4</parameter>
</invoke>
</function_calls>

3.运行综合图像分析

有关结合对象检测、分类等的更详细分析:

Can you perform a comprehensive analysis on this image?

<function_calls>
<invoke name="comprehensive_image_analysis">
<parameter name="image_path">/path/to/your/image.jpg</parameter>
<parameter name="confidence">0.3</parameter>
</invoke>
</function_calls>

4.图像分割

为了识别对象边界和创建分割蒙版:

Can you perform image segmentation on this photo?

<function_calls>
<invoke name="segment_objects">
<parameter name="image_data">/path/to/your/image.jpg</parameter>
<parameter name="is_path">true</parameter>
<parameter name="model_name">yolov8n-seg.pt</parameter>
</invoke>
</function_calls>

5.图像分类

为了对整个图像内容进行分类:

What does this image show? Can you classify it?

<function_calls>
<invoke name="classify_image">
<parameter name="image_data">/path/to/your/image.jpg</parameter>
<parameter name="is_path">true</parameter>
<parameter name="model_name">yolov8n-cls.pt</parameter>
<parameter name="top_k">5</parameter>
</invoke>
</function_calls>

6.使用电脑的摄像头

使用计算机的摄像头开始实时对象检测:

Can you turn on my camera and detect objects in real-time?

<function_calls>
<invoke name="start_camera_detection">
<parameter name="model_name">yolov8n.pt</parameter>
<parameter name="confidence">0.3</parameter>
</invoke>
</function_calls>

获取最新的摄像头检测结果:

What are you seeing through my camera right now?

<function_calls>
<invoke name="get_camera_detections">
</invoke>
</function_calls>

完成后停止相机:

Please turn off the camera.

<function_calls>
<invoke name="stop_camera_detection">
</invoke>
</function_calls>

7.高级模型操作

训练自定义模型

I want to train a custom object detection model on my dataset.

<function_calls>
<invoke name="train_model">
<parameter name="dataset_path">/path/to/your/dataset</parameter>
<parameter name="model_name">yolov8n.pt</parameter>
<parameter name="epochs">50</parameter>
</invoke>
</function_calls>

验证模型

Can you validate the performance of my model on a test dataset?

<function_calls>
<invoke name="validate_model">
<parameter name="model_path">/path/to/your/trained/model.pt</parameter>
<parameter name="data_path">/path/to/validation/dataset</parameter>
</invoke>
</function_calls>

将模型导出为不同格式

I need to export my YOLO model to ONNX format.

<function_calls>
<invoke name="export_model">
<parameter name="model_path">/path/to/your/model.pt</parameter>
<parameter name="format">onnx</parameter>
</invoke>
</function_calls>

8.测试连接

检查YOLO服务是否正常运行:

Is the YOLO service running correctly?

<function_calls>
<invoke name="test_connection">
</invoke>
</function_calls>

故障排除

摄像头问题

如果相机不工作,请尝试不同的相机ID:

<function_calls>
<invoke name="start_camera_detection">
<parameter name="camera_id">1</parameter>  <!-- Try 0, 1, or 2 -->
</invoke>
</function_calls>

未找到型号

如果找不到模型,请确保已将其下载到配置的目录之一:

<function_calls>
<invoke name="get_model_directories">
</invoke>
</function_calls>

性能问题

为了在资源有限的情况下获得更好的性能,请使用较小的型号(例如,yolov8n.pt而不是yolov8x.pt)