回到首页 返回首页
回到顶部 回到顶部
返回上一页 返回上一页

掌控板语音识别+简单的ai 简单

头像 dfrwh 2025.04.04 136 2

复制代码到mind+中的上传模式,切换语言为mpython。要用WiFi,可以实现以下简单AI

1.说“你好”(打招呼)

2.说“1+1等于几”

3.说“666”

4.说“时间”

5.说“钢琴”

 

####################################################################

deepseek仅为图片,不要当真!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

deepseek仅为图片,不要当真!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

deepseek仅为图片,不要当真!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

####################################################################

 

# MindPlus
# mpython
from mpython import *
import ubinascii
import urequests
import network
import ntptime
import machine
import audio
import music
import time
import json


mpyPostFileParamAddr = "http://119.23.66.134:8085/file_upload"
mpyPostFileParamPath = "1.wav"
mpyPostFileParamType = "audio/wav"

touch_threshold = {'P': 400, 'Y': 400, 'T': 400, 'H': 400, 'O': 400, 'N': 400}

# 自定义函数
def GangQin():
 global mac_address, dict, resp, ShiChang
 oled.DispChar((str("结果:") + str((resp["text"]))), 0, (1-1)*16, 1)
 touch_threshold = {'P': 500, 'Y': 500, 'T': 500, 'H': 500, 'O': 500, 'N': 500}
 while not ((button_a.value() == 0 or button_b.value() == 0)):
   if touchPad_P.read() < touch_threshold['P']:
     while not (touchPad_P.read() > touch_threshold['P']):
       music.pitch(131, 250)
   elif touchPad_Y.read() < touch_threshold['Y']:
     while not (touchPad_Y.read() > touch_threshold['Y']):
       music.pitch(147, 250)
   elif touchPad_T.read() < touch_threshold['T']:
     while not (touchPad_T.read() > touch_threshold['T']):
       music.pitch(165, 250)
   elif touchPad_H.read() < touch_threshold['H']:
     while not (touchPad_H.read() > touch_threshold['H']):
       music.pitch(196, 250)
   elif touchPad_O.read() < touch_threshold['O']:
     while not (touchPad_O.read() > touch_threshold['O']):
       music.pitch(196, 250)
   elif touchPad_N.read() < touch_threshold['N']:
     if touchPad_N.read() > touch_threshold['N']:
       music.pitch(220, 250)

# 事件回调函数
def on_button_b_down(_):
 time.sleep_ms(10)
 if button_b.value() == 1: return
 oled.fill(0)
 oled.DispChar("加载中...", 43, 14, 1)
 oled.Bitmap(51, 32, bytearray([0x0,0x38,0x0,0x7,0x39,0xc0,0x7,0x39,0xc0,0x7,0xbb,0xc0,0x7,0xff,0xc0,0x7b,0xff,0xbc,0x7d,0xff,0x7c,0x7f,0xc3,0xfc,0x1e,0x1,0xf0,0xc,0x0,0xe0,0xf8,0x0,0x7e,0xf8,0x0,0xfe,0xf8,0x0,0xfe,0x0,0x0,0x0,0x18,0x0,0x0,0x78,0x0,0x0,0x78,0x0,0x0,0x78,0x0,0x0,0x7,0x0,0x0,0x7,0x1,0xc0,0x7,0x39,0xc0,0x7,0x39,0xc0,0x0,0x38,0x0]), 23, 23, 1)
 oled.show()
 resp = {}
 mac_address = ubinascii.hexlify(machine.unique_id()).decode().upper()
 dict = {"appid": "1", "mediatype":"2", "deviceid":0}
 dict["deviceid"]=mac_address
 _response = urequests.post(mpyPostFileParamAddr, files={"file":(mpyPostFileParamPath,mpyPostFileParamType)}, params=dict)
 oled.fill(0)
 print(_response.text)
 try:
   resp = _response.json()
   if "text" in resp:
     oled.fill(0)
     oled.DispChar("识别成功", 40, 23, 1)
     oled.show()
     time.sleep(0.1)
     oled.fill(0)
     if ((resp["text"]) == (str("你好"))):
       oled.DispChar((str("结果:") + str((resp["text"]))), 0, (1-1)*16, 1)
       oled.DispChar("AI:你好!我是掌控板AI!", 0, (2-1)*16, 1)
     elif ((resp["text"]) == (str("1+1等于几"))):
       oled.DispChar((str("结果:") + str((resp["text"]))), 0, (1-1)*16, 1)
       oled.DispChar("AI: 2", 0, (2-1)*16, 1)
     elif ((resp["text"]) == (str("666"))):
       oled.DispChar((str("结果:") + str((resp["text"]))), 0, (1-1)*16, 1)
       oled.DispChar("AI:666", 0, (2-1)*16, 1)
     elif ((resp["text"]) == (str("时间"))):
       oled.DispChar((str("结果:") + str((resp["text"]))), 0, (1-1)*16, 1)
       oled.DispChar((str("AI:") + str((str(time.localtime()[0]) + str((str("年") + str((str(time.localtime()[1]) + str((str("月") + str((str(time.localtime()[2]) + str("日")))))))))))), 0, (2-1)*16, 1)
       oled.DispChar((str(time.localtime()[3]) + str((str("时") + str((str(time.localtime()[4]) + str((str("分") + str((str(time.localtime()[5]) + str("秒")))))))))), 0, (3-1)*16, 1)
     elif ((resp["text"]) == (str("钢琴"))):
       GangQin()
     elif ((resp["text"]) == (str(""))):
       oled.DispChar((str("结果:") + str((resp["text"]))), 0, (1-1)*16, 1)
     else:
       oled.DispChar((str("结果:") + str((resp["text"]))), 0, (1-1)*16, 1)
   else:
     oled.DispChar("服务器响应异常", 0, (1-1)*16, 1)
 except:
   oled.DispChar("识别失败", 0, (1-1)*16, 1)
 oled.show()
def on_button_a_down(_):
 time.sleep_ms(10)
 if button_a.value() == 1: return
 oled.fill(0)
 oled.DispChar("正在录音...", 36, 23, 1)
 oled.show()
 audio.recorder_init()
 while not (button_a.value() == 1):
   audio.record("1.wav", 4)
 audio.recorder_deinit()
 oled.fill(0)
 oled.DispChar("录音完成", 40, 15, 1)
 oled.DispChar("按下B键开始识别", 19, 29, 1)
 oled.show()


my_wifi = wifi()
button_b.irq(trigger=Pin.IRQ_FALLING, handler=on_button_b_down)
button_a.irq(trigger=Pin.IRQ_FALLING, handler=on_button_a_down)
oled.Bitmap(14, 9, bytearray([0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x38,0x40,0x0,0x0,0x0,0x3f,0xf8,0xe0,0x40,0x0,0x0,0x7f,0xf8,0xf9,0xc0,0x0,0x1,0xff,0xfc,0xff,0xc0,0x0,0x1,0xff,0xfc,0x7f,0xc0,0x0,0x3,0xff,0xfe,0x7f,0xc0,0x0,0x3,0xff,0xff,0xff,0x80,0x0,0x7,0xff,0xff,0xff,0x0,0x0,0x7,0x1f,0xff,0xfc,0x0,0x0,0x7,0x3,0xff,0xfc,0x0,0x0,0x7,0x1,0xfd,0xfc,0x0,0x0,0x7,0x80,0xfd,0xfc,0x0,0x0,0x7,0x80,0xff,0xf8,0x0,0x0,0x7,0xc0,0x7f,0xf8,0x0,0x0,0x3,0xc0,0x3f,0xf0,0x0,0x0,0x3,0xe3,0x9f,0xf0,0x0,0x0,0x1,0xf3,0xcf,0xe0,0x0,0x0,0x1,0xf9,0xef,0xe0,0x0,0x0,0x0,0x7f,0xff,0xf0,0x0,0x0,0x0,0x3f,0xff,0xf0,0x0,0x0,0x0,0x1f,0xfc,0x0,0x0,0x0,0x0,0x3,0xf0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0]), 48, 35, 1)
oled.DispChar("deepseek", 52, 17, 1)
oled.DispChar("正在连接WiFi...", 26, 39, 1)
oled.show()
my_wifi.connectWiFi("你的WiFi名","你的WiFi密码")
while not (my_wifi.sta.isconnected()):
 pass
