首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在文本文件中存储语音识别输出

如何在文本文件中存储语音识别输出
EN

Stack Overflow用户
提问于 2016-03-29 19:34:21
回答 1查看 3.7K关注 0票数 0

我正在开发TouchDesigner的一个补丁,我希望它使用SpeechRecognition,并从表格中记录的单词/短语构建一个安装。要做到这一点,我希望能够将它打印的内容保存到文本文件中,可惜我是一个糟糕的程序员,到目前为止还不能让脚本完全工作。

需要澄清的是,语音识别部分正在工作,这是一个精简的原始脚本,只剩下Google API。我所需要的只是将结果(当它们被发送到控制台时)写入一个文本文件以供以后使用。

这是发送识别出的单词/短语的部分。

代码语言:javascript
复制
 print(u"{}".format(value).encode("utf-8"))
 else: 
 print("{}".format(value))

我需要每次附加的词(而脚本连续运行)。

非常感谢您的帮助。

代码语言:javascript
复制
import speech_recognition as sr
r = sr.Recognizer()
m = sr.Microphone()


try:


    with m as source: r.adjust_for_ambient_noise(source)

       while True:

    with m as source: audio = r.listen(source)
    print("")

    try:

        value = r.recognize_google(audio)

        if str is bytes: 
            print(u"{}".format(value).encode("utf-8"))

         else: 
            print("{}".format(value))

    except sr.UnknownValueError:
        print("")
    except sr.RequestError as e:
        print("{0}".format(e))
  except KeyboardInterrupt:
    pass
EN

回答 1

Stack Overflow用户

发布于 2016-03-30 00:27:11

要将其输出到一个文件中,您可以执行如下操作

代码语言:javascript
复制
import speech_recognition as sr
r = sr.Recognizer()
m = sr.Microphone()


try:
    with m as source: r.adjust_for_ambient_noise(source)

        while True:

    with m as source: audio = r.listen(source)
    print("")

    try:

        value = r.recognize_google(audio)

        if str is bytes: 
            result = u"{}".format(value).encode("utf-8")

        else: 
            result = "{}".format(value)

        with open("outputs.txt","a") as f:
            f.write(result)
        print(result)

    except sr.UnknownValueError:
        print("")
    except sr.RequestError as e:
        print("{0}".format(e))
    except KeyboardInterrupt:
        pass

我的a with open ...所做的是附加到一个文件中。with x as y:是一种创建x作为y的蟒蛇方法,您只需要在这段脚本中使用它。open("all_outputs.txt","a")打开文件all_outputs.txt作为输出文件(如果它不存在就创建它),"a“将它设置为附加文件,所以它只是添加您在末尾编写的任何内容。f.write(result)会将结果写入该输出文件。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36283124

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档