Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏

15.2. 语音识别(Automatic Speech Recognition)

语音转文本 STT(Speech To Text)

15.2.1. OpenAI Whisper

https://github.com/openai/whisper

         
import openai
audio_file= open("/path/to/file/audio.mp3", "rb")
transcript = openai.Audio.transcribe("whisper-1", audio_file)                
        
		

15.2.2. FunASR - 语音识别

15.2.2.1. 安装与演示

    	
pip install --upgrade pip    	
pip install -U funasr modelscope
    	
			
    	
from funasr import AutoModel
from funasr.utils.postprocess_utils import rich_transcription_postprocess

model_dir = "iic/SenseVoiceSmall"


model = AutoModel(
    model=model_dir,
    # trust_remote_code=True,
    # remote_code="./model.py",
    vad_model="fsmn-vad",
    vad_kwargs={"max_single_segment_time": 30000},
    # device="cuda:0",
    disable_update=True
)

# en
res = model.generate(
    input=f"{model.model_path}/example/zh.mp3",
    cache={},
    language="auto",  # "zn", "en", "yue", "ja", "ko", "nospeech"
    use_itn=True,
    batch_size_s=60,
    merge_vad=True,  #
    merge_length_s=15,
)
text = rich_transcription_postprocess(res[0]["text"])
print(text)
    	
			
    		
/Users/neo/tmp/social/.venv/bin/python /Users/neo/tmp/social/test.py 
funasr version: 1.3.0.
Downloading Model from https://www.modelscope.cn to directory: /Users/neo/.cache/modelscope/hub/models/iic/SenseVoiceSmall
WARNING:root:trust_remote_code: False
Downloading Model from https://www.modelscope.cn to directory: /Users/neo/.cache/modelscope/hub/models/iic/speech_fsmn_vad_zh-cn-16k-common-pytorch
WARNING:root:trust_remote_code: False
rtf_avg: 0.009: 100%|██████████| 1/1 [00:00<00:00, 19.43it/s]
  0%|          | 0/1 [00:00<?, ?it/s]
  0%|          | 0/1 [00:00<?, ?it/s]
100%|██████████| 1/1 [00:00<00:00,  1.86it/s]
{'load_data': '0.000', 'extract_feat': '0.003', 'forward': '0.538', 'batch_size': '1', 'rtf': '0.104'}, : 100%|██████████| 1/1 [00:00<00:00,  1.86it/s]
rtf_avg: 0.104: 100%|██████████| 1/1 [00:00<00:00,  1.86it/s]
rtf_avg: 0.096, time_speech:  5.616, time_escape: 0.540: 100%|██████████| 1/1 [00:00<00:00,  1.77it/s]
开放时间早上9点至下午5点。

Process finished with exit code 0    	
    		
			

15.2.3. 微软开源语音识别 VibeVoice-ASR-BitNet(VibeASR.cpp)

https://github.com/microsoft/VibeASR.cpp

https://huggingface.co/microsoft/VibeVoice-ASR-BitNet

在线体验:https://huggingface.co/spaces/microsoft/vibevoice-asr-bitnet-demo

 
			 
			
			
		

15.2.4. SpeechRecognition

https://github.com/Uberi/speech_recognition

15.2.4.1. 安装

			
pip install SpeechRecognition
			
			

麦克风相关

			
brew install portaudio
pip install pyaudio
			
			

运行下面命令授权访问麦克风

			
neo@MacBook-Pro-Neo ~ % python3 -m speech_recognition			
			
			

15.2.4.2. 查看麦克风列表

			
import speech_recognition as sr

for index, name in enumerate(sr.Microphone.list_microphone_names()):
    print("Microphone with name \"{1}\" found for `Microphone(device_index={0})`".format(index, name))			
			
			

输出结果

			
neo@MacBook-Pro-Neo ~/workspace/python/speech % python3 microphone.py
Microphone with name "Built-in Microphone" found for `Microphone(device_index=0)`
Microphone with name "Built-in Output" found for `Microphone(device_index=1)`			
			
			

指定麦克风设备

			
import speech_recognition as sr
print(sr.__version__) # just to print the version not required
r = sr.Recognizer()
mic = sr.Microphone(device_index=1) #my device index is 1, you have to put your device index			
			
			

噪声抑制

			
import speech_recognition as sr
print(sr.__version__) # just to print the version not required
r = sr.Recognizer()
my_mic = sr.Microphone(device_index=1) #my device index is 1, you have to put your device index
with my_mic as source:
    print("Say now!!!!")
    r.adjust_for_ambient_noise(source) #reduce noise
    audio = r.listen(source) #take voice input from the microphone
print(r.recognize_google(audio)) #to print voice into text
			
			

15.2.5. DeepSpeech

https://deepspeech.readthedocs.io/en/latest/index.html

			 
# Install DeepSpeech
pip3 install deepspeech

# Download pre-trained English model files
curl -LO https://github.com/mozilla/DeepSpeech/releases/download/v0.9.3/deepspeech-0.9.3-models.pbmm
curl -LO https://github.com/mozilla/DeepSpeech/releases/download/v0.9.3/deepspeech-0.9.3-models.scorer

# Download example audio files
curl -LO https://github.com/mozilla/DeepSpeech/releases/download/v0.9.3/audio-0.9.3.tar.gz
tar xvf audio-0.9.3.tar.gz

# Transcribe an audio file
deepspeech --model deepspeech-0.9.3-models.pbmm --scorer deepspeech-0.9.3-models.scorer --audio audio/2830-3980-0043.wav
			
		

15.2.6. kaldi

         
docker run -it kaldiasr/kaldi:latest bash
docker run -it --runtime=nvidia kaldiasr/kaldi:gpu-latest bash
        
		
         
docker run -it kaldiasr/kaldi:latest bash        
        
		

15.2.7. PocketSphinx 文件转文本

PocketSphinx默认仅支持英文识别,中文需要下载 语言模型文件 ,Mandarin 为中文普通话。

			
brew install swig
brew install pocketsphinx
pip install PocketSphinx			
			
		

从文件识别

			
import speech_recognition as sr

# obtain audio from the file
recognizer = sr.Recognizer()
audioFile = sr.AudioFile(r"english.wav")
with audioFile as source:
    audio = recognizer.record(source)
# recognize speech using Sphinx
try:
    print("Sphinx thinks you said: " + recognizer.recognize_sphinx(audio))
except sr.UnknownValueError:
    print("Sphinx could not understand audio")
except sr.RequestError as e:
    print("Sphinx error; {0}".format(e))

			
			
		

从麦克风识别

			
#!/usr/bin/env python3

import speech_recognition as sr

print(sr.__version__)

for index, name in enumerate(sr.Microphone.list_microphone_names()):
    print("Microphone with name \"{1}\" found for `Microphone(device_index={0})`".format(index, name))

# obtain audio from the microphone
r = sr.Recognizer()
with sr.Microphone() as source:
    print("Say something!")
    audio = r.listen(source)

# recognize speech using Sphinx
try:
    print("Sphinx thinks you said: " + r.recognize_sphinx(audio))
except sr.UnknownValueError:
    print("Sphinx could not understand audio")
except sr.RequestError as e:
    print("Sphinx error; {0}".format(e))