我收到的是这个{“text”:“hello”},而不是我说的“你好”。我怎样才能摆脱剩下的东西
stuff = {"text" : ''"} 部分
我尝试导入 JSON。我试图让 pip 语音来修复它。我不知道我是否遗漏了一些小东西 我使用语音识别来完成大部分工作。
# Get rid of ALSA lib error messages in Linux
import platform
if platform.system() == "Linux":
from ctypes import *
# Define error handler
error_handler = CFUNCTYPE(None, c_char_p, c_int, c_char_p, c_int, c_char_p)
# Don't do anything if there is an error message
def py_error_handler(filename, line, function, err, fmt):
pass
# Pass to C
c_error_handler = error_handler(py_error_handler)
asound = cdll.LoadLibrary('libasound.so')
asound.snd_lib_error_set_handler(c_error_handler)
# Now define the voice_to_text() function for all platforms
import speech_recognition as sr
import vosk
import json
vosk.Model('folder_model for vosk')
speech = sr.Recognizer()
vosk.SetLogLevel(-1)
def newvtt():
voice_input = ""
with sr.Microphone() as source:
speech.adjust_for_ambient_noise(source)
try:
audio = speech.listen(source)
voice_input = speech.recognize_vosk(audio)
except sr.UnknownValueError:
pass
except sr.RequestError:
pass
except sr.WaitTimeoutError:
pass
return voice_input
print(newvtt)
您得到的
voice_input
字符串是一个 JSON 字符串。因此,要获取实际的语音字符串,您必须解析 JSON。
实施示例:
import speech_recognition as sr
import vosk
# Import json library to parse JSON
import json
vosk.Model('folder_model for vosk')
speech = sr.Recognizer()
vosk.SetLogLevel(-1)
def newvtt():
voice_input = ""
with sr.Microphone() as source:
speech.adjust_for_ambient_noise(source)
try:
audio = speech.listen(source)
# Recognize speech using Vosk and get the result as JSON
result = speech.recognize_vosk(audio)
# Parse the JSON result to extract the text
result_dict = json.loads(result)
voice_input = result_dict.get("text", "") # Get only the "text" part
except sr.UnknownValueError:
pass
except sr.RequestError:
pass
except sr.WaitTimeoutError:
pass
return voice_input
print(newvtt()) # This will ensure that only 'Hello' string is returned instead of JSON
希望这有帮助。