zoukankan      html  css  js  c++  java
  • python3

    使用 record() 从文件中获取数据

    在解释器会话框键入以下命令来处理 “harvard.wav” 文件的内容:

     harvard = sr.AudioFile('harvard.wav')
    >>> with harvard as source:
    ...   audio = r.record(source)
    

      通过上下文管理器打开文件并读取文件内容,并将数据存储在 AudioFile 实例中,然后通过 record()将整个文件中的数据记录到 AudioData 实例中,可通过检查音频类型来确认:

    type(audio)
    <class 'speech_recognition.AudioData'>
    

      现在可以调用 recognition_google()来尝试识别音频中的语音。

    >>> r.recognize_google(audio)
    'the stale smell of old beer lingers it takes heat
    to bring out the odor a cold dip restores health and
    zest a salt pickle taste fine with ham tacos al
    Pastore are my favorite a zestful food is the hot
    cross bun'
    

     

    以上就完成了第一个音频文件的录制。

    利用偏移量和持续时间获取音频片段

    若只想捕捉文件中部分演讲内容该怎么办?record() 命令中有一个 duration 关键字参数,可使得该命令在指定的秒数后停止记录。

     

    例如,以下内容仅获取文件前四秒内的语音:

    >>> with harvard as source:
    ...   audio = r.record(source, duration=4)
    ...
    >>> r.recognize_google(audio)
    'the stale smell of old beer lingers'
    

      在with块中调用record() 命令时,文件流会向前移动。这意味着若先录制四秒钟,再录制四秒钟,则第一个四秒后将返回第二个四秒钟的音频。

    >>> with harvard as source:
    ...   audio1 = r.record(source, duration=4)
    ...   audio2 = r.record(source, duration=4)
    ...
    >>> r.recognize_google(audio1)
    'the stale smell of old beer lingers'
    >>> r.recognize_google(audio2)
    'it takes heat to bring out the odor a cold dip'
    

      除了指定记录持续时间之外,还可以使用 offset 参数为 record() 命令指定起点,其值表示在开始记录的时间。如:仅获取文件中的第二个短语,可设置 4 秒的偏移量并记录 3 秒的持续时间。

    >>> with harvard as source:
    ...   audio = r.record(source, offset=4, duration=3)
    ...
    >>> recognizer.recognize_google(audio)
    'it takes heat to bring out the odor'
    

      在事先知道文件中语音结构的情况下,offset 和 duration 关键字参数对于分割音频文件非常有用。但使用不准确会导致转录不佳。

    >>> with harvard as source:
    ...   audio = r.record(source, offset=4.7, duration=2.8)
    ...
    >>> recognizer.recognize_google(audio)
    'Mesquite to bring out the odor Aiko'
    

     

    本程序从第 4.7 秒开始记录,从而使得词组 “it takes heat to bring out the odor” ,中的 “it t” 没有被记录下来,此时 API 只得到 “akes heat” 这个输入,而与之匹配的是 “Mesquite” 这个结果。

    同样的,在获取录音结尾词组 “a cold dip restores health and zest” 时 API 仅仅捕获了 “a co” ,从而被错误匹配为 “Aiko” 。

    噪音也是影响翻译准确度的一大元凶。上面的例子中由于音频文件干净从而运行良好,但在现实中,除非事先对音频文件进行处理,否则不可能得到无噪声音频。

     

  • 相关阅读:
    python3+requests接口自动化-其他接口封装
    python3+requests接口自动化-登陆模块封装
    python3+requests接口自动化-测试登陆
    python3+requests接口自动化-日志封装
    python3+requests接口自动化-配置文件
    python3接口自动化-run_all_case
    python3+requests接口自动化session操作
    selenium自动化-数据驱动2
    js弹出对话框
    "System.StackOverflowException"类型的未经处理的异常在SharingPlatform.dll中发生
  • 原文地址:https://www.cnblogs.com/huaobin/p/15677084.html
Copyright © 2011-2022 走看看