AI 与摄像头功能
比赛现场禁止联网,重点掌握离线/本地AI功能 行空板M10 + USB摄像头是核心组合
一、USB 摄像头操作
接线
USB摄像头 → 行空板USB-A口(即插即用)Mind+ 图形化
扩展 → 官方库 → 行空板M10 → 拖出摄像头相关积木
Python 拍照代码
python
import cv2
# ===== 单次拍照 =====
def take_photo():
vd = cv2.VideoCapture()
vd.open(0) # 0=第一个USB摄像头
while not vd.isOpened(): # 等待摄像头就绪
pass
ret, frame = vd.read() # 拍照
cv2.imwrite("photo.png", frame) # 保存
vd.release() # 释放
return frame
# 拍照并在屏幕显示
take_photo()
u_gui.draw_image(image="photo.png", x=20, y=40, w=200)百度AI植物识别⬇ 下载物体分类-采集垃圾图片⬇ 下载二哈视觉传感器PDF📖 预览
定时自动拍照
python
import datetime
def auto_photo():
# 每20秒拍一张
miao = datetime.datetime.now().second
if miao == 0 or miao == 20 or miao == 40:
take_photo()
print("自动拍照完成")拍照后Base64编码(MQTT传输用)
python
import base64, json
def photo_to_base64(filename):
with open(filename, "rb") as f:
return base64.b64encode(f.read()).decode('utf-8')
# MQTT发送图片
img_b64 = photo_to_base64("photo.png")
client.publish("aaa/c", json.dumps({"params": img_b64}))二、语音合成(文字转语音)
方案A:百度语音合成(代码模式,需网络)
python
from aip import AipSpeech
client = AipSpeech("APP_ID", "API_KEY", "SECRET_KEY")
def speak(text):
result = client.synthesis(text, "zh", 1, {
'aue': 6, # 格式(pcm→wav)
'per': 0, # 0=女声, 1=男声, 3=度逍遥, 4=度丫丫
'spd': 5, # 语速(0-9)
'pit': 5, # 音调(0-9)
'vol': 5, # 音量(0-15)
})
if not isinstance(result, dict): # 成功返回音频数据
with open("speech.wav", "wb") as f:
f.write(result)
import os
os.system("mplayer speech.wav") # 播放
# 使用
speak("欢迎使用智能系统")
speak("当前温度为25度")方案B:外接语音合成模块(I2C,离线可用)
python
from DFRobot_SpeechSynthesis_I2C import DFRobot_SpeechSynthesis_I2C
import time
stts = DFRobot_SpeechSynthesis_I2C()
stts.begin(stts.eV2)
stts.setVolume(8) # 音量 0-9
stts.setSpeed(5) # 语速 0-9
stts.setSoundType(stts.eFemale2) # 声音:女声
stts.speak("火灾警告,请立即撤离") # 直接朗读三、语音识别(语音转文字)
讯飞语音识别(需网络)
python
# 流程:录音 → 上传讯飞 → 返回文字
import os
# 录音
u_audio.record("reco.wav", 5)
# 调用讯飞识别脚本(ost-fast.py 在培训资料中)
os.system("python3 ost-fast.py")
# 读取识别结果
with open("xunfei_text.txt", "r") as f:
text = f.read().strip()
print(f"识别结果: {text}")关键词触发(简单替代方案)
python
# 思路:录音 → 语音识别 → 查找关键词 → 触发动作
key_words = {
"开灯": lambda: led.write_digital(1),
"关灯": lambda: led.write_digital(0),
"拍照": take_photo,
"报警": lambda: buzzer.play(buzzer.DADADADUM, buzzer.Once),
}
def voice_trigger(speech_text, keywords_dict):
for keyword, action in keywords_dict.items():
if keyword in speech_text:
action()
print(f"触发关键词: {keyword}")
return True
return False四、AI 大模型桥接(SIoT中转方案)
为什么用桥接?
- 行空板M10本身算力不够跑大模型
- 通过局域网SIoT中转,让电脑端运行AI
完整流程
1. 行空板M10 录音 → 语音识别 → 得到文字问题
2. 行空板M10 发布问题到 SIoT(topic/a)
3. 电脑端收到问题 → 调用DeepSeek/Kimi
4. 电脑端将答案发布到 SIoT(topic/b)
5. 行空板M10 收到答案 → 语音播报/屏幕显示电脑端 DeepSeek 桥接程序
python
# deepseek_siot_bridge.py(电脑上运行)
import paho.mqtt.client as mqtt
import requests, json
SIOT_IP = "192.168.123.1"
OLLAMA_URL = "http://localhost:11434/api/chat"
def on_message(client, userdata, msg):
question = msg.payload.decode("utf-8")
print(f"收到问题: {question}")
try:
resp = requests.post(OLLAMA_URL, json={
"model": "deepseek-r1:1.5b", # 或 qwen:1.8b
"messages": [{"role": "user", "content": question}]
}, timeout=60)
answer = resp.json()["message"]["content"]
# 过滤DeepSeek R1的思考过程
if "</think>" in answer:
answer = answer.split("</think>", 1)[-1].strip()
# 限制长度
if len(answer) > 120:
answer = answer[:117] + "..."
client.publish("topic/b", answer)
print(f"已回复: {answer}")
except Exception as e:
client.publish("topic/b", f"错误: {str(e)}")
client = mqtt.Client()
client.username_pw_set("siot", "dfrobot")
client.on_message = on_message
client.connect(SIOT_IP, 1883, 60)
client.subscribe("topic/a")
print("桥接服务已启动,等待请求...")
client.loop_forever()启动脚本(bat文件)
bat
@echo off
cd D:\siot_DeepSeek
python deepseek_siot_bridge.py
pause五、AI功能选择建议
| 场景 | 推荐方案 | 网络要求 |
|---|---|---|
| 语音朗读通知 | 外接语音合成模块(I2C) | ❌ 离线 |
| 语音朗读(自然) | 百度语音合成API | ✅ 需网络 |
| 语音命令识别 | 讯飞语音识别 + 关键词匹配 | ✅ 需网络 |
| 智能问答 | SIoT桥接 + 电脑本地DeepSeek | ❌ 局域网 |
| 图像物体识别 | 百度图像识别API | ✅ 需网络 |
| 拍照监控 | USB摄像头 + OpenCV | ❌ 离线 |
| 人脸检测 | 外接HUSKYLENS模块(I2C) | ❌ 离线 |
⭐ 比赛最佳组合:USB摄像头拍照 + 语音合成播报 + SIoT桥接AI问答(摄像头和AI播报离线可用,桥接AI通过局域网实现)
参考:培训资料 Baidu AI / DeepSeek / HUSKYLENS 教程