Guest User

Untitled

a guest
Aug 14th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.58 KB | None | 0 0
  1. import speech_recognition
  2. import pyttsx3
  3. import os
  4.  
  5. key = '' # key for microsoft would go here
  6. engine = pyttsx3.init()
  7.  
  8. wire_colors = ['red', 'white', 'blue', 'yellow', 'black']
  9.  
  10. symbol_table_cols = 6 # It looks like columns on manual
  11. symbol_table = [['racket', 'at', 'lambda', 'lightning', 'kitty', 'h', 'backward'],
  12. ['e', 'racket', 'backward', 'curly', 'light', 'h', 'question'],
  13. ['copyright', 'ear', 'curly', 'spider', 'r', 'lambda', 'light'],
  14. ['6', 'paragraph', 'tb', 'kitty', 'spider', 'question', 'smile'],
  15. ['trident', 'smile', 'tb', 'forward', 'paragraph', '3', 'dark'],
  16. ['6', 'e', 'railroad', 'ae', 'trident', 'n', 'omega']]
  17.  
  18. def get_speech():
  19. recog = speech_recognition.Recognizer()
  20. mic = speech_recognition.Microphone()
  21. os.system('cls')
  22. print('--- Decode Bot ---')
  23. with mic as source:
  24. recog.adjust_for_ambient_noise(source)
  25. print('Listening...')
  26. audio = recog.listen(source)
  27. try:
  28. print('Interpreting...')
  29. result = recog.recognize_bing(audio, key)
  30. if result == 'Stop.':
  31. exit(0)
  32. return result
  33. except speech_recognition.UnknownValueError:
  34. return 'ERROR'
  35.  
  36. def wait_for_module():
  37. while True:
  38. command = get_speech()
  39. if (command.startswith('Decode')):
  40. module = command[7:-1]
  41. if module == 'wires':
  42. do_wires()
  43. if module == 'button':
  44. do_button()
  45. if module == 'keypad':
  46. do_keypad()
  47. if command == 'Thank you.':
  48. speak('You\'re welcome. We did it together.')
  49. return
  50.  
  51. def speak(str):
  52. engine.say(str)
  53. engine.runAndWait()
  54.  
  55. def do_wires():
  56. speak('Decoding wires.')
  57. gettingSequence = True
  58. while gettingSequence:
  59. speak('What is the sequence of wires?')
  60. sequence = get_speech()[0:-1].lower().split()
  61. gettingSequence = not is_valid_sequence(sequence)
  62. if (gettingSequence):
  63. speak('I didn\'t get that, please try again.')
  64. speak('Cut the ' + get_wire_to_cut(sequence) + ' wire')
  65.  
  66. def is_valid_sequence(sequence):
  67. for color in sequence:
  68. if color not in wire_colors:
  69. return False
  70. return True
  71.  
  72. def get_wire_to_cut(sequence):
  73. length = len(sequence)
  74. if length == 3:
  75. if 'red' not in sequence:
  76. return 'second'
  77. if sequence[2] == 'white':
  78. return 'third'
  79. blue_info = get_color_count_info(sequence, 'blue')
  80. if blue_info[0] > 1:
  81. return get_order_from_num(blue_info[1])
  82. return 'third'
  83. if length == 4:
  84. red_info = get_color_count_info(sequence, 'red')
  85. if red_info[0] > 1:
  86. if get_last_serial_digit() % 2 == 1:
  87. return get_order_from_num(red_info[1])
  88. if sequence[3] == 'yellow' and 'red' not in sequence:
  89. return 'first'
  90. if sequence.count('blue') == 1:
  91. return 'first'
  92. if sequence.count('yellow') > 1:
  93. return 'fourth'
  94. return 'second'
  95. if length == 5:
  96. if sequence[4] == 'black':
  97. if get_last_serial_digit() % 2 == 1:
  98. return 'fourth'
  99. if sequence.count('red') == 1 and sequence.count('yellow') > 1:
  100. return 'first'
  101. if 'black' not in sequence:
  102. return 'second'
  103. return 'first'
  104. if length == 6:
  105. if 'yellow' not in sequence:
  106. if get_last_serial_digit() % 2 == 1:
  107. return 'third'
  108. if sequence.count('yellow') == 1 and sequence.count('white') > 1:
  109. return 'fourth'
  110. if 'red' not in sequence:
  111. return 'sixth'
  112. return 'fourth'
  113. return 'ERROR'
  114.  
  115. def get_color_count_info(sequence, color):
  116. count = 0; index = 0;
  117. for i in range(len(sequence)):
  118. if sequence[i] == color:
  119. count += 1; index = i;
  120. return ((count, index))
  121.  
  122. def get_order_from_num(num):
  123. if num == 0: return 'first'
  124. if num == 1: return 'second'
  125. if num == 2: return 'third'
  126. if num == 3: return 'fourth'
  127. if num == 4: return 'fifth'
  128. if num == 5: return 'sixth'
  129.  
  130. def get_last_serial_digit():
  131. speak('What is the last digit of the serial number?')
  132. return int(get_speech()[0:-1])
  133.  
  134. def do_button():
  135. speak('Decoding the button.')
  136. speak('What is the color of the button?')
  137. color = get_speech()
  138. speak('What does the button say?')
  139. text = get_speech()
  140. if color == 'Blue.' and text == 'Abort.':
  141. press_and_hold()
  142. return
  143. speak('How many batteries are on the bomb?')
  144. batteries = int(get_speech()[0:-1])
  145. if batteries > 1 and text == 'Detonate.':
  146. speak('Press and immediately release the button.')
  147. return
  148. if color == 'White.':
  149. speak('Is there a lit indicator with the label CAR?')
  150. if get_speech() == 'Yes.':
  151. press_and_hold()
  152. return
  153. if batteries > 2:
  154. speak('Is there a lit indicator with the label FRK?')
  155. if get_speech() == 'Yes.':
  156. speak('Press and immediately release the button.')
  157. return
  158. if color == 'Yellow.':
  159. press_and_hold()
  160. return
  161. if color == 'Red.' and text == 'Hold.':
  162. speak('Press and immediately release the button.')
  163. return
  164. press_and_hold()
  165.  
  166. def press_and_hold():
  167. speak('Press and hold the button. What is the color of the strip?')
  168. strip_color = get_speech()
  169. timer_num = 1
  170. if strip_color == 'Blue.': timer_num = 4
  171. if strip_color == 'White.': timer_num = 1
  172. if strip_color == 'Yellow.': timer_num = 5
  173. speak('Release the button when the timer has a ' + str(timer_num) + ' in any position.')
  174.  
  175. def do_keypad():
  176. speak('Decoding the keypad.')
  177. speak('What are the four symbols?')
  178. symbols = get_speech()[0:-1].lower().split()
  179. occ = [] # occurances
  180. for i in range(len(symbols)):
  181. occ.append([])
  182. for j in range(symbol_table_cols):
  183. if symbols[i] in symbol_table[j]:
  184. occ[i].append(j)
  185. col = -1
  186. for j in range(symbol_table_cols):
  187. if j in occ[0] and j in occ[1] and j in occ[2] and j in occ[3]:
  188. col = j
  189. # print(symbols)
  190. # print(occ)
  191. if col == -1:
  192. speak('Those symbols are not valid. Please try again.')
  193. return
  194. new_symbols = []
  195. for symbol in symbol_table[col]:
  196. if symbol in symbols:
  197. new_symbols.append(symbol)
  198. speak('Press the symbols in the following order:')
  199. for symbol in new_symbols:
  200. speak(symbol)
  201.  
  202. def main():
  203. speak('Let\'s defuse this bomb.')
  204. wait_for_module()
  205. #print(get_speech())
  206.  
  207. main()
Add Comment
Please, Sign In to add comment