Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.17 KB | None | 0 0
  1. import json
  2. import os
  3. import pygame
  4. from subprocess import call
  5. from pathlib import Path
  6.  
  7.  
  8. def file_check():
  9. synth_speech('responses/ask_name.mp3', 'What is your name? or say quit to leave')
  10. synth_speech('responses/ask_pass.mp3', 'Please provide your password or say quit to leave')
  11. synth_speech('responses/take_picture.mp3', 'Stand in front of the camera')
  12. synth_speech('responses/no_account_exists.mp3', 'No current account registration will begin in a moment')
  13. synth_speech('responses/account_exists.mp3', 'A current account owner will be needed to create a new account')
  14. synth_speech('responses/ask_account_name.mp3', 'Account owner what is your name?')
  15. synth_speech('responses/start_registration.mp3', 'We can now start registration')
  16.  
  17. def synth_speech(file_name, text):
  18. with open('config.json') as json_config:
  19. config = json.load(json_config)
  20.  
  21. base_call = 'curl -X POST -u "{}":"{}" --header "Content-Type: application/json" --header "Accept: audio/mp3" --data '.format(config['text-to-speech-user'], config['text-to-speech-password'])
  22.  
  23. file = Path(file_name)
  24. if not file.is_file():
  25. divider = '\\\"'
  26. text_string = '\"{' + divider + 'text' + divider + ':' + divider + text + divider + '}\"'
  27. complete_call = base_call + text_string + ' --output {} "https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize?voice=en-US_MichaelVoice"'.format(file_name)
  28. call(complete_call, shell=True)
  29. print complete_call
  30.  
  31. def recognize_speech():
  32. with open('config.json') as json_config:
  33. config = json.load(json_config)
  34.  
  35. base_call = 'curl -X POST -u "{}":"{}" --header "Content-Type: audio/mp3" --data-binary "@response.mp3" '.format(config['speech-to-text-user'], config['speech-to-text-pass']) + \
  36. '"https://stream.watsonplatform.net/speech-to-text/api/v1/recognize?timestamps=true&word_alternatives_threshold=0.9" >> response.json'
  37.  
  38.  
  39. def register():
  40. accounts_file = Path('accounts.json')
  41. accounts = []
  42. pygame.mixer.init()
  43. if accounts_file.is_file():
  44. call('mpg123 responses/account_exists.mp3', shell=True)
  45. if(not verify_account()):
  46. return False
  47. else:
  48. call('mpg123 responses/no_account_exists.mp3', shell=True)
  49. pass
  50.  
  51.  
  52. call('mpg123 responses/start_registration.mp3', shell=True)
  53. confirmed = False
  54. while(not confirmed):
  55. call('mpg123 responses/ask_name.mp3', shell=True)
  56. call('arecord -D plughw:1 --duration=10 -f cd response.mp3', shell=True)
  57. recognize_speech()
  58. with open('response.json') as json_response:
  59. response = json.load(json_response)
  60. name = temp_data['results'][0]['alternatives'][0]['transcript']
  61. synth_speech('responses/confirm_name.mp3', 'Confirm with yes that your name is {}'.format(name))
  62. call('mpg123 responses/confirm_name.mp3', shell=True)
  63. call('arecord -D plughw:1 --duration=10 -f cd response.mp3')
  64. recognize_speech()
  65. with open('response.json') as json_response:
  66. response = json.load(json_response)
  67. confirmation_text = temp_data['results'][0]['alternatives'][0]['transcript']
  68. if confirmation_text.upper() == "YES":
  69. break
  70. else:
  71. pass
  72. confirmed = False
  73. while(not confirmed):
  74. call('mpg123 responses/ask_pass.mp3', shell=True)
  75. record_speech()
  76. recognize_speech()
  77. with open('response.json') as json_response:
  78. response = json.load(json_response)
  79. password = temp_data['results'][0]['alternatives'][0]['transcript']
  80. synth_speech('responses/confirm_pass.mp3', 'Confirm with yes that your password is {}'.format(password))
  81. call('mpg123 responses/confirm_pass.mp3', shell=True)
  82. record_speech()
  83. recognize_speech()
  84. with open('response.json') as json_response:
  85. response = json.load(json_response)
  86. confirmation_text = temp_data['results'][0]['alternatives'][0]['transcript']
  87. if confirmation_text.upper() == "YES":
  88. break
  89. else:
  90. pass
  91. account = {{
  92. "name": name,
  93. "password": password
  94. }}
  95. with open('accounts.json', 'w') as outfile:
  96. json.dump(account, outfile)
  97.  
  98. def verify_account():
  99. with open('accounts.json') as json_accounts:
  100. accounts = json.load(json_accounts)
  101.  
  102. account_found = False
  103. while(not account_found):
  104. call('mpg123 responses/ask_account_name.mp3', shell=True)
  105. record_speech()
  106. recognize_speech()
  107. with open('response.json') as json_response:
  108. response = json.load(json_response)
  109. text = temp_data['results'][0]['alternatives'][0]['transcript']
  110. for account in accounts:
  111. if account['name'].upper() == text.upper():
  112. account_found = true
  113. elif text.upper() == 'QUIT' or text.upper() == '':
  114. return False
  115.  
  116. password_valid = False
  117. while(not password_valid):
  118. call('mpg123 responses/ask_password.mp3', shell=True)
  119. record_speech()
  120. recognize_speech()
  121. with open('response.json') as json_response:
  122. response = json.load(json_response)
  123. text = temp_data['results'][0]['alternatives'][0]['transcript']
  124. for account in accounts:
  125. if account['password'].upper() == text.upper():
  126. account_found = true
  127. elif text.upper() == 'QUIT' or text.upper() == '':
  128. return False
  129. return True
  130.  
  131. def record_speech():
  132. call('rm responses/response.mp3', shell=True)
  133. call('arecord -D plughw:1 --duration=10 -f cd responses/response.mp3', shell=True)
  134.  
  135.  
  136. if __name__ == "__main__":
  137. file_check()
  138. register()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement