gpt4 book ai didi

Python检测关键字

转载 作者:太空狗 更新时间:2023-10-29 21:42:20 24 4
gpt4 key购买 nike

我正在做的和做休闲的应用:

1:如果麦克风检测到一些噪音,它就会开始录制音频,直到检测不到噪音为止。之后,音频被录制到一个 wav 文件中。

2:我必须检测上面的一些单词。只有 5 到 10 个单词需要检测。

到目前为止,我的代码只做了第一部分(检测噪音和录制音频)。现在,我有一个包含以下词的列表:help, please, yes, no, could, you, after, tomorrow。我需要一种离线方式来检测我的声音是否包含这些词。这可能吗?我怎样才能做到这一点?我正在使用 linux,无法将我的操作系统更改为 Windows 或使用虚拟机。

我正在考虑使用声音的频谱图,创建一个火车数据库并使用一些分类器进行预测。例如,this是一个词的频谱图。这是一个很好的技术吗?

谢谢。

最佳答案

您可以使用 python 中的 pocketsphinx,使用 pip install pocketsphinx 安装。代码如下所示:

import sys, os
from pocketsphinx.pocketsphinx import *
from sphinxbase.sphinxbase import *


modeldir = "../../../model"
datadir = "../../../test/data"

# Create a decoder with certain model
config = Decoder.default_config()
config.set_string('-hmm', os.path.join(modeldir, 'en-us/en-us'))
config.set_string('-dict', os.path.join(modeldir, 'en-us/cmudict-en-us.dict'))
config.set_string('-kws', 'command.list')


# Open file to read the data
stream = open(os.path.join(datadir, "goforward.raw"), "rb")

# Alternatively you can read from microphone
# import pyaudio
#
# p = pyaudio.PyAudio()
# stream = p.open(format=pyaudio.paInt16, channels=1, rate=16000, input=True, frames_per_buffer=1024)
# stream.start_stream()

# Process audio chunk by chunk. On keyword detected perform action and restart search
decoder = Decoder(config)
decoder.start_utt()
while True:
buf = stream.read(1024)
if buf:
decoder.process_raw(buf, False, False)
else:
break
if decoder.hyp() != None:
print ([(seg.word, seg.prob, seg.start_frame, seg.end_frame) for seg in decoder.seg()])
print ("Detected keyword, restarting search")
decoder.end_utt()
decoder.start_utt()

关键字列表应如下所示:

  forward /1e-1/
down /1e-1/
other phrase /1e-20/

数字是检测的阈值

关于Python检测关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35246786/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com