ntptime.settime(8, "edu.ntp.org.cn")
oled.fill(0)
oled.DispChar("按下A键开始录音", 19, 23, 1)
oled.show()
 

####################################################################

deepseek仅为图片,不要当真!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

deepseek仅为图片,不要当真!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

deepseek仅为图片,不要当真!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

####################################################################

评论

user-avatar
  • woshitiger

    woshitiger2025.04.22

    你这个难得看懂

    1
    • dfrwh

      dfrwh2025.05.04

      # 引入掌控板相关模块 from mpython import * import ubinascii # 用于获取设备唯一ID import urequests # 用于HTTP请求 import network # 用于连接WiFi import ntptime # 用于同步网络时间 import machine # 控制硬件 import audio # 控制录音功能 import music # 播放音乐 import time import json # 处理JSON格式数据 # 上传接口参数配置 mpyPostFileParamAddr = &quot;http://119.23.66.134:8085/file_upload&quot; # 语音识别服务器地址 mpyPostFileParamPath = &quot;1.wav&quot; # 上传的文件名 mpyPostFileParamType = &quot;audio/wav&quot; # 音频类型 # 触摸传感器的初始触发阈值 touch_threshold = {'P': 400, 'Y': 400, 'T': 400, 'H': 400, 'O': 400, 'N': 400} # 自定义钢琴功能 def GangQin(): global mac_address, dict, resp, ShiChang # 显示识别结果 oled.DispChar((str(&quot;结果:&quot;) + str((resp[&quot;text&quot;]))), 0, 0, 1) # 提高触摸灵敏度 touch_threshold = {'P': 500, 'Y': 500, 'T': 500, 'H': 500, 'O': 500, 'N': 500} # 等待用户按住按键,弹奏钢琴 while not ((button_a.value() == 0 or button_b.value() == 0)): if touchPad_P.read() &lt; touch_threshold['P']: while not (touchPad_P.read() &gt; touch_threshold['P']): music.pitch(131, 250) # 播放低音C elif touchPad_Y.read() &lt; touch_threshold['Y']: while not (touchPad_Y.read() &gt; touch_threshold['Y']): music.pitch(147, 250) # 播放D elif touchPad_T.read() &lt; touch_threshold['T']: while not (touchPad_T.read() &gt; touch_threshold['T']): music.pitch(165, 250) # 播放E elif touchPad_H.read() &lt; touch_threshold['H']: while not (touchPad_H.read() &gt; touch_threshold['H']): music.pitch(196, 250) # 播放G elif touchPad_O.read() &lt; touch_threshold['O']: while not (touchPad_O.read() &gt; touch_threshold['O']): music.pitch(196, 250) # 再次G elif touchPad_N.read() &lt; touch_threshold['N']: if touchPad_N.read() &gt; touch_threshold['N']: music.pitch(220, 250) # 播放A # B键按下回调事件:上传语音进行识别 def on_button_b_down(_): time.sleep_ms(10) if button_b.value() == 1: return # 显示“加载中”动画 oled.fill(0) oled.DispChar(&quot;加载中...&quot;, 43, 14, 1) oled.Bitmap(51, 32, bytearray([...]), 23, 23, 1) # 显示加载图标 oled.show() resp = {} # 初始化响应内容 mac_address = ubinascii.hexlify(machine.unique_id()).decode().upper() # 获取设备唯一ID dict = {&quot;appid&quot;: &quot;1&quot;, &quot;mediatype&quot;:&quot;2&quot;, &quot;deviceid&quot;:mac_address} # 发送录音文件到服务器进行语音识别 _response = urequests.post(mpyPostFileParamAddr, files={&quot;file&quot;:(mpyPostFileParamPath, mpyPostFileParamType)}, params=dict) oled.fill(0) print(_response.text) try: resp = _response.json() # 如果返回包含识别文本 if &quot;text&quot; in resp: oled.fill(0) oled.DispChar(&quot;识别成功&quot;, 40, 23, 1) oled.show() time.sleep(0.1) oled.fill(0) # 进行语音指令识别与响应 if resp[&quot;text&quot;] == &quot;你好&quot;: oled.DispChar(&quot;结果:&quot; + resp[&quot;text&quot;], 0, 0, 1) oled.DispChar(&quot;AI:你好!我是掌控板AI!&quot;, 0, 16, 1) elif resp[&quot;text&quot;] == &quot;1+1等于几&quot;: oled.DispChar(&quot;结果:&quot; + resp[&quot;text&quot;], 0, 0, 1) oled.DispChar(&quot;AI: 2&quot;, 0, 16, 1) elif resp[&quot;text&quot;] == &quot;666&quot;: oled.DispChar(&quot;结果:&quot; + resp[&quot;text&quot;], 0, 0, 1) oled.DispChar(&quot;AI:666&quot;, 0, 16, 1) elif resp[&quot;text&quot;] == &quot;时间&quot;: oled.DispChar(&quot;结果:&quot; + resp[&quot;text&quot;], 0, 0, 1) oled.DispChar(&quot;AI:&quot; + str(time.localtime()[0]) + &quot;年&quot; + str(time.localtime()[1]) + &quot;月&quot; + str(time.localtime()[2]) + &quot;日&quot;, 0, 16, 1) oled.DispChar(str(time.localtime()[3]) + &quot;时&quot; + str(time.localtime()[4]) + &quot;分&quot; + str(time.localtime()[5]) + &quot;秒&quot;, 0, 32, 1) elif resp[&quot;text&quot;] == &quot;钢琴&quot;: GangQin() # 进入钢琴模式 else: oled.DispChar(&quot;结果:&quot; + resp[&quot;text&quot;], 0, 0, 1) else: oled.DispChar(&quot;服务器响应异常&quot;, 0, 0, 1) except: oled.DispChar(&quot;识别失败&quot;, 0, 0, 1) oled.show() # A键按下回调事件:开始录音 def on_button_a_down(_): time.sleep_ms(10) if button_a.value() == 1: return oled.fill(0) oled.DispChar(&quot;正在录音...&quot;, 36, 23, 1) oled.show() # 初始化录音设备 audio.recorder_init() # 等待用户松开按钮之前,持续录音 while not (button_a.value() == 1): audio.record(&quot;1.wav&quot;, 4) # 停止录音 audio.recorder_deinit() oled.fill(0) oled.DispChar(&quot;录音完成&quot;, 40, 15, 1) oled.DispChar(&quot;按下B键开始识别&quot;, 19, 29, 1) oled.show() # 初始化WiFi my_wifi = wifi() # 注册按钮事件 button_b.irq(trigger=Pin.IRQ_FALLING, handler=on_button_b_down) button_a.irq(trigger=Pin.IRQ_FALLING, handler=on_button_a_down) # 显示启动界面图标 oled.Bitmap(14, 9, bytearray([...]), 48, 35, 1) oled.DispChar(&quot;deepseek&quot;, 52, 17, 1) oled.DispChar(&quot;正在连接WiFi...&quot;, 26, 39, 1) oled.show() # 连接WiFi(请替换为你自己的WiFi账号和密码) my_wifi.connectWiFi(&quot;你的WiFi名&quot;, &quot;你的WiFi密码&quot;) while not my_wifi.sta.isconnected(): pass # 设置网络时间,时区为 +8 ntptime.settime(8, &quot;edu.ntp.org.cn&quot;) # 显示准备就绪界面 oled.fill(0) oled.DispChar(&quot;按下A键开始录音&quot;, 19, 23, 1) oled.show()