Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- import os
- import pygame
- from subprocess import call
- from pathlib import Path
- def file_check():
- synth_speech('responses/ask_name.mp3', 'What is your name? or say quit to leave')
- synth_speech('responses/ask_pass.mp3', 'Please provide your password or say quit to leave')
- synth_speech('responses/take_picture.mp3', 'Stand in front of the camera')
- synth_speech('responses/no_account_exists.mp3', 'No current account registration will begin in a moment')
- synth_speech('responses/account_exists.mp3', 'A current account owner will be needed to create a new account')
- synth_speech('responses/ask_account_name.mp3', 'Account owner what is your name?')
- synth_speech('responses/start_registration.mp3', 'We can now start registration')
- def synth_speech(file_name, text):
- with open('config.json') as json_config:
- config = json.load(json_config)
- 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'])
- file = Path(file_name)
- if not file.is_file():
- divider = '\\\"'
- text_string = '\"{' + divider + 'text' + divider + ':' + divider + text + divider + '}\"'
- complete_call = base_call + text_string + ' --output {} "https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize?voice=en-US_MichaelVoice"'.format(file_name)
- call(complete_call, shell=True)
- print complete_call
- def recognize_speech():
- with open('config.json') as json_config:
- config = json.load(json_config)
- 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']) + \
- '"https://stream.watsonplatform.net/speech-to-text/api/v1/recognize?timestamps=true&word_alternatives_threshold=0.9" >> response.json'
- def register():
- accounts_file = Path('accounts.json')
- accounts = []
- pygame.mixer.init()
- if accounts_file.is_file():
- call('mpg123 responses/account_exists.mp3', shell=True)
- if(not verify_account()):
- return False
- else:
- call('mpg123 responses/no_account_exists.mp3', shell=True)
- pass
- call('mpg123 responses/start_registration.mp3', shell=True)
- confirmed = False
- while(not confirmed):
- call('mpg123 responses/ask_name.mp3', shell=True)
- call('arecord -D plughw:1 --duration=10 -f cd response.mp3', shell=True)
- recognize_speech()
- with open('response.json') as json_response:
- response = json.load(json_response)
- name = temp_data['results'][0]['alternatives'][0]['transcript']
- synth_speech('responses/confirm_name.mp3', 'Confirm with yes that your name is {}'.format(name))
- call('mpg123 responses/confirm_name.mp3', shell=True)
- call('arecord -D plughw:1 --duration=10 -f cd response.mp3')
- recognize_speech()
- with open('response.json') as json_response:
- response = json.load(json_response)
- confirmation_text = temp_data['results'][0]['alternatives'][0]['transcript']
- if confirmation_text.upper() == "YES":
- break
- else:
- pass
- confirmed = False
- while(not confirmed):
- call('mpg123 responses/ask_pass.mp3', shell=True)
- record_speech()
- recognize_speech()
- with open('response.json') as json_response:
- response = json.load(json_response)
- password = temp_data['results'][0]['alternatives'][0]['transcript']
- synth_speech('responses/confirm_pass.mp3', 'Confirm with yes that your password is {}'.format(password))
- call('mpg123 responses/confirm_pass.mp3', shell=True)
- record_speech()
- recognize_speech()
- with open('response.json') as json_response:
- response = json.load(json_response)
- confirmation_text = temp_data['results'][0]['alternatives'][0]['transcript']
- if confirmation_text.upper() == "YES":
- break
- else:
- pass
- account = {{
- "name": name,
- "password": password
- }}
- with open('accounts.json', 'w') as outfile:
- json.dump(account, outfile)
- def verify_account():
- with open('accounts.json') as json_accounts:
- accounts = json.load(json_accounts)
- account_found = False
- while(not account_found):
- call('mpg123 responses/ask_account_name.mp3', shell=True)
- record_speech()
- recognize_speech()
- with open('response.json') as json_response:
- response = json.load(json_response)
- text = temp_data['results'][0]['alternatives'][0]['transcript']
- for account in accounts:
- if account['name'].upper() == text.upper():
- account_found = true
- elif text.upper() == 'QUIT' or text.upper() == '':
- return False
- password_valid = False
- while(not password_valid):
- call('mpg123 responses/ask_password.mp3', shell=True)
- record_speech()
- recognize_speech()
- with open('response.json') as json_response:
- response = json.load(json_response)
- text = temp_data['results'][0]['alternatives'][0]['transcript']
- for account in accounts:
- if account['password'].upper() == text.upper():
- account_found = true
- elif text.upper() == 'QUIT' or text.upper() == '':
- return False
- return True
- def record_speech():
- call('rm responses/response.mp3', shell=True)
- call('arecord -D plughw:1 --duration=10 -f cd responses/response.mp3', shell=True)
- if __name__ == "__main__":
- file_check()
- register()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement