import torchaudio
from torchaudio.pipelines import WAV2VEC2_ASR_BASE_960H
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
# Load the pre-trained Wav2Vec2 model and tokenizer
bundle = WAV2VEC2_ASR_BASE_960H
model = bundle.get_model().to('cpu')
tokenizer = bundle.get_tokenizer()
# Function to transcribe audio file
def transcribe_audio(audio_file_path):
# Load audio file
waveform, sample_rate = torchaudio.load(audio_file_path)
# Resample if necessary
resampler = torchaudio.transforms.Resample(orig_freq=sample_rate, new_freq=bundle.sample_rate)
waveform = resampler(waveform).squeeze(0)
# Generate features
with torch.inference_mode():
features, _ = model.extract_features(waveform.unsqueeze(0))
# Decode features to text
emissions = model.classifier(features)
emission_log_probs = torch.log_softmax(emissions, dim=-1)
decoded_ids = torch.argmax(emission_log_probs, dim=-1)
transcript = tokenizer.decode(decoded_ids[0].tolist())
return transcript
# Example usage
audio_file_path = "example.wav" # Replace with your audio file path
transcription = transcribe_audio(audio_file_path)
print(f"Transcription: {transcription}")
torchaudio语音识别
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 本作者源代码链接:my.oschina.net/xshuai/blog/1585505 【百度语音识别】JavaA...
- silk v3录音转olami语音识别和语义处理的api服务(ubuntu16.04服务器上实现) 重要的写在前面...
- 1 简介 语音识别技术为人们提供一种更方便的人机界面,使人与计算机之间、人与人之间的通信更加方便、快捷。随着对语音...
- CMU Sphinx是目前语音识别技术中比较热门的开源技术之一。CMU Sphinx 是一款源于卡内基梅隆大...