OpenClaw 实战:WSL2 环境下使用 Python 调用摄像头
在 WSL2 环境下让 OpenClaw AI 助手访问摄像头的三种方案。第一阶段尝试通过 Node.js 配对,因 WSL2 网络隔离和端口占用失败;第二阶段使用浏览器方案,虽可预览但无法自动化;第三阶段采用 Python + OpenCV 本地程序,成功实现摄像头调用及拍照功能。结论是 WSL2 无法直接访问摄像头硬件,需借助 Windows 原生程序或特定桥接方案。

在 WSL2 环境下让 OpenClaw AI 助手访问摄像头的三种方案。第一阶段尝试通过 Node.js 配对,因 WSL2 网络隔离和端口占用失败;第二阶段使用浏览器方案,虽可预览但无法自动化;第三阶段采用 Python + OpenCV 本地程序,成功实现摄像头调用及拍照功能。结论是 WSL2 无法直接访问摄像头硬件,需借助 Windows 原生程序或特定桥接方案。

本文介绍如何在 WSL2 环境下让 OpenClaw AI 助手访问摄像头画面。
在 WSL2 中安装 OpenClaw,尝试控制摄像头。需要在 Windows 上安装 Node.js 和 npm,但遇到网络隔离问题,最终放弃。
下载绿色版 Node.js (v24.14.0),配置环境变量:
$nodePath = "D:\WSL\node-v24.14.0-win-x64"
$env:Path += ";$nodePath"
[Environment]::SetEnvironmentVariable("Path", $env:Path + ";$nodePath", "User")
问题 1:PowerShell 执行策略限制
错误信息:npm : File ... cannot be loaded because running scripts is disabled on this system.
解决:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
问题 2:npm 需要 Git
错误信息:npm error code ENOENT ... spawn git
解决:手动下载并安装 Git for Windows (v2.47.1)。
问题 3:npm 配置错误
之前尝试配置 npm config set git false 导致后续安装失败。
解决:
npm config delete git
# 安装 openclaw
npm install -g openclaw
# 安装 node 服务
openclaw node install
# 启动 node
openclaw node restart
# 查看状态
openclaw node status
问题 1:端口占用 WSL Gateway 已占用 18789 端口,Windows Node 无法绑定。
问题 2:网络隔离 WSL2 运行在 Hyper-V 虚拟机中,与 Windows 主机网络隔离。WSL 的 localhost (127.0.0.1) Windows 访问不到。
错误日志:gateway connect failed: Error: device signature invalid
问题 3:配置验证失败
错误日志:Invalid config at /home/kim/.openclaw/openclaw.json: - plugins.slots.memory: plugin not found: memory-core
尝试修复配置:
openclaw config.set agents.defaults.memorySearch.enabled false
openclaw doctor --fix
最终放弃原因: WSL2 架构限制导致 Windows Node 无法与 WSL Gateway 建立稳定连接。即使通过端口转发能连通,device signature 验证也过不去。
折腾 3 小时以上,决定换方案。
使用浏览器网站直接调用设备摄像头,通过 OpenClaw Browser Relay 插件实现,但操作繁琐且无法自动化。
功能:
摄像头信息示例:
优点:
缺点:
实际测试: 成功拍到摄像头画面,确认摄像头硬件正常,能拍到清晰画面。但浏览器方案无法满足自动化需求。
这是最满意的方案。只要告诉 AI:'看看这是啥',它就能调用摄像头看,然后回答能看到什么。
pip install opencv-python
OpenCV 是成熟的计算机视觉库,支持摄像头访问、图像处理、目标检测等功能。
编写了两个版本的脚本:
版本 1:实时预览版 (webcam.py)
#!/usr/bin/env python3
""" 本地摄像头测试工具 - 复刻 webcamtests.com 核心功能
功能:实时预览、拍照、录像、摄像头信息
"""
import cv2
import os
from datetime import datetime
class WebcamTest:
def __init__(self, camera_id=0):
self.camera_id = camera_id
self.cap = None
self.save_dir = os.path.expanduser("~/Pictures/WebcamTest")
os.makedirs(self.save_dir, exist_ok=True)
def open_camera(self):
"""打开摄像头"""
self.cap = cv2.VideoCapture(self.camera_id)
if not self.cap.isOpened():
print(f"无法打开摄像头 (ID: {self.camera_id})")
return False
width = int(self.cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(self.cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = int(self.cap.get(cv2.CAP_PROP_FPS))
print(f"摄像头已打开")
print(f" 分辨率:{width}x{height}")
print(f" FPS: {fps}")
return
():
()
()
()
()
:
ret, frame = .cap.read()
cv2.imshow(, frame)
key = cv2.waitKey() &
key == ():
key == ():
.take_photo(frame)
cv2.destroyAllWindows()
版本 2:自动拍照版 (webcam-snap.py)
#!/usr/bin/env python3
""" 本地摄像头测试工具 - 自动拍照版本 """
import cv2
import os
from datetime import datetime
def main():
save_dir = os.path.expanduser("~/Pictures/WebcamTest")
os.makedirs(save_dir, exist_ok=True)
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("无法打开摄像头")
return
print("摄像头已打开")
print(f" 分辨率:{int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))}x{int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))}")
# 预热摄像头
for i in range(30):
cap.read()
# 拍照
ret, frame = cap.read()
if ret:
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = os.path.join(save_dir, f"photo_{timestamp}.jpg")
cv2.imwrite(filename, frame)
print(f"照片已保存:{filename}")
cap.release()
if __name__ == "__main__":
main()
python D:\openclaw-scripts\webcam-snap.py
成功输出:
[图片:摄像头预览效果]
[图片:房间画面]
[图片:拍摄的照片]
WSL2 无法直接访问摄像头硬件,需要 Windows 原生程序。

微信公众号「极客日志」,在微信中扫描左侧二维码关注。展示文案:极客日志 zeeklog
使用加密算法(如AES、TripleDES、Rabbit或RC4)加密和解密文本明文。 在线工具,加密/解密文本在线工具,online
生成新的随机RSA私钥和公钥pem证书。 在线工具,RSA密钥对生成器在线工具,online
基于 Mermaid.js 实时预览流程图、时序图等图表,支持源码编辑与即时渲染。 在线工具,Mermaid 预览与可视化编辑在线工具,online
解析常见 curl 参数并生成 fetch、axios、PHP curl 或 Python requests 示例代码。 在线工具,curl 转代码在线工具,online
将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online
将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online