微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

不断从子进程中获取变量数据

如何解决不断从子进程中获取变量数据

我有一个子进程,它不断收听麦克风,将音频转换为文本并存储结果。这个代码

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。