Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import speech_recognition
- import pyttsx3
- import os
- key = '' # key for microsoft would go here
- engine = pyttsx3.init()
- wire_colors = ['red', 'white', 'blue', 'yellow', 'black']
- symbol_table_cols = 6 # It looks like columns on manual
- symbol_table = [['racket', 'at', 'lambda', 'lightning', 'kitty', 'h', 'backward'],
- ['e', 'racket', 'backward', 'curly', 'light', 'h', 'question'],
- ['copyright', 'ear', 'curly', 'spider', 'r', 'lambda', 'light'],
- ['6', 'paragraph', 'tb', 'kitty', 'spider', 'question', 'smile'],
- ['trident', 'smile', 'tb', 'forward', 'paragraph', '3', 'dark'],
- ['6', 'e', 'railroad', 'ae', 'trident', 'n', 'omega']]
- def get_speech():
- recog = speech_recognition.Recognizer()
- mic = speech_recognition.Microphone()
- os.system('cls')
- print('--- Decode Bot ---')
- with mic as source:
- recog.adjust_for_ambient_noise(source)
- print('Listening...')
- audio = recog.listen(source)
- try:
- print('Interpreting...')
- result = recog.recognize_bing(audio, key)
- if result == 'Stop.':
- exit(0)
- return result
- except speech_recognition.UnknownValueError:
- return 'ERROR'
- def wait_for_module():
- while True:
- command = get_speech()
- if (command.startswith('Decode')):
- module = command[7:-1]
- if module == 'wires':
- do_wires()
- if module == 'button':
- do_button()
- if module == 'keypad':
- do_keypad()
- if command == 'Thank you.':
- speak('You\'re welcome. We did it together.')
- return
- def speak(str):
- engine.say(str)
- engine.runAndWait()
- def do_wires():
- speak('Decoding wires.')
- gettingSequence = True
- while gettingSequence:
- speak('What is the sequence of wires?')
- sequence = get_speech()[0:-1].lower().split()
- gettingSequence = not is_valid_sequence(sequence)
- if (gettingSequence):
- speak('I didn\'t get that, please try again.')
- speak('Cut the ' + get_wire_to_cut(sequence) + ' wire')
- def is_valid_sequence(sequence):
- for color in sequence:
- if color not in wire_colors:
- return False
- return True
- def get_wire_to_cut(sequence):
- length = len(sequence)
- if length == 3:
- if 'red' not in sequence:
- return 'second'
- if sequence[2] == 'white':
- return 'third'
- blue_info = get_color_count_info(sequence, 'blue')
- if blue_info[0] > 1:
- return get_order_from_num(blue_info[1])
- return 'third'
- if length == 4:
- red_info = get_color_count_info(sequence, 'red')
- if red_info[0] > 1:
- if get_last_serial_digit() % 2 == 1:
- return get_order_from_num(red_info[1])
- if sequence[3] == 'yellow' and 'red' not in sequence:
- return 'first'
- if sequence.count('blue') == 1:
- return 'first'
- if sequence.count('yellow') > 1:
- return 'fourth'
- return 'second'
- if length == 5:
- if sequence[4] == 'black':
- if get_last_serial_digit() % 2 == 1:
- return 'fourth'
- if sequence.count('red') == 1 and sequence.count('yellow') > 1:
- return 'first'
- if 'black' not in sequence:
- return 'second'
- return 'first'
- if length == 6:
- if 'yellow' not in sequence:
- if get_last_serial_digit() % 2 == 1:
- return 'third'
- if sequence.count('yellow') == 1 and sequence.count('white') > 1:
- return 'fourth'
- if 'red' not in sequence:
- return 'sixth'
- return 'fourth'
- return 'ERROR'
- def get_color_count_info(sequence, color):
- count = 0; index = 0;
- for i in range(len(sequence)):
- if sequence[i] == color:
- count += 1; index = i;
- return ((count, index))
- def get_order_from_num(num):
- if num == 0: return 'first'
- if num == 1: return 'second'
- if num == 2: return 'third'
- if num == 3: return 'fourth'
- if num == 4: return 'fifth'
- if num == 5: return 'sixth'
- def get_last_serial_digit():
- speak('What is the last digit of the serial number?')
- return int(get_speech()[0:-1])
- def do_button():
- speak('Decoding the button.')
- speak('What is the color of the button?')
- color = get_speech()
- speak('What does the button say?')
- text = get_speech()
- if color == 'Blue.' and text == 'Abort.':
- press_and_hold()
- return
- speak('How many batteries are on the bomb?')
- batteries = int(get_speech()[0:-1])
- if batteries > 1 and text == 'Detonate.':
- speak('Press and immediately release the button.')
- return
- if color == 'White.':
- speak('Is there a lit indicator with the label CAR?')
- if get_speech() == 'Yes.':
- press_and_hold()
- return
- if batteries > 2:
- speak('Is there a lit indicator with the label FRK?')
- if get_speech() == 'Yes.':
- speak('Press and immediately release the button.')
- return
- if color == 'Yellow.':
- press_and_hold()
- return
- if color == 'Red.' and text == 'Hold.':
- speak('Press and immediately release the button.')
- return
- press_and_hold()
- def press_and_hold():
- speak('Press and hold the button. What is the color of the strip?')
- strip_color = get_speech()
- timer_num = 1
- if strip_color == 'Blue.': timer_num = 4
- if strip_color == 'White.': timer_num = 1
- if strip_color == 'Yellow.': timer_num = 5
- speak('Release the button when the timer has a ' + str(timer_num) + ' in any position.')
- def do_keypad():
- speak('Decoding the keypad.')
- speak('What are the four symbols?')
- symbols = get_speech()[0:-1].lower().split()
- occ = [] # occurances
- for i in range(len(symbols)):
- occ.append([])
- for j in range(symbol_table_cols):
- if symbols[i] in symbol_table[j]:
- occ[i].append(j)
- col = -1
- for j in range(symbol_table_cols):
- if j in occ[0] and j in occ[1] and j in occ[2] and j in occ[3]:
- col = j
- # print(symbols)
- # print(occ)
- if col == -1:
- speak('Those symbols are not valid. Please try again.')
- return
- new_symbols = []
- for symbol in symbol_table[col]:
- if symbol in symbols:
- new_symbols.append(symbol)
- speak('Press the symbols in the following order:')
- for symbol in new_symbols:
- speak(symbol)
- def main():
- speak('Let\'s defuse this bomb.')
- wait_for_module()
- #print(get_speech())
- main()
Add Comment
Please, Sign In to add comment