-
Notifications
You must be signed in to change notification settings - Fork 0
/
voiceChess.py
101 lines (85 loc) · 3.41 KB
/
voiceChess.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# Check if all the packages are installed and can be imported
# if not, raise an error
# TODO
import os
import time
import pyaudio
import playsound
import speech_recognition as sr
from gtts import gTTS
import util
CHESSBOARD_SQUARES = [
col + str(row) for col in ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'] for row in range(1, 9)
]
CHESSBOARD_PIECES = set(
["pawn", "knight", "rook", "bishop", "king", "queen"])
CASTLE = "castle"
class voiceChess():
def __init__(self, language):
# Check if all the packages are installed and can be imported
# if not, raise an error
# TODO
self.recognizer = sr.Recognizer()
self.language = language
# Some autotune, including finding the working microphone and deleting background noise
# TODO
def _convert_move_predictions_to_move(self, move_predictions):
""" Given a dictionary of predictions from the recognizer, extract the relevant move, if any
Returns a dict with the piece to move and the square to move to
"""
if not(isinstance(move_predictions, dict)):
return {}
transcripts = move_predictions["alternative"]
for transcript_dict in transcripts:
piece_to_move, square_to_move = None, None
transcript = transcript_dict["transcript"].lower()
if CASTLE in transcript:
# A castle move
# TODO
return {
"piece": "king", "move": "castle"
}
else:
for piece in CHESSBOARD_PIECES:
if piece in transcript:
piece_to_move = piece
break
# If there is no piece available then continue on the next transcript
if not(piece_to_move):
continue
else:
for square in CHESSBOARD_SQUARES:
if square in transcript:
square_to_move = util.from_pgn_to_square_coor(
square)
break
if square_to_move:
return {
"piece": piece_to_move, "move": square_to_move
}
return {}
def listen(self):
""" Listen to the speech and extract the moves """
move = None
with sr.Microphone() as source:
self.recognizer.adjust_for_ambient_noise(source)
print("Ready to get microphone input")
audio = self.recognizer.listen(source)
move_predictions = ""
try:
move_predictions = self.recognizer.recognize_google(
audio, show_all=True)
# print(move_predictions)
move = self._convert_move_predictions_to_move(move_predictions)
# print(move)
except Exception as e:
print("There's an error: " + str(e))
if not(move):
print("Cannot detect move")
if __name__ == "__main__":
a = voiceChess("en-US")
a.listen()
# For debugging
# transcripts = {'alternative': [{'transcript': '94', 'confidence': 0.97182459}, {'transcript': 'nicety for'}, {
# 'transcript': 'nice D4'}, {'transcript': 'nice tea for'}, {'transcript': 'nice T4'}], 'final': True}
# print(a._convert_move_predictions_to_move(transcripts))