PyAudio警告污染输出 [英] PyAudio warnings poluting output

查看:95
本文介绍了PyAudio警告污染输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个程序,可以监听麦克风并尝试识别出所说的内容:

I have this program that listens to the microphone and tries to recognize what was said:

#!/usr/bin/env python3

import speech_recognition

recognizer = speech_recognition.Recognizer()

class Recognition:

    def __init__(self):
        self.recognizer = speech_recognition.Recognizer()

    def listen(self):
        with speech_recognition.Microphone() as source:
            self.recognizer.adjust_for_ambient_noise(source)
            self.audio = self.recognizer.listen(source)

    def recognize_sphinx(self):
        decoder = self.recognizer.recognize_sphinx(self.audio, show_all=True)
        for best, i in zip(decoder.nbest(), range(10)):
            return best.hypstr


r = Recognition()
r.listen()
print(r.recognize_sphinx())

尽管代码可以正常工作,但它的确会在输出中显示以下警告:

Although the code works, it does display the following warnings along with the output:

ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm_route.c:867:(find_matching_chmap) Found no matching channel map
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jack server is not running or cannot be started
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for 4294967295, skipping unlock
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for 4294967295, skipping unlock
potato

问题是我希望脚本执行上面的代码并读取其输出(马铃薯),但是返回的结果与所有这些错误混合在一起.我尝试了在此处看到的解决方案,但它只能抑制alsa警告.也许弄乱alsa/pulseaudio配置可以解决此问题,但是我不想在运行代码的每台机器上都这样做.

The problem is I want a script to execute the code above and read its output (potato), but the returned result is mixed with all those errors. I tried the solution I saw here, but it only suppress the alsa warnings. Probably messing with alsa/pulseaudio configs will solve this problem, but I don't want to do that on every machine that runs the code.

我还尝试将stdin/stderr重定向为null,但没有抑制任何作用.

I also tried redirecting stdin/stderr to null, but didn't suppress anything.

我有一个解决方法,就是当脚本运行该程序时,仅获取最后一行,但我只希望将其作为最后一个资源.

I have a workaround in mind that is, when the script runs this program, to get only the last line, but I want that only as a last resource.

推荐答案

警告/状态消息进入标准错误输出( stderr ),而您的 print()输出调用转到标准输出( stdout ).分开两者应该是微不足道的.

The warning/status messages go to the standard error output (stderr), while the output of your print() call goes to the standard output (stdout). It should be trivial to separate the two.

如果您想实际禁止显示警告/状态消息,可以查看我对SO的回答-您提到的问题,或者我从那里链接到的更详细的答案.

If you want to actually suppress the warning/status messages, you can have a look at my answer to the SO-question you were mentioning, or the more verbose answer I've linked to from there.

这篇关于PyAudio警告污染输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