视觉服务

提供屏幕截图、OCR文本提取和视觉语言模型场景理解的视觉服务,适用于自动化监控和智能分析场景。

  • by lukaizhi5559
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 lukaizhi5559/thinkdrop-vision-service

使用说明

视觉服务-MCP

ThinkDrop AI的视觉功能:屏幕捕捉、OCR和VLM场景理解。

特性

  • 电脑屏幕截图工具 -快速跨平台屏幕截图
  • 光学字符识别 -使用PaddleOCR进行文本提取(本地、多语言)
  • 视觉语言模型 -使用MiniCPM-V 2.6进行场景理解(延迟加载,可选)
  • 观看模式 -通过变化检测进行持续监控
  • 内存集成 -自动存储到用户内存服务作为嵌入

快速开始

# 1. Copy environment config
cp .env.example .env

# 2. Edit .env (set API keys, configure VLM, etc.)
nano .env

# 3. Start service
./start.sh

服务将在 http://localhost:3006

安装选项

最小(仅OCR-不需要GPU)

pip install -r requirements.txt
  • 仅截图+OCR
  • 每次捕获约200-500ms
  • 无VLM依赖关系

完整(OCR+VLM-建议使用GPU)

# Uncomment VLM dependencies in requirements.txt
pip install torch transformers accelerate

# Or with CUDA support
pip install torch --index-url https://download.pytorch.org/whl/cu118
pip install transformers accelerate
  • 截图+OCR+VLM
  • 配备GPU的600-1500ms,配备CPU的2-6s
  • 首次使用时下载约2.4GB型号

API终点

健康检查

GET /health

屏幕捕获

POST /vision/capture
{
  "region": [x, y, width, height],  # Optional
  "format": "png"
}

提取文本(OCR)

POST /vision/ocr
{
  "region": [x, y, width, height],  # Optional
  "language": "en"                   # Optional
}

描述屏幕(VLM)

POST /vision/describe
{
  "region": [x, y, width, height],  # Optional
  "task": "Find the Save button",   # Optional focus
  "include_ocr": true,               # Include OCR text
  "store_to_memory": true            # Auto-store to user-memory
}

启动监视模式

POST /vision/watch/start
{
  "interval_ms": 2000,
  "change_threshold": 0.08,
  "run_ocr": true,
  "run_vlm": false,
  "task": "Monitor for errors"
}

停止监视模式

POST /vision/watch/stop

观察状态

GET /vision/watch/status

配置

关键环境变量 .env:

# Service
PORT=3006
API_KEY=your-vision-api-key-here

# OCR
OCR_ENGINE=paddleocr
OCR_LANGUAGE=en

# VLM (lazy-loaded)
VLM_ENABLED=true
VLM_MODEL=openbmb/MiniCPM-V-2_6
VLM_DEVICE=auto  # auto, cpu, cuda

# Watch
WATCH_DEFAULT_INTERVAL_MS=2000
WATCH_CHANGE_THRESHOLD=0.08

# User Memory Integration
USER_MEMORY_SERVICE_URL=http://localhost:3003
USER_MEMORY_API_KEY=your-user-memory-api-key

演出

仅OCR(最低设置)

  • 捕捉:10-20ms
  • 光学字符识别:200-500毫秒
  • 总计:每个请求约300-600ms
  • 记忆:~500MB

OCR+VLM(完整设置)

  • 捕捉:10-20ms
  • 光学字符识别:200-500毫秒
  • VLM(GPU):300-800ms
  • VLM(CPU):2-5s
  • 总计(GPU):~600-1500毫秒
  • 总计(CPU):~2.5-6s
  • 记忆:~3-4GB(已加载型号)

观看模式策略

监视模式使用智能变化检测来最大限度地减少VLM调用:

  1. 每个间隔:采集+指纹比对
  2. 关于变化:运行OCR(如果启用)
  3. 关于重大变化:运行VLM(如果启用)
  4. 汽车专卖店:作为嵌入发送到用户内存服务

这可以保持VLM的使用效率,同时保持持续的意识。

与ThinkDrop AI集成

视觉服务与MCP状态图集成:

// In AgentOrchestrator state graph
const visionResult = await mcpClient.callService('vision', 'describe', {
  include_ocr: true,
  store_to_memory: true,
  task: userMessage
});

// Result automatically stored as embedding in user-memory
// No screenshot files to manage!

测试

测试捕获

curl -X POST http://localhost:3006/vision/capture \
  -H "Content-Type: application/json" \
  -d '{}'

测试OCR

curl -X POST http://localhost:3006/vision/ocr \
  -H "Content-Type: application/json" \
  -d '{}'

测试VLM(如果启用)

curl -X POST http://localhost:3006/vision/describe \
  -H "Content-Type: application/json" \
  -d '{"include_ocr": true, "store_to_memory": false}'

测试手表

# Start
curl -X POST http://localhost:3006/vision/watch/start \
  -H "Content-Type: application/json" \
  -d '{"interval_ms": 2000, "run_ocr": true}'

# Status
curl http://localhost:3006/vision/watch/status

# Stop
curl -X POST http://localhost:3006/vision/watch/stop

故障排除

OCR不工作

  • 检查PaddleOCR安装: pip list | grep paddleocr
  • 首次使用时下载型号(~100MB)
  • 检查日志以了解下载进度

VLM未加载

  • 确保安装了依赖项: pip list | grep transformers
  • 检查可用内存(需要4-8GB)
  • 集 VLM_ENABLED=false 禁用
  • 首次使用时下载模型(~2.4GB)

性能问题

  • CPU太慢:禁用VLM,仅使用OCR
  • 内存问题:减少观察间隔,禁用VLM
  • 未检测到GPU:检查CUDA安装

建筑

vision-service/
├── server.py              # FastAPI app
├── src/
│   ├── services/
│   │   ├── screenshot.py  # mss wrapper
│   │   ├── ocr_engine.py  # PaddleOCR wrapper
│   │   ├── vlm_engine.py  # VLM wrapper (lazy)
│   │   └── watch_manager.py  # Watch loop
│   ├── routes/
│   │   ├── capture.py     # /vision/capture
│   │   ├── ocr.py         # /vision/ocr
│   │   ├── describe.py    # /vision/describe
│   │   └── watch.py       # /vision/watch/*
│   └── middleware/
│       └── validation.py  # API key validation
├── requirements.txt
├── start.sh
└── README.md

许可证

ThinkDrop AI项目的一部分。