Advertisement
gregwa

FCM153 - mysrec.py

Jan 9th, 2020
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.41 KB | None | 0 0
  1. # ======================================================
  2. # speechrec-test.py
  3. # ------------------------------------------------------
  4. # written by G.D. Walters
  5. # for Full Circle Magazine #153
  6. # ------------------------------------------------------
  7. # Borrowed from speechrecognition library __main__.py
  8. # ======================================================
  9.  
  10. import speech_recognition as sr
  11.  
  12. r = sr.Recognizer()
  13. m = sr.Microphone()
  14.  
  15. loop = True
  16. while loop:
  17.     try:
  18.         print('Silence!!!!!!!!!!')
  19.         with m as source:
  20.             r.adjust_for_ambient_noise(source)
  21.         while True:
  22.             print('SPEAK HUMAN...')
  23.             with m as source:
  24.                 audio = r.listen(source)
  25.             print('Quiet while I think...')
  26.             try:
  27.                 value = r.recognize_google(audio)
  28.                 if str is bytes:
  29.                     print(f'You said "{value.encode("utf-8")}"')
  30.                 else:
  31.                     print(f'You said "{value}"')
  32.                 if (value == 'please quit') or (value == 'please stop'):
  33.                     print('Program ends...')
  34.                     loop = False
  35.                     break
  36.             except sr.UnknownValueError:
  37.                 print('try that again')
  38.             except sr.RequestError as e:
  39.                 print('unable to get requests from engine')
  40.     except KeyboardInterrupt:
  41.         loop = False
  42.         pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement