PySide2で音声を録音・文字起こしをする【PySide2,Python】

PySide2でリアルタイムで文字起こし,音声の録音をやってみたいと思います

実行結果

実行すると録音の音声ファイル recorded.wavとその録音音声内容のrecordedText.txtが保存されます f:id:ayousanz:20200904104754p:plain

音声の認識が良くないのか,自分の発音が良くないのか...

正確には録音できない時があります f:id:ayousanz:20200904104908p:plain

準備

今回必要なものをconda でinstallします

音声を扱うためのライブラリ

conda install -c anaconda pyaudio

録音をするためにライブラリ

conda install -c conda-forge speechrecognition

Code

import speech_recognition as sr


def main():
    r = sr.Recognizer()

    with sr.Microphone() as source:
        r.adjust_for_ambient_noise(source)

        print("Please say something")

        audio = r.listen(source)

        print("Recognizing Now .... ")

        # recognize speech using google

        try:
            print("You have said \n" + r.recognize_google(audio))
            print("Audio Recorded Successfully \n ")


        except Exception as e:
            print("Error :  " + str(e))

        # write audio and text

        with open("recorded.wav", "wb") as f:
            f.write(audio.get_wav_data())

        with open("recordedText.txt", "w") as f:
            s = str(r.recognize_google(audio))
            f.write(s)

if __name__ == "__main__":
    main()

参考サイト

codeloop.org