Guest User

Untitled

a guest
Jan 27th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. import json
  2. from watson_developer_cloud import SpeechToTextV1
  3. import time
  4. from datetime import timedelta
  5. import sys
  6. import os
  7. import io
  8. import argparse
  9.  
  10.  
  11. #We need to get our API credentials in the code for authentication that we have stored as Environment Variables locally
  12. STT_USER_WATSON = os.environ.get("STT_USER_WATSON")
  13. STT_PASS_WATSON = os.environ.get("STT_PASS_WATSON")
  14.  
  15. #Following line is used to save all the console output into a text file
  16. sys.stdout = open('speech_api_content_stt_output.txt', 'w')
  17.  
  18. start_time = time.monotonic()
  19.  
  20.  
  21. def input_file(speech_file_path):
  22. global result
  23. #Initialize SpeechToText function using the API credentials
  24. stt = SpeechToTextV1(
  25. username = STT_USER_WATSON,
  26. password = STT_PASS_WATSON)
  27. if os.path.isfile(speech_file_path):
  28. with io.open(speech_file_path, 'rb') as audio_file:
  29. result = stt.recognize(audio_file, content_type = 'audio/wav') #This line will recognize the audio file and transcribe it into text format
  30. else:
  31. print("File doesn't exist in the directory!")
  32.  
  33.  
  34. def speech_stt():
  35. text = result['results'][0]['alternatives'][0]['transcript'] #Text output of the audio file after transcription
  36. print("Text: " + text + "\n")
  37.  
  38.  
  39. if __name__ == '__main__':
  40. parser = argparse.ArgumentParser(
  41. description = __doc__,
  42. formatter_class = argparse.RawDescriptionHelpFormatter)
  43. parser.add_argument(
  44. 'speech_file_path',
  45. help = 'The complete file path of the speech file you want to convert from speech to text.')
  46. args = parser.parse_args()
  47.  
  48. input_file(args.speech_file_path)
  49. speech_stt()
  50.  
  51.  
  52. end_time = time.monotonic()
  53. print("Execution_Time:", timedelta(seconds = end_time - start_time))
Add Comment
Please, Sign In to add comment