Mac で音声認識
Python で音声認識をする
Pythonで日本語を音声認識をして音声をテキスト化し、それを音声合成して発音する方法簡単解説【オウム返し by Mac その1:音声認識編】
を参考に
pip install speechrecognition
でインストール
import subprocess
import tempfile
# 音声入力
while True:
r = sr.Recognizer()
with sr.Microphone() as source:
print("何かお話しして下さい。")
audio = r.listen(source)
try:
# Google Web Speech APIで音声認識
text = r.recognize_google(audio, language="ja-JP")
except sr.UnknownValueError:
print("Google Web Speech APIは音声を認識できませんでした。")
except sr.RequestError as e:
print("GoogleWeb Speech APIに音声認識を要求できませんでした;"
" {0}".format(e))
else:
print(text)
if text == "終わりだよ":
break
print("完了。")
を
Mic,py
として実行したけど
Traceback (most recent call last):
File "/Users/snowpool/mic.py", line 7, in <module>
r = sr.Recognizer()
NameError: name 'sr' is not defined. Did you mean: 'str'?
となる
pip install Pyaudio
を実行したが
Building wheel for Pyaudio (pyproject.toml) ... error
error: subprocess-exited-with-error
× Building wheel for Pyaudio (pyproject.toml) did not run successfully.
│ exit code: 1
╰─> [18 lines of output]
running bdist_wheel
running build
running build_py
creating build
creating build/lib.macosx-12.5-arm64-cpython-310
creating build/lib.macosx-12.5-arm64-cpython-310/pyaudio
copying src/pyaudio/__init__.py -> build/lib.macosx-12.5-arm64-cpython-310/pyaudio
running build_ext
building 'pyaudio._portaudio' extension
creating build/temp.macosx-12.5-arm64-cpython-310
creating build/temp.macosx-12.5-arm64-cpython-310/src
creating build/temp.macosx-12.5-arm64-cpython-310/src/pyaudio
clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -DMACOS=1 -I/usr/local/include -I/usr/include -I/opt/homebrew/include -I/Users/snowpool/.pyenv/versions/3.10.6/include/python3.10 -c src/pyaudio/device_api.c -o build/temp.macosx-12.5-arm64-cpython-310/src/pyaudio/device_api.o
src/pyaudio/device_api.c:9:10: fatal error: 'portaudio.h' file not found
#include "portaudio.h"
^~~~~~~~~~~~~
1 error generated.
error: command '/usr/bin/clang' failed with exit code 1
[end of output]
note: This error originates from a subprocess, and is likely not a problem with pip.
ERROR: Failed building wheel for Pyaudio
Failed to build Pyaudio
ERROR: Could not build wheels for Pyaudio, which is required to install pyproject.toml-based projects
となる
PythonのSpeechRecognitionでマイク録音と文字起こしを簡単実装
を参考に
brew install portaudio
のあと
pip install PyAudio
でインストール成功
しかし
pip install speech_recognition
を実行すると
ERROR: Could not find a version that satisfies the requirement speech_recognition (from versions: none) ERROR: No matching distribution found for speech_recognition
となる
とりあえず
Mkdir test_cpt Cd test_gpt Vim mic.py
として
import speech_recognition as sr
r = sr.Recognizer()
with sr.Microphone() as input:
print("録音中:")
audio = r.listen(input)
で保存し
Python mic.py
を実行すれば
録音中
と表示された
あとは
text = r.recognize_google(audio, language='ja-JP')
として
録音したデータをrecognize_google関数の引数に与えるだけ
第二引数のlanguageを変えれば英語とか他の言語として文字起こしできるらしい
入れる場所がいまいちわからんので
音声入力で文章作成するアプリの作り方【Python】
を参考にやってみる
Python】音声認識ライブラリのSpeechRecognitionでサクッと文字起こし
を参考に
もう少しやりやすく実際にマイクの認識と文字列かだけにしたいので
# micro.py
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))
の実行結果は
Microphone with name "MacBook Airのマイク" found for `Microphone(device_index=0)` Microphone with name "MacBook Airのスピーカー" found for `Microphone(device_index=1)`
これをもとに
# voice_test.py
import speech_recognition as sr
r = sr.Recognizer()
with sr.Microphone(device_index=0) as source:
print('録音中: ')
audio = r.listen(source)
try:
text = r.recognize_google(audio, language='ja-JP')
print(text)
except:
print("録音されていません")
を実行
しかし
録音されていません
となる
# voice_test.py
import speech_recognition as sr
r = sr.Recognizer()
with sr.Microphone(device_index=0) as source:
print('録音中: ')
audio = r.listen(source)
try:
text = r.recognize_google(audio, language='ja-JP')
print(text)
# 録音された音声をファイルへ保存
with open("record.wav", "wb") as f:
f.write(audio.get_raw_data())
except:
print("録音されていません")
でテキストにできるらしいけど
そもそもマイクを認識していない?
参考サイトはwin10
Mac での動いているのを探す
Pythonで日本語を音声認識をして音声をテキスト化し、それを音声合成して発音する方法簡単解説【オウム返し by Mac その1:音声認識編】
を参考に
import speech_recognition as sr
import subprocess
import tempfile
# 音声入力
while True:
r = sr.Recognizer()
with sr.Microphone() as source:
print("何かお話しして下さい。")
audio = r.listen(source)
try:
# Google Web Speech APIで音声認識
text = r.recognize_google(audio, language="ja-JP")
except sr.UnknownValueError:
print("Google Web Speech APIは音声を認識できませんでした。")
except sr.RequestError as e:
print("GoogleWeb Speech APIに音声認識を要求できませんでした;"
" {0}".format(e))
else:
print(text)
if text == "終わりだよ":
break
print("完了。")
を実行したけど
Traceback (most recent call last):
File "/Users/snowpool/test_gpt/voice_text.py", line 15, in <module>
text = r.recognize_google(audio, language="ja-JP")
File "/Users/snowpool/.pyenv/versions/3.10.6/lib/python3.10/site-packages/speech_recognition/__init__.py", line 879, in recognize_google
flac_data = audio_data.get_flac_data(
File "/Users/snowpool/.pyenv/versions/3.10.6/lib/python3.10/site-packages/speech_recognition/__init__.py", line 495, in get_flac_data
flac_converter = get_flac_converter()
File "/Users/snowpool/.pyenv/versions/3.10.6/lib/python3.10/site-packages/speech_recognition/__init__.py", line 1744, in get_flac_converter
raise OSError("FLAC conversion utility not available - consider installing the FLAC command line application by running `apt-get install flac` or your operating system's equivalent")
OSError: FLAC conversion utility not available - consider installing the FLAC command line application by running `apt-get install flac` or your operating system's equivalent
となる
OSError: FLAC conversion utility not available - consider installing the FLAC command line application by running `apt-get install flac` or your operating system's equivalent
で検索したら
音声データをテキスト変換 / Speech to Text / Python
によれば
Flac が必要らしい
https://xiph.org/flac/download.html
で調べて
FLAC tools for OS X from Homebrew.
で検索
https://formulae.brew.sh/formula/flac
brew install flac
でインストール
import speech_recognition as sr
r = sr.Recognizer()
with sr.Microphone() as input:
print("録音中:")
audio = r.listen(input)
text = r.recognize_google(audio, language='ja-JP')
print(text)
として保存
これで再度実行すると
result2:
{ 'alternative': [ {'confidence': 0.77176797, 'transcript': 'チャート GPT'},
{'transcript': 'チャート jpg'},
{'transcript': 'チャート jpt'},
{'transcript': 'チャート gpd'},
{'transcript': 'じゃあ と jpg'}],
'final': True}
チャート GPT
となり
音声の認識とテキストにすることができた
次に
import speech_recognition as sr
r = sr.Recognizer()
with sr.Microphone(device_index=0) as source:
print('録音中: ')
audio = r.listen(source)
try:
text = r.recognize_google(audio, language='ja-JP')
print(text)
# 録音された音声をファイルへ保存
with open("record.wav", "wb") as f:
f.write(audio.get_raw_data())
except:
print("録音されていません")
を
voice_rec.py
として実行
これでマイクの音声のファイル化ができた
https://dev.classmethod.jp/articles/pyenv-command-not-found/
を参考に
brew unlink pyenv && brew link pyenv
したけど
Notebook が起動しない….
JupyterLabとJupyter Notebookの違いを簡単に解説【Mac】
https://miyukimedaka.com/2020/06/07/0143-jupyterlab-explanation/
を参考に
pip install jupyterlab
で
インストール
jupyter lab
で起動
!pip install pyttsx3
import pyttsx3
engine = pyttsx3.init()
engine.say("音声読み上げライブラリ")
engine.runAndWait()
を実行したら日本語の読み上げができた