该项目的想法是 使用语音转文本 Whisper 模型通过我们的 PC 或 Raspberry Pi 发出语音指令以进行交互.
我们将给出一个命令,该命令将被转录,转换为文本,使用 Whisper,然后分析以执行适当的命令,可以是从执行程序到为 RaspberryPi 引脚提供电压。
我将使用一个旧的 Raspberry Pi 2,一个微型 USB,我将使用 OpenAI 最近发布的语音到文本模型, 耳语. 在文章的最后你可以看到 多一点耳语.
全部编入 蟒蛇.
我在此视频中向您展示了它是如何工作的,通过语音控制 PC。
安装
要在 PC 上使用它,我们只需要一个麦克风。
如果要将它安装在 RaspberryPi 上,则需要一个 USB 麦克风,因为它的插孔仅用于输出。
我们需要:
由于该工具的一般用途是语音识别。 我发现将它集成到其他设备的操作中非常有用。
- 微型USB
- 带有操作系统的 Raspberry PI(Raspbian pro 示例)
- 电子产品(LED、电线、480 欧姆电阻器和面包板)
我们将 LED 连接到引脚 17,这是我们将为此体验激活和停用的引脚。
代码开发
它分为三个部分,第一部分是我从中提取代码的录音 极客们,因为我不认识那些书店。 第二,使用 Whisper 将音频转换为文本,第三,在 RaspberryPi 中处理该文本和响应
在测试示例中,我只会与 Led 交互,使其亮起或闪烁,但我们可以开发脚本以根据需要对其进行调整。
我知道这是一个 Raspberry Pi 2,它会比 Raspberry Pi 4 慢得多,但对于测试它很好。
在你让它工作之前,你需要安装以下
#Instalar whisper pip install git+https://github.com/openai/whisper.git sudo apt update && sudo apt install ffmpeg #para que funcione la grabación de audio python3 -m pip install sounddevice --user pip install git+https://github.com/WarrenWeckesser/wavio.git #si vas a instalarlo en la raspberry #dar permisos para usar la GPIO sudo apt install python3-gpiozero sudo usermode -aG gpio <username>
所有代码
#!/usr/bin/env python3 import whisper import time from gpiozero import LED import sounddevice as sd from scipy.io.wavfile import write import wavio as wv def main (): inicio = time.time() record_audio () model = whisper.load_model("tiny") result = model.transcribe("audio1.wav") words = result["text"].split() for word in words: word = word.replace(',', '').replace('.', '').lower() if word == 'enciende' or 'encender': encender() break if word == 'parpadea' or 'parpadear': parpadear() break fin = time.time() print(fin-inicio) def encender (): LED(17).on() def parpadear (): light = LED(17) while True: light.on() sleep(1) light.off() sleep(1) def record_audio (): # Sampling frequency freq = 44100 # Recording duration duration = 5 # Start recorder with the given values # of duration and sample frequency recording = sd.rec(int(duration * freq), samplerate=freq, channels=2) # Record audio for the given number of seconds sd.wait() # This will convert the NumPy array to an audio # file with the given sampling frequency write("audio0.wav", freq, recording) # Convert the NumPy array to audio file wv.write("audio1.wav", recording, freq, sampwidth=2) main () #dar permisos para usar la GPIO #sudo apt install python3-gpiozero #sudo usermode -aG gpio <username> #Instalar whisper #pip install git+https://github.com/openai/whisper.git #sudo apt update && sudo apt install ffmpeg
我无法测试它,因为我没有用于 RaspberryPi 的 microSD 或 USB 扬声器来连接,但我一尝试就纠正了一些很容易滑入的错误。
逐步解释代码
#!/usr/bin/env python3
Shebang 告诉设备我们用什么语言编程以及使用什么解释器。 尽管它看起来微不足道,但不放它会在很多情况下导致错误。
导入的库
import whisper import time from gpiozero import LED import sounddevice as sd from scipy.io.wavfile import write import wavio as wv
耳语与模型合作
时间,因为我使用它来控制执行脚本所需的时间,gpiozero 与 Raspberry 和 sounddevice 的 GPIO 引脚一起工作,scipy 和 wavio 来录制音频
功能介绍
我创建了 4 个函数:
- 主要()
- 光 ()
- 眨眼 ()
- 录制音频()
打开()只是给覆盆子的引脚 17 提供电压,在这种情况下我们连接了要测试的 LED
def encender (): LED(17).on()
blink() 与 on() 类似,但它通过在循环中打开和关闭 LED 来使 LED 闪烁。
def parpadear (): light = LED(17) while True: light.on() sleep(1) light.off() sleep(1)
使用 record_audio() 我们录制音频文件
def record_audio (): # Sampling frequency freq = 44100 # Recording duration duration = 5 # Start recorder with the given values # of duration and sample frequency recording = sd.rec(int(duration * freq), samplerate=freq, channels=2) # Record audio for the given number of seconds sd.wait() # This will convert the NumPy array to an audio # file with the given sampling frequency write("audio0.wav", freq, recording) # Convert the NumPy array to audio file wv.write("audio1.wav", recording, freq, sampwidth=2)
Main 是主函数,请注意,我们在函数之外唯一拥有的就是在脚本末尾调用 main()。 这样在启动时,它将导入库,然后进行函数调用。
def main (): inicio = time.time() record_audio () model = whisper.load_model("tiny") result = model.transcribe("audio1.wav") words = result["text"].split() for word in words: word = word.replace(',', '').replace('.', '').lower() if word == 'enciende' or 'encender': encender() break if word == 'parpadea' or 'parpadear': parpadear() break fin = time.time() print(fin-inicio)
我们保存开始执行函数的时间,然后调用记录音频函数,它将我们的指令记录在 .wav、.mp3 等文件中,稍后我们将转换为文本
inicio = time.time() record_audio ()
一旦我们有了音频,就会调用whisper并告诉它我们想要使用的模型,有5个可用,我们将使用tiny,虽然它是最不精确的,因为它是最快的并且音频很简单,只有三四个字。
model = whisper.load_model("tiny") result = model.transcribe("audio1.wav")
有了这个,我们将音频转换为文本并保存在一个变量中。 让我们稍微修改一下。
我们将结果转换为包含音频中每个单词的列表
words = result["text"].split()
一切都准备好与我们的设备进行交互。 现在我们只需要创造我们想要的条件。
如果音频有单词 X,做 Y。因为我们有一个列表中的单词,所以很容易添加条件
for word in words: word = word.replace(',', '').replace('.', '').lower() if word == 'enciende' or 'encender': encender() break if word == 'parpadea' or 'parpadear': parpadear() break
这条线
word = word.replace(',', '').replace('.', '').lower()
我使用它将音频中的单词转换为小写并删除逗号和句点。 这样可以避免比较中的错误
在每个 if 中,如果满足我们选择的任何单词的条件,它就会调用一个函数来执行我们想要的操作,
这就是我们告诉它激活一个可以点亮 LED 或使其闪烁的 PIN 的地方。 要么运行一些代码,要么关闭计算机。
这一切都是一个基本的想法。 从这里您可以根据需要开发和改进项目。 每个人都可以为它找到不同的用途。
我们可以用这个蒙太奇做的事情
这些是我想利用这个蒙太奇的想法。 一旦骨架被武装起来,我们就可以用它来激活通过声音想到的一切,我们可以激活一个继电器来启动电机,或者我们可以启动一个脚本来执行脚本、电子邮件或其他任何东西。
什么是耳语
Whisper 是一个 vol 识别模型,可以在多种语言中工作,并允许翻译成英语。 这是我们所知道的文本到语音的工具,但这是开源的,由稳定扩散的创建者 OpenAI 团队发布。