LoginSignup
1
1

More than 1 year has passed since last update.

Google音声認識ライブラリを使用して、音声オセロを作成

Last updated at Posted at 2022-06-14

@y-tetsu 氏のページ 👉http://github.com/y-tetsu/reversi
のオセロ用ライブラリと、google音声認識ライブラリを使用して、音声認識オセロを作ってみました。

y-tetsu 氏のreversiはリバーシ(オセロ)のPython用ライブラリです。
手軽にリバーシAIをプログラミングして、アプリケーションが作れます。

しかし、今回はgoogleの音声認識AIを利用して、音声認識オセロを作って、声で打てるようにしてみました。

exeファイルは、https://plainstudio.org/
にフリーで置いておきます。

メニューバーから、黒(先手)のプレイヤー、白(後手)のプレイヤーを選択します。
プレーヤーから、「音声入力」を選択すると、音声入力出来ます。

音声入力は、「Aの1」あるいは「A1」というように、横軸のアルファベットと、縦軸の数字を発話することで入力します。

(認識しない時は、認識するまでトライしてください。)

音声認識ライブラリは、
https://pypi.org/project/SpeechRecognition/
にあります。使い方は、解説のページがいろいろとネット上にあるので、参考にしてみてください。

Google音声認識ライブラリはフリーで使えて便利ですが、さらに認識を高めたりする方法や、他のライブラリを使った方法など、何かもっといい方法があればと思いますが、どうでしょうか。

from reversi import Reversi
from reversi.strategies import SlowStarter
import speech_recognition as sr
from reversi.strategies import AbstractStrategy
import string
import win32com.client as wincl
from tenacity import retry
import re

class Onsei(AbstractStrategy):
  
  @retry()
  def next_move(self, color, board):
    size = board.size
    legal_moves = board.get_legal_moves(color)
    r = sr.Recognizer()
    mic = sr.Microphone()
    voice = wincl.Dispatch("SAPI.SpVoice")

    def conv(s):
      s = s.replace('の','').lower()
      return string.ascii_lowercase.find(s[0]),int(s[1:])-1

    while True:
        rete = color
        u_dict = { 'black':'くろ', 'white':'しろ'}
        for word,read in u_dict.items():
            rete = rete.replace(word, read)
            
        voice.Speak(rete + "のばんです" )

        excg = "_ABCDEFGH"
        lst = legal_moves
        ans = [f'{excg[v[0]+1]}の{v[1]+1}' for v in lst]
        print(ans)
        result1=re.sub("'","",str(ans))
        
        voice.Speak ('うてるばしょわ'+str(result1)+'です')     
        voice.Speak("どこにうちますか" )
        
        with mic as source:
            r.adjust_for_ambient_noise(source) #雑音対策
            audio = r.listen(source)

        print ("Now to recognize it...")
        s = r.recognize_google(audio, language='ja-JP')
        print(s)
        
        if conv(s) in legal_moves:
            voice.Speak (s)
            
            return(conv(s))
        else:
          voice.Speak("そこはうてません")

Reversi(
    {'音声入力': Onsei(),
     'コンピューター': SlowStarter()
     }
).start()

1
1
2

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