问题描述
我有一个子进程,它不断收听麦克风,将音频转换为文本并存储结果。这个代码是
from os import environ,path
import pyaudio
from utils import micStream
from pocketsphinx.pocketsphinx import *
from sphinxbase.sphinxbase import *
MODELDIR = "PATH TO MODEL"
config = Decoder.default_config()
config.set_string('-hmm',path.join(MODELDIR,'en-us/acoustic-model'))
config.set_string('-lm','en-us/language-model.lm.bin'))
config.set_string('-dict','en-us/language-dictionary.dict'))
config.set_string('-logfn','nul')
decoder = Decoder(config)
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16,channels=1,rate=16000,input=True,frames_per_buffer=1024)
stream.start_stream()
in_speech_bf = False
decoder.start_utt()
while True:
buf = stream.read(1024)
if buf:
decoder.process_raw(buf,False,False)
if decoder.get_in_speech() != in_speech_bf:
in_speech_bf = decoder.get_in_speech()
if not in_speech_bf:
decoder.end_utt()
print(decoder.hyp().hypstr.lower())
decoder.start_utt()
else:
break
decoder.end_utt()
我试图将它作为子进程运行,并让父进程连续读取字符串 decoder.hyp().hypstr.lower()
,而不阻塞主进程的其余部分。我试过了
subprocess.check_output()
和
listener=subprocess.Popen([sys.executable,'file_name'])
但两者似乎都阻止了我的代码。有没有办法做到这一点?
解决方法
你可以试试这样的:
from multiprocessing import Process,Queue # ADD IMPORT
from os import environ,path
import pyaudio
from utils import micStream
from pocketsphinx.pocketsphinx import *
from sphinxbase.sphinxbase import *
def get_audio(q):
MODELDIR = "PATH TO MODEL"
config = Decoder.default_config()
config.set_string('-hmm',path.join(MODELDIR,'en-us/acoustic-model'))
config.set_string('-lm','en-us/language-model.lm.bin'))
config.set_string('-dict','en-us/language-dictionary.dict'))
config.set_string('-logfn','nul')
decoder = Decoder(config)
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16,channels=1,rate=16000,input=True,frames_per_buffer=1024)
stream.start_stream()
in_speech_bf = False
decoder.start_utt()
while True:
buf = stream.read(1024)
if buf:
decoder.process_raw(buf,False,False)
if decoder.get_in_speech() != in_speech_bf:
in_speech_bf = decoder.get_in_speech()
if not in_speech_bf:
decoder.end_utt()
q.put(decoder.hyp().hypstr.lower()) # PUT THE OUTPUT INTO QUEUE
decoder.start_utt()
else:
break
decoder.end_utt()
out_q = Queue()
audio_p = Process(target=get_audio,args=(out_q,))
audio_p.start()
while True:
# If data has been received process it
if not out_q.empty():
res = out_q.get()
# Perform whatever needs to happen when the parent receives the output here
# After processing the data,or if no data has been received
# Put the code that needs to run in the main process here