我觉得这是一个相当普遍的问题,但我还没有找到合适的答案。我有很多人类语音的音频文件,我想打破单词,这可以通过查看波形中的暂停来启发式地完成,但是任何人都可以指出我在 python 中自动执行此操作的函数/库吗?
原文由 user3059201 发布,翻译遵循 CC BY-SA 4.0 许可协议
我觉得这是一个相当普遍的问题,但我还没有找到合适的答案。我有很多人类语音的音频文件,我想打破单词,这可以通过查看波形中的暂停来启发式地完成,但是任何人都可以指出我在 python 中自动执行此操作的函数/库吗?
原文由 user3059201 发布,翻译遵循 CC BY-SA 4.0 许可协议
使用 IBM STT 。使用 timestamps=true
你会在系统检测到它们被说出时得到单词 break up 。
还有许多其他很酷的功能,例如 word_alternatives_threshold
以获得单词的其他可能性和 word_confidence
获得系统预测单词的信心。将 word_alternatives_threshold
设置在(0.1 和 0.01)之间以获得真正的想法。
这需要登录,之后您可以使用生成的用户名和密码。
IBM STT 已经是提到的speechrecognition 模块的一部分,但是要获取单词时间戳,您需要修改函数。
提取和修改后的表单如下所示:
def extracted_from_sr_recognize_ibm(audio_data, username=IBM_USERNAME, password=IBM_PASSWORD, language="en-US", show_all=False, timestamps=False,
word_confidence=False, word_alternatives_threshold=0.1):
assert isinstance(username, str), "``username`` must be a string"
assert isinstance(password, str), "``password`` must be a string"
flac_data = audio_data.get_flac_data(
convert_rate=None if audio_data.sample_rate >= 16000 else 16000, # audio samples should be at least 16 kHz
convert_width=None if audio_data.sample_width >= 2 else 2 # audio samples should be at least 16-bit
)
url = "https://stream-fra.watsonplatform.net/speech-to-text/api/v1/recognize?{}".format(urlencode({
"profanity_filter": "false",
"continuous": "true",
"model": "{}_BroadbandModel".format(language),
"timestamps": "{}".format(str(timestamps).lower()),
"word_confidence": "{}".format(str(word_confidence).lower()),
"word_alternatives_threshold": "{}".format(word_alternatives_threshold)
}))
request = Request(url, data=flac_data, headers={
"Content-Type": "audio/x-flac",
"X-Watson-Learning-Opt-Out": "true", # prevent requests from being logged, for improved privacy
})
authorization_value = base64.standard_b64encode("{}:{}".format(username, password).encode("utf-8")).decode("utf-8")
request.add_header("Authorization", "Basic {}".format(authorization_value))
try:
response = urlopen(request, timeout=None)
except HTTPError as e:
raise sr.RequestError("recognition request failed: {}".format(e.reason))
except URLError as e:
raise sr.RequestError("recognition connection failed: {}".format(e.reason))
response_text = response.read().decode("utf-8")
result = json.loads(response_text)
# return results
if show_all: return result
if "results" not in result or len(result["results"]) < 1 or "alternatives" not in result["results"][0]:
raise Exception("Unknown Value Exception")
transcription = []
for utterance in result["results"]:
if "alternatives" not in utterance:
raise Exception("Unknown Value Exception. No Alternatives returned")
for hypothesis in utterance["alternatives"]:
if "transcript" in hypothesis:
transcription.append(hypothesis["transcript"])
return "\n".join(transcription)
原文由 MonsieurBeilto 发布,翻译遵循 CC BY-SA 3.0 许可协议
2 回答5.2k 阅读✓ 已解决
2 回答1.1k 阅读✓ 已解决
4 回答1.4k 阅读✓ 已解决
3 回答1.3k 阅读✓ 已解决
3 回答1.3k 阅读✓ 已解决
2 回答884 阅读✓ 已解决
1 回答1.8k 阅读✓ 已解决
一种更简单的方法是使用 pydub 模块。最近添加的 静默实用程序 完成了所有繁重的工作,例如
setting up silence threahold
,setting up silence length
。等等,并大大简化了代码,而不是提到的其他方法。这是一个演示实现,灵感来自 这里
设置:
我在文件“az.wav”中有一个音频文件,其中包含从
A
到Z
的口语英语字母。在当前工作目录中创建了一个子目录splitAudio
。执行演示代码后,文件被分成 26 个单独的文件,每个音频文件存储每个音节。观察: 一些音节被切断,可能需要修改以下参数,
min_silence_len=500
silence_thresh=-16
人们可能想根据自己的要求调整这些。
演示代码:
输出: