1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

簡単な音声認識で疑似音声通話

Last updated at Posted at 2022-05-07

以前ラズパイで音声認識をしようとしたところ日本語の発声がバグって上手くいかなかったので音声ファイルの再生をすることで上手く出来ました。
動画再生・ページ検索・現在時刻・Wikipedia朗読ができます
①MP3ファイルを作成

from gtts import gTTS
from pydub import AudioSegment
from pydub.playback import play

tts = gTTS(text="キーワード",lang="ja")
tts.save("keyword.mp3.mp3")
play(AudioSegment.from_mp3("hello.mp3"))

※キーワードの部分は喋らせたい言葉を
 keyword.mp3は好きなファイル名を(.mp3は必須)

②音声認識

from math import inf
import speech_recognition
import pyttsx3
import pywhatkit
import datetime
import wikipedia
from gtts import gTTS
from pydub import AudioSegment
from pydub.playback import play
import time

recognizer = speech_recognition.Recognizer()
speaker = pyttsx3.init()
voices = speaker.getProperty('voices')
speaker.setProperty('voice',100)
wikipedia.set_lang("ja")


def take_command():
    try:
        with speech_recognition.Microphone() as source:
            recognizer.adjust_for_ambient_noise(source)
            voice = recognizer.listen(source)
            commnd = recognizer.recognize_google(voice,language="ja-JP")
            commnd = commnd.replace('聞きたいことは何ですか','')
            print(commnd)
            commnd= text.lower()
            
    except:
        pass
    return commnd

def run_speech():
    try:
        cmd = take_command()

        if '動画' in cmd:
            song = cmd.replace('動画','')
            print(song + "\n")
            tts = gTTS(text = f"{song}を再生します",lang="ja")
            tts.save("song.mp3")
            play(AudioSegment.from_mp3("song.mp3"))
            pywhatkit.playonyt(song,open_video=True)
        
        elif '時間' in cmd:
            time = datetime.datetime.now().strftime('%H:%M')
            print(f'{time}')
            tts = gTTS(text = time,lang="ja")
            tts.save("time.mp3")
            play(AudioSegment.from_mp3("time.mp3"))
        elif 'について' in cmd:
            cmd = cmd.replace('について','')
            result = wikipedia.page(cmd).content
            result = result[:200] + "以下省略"
            print(result + "\n")
            tts = gTTS(text = result,lang="ja")
            tts.save("tell_me.mp3")
            play(AudioSegment.from_mp3("tell_me.mp3"))
        elif '検索' in cmd:
            pywhatkit.search(cmd)
        elif 'こんにちは' in cmd:
            print("こんにちは")
            play(AudioSegment.from_mp3("hello.mp3"))
        else:
            print("もう一度言ってください")
            play(AudioSegment.from_mp3("agein.mp3"))
    except:
        pass
print("聞きたいことは何ですか....\n")
play(AudioSegment.from_mp3("ask.mp3"))
    
while True:
    run_speech()
 elif 'キーボード' in cmd:
            print("キーボード")
            play(AudioSegment.from_mp3("keyword.mp3"))

※キーボードの部分は好きな言葉を
 keyword.mp3の部分は①作ったファイルを指定します。

1
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?