MCP服务器提供强大的OpenCV计算机视觉功能,适用于AI助手。
OpenCV MCP Server 是一个Python包,通过模型上下文协议(MCP)提供了OpenCV的图像和视频处理能力。这使得AI助手和语言模型能够访问从基本图像操作到高级对象检测和跟踪的各种任务的强大计算机视觉工具。
使用OpenCV MCP Server,AI系统可以:





应用于上述原始视频的轮廓检测处理示例
pip install opencv-mcp-server
对于开发:
# 克隆仓库
git clone https://github.com/yourusername/opencv-mcp-server.git
cd opencv-mcp-server
# 创建虚拟环境
python -m venv .venv
source .venv/bin/activate # 在Windows上:.venv\Scripts\activate
# 安装依赖
pip install -e .
添加到您的Claude Desktop配置中:
{
"mcpServers": {
"opencv": {
"command": "uvx",
"args": [
"opencv-mcp-server"
]
}
}
}
安装uvx
brew install uv
然后重启Claude Desktop
<img width="1026" height="749" alt="image" src="https://gips2.baidu.com/it/u=3727671093,1530590468&fm=3081&app=3081&f=PNG?w=2052&h=1498" />from opencv_mcp_server import opencv_client
# 初始化客户端
client = opencv_client.OpenCVClient()
# 使用工具
result = client.resize_image(
image_path="input.jpg",
width=800,
height=600
)
由于所有必需的模型已经在OPENCV_DNN_MODELS_DIR中配置好,您可以无需指定模型路径即可使用对象检测:
# 使用预配置的YOLO模型检测对象
result = detect_objects_tool(
image_path="street.jpg",
confidence_threshold=0.5,
nms_threshold=0.4
)
服务器可以通过环境变量进行配置:
MCP_TRANSPORT:传输方式(默认:"stdio")OPENCV_DNN_MODELS_DIR:存储DNN模型的目录(默认:"models")CV_HAAR_CASCADE_DIR:存储Haar级联文件的目录(可选)计算机视觉和对象检测工具需要预训练模型才能正常工作。这些模型应放置在由OPENCV_DNN_MODELS_DIR环境变量指定的目录中(默认:"./models")。
以下模型已被预配置:
面部检测(DNN方法)
deploy.prototxt - 面部检测配置res10_300x300_ssd_iter_140000.caffemodel - 面部检测模型权重对象检测(YOLO)
yolov3.weights - YOLO模型权重yolov3.cfg - YOLO配置文件coco.names - 检测对象的类别名称method="dnn"时,detect_faces_tool使用DNN模型detect_objects_tool使用YOLO模型进行通用对象检测对于需要下载这些模型的人,请参阅“安装”部分或访问:
OpenCV MCP Server 提供了一系列组织成四个类别的计算机视觉工具:
这些工具提供了基本的图像操作能力:
save_image_tool:将图像保存到文件
path_in(输入图像路径),path_out(输出文件路径)save_image_tool(path_in="processed.jpg", path_out="final.jpg")convert_color_space_tool:在不同颜色空间之间转换图像(BGR、RGB、GRAY、HSV等)
image_path,source_space,target_spaceconvert_color_space_tool(image_path="image.jpg", source_space="BGR", target_space="HSV")resize_image_tool:将图像调整到特定尺寸
image_path,width,height,interpolation(可选)resize_image_tool(image_path="large.jpg", width=800, height=600)crop_image_tool:从图像中裁剪区域
image_path,x,y,width,heightcrop_image_tool(image_path="scene.jpg", x=100, y=150, width=300, height=200)get_image_stats_tool:获取有关图像的统计信息
image_path,channels(布尔值,默认:true)get_image_stats_tool(image_path="photo.jpg", channels=True)这些工具提供了高级的图像处理和变换能力:
apply_filter_tool:对图像应用各种滤镜(模糊、高斯、中值、双边)
image_path,filter_type,kernel_size,以及滤镜特定参数apply_filter_tool(image_path="noisy.jpg", filter_type="gaussian", kernel_size=5)detect_edges_tool:使用不同的方法(Canny、Sobel、Laplacian、Scharr)检测图像中的边缘
image_path,method,阈值参数,以及方法特定参数detect_edges_tool(image_path="objects.jpg", method="canny", threshold1=100, threshold2=200)apply_threshold_tool:对图像应用阈值(二进制、自适应等)
image_path,threshold_type,阈值值和方法特定参数apply_threshold_tool(image_path="scan.jpg", threshold_type="binary", threshold_value=127)detect_contours_tool:检测并可选地绘制图像中的轮廓
image_path,mode,method,绘制参数detect_contours_tool(image_path="shapes.jpg", mode="external", method="simple")find_shapes_tool:在图像中查找基本形状(圆、线)
image_path,shape_type,形状特定参数find_shapes_tool(image_path="geometry.jpg", shape_type="circles", min_radius=10)match_template_tool:在图像中查找模板
image_path,template_path,匹配参数match_template_tool(image_path="scene.jpg", template_path="object.jpg", threshold=0.8)这些工具提供了高级的计算机视觉能力:
detect_features_tool:使用诸如SIFT、ORB、BRISK等方法检测图像中的特征
image_path,method,max_features,绘制参数detect_features_tool(image_path="landmark.jpg", method="sift", max_features=500)match_features_tool:匹配两张图像之间的特征
image1_path,image2_path,method,匹配参数match_features_tool(image1_path="scene1.jpg", image2_path="scene2.jpg", method="sift")detect_faces_tool:使用Haar级联或DNN检测图像中的面部
image_path,method,方法特定参数detect_faces_tool(image_path="group.jpg", method="haar", min_neighbors=5)detect_objects_tool:使用预训练的DNN模型(如YOLO)检测对象
image_path,模型路径,检测参数detect_objects_tool(image_path="street.jpg", confidence_threshold=0.5)这些工具提供了视频分析和处理能力:
extract_video_frames_tool:从视频文件中提取帧
video_path,帧选择参数extract_video_frames_tool(video_path="clip.mp4", start_frame=0, step=10, max_frames=20)detect_motion_tool:检测两帧之间的运动
frame1_path,frame2_path,检测参数detect_motion_tool(frame1_path="frame1.jpg", frame2_path="frame2.jpg", threshold=25)track_object_tool:跨视频帧追踪对象
video_path,initial_bbox,追踪参数track_object_tool(video_path="tracking.mp4", initial_bbox=[100, 100, 50, 50])combine_frames_to_video_tool:将帧组合成视频文件
frame_paths,output_path,视频参数combine_frames_to_video_tool(frame_paths=["frame1.jpg", "frame2.jpg"], output_path="output.mp4")create_mp4_from_video_tool:将视频文件转换为MP4格式
video_path,output_path,转换参数create_mp4_from_video_tool(video_path="input.avi", output_path="output.mp4")detect_video_objects_tool:在视频中检测对象并生成检测结果视频
video_path,模型路径,检测参数detect_video_objects_tool(video_path="traffic.mp4", confidence_threshold=0.5)detect_camera_objects_tool:从计算机摄像头检测对象并保存为视频
camera_id,录制参数,模型路径,检测参数detect_camera_objects_tool(camera_id=0, duration=30, confidence_threshold=0.5)# 调整图像大小
result = resize_image_tool(
image_path="input.jpg",
width=800,
height=600
)
# 访问调整后的图像路径
resized_image_path = result["output_path"]
# 应用高斯模糊滤镜
result = apply_filter_tool(
image_path="input.jpg",
filter_type="gaussian",
kernel_size=5,
sigma=1.5
)
# 使用YOLO检测图像中的对象
result = detect_objects_tool(
image_path="scene.jpg",
confidence_threshold=0.5,
nms_threshold=0.4
)
# 访问检测到的对象
objects = result["objects"]
for obj in objects:
print(f"检测到 {obj['class_name']},置信度 {obj['confidence']}")
# 从视频中提取帧
result = extract_video_frames_tool(
video_path="input.mp4",
start_frame=0,
step=10,
max_frames=10
)
# 访问提取的帧
frames = result["frames"]
# 在视频中检测对象
result = detect_video_objects_tool(
video_path="input.mp4",
confidence_threshold=0.5,
frame_step=5
)
通过将一个工具的output_path作为另一个工具的输入,可以将工具链接起来:
# 首先调整图像大小
result1 = resize_image_tool(
image_path="input.jpg",
width=800,
height=600
)
# 然后对调整后的图像应用边缘检测
result2 = detect_edges_tool(
image_path=result1["output_path"],
method="canny",
threshold1=100,
threshold2=200
)
# 最后在边缘检测后的图像中检测轮廓
result3 = detect_contours_tool(
image_path=result2["output_path"],
mode="external",
method="simple"
)
OpenCV MCP Server 可用于广泛的用途:
未来计划为OpenCV MCP Server增加的功能:
欢迎贡献!请随时提交Pull Request。
git checkout -b feature/amazing-feature)git commit -m '添加一些精彩功能')git push origin feature/amazing-feature)MIT许可证 - 详情见LICENSE文件。
如有问题或反馈,请在GitHub仓库中打开Issue。
使用OpenCV和Python构建,充满爱心。