python - 在按键上停止语音识别
问题描述
我可以停止在键盘按下时收听音频吗?我尝试像这样更改记录函数(在init.py中):
def record(self, source, duration=None, offset=None):
"""
Records up to ``duration`` seconds of audio from ``source`` (an ``AudioSource`` instance) starting at ``offset`` (or at the beginning if not specified) into an ``AudioData`` instance, which it returns.
If ``duration`` is not specified, then it will record until there is no more audio input.
"""
assert isinstance(source, AudioSource), "Source must be an audio source"
assert source.stream is not None, "Audio source must be entered before recording, see documentation for ``AudioSource``; are you using ``source`` outside of a ``with`` statement?"
frames = io.BytesIO()
seconds_per_buffer = (source.CHUNK + 0.0) / source.SAMPLE_RATE
elapsed_time = 0
offset_time = 0
offset_reached = False
while True: # loop for the total number of chunks needed
if offset and not offset_reached:
offset_time += seconds_per_buffer
if offset_time > offset:
offset_reached = True
buffer = source.stream.read(source.CHUNK)
if len(buffer) == 0: break
if offset_reached or not offset:
elapsed_time += seconds_per_buffer
if keyboard.read_key() == "p":
print("\nYou pressed p")
break
frames.write(buffer)
frame_data = frames.getvalue()
frames.close()
return AudioData(frame_data, source.SAMPLE_RATE, source.SAMPLE_WIDTH)
并像这样从我的主脚本中调用它:
def Main():
r = sr.Recognizer()
try:
with sr.Microphone() as source:
print("Listening....")
audio = r.record(source)
print("Recognizing....")
r.adjust_for_ambient_noise(source)
text = r.recognize_google(audio)
print(text.lower())
if "lock computer" in text.lower():
ctypes.windll.user32.LockWorkStation()
elif "joke" in text.lower():
joke = pyjokes.get_joke()
speak(joke)
except Exception as e:
print(e)
Main()
当我按 p 但无法识别时,这会收听音频并停止收听
解决方案
我想通了,我保存了输入文件,发现里面没有音频,所以谷歌无法重新识别它。错误出现在此块中:
if keyboard.read_key() == "p":
print("\nYou pressed p")
break
我将其更改为:
if keyboard.read_key() == "p":
print("\nYou pressed p")
pressed = True
break
并复制监听功能并复制它,将其名称更改为listen1 现在当我按P 时它停止监听并且识别也在工作。奇迹般有效 。
推荐阅读
- javascript - 使用滑块设置元素过渡(向前、向后)中的动画百分比
- typescript - 如何在代码沙箱中使用 nuxt-property-decorator?
- css - 媒体查询、图像和加载时间
- flutter - Flutter 构建和 Xcode 归档混淆
- python - 图像不会显示
- imap - 是否有任何 XYMHIGHESTMODSEQ 文档?
- javascript - 为什么我不能在函数中返回计算出的距离(来自 Google Maps API)?
- sql - Redshift - 带条件的最大窗口函数
- string - 从 Clojure 字符串列表中删除多余的转义双引号
- amazon-s3 - 当我运行雪花阶段查询时,我收到 aws 错误