1635939000
音声認識は、話し言葉で単語やフレーズを識別し、それらを人間が読めるテキストに変換するコンピュータソフトウェアの機能です。このチュートリアルでは、SpeechRecognition ライブラリを使用してPythonで音声をテキストに変換する方法を学習します 。
その結果、機械学習モデルを最初から構築する必要はありません。このライブラリは、さまざまな有名な公共音声認識API(Google Cloud Speech API、IBM Speech To Textなど)の便利なラッパーを提供します。
さて、始めましょうpip
:を使用してライブラリをインストールします:
pip3 install SpeechRecognition pydub
さて、新しいPythonファイルを開いてインポートします。
import speech_recognition as sr
現在のディレクトリに英語のスピーチを含むオーディオファイルがあることを確認してください(私と一緒にフォローしたい場合は、ここでオーディオファイルを入手してください):
filename = "16-122828-0002.wav"
このファイルはLibriSpeechデータセットから取得されましたが、任意のオーディオWAVファイルを使用できます。ファイルの名前を変更するだけで、音声認識機能を初期化できます。
# initialize the recognizer
r = sr.Recognizer()
以下のコードは、音声ファイルをロードし、Google音声認識を使用して音声をテキストに変換する役割を果たします。
# open the file
with sr.AudioFile(filename) as source:
# listen for the data (load audio to memory)
audio_data = r.record(source)
# recognize (convert from speech to text)
text = r.recognize_google(audio_data)
print(text)
ファイルをGoogleにアップロードして出力を取得するため、これが完了するまでに数秒かかります。結果は次のとおりです。
I believe you're just talking nonsense
上記のコードは、小または中サイズのオーディオファイルに適しています。次のセクションでは、大きなファイルのコードを記述します。
長いオーディオファイルの音声認識を実行する場合、以下の関数はそれを非常にうまく処理します。
# importing libraries
import speech_recognition as sr
import os
from pydub import AudioSegment
from pydub.silence import split_on_silence
# create a speech recognition object
r = sr.Recognizer()
# a function that splits the audio file into chunks
# and applies speech recognition
def get_large_audio_transcription(path):
"""
Splitting the large audio file into chunks
and apply speech recognition on each of these chunks
"""
# open the audio file using pydub
sound = AudioSegment.from_wav(path)
# split audio sound where silence is 700 miliseconds or more and get chunks
chunks = split_on_silence(sound,
# experiment with this value for your target audio file
min_silence_len = 500,
# adjust this per requirement
silence_thresh = sound.dBFS-14,
# keep the silence for 1 second, adjustable as well
keep_silence=500,
)
folder_name = "audio-chunks"
# create a directory to store the audio chunks
if not os.path.isdir(folder_name):
os.mkdir(folder_name)
whole_text = ""
# process each chunk
for i, audio_chunk in enumerate(chunks, start=1):
# export audio chunk and save it in
# the `folder_name` directory.
chunk_filename = os.path.join(folder_name, f"chunk{i}.wav")
audio_chunk.export(chunk_filename, format="wav")
# recognize the chunk
with sr.AudioFile(chunk_filename) as source:
audio_listened = r.record(source)
# try converting it to text
try:
text = r.recognize_google(audio_listened)
except sr.UnknownValueError as e:
print("Error:", str(e))
else:
text = f"{text.capitalize()}. "
print(chunk_filename, ":", text)
whole_text += text
# return the text for all chunks detected
return whole_text
注:上記のコードを機能させるには、を使用してPydubをインストールする必要がありpip
ます。
上記の関数はsplit_on_silence()
、pydub.silence
モジュールの関数を使用して、無音でオーディオデータをチャンクに分割します。min_silence_len
パラメータは、分割に使用される無音の最小の長さです。
silence_thresh
は、これよりも静かなものが無音と見なされるしきい値です。平均dBFSから14を引いた値に設定しました。keep_silence
引数は、ミリ秒単位で検出された各チャンクの最初と最後に残す無音の量です。
これらのパラメータはすべてのサウンドファイルに完全ではありません。大きなオーディオのニーズでこれらのパラメータを試してみてください。
その後、すべてのチャンクを反復処理し、各音声オーディオをテキストに変換して、それらをすべて合計します。実行例を次に示します。
path = "7601-291468-0006.wav"
print("\nFull text:", get_large_audio_transcription(path))
注:ここで7601-291468-0006.wav
ファイルを取得できます。
出力:
audio-chunks\chunk1.wav : His abode which you had fixed in a bowery or country seat.
audio-chunks\chunk2.wav : At a short distance from the city.
audio-chunks\chunk3.wav : Just at what is now called dutch street.
audio-chunks\chunk4.wav : Sooner bounded with proofs of his ingenuity.
audio-chunks\chunk5.wav : Patent smokejacks.
audio-chunks\chunk6.wav : It required a horse to work some.
audio-chunks\chunk7.wav : Dutch oven roasted meat without fire.
audio-chunks\chunk8.wav : Carts that went before the horses.
audio-chunks\chunk9.wav : Weather cox that turned against the wind and other wrongheaded contrivances.
audio-chunks\chunk10.wav : So just understand can found it all beholders.
Full text: His abode which you had fixed in a bowery or country seat. At a short distance from the city. Just at what is now called dutch street. Sooner bounded with proofs of his ingenuity. Patent smokejacks. It required a horse to work some. Dutch oven roasted meat without fire. Carts that went before the horses. Weather cox that turned against the wind and other wrongheaded contrivances. So just understand can found it all beholders.
したがって、この関数は自動的にフォルダーを作成し、指定した元のオーディオファイルのチャンクを配置してから、それらすべてに対して音声認識を実行します。
これには、PyAudioがマシンにインストールされている必要があります。オペレーティングシステムに応じたインストールプロセスは次のとおりです。
あなたはそれをpipインストールすることができます:
pip3 install pyaudio
最初に依存関係をインストールする必要があります。
sudo apt-get install python-pyaudio python3-pyaudio
pip3 install pyaudio
最初にportaudioをインストールする必要があります。その後、pipインストールするだけです。
brew install portaudio
pip3 install pyaudio
次に、マイクを使用して音声を変換しましょう。
with sr.Microphone() as source:
# read the audio data from the default microphone
audio_data = r.record(source, duration=5)
print("Recognizing...")
# convert speech to text
text = r.recognize_google(audio_data)
print(text)
これはマイクから5秒間聞こえ、その音声をテキストに変換しようとします。
前のコードと非常に似ていますが、ここではMicrophone()オブジェクトを使用してデフォルトのマイクからオーディオを読み取り、record()関数のdurationパラメーターを使用して5秒後に読み取りを停止し、オーディオデータをアップロードします。 Googleに出力テキストを取得します。
record()関数のoffsetパラメーターを使用して、オフセット秒後に記録を開始することもできます。
また、言語パラメータをrecognize_google()関数に渡すことで、さまざまな言語を認識できます。たとえば、スペイン語のスピーチを認識したい場合は、次を使用します。
text = r.recognize_google(audio_data, language="es-ES")
このstackoverflowの回答でサポートされている言語を確認してください。
ご覧のとおり、このライブラリを使用して音声をテキストに変換するのは非常に簡単で簡単です。このライブラリは、世界中で広く使用されています。公式ドキュメントを確認してください。
Pythonを使用せず、それを自動的に実行するサービスが必要な場合は、オーディオをオンラインですばやくテキストに変換するaudextを使用することをお勧めします。見てみな!
Pythonでもテキストを音声に変換する場合は、このチュートリアルを確認してください。
リンク: https://www.thepythoncode.com/article/using-speech-recognition-to-convert-speech-to-text-python
1635939000
音声認識は、話し言葉で単語やフレーズを識別し、それらを人間が読めるテキストに変換するコンピュータソフトウェアの機能です。このチュートリアルでは、SpeechRecognition ライブラリを使用してPythonで音声をテキストに変換する方法を学習します 。
その結果、機械学習モデルを最初から構築する必要はありません。このライブラリは、さまざまな有名な公共音声認識API(Google Cloud Speech API、IBM Speech To Textなど)の便利なラッパーを提供します。
さて、始めましょうpip
:を使用してライブラリをインストールします:
pip3 install SpeechRecognition pydub
さて、新しいPythonファイルを開いてインポートします。
import speech_recognition as sr
現在のディレクトリに英語のスピーチを含むオーディオファイルがあることを確認してください(私と一緒にフォローしたい場合は、ここでオーディオファイルを入手してください):
filename = "16-122828-0002.wav"
このファイルはLibriSpeechデータセットから取得されましたが、任意のオーディオWAVファイルを使用できます。ファイルの名前を変更するだけで、音声認識機能を初期化できます。
# initialize the recognizer
r = sr.Recognizer()
以下のコードは、音声ファイルをロードし、Google音声認識を使用して音声をテキストに変換する役割を果たします。
# open the file
with sr.AudioFile(filename) as source:
# listen for the data (load audio to memory)
audio_data = r.record(source)
# recognize (convert from speech to text)
text = r.recognize_google(audio_data)
print(text)
ファイルをGoogleにアップロードして出力を取得するため、これが完了するまでに数秒かかります。結果は次のとおりです。
I believe you're just talking nonsense
上記のコードは、小または中サイズのオーディオファイルに適しています。次のセクションでは、大きなファイルのコードを記述します。
長いオーディオファイルの音声認識を実行する場合、以下の関数はそれを非常にうまく処理します。
# importing libraries
import speech_recognition as sr
import os
from pydub import AudioSegment
from pydub.silence import split_on_silence
# create a speech recognition object
r = sr.Recognizer()
# a function that splits the audio file into chunks
# and applies speech recognition
def get_large_audio_transcription(path):
"""
Splitting the large audio file into chunks
and apply speech recognition on each of these chunks
"""
# open the audio file using pydub
sound = AudioSegment.from_wav(path)
# split audio sound where silence is 700 miliseconds or more and get chunks
chunks = split_on_silence(sound,
# experiment with this value for your target audio file
min_silence_len = 500,
# adjust this per requirement
silence_thresh = sound.dBFS-14,
# keep the silence for 1 second, adjustable as well
keep_silence=500,
)
folder_name = "audio-chunks"
# create a directory to store the audio chunks
if not os.path.isdir(folder_name):
os.mkdir(folder_name)
whole_text = ""
# process each chunk
for i, audio_chunk in enumerate(chunks, start=1):
# export audio chunk and save it in
# the `folder_name` directory.
chunk_filename = os.path.join(folder_name, f"chunk{i}.wav")
audio_chunk.export(chunk_filename, format="wav")
# recognize the chunk
with sr.AudioFile(chunk_filename) as source:
audio_listened = r.record(source)
# try converting it to text
try:
text = r.recognize_google(audio_listened)
except sr.UnknownValueError as e:
print("Error:", str(e))
else:
text = f"{text.capitalize()}. "
print(chunk_filename, ":", text)
whole_text += text
# return the text for all chunks detected
return whole_text
注:上記のコードを機能させるには、を使用してPydubをインストールする必要がありpip
ます。
上記の関数はsplit_on_silence()
、pydub.silence
モジュールの関数を使用して、無音でオーディオデータをチャンクに分割します。min_silence_len
パラメータは、分割に使用される無音の最小の長さです。
silence_thresh
は、これよりも静かなものが無音と見なされるしきい値です。平均dBFSから14を引いた値に設定しました。keep_silence
引数は、ミリ秒単位で検出された各チャンクの最初と最後に残す無音の量です。
これらのパラメータはすべてのサウンドファイルに完全ではありません。大きなオーディオのニーズでこれらのパラメータを試してみてください。
その後、すべてのチャンクを反復処理し、各音声オーディオをテキストに変換して、それらをすべて合計します。実行例を次に示します。
path = "7601-291468-0006.wav"
print("\nFull text:", get_large_audio_transcription(path))
注:ここで7601-291468-0006.wav
ファイルを取得できます。
出力:
audio-chunks\chunk1.wav : His abode which you had fixed in a bowery or country seat.
audio-chunks\chunk2.wav : At a short distance from the city.
audio-chunks\chunk3.wav : Just at what is now called dutch street.
audio-chunks\chunk4.wav : Sooner bounded with proofs of his ingenuity.
audio-chunks\chunk5.wav : Patent smokejacks.
audio-chunks\chunk6.wav : It required a horse to work some.
audio-chunks\chunk7.wav : Dutch oven roasted meat without fire.
audio-chunks\chunk8.wav : Carts that went before the horses.
audio-chunks\chunk9.wav : Weather cox that turned against the wind and other wrongheaded contrivances.
audio-chunks\chunk10.wav : So just understand can found it all beholders.
Full text: His abode which you had fixed in a bowery or country seat. At a short distance from the city. Just at what is now called dutch street. Sooner bounded with proofs of his ingenuity. Patent smokejacks. It required a horse to work some. Dutch oven roasted meat without fire. Carts that went before the horses. Weather cox that turned against the wind and other wrongheaded contrivances. So just understand can found it all beholders.
したがって、この関数は自動的にフォルダーを作成し、指定した元のオーディオファイルのチャンクを配置してから、それらすべてに対して音声認識を実行します。
これには、PyAudioがマシンにインストールされている必要があります。オペレーティングシステムに応じたインストールプロセスは次のとおりです。
あなたはそれをpipインストールすることができます:
pip3 install pyaudio
最初に依存関係をインストールする必要があります。
sudo apt-get install python-pyaudio python3-pyaudio
pip3 install pyaudio
最初にportaudioをインストールする必要があります。その後、pipインストールするだけです。
brew install portaudio
pip3 install pyaudio
次に、マイクを使用して音声を変換しましょう。
with sr.Microphone() as source:
# read the audio data from the default microphone
audio_data = r.record(source, duration=5)
print("Recognizing...")
# convert speech to text
text = r.recognize_google(audio_data)
print(text)
これはマイクから5秒間聞こえ、その音声をテキストに変換しようとします。
前のコードと非常に似ていますが、ここではMicrophone()オブジェクトを使用してデフォルトのマイクからオーディオを読み取り、record()関数のdurationパラメーターを使用して5秒後に読み取りを停止し、オーディオデータをアップロードします。 Googleに出力テキストを取得します。
record()関数のoffsetパラメーターを使用して、オフセット秒後に記録を開始することもできます。
また、言語パラメータをrecognize_google()関数に渡すことで、さまざまな言語を認識できます。たとえば、スペイン語のスピーチを認識したい場合は、次を使用します。
text = r.recognize_google(audio_data, language="es-ES")
このstackoverflowの回答でサポートされている言語を確認してください。
ご覧のとおり、このライブラリを使用して音声をテキストに変換するのは非常に簡単で簡単です。このライブラリは、世界中で広く使用されています。公式ドキュメントを確認してください。
Pythonを使用せず、それを自動的に実行するサービスが必要な場合は、オーディオをオンラインですばやくテキストに変換するaudextを使用することをお勧めします。見てみな!
Pythonでもテキストを音声に変換する場合は、このチュートリアルを確認してください。
リンク: https://www.thepythoncode.com/article/using-speech-recognition-to-convert-speech-to-text-python