Guest User

Untitled

a guest
Nov 21st, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. import sys, os
  2. from pocketsphinx.pocketsphinx import *
  3. from sphinxbase.sphinxbase import *
  4. import pyaudio
  5.  
  6. # modeldir = "../../../model"
  7. # datadir = "../../../test/data"
  8.  
  9. modeldir="C://Users//hp//AppData//Local//Programs//Python//Python35//Lib//site-packages//pocketsphinx//model//en-us"
  10. dictdir="C://Users//hp//AppData//Local//Programs//Python//Python35//Lib//site-packages//pocketsphinx//model//cmudict-en-us.dict"
  11. lmdir="C://Users//hp//AppData//Local//Programs//Python//Python35//Lib//site-packages//pocketsphinx//model//en-us.lm.bin"
  12. # Create a decoder with certain model
  13. config = Decoder.default_config()
  14. config.set_string('-hmm', modeldir)
  15. config.set_string('-lm', lmdir )
  16. config.set_string('-dict', dictdir)
  17. config.set_string('-keyphrase', 'forward')
  18. config.set_float('-kws_threshold', 1e+20)
  19.  
  20. p = pyaudio.PyAudio()
  21. stream = p.open(format=pyaudio.paInt16, channels=1, rate=16000, input=True, frames_per_buffer=1024)
  22. stream.start_stream()
  23.  
  24. # Process audio chunk by chunk. On keyword detected perform action and restart search
  25. decoder = Decoder(config)
  26. decoder.start_utt()
  27. while True:
  28. buf = stream.read(1024)
  29. if buf:
  30. decoder.process_raw(buf, False, False)
  31. else:
  32. break
  33. if decoder.hyp() != None:
  34. #print(decoder.hyp().hypstr)
  35. if decoder.hyp().hypstr == 'forward':
  36. print ([(seg.word, seg.prob, seg.start_frame, seg.end_frame) for seg in decoder.seg()])
  37. print ("Detected keyword, restarting search")
  38. decoder.end_utt()
  39. decoder.start_utt()
  40.  
  41. the
  42. the
  43. the
  44. the
  45. the
  46. the
  47. the
  48. the
  49. the
  50. the
  51. the
  52. the
  53. the
  54. the
  55. the
  56. the
  57. the
  58. the
  59. the
  60. the
  61. the
  62. the
  63. the
  64. the
  65. the
  66. the
  67. the
  68. the
  69. the
  70. the
  71. the
  72. the
  73. the da
  74. the head
  75. the bed
  76. the bedding
  77. the heading of
  78. the bedding and
  79. the bedding and
  80. the bedding and
  81. the bedding and
  82. the bedding and
  83. the bedding and
  84. the bedding and
  85. the bedding and
  86. the bedding and
  87. the bedding and
  88. the bedding and
  89. the bedding and
  90. the bedding and well
  91. the bedding and well
  92. the bedding and well
  93. the bedding and butler
  94. the bedding and what lingus
  95. the bedding and what lingus
  96. the bedding and what lingus
  97. the bedding and what lingus ha
  98. the bedding and blessed are
  99. the bedding and blessed are
  100. the bedding and what lingus on
  101. the bedding and what lingus want
  102. the bedding and what lingus want
  103. the bedding and what lingus want
  104. the bedding and what lingus want
  105. the bedding and what lingus want or
  106. the bedding and what lingus want to talk
  107. the bedding and what lingus current top
  108. the bedding and what lingus want to talk
  109. the bedding and what lingus want to talk
  110. the bedding and what lingus want to talk
  111. the bedding and what lingus want to talk
  112. the bedding and what lingus want to talk to her
  113. the bedding and what lingus want to talk to her
  114. the bedding and what lingus want to talk to her
  115. the bedding and what lingus want to talk to her
  116.  
  117. import os
  118. import pyaudio
  119. import pocketsphinx as ps
  120.  
  121. modeldir = "C://Users//hp//AppData//Local//Programs//Python//Python35//Lib//site-packages//pocketsphinx//model//"
  122.  
  123. # Create a decoder with certain model
  124. config = ps.Decoder.default_config()
  125. config.set_string('-hmm', os.path.join(modeldir, 'en-us'))
  126. config.set_string('-lm', os.path.join(modeldir, 'en-us.lm.bin'))
  127. config.set_string('-dict', os.path.join(modeldir, 'cmudict-en-us.dict'))
  128. config.set_string('-keyphrase', 'forward')
  129. config.set_float('-kws_threshold', 1e+20)
  130.  
  131. p = pyaudio.PyAudio()
  132. stream = p.open(format=pyaudio.paInt16, channels=1, rate=16000, input=True, frames_per_buffer=1024)
  133. stream.start_stream()
  134.  
  135. # Process audio chunk by chunk. On keyword detected perform action and restart search
  136. decoder = ps.Decoder(config)
  137. decoder.start_utt()
  138.  
  139. while True:
  140. buf = stream.read(1024)
  141. if buf:
  142. decoder.process_raw(buf, False, False)
  143. else:
  144. break
  145. if decoder.hyp() is not None:
  146. print(decoder.hyp().hypstr)
  147. if 'forward' in decoder.hyp().hypstr:
  148. print([(seg.word, seg.prob, seg.start_frame, seg.end_frame) for seg in decoder.seg()])
  149. print("Detected keyword, restarting search")
  150. decoder.end_utt()
  151. decoder.start_utt()
  152.  
  153. config.set_string('-lm', lmdir )
Add Comment
Please, Sign In to add comment