【问题标题】:Voice recognition program works sporadically语音识别程序偶尔工作
【发布时间】:2019-02-11 05:23:36
【问题描述】:

我不知道为什么,但它只是偶尔找到、转换和转录目录中的所有 mp3 文件,但从来没有 100% 的时间。我不确定为什么。我希望我在正确的地方问。我的目标是找到所有 m4a 文件,然后转换为 wav 文件,然后找到所有 wav 文件并转录它们。程序有时会这样做,但不是所有时间。

#!/usr/bin/env python3

    import speech_recognition as sr
    import time
    import subprocess
    import os
    from os import path
    from os import listdir


    # find and convert mp3 files to wav files

    # find all files with an extension, convert to wav files, transcribe to text file then transfer all wav files and mp3 files to finished directory and email transcribed files.

    # find files with mp3 extension

    def list_files1(directory, extension):
        ext = []
        for root, dirs, files in os.walk(directory):
            for file in files:
                if file.endswith(extension):
                    ext.append(file)
        print(ext)
        return ext


    # get directory path
    originalDir = os.getcwd()

    # call function to find files with mp3 extension
    mp3files = list_files1(originalDir, "m4a")

    # os.chdir("/Users/williamjohnson/Dropbox/goodscrapers/publimaison2/")

    # convert all mp3 files to wav files
    for x in mp3files:
        print(x)
        timestr = time.strftime("%Y%m%d-%H%M%S")
        command = "ffmpeg -i " + x + ' ' + timestr + ".wav"
        print(command)
        subprocess.call(command, shell=True)

    # find all converted wav files

    wavfiles = list_files1(originalDir, "wav")

    for y in wavfiles:
        print(y)
        # obtain path to "english.wav" in the same folder as this script
        AUDIO_FILE = path.join(path.dirname(path.realpath(__file__)), y)
        # AUDIO_FILE = path.join(path.dirname(path.realpath(__file__)), "french.aiff")
        # AUDIO_FILE = path.join(path.dirname(path.realpath(__file__)), "chinese.flac")

        # use the audio file as the audio source
        r = sr.Recognizer()
        with sr.AudioFile(AUDIO_FILE) as source:
            audio = r.record(source)  # read the entire audio file

        # recognize speech using Sphinx
        try:
            print("Sphinx thinks you said " + r.recognize_sphinx(audio))
            timestr = time.strftime("%Y%m%d-%H%M%S")
            text_file = open(timestr + ".txt", "a")
            text_file.write(r.recognize_sphinx(audio))
            text_file.close()
        except sr.UnknownValueError:
            print("Sphinx could not understand audio")
        except sr.RequestError as e:
            print("Sphinx error; {0}".format(e))

编辑:这是我犯的一个非常愚蠢的错误,我将所有输出的文本文件命名为相同的名称,因此它们被覆盖我确保通过将名称精确到毫秒来给它们一个唯一的名称然后在文件名中添加一个随机数,以便更好地衡量。

【问题讨论】:

  • 当它不起作用时会发生什么?你有错误吗?成绩单不好?还有什么?问题是可重现的,还是下次可以解决?

标签: python voice-recognition voice cmusphinx


【解决方案1】:

当您使用os.walk() 时,它返回的文件没有任何目录,因此您正在收集文件列表但丢弃它们的目录名。

您的包装器似乎没有为os.walk() 添加任何价值;无论如何,我会重构一次转换一个文件。切线,如果你不是特别关心当前目录的绝对路径,你也不需要调用getcwd

import os

def get_next_file(directory, extension):
    for root, dirs, files in os.walk(directory):
        for file in files:
            if file.endswith(extension):
                yield os.path.join(root, file)

for x in get_next_file('.', 'm4a'):
    print(x)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-20
    • 2014-04-12
    • 1970-01-01
    • 2016-04-08
    相关资源
    最近更新 更多