【问题标题】:Loop of function for taking multiple audio files from a directory从目录中获取多个音频文件的函数循环
【发布时间】:2016-10-06 07:09:46
【问题描述】:

我目前正在从一个目录中获取单个音频文件的输入,并且我将我的输出保存在 CSV 文件中,带有文件名并将语音转换为文本输出,但我在该目录中有 100 个文件(即 001.wav ,002.wav,003.wav.......100.wav)

我想编写一个循环或函数,将语音自动保存为 CSV 中的文本输出,并在不同的行中使用相应的文件名。

代码如下:

import speech_recognition as sr
import csv
import os
AUDIO_FILE =path.join(path.dirname('C:/path/to/directory'), "001.wav")
file_name = os.path.basename(AUDIO_FILE)
name = os.path.basename(AUDIO_FILE)

# 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 Google Speech Recognition
try:
    # for testing purposes, we're just using the default API key
    # to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
    # instead of `r.recognize_google(audio)`
    a =  r.recognize_google(audio)        
except sr.UnknownValueError:
    a = "Google Speech Recognition could not understand audio"
except sr.RequestError as e:
    a = "Could not request results from Google Speech Recognition service; {0}".format(e)

try:
    b = r.recognize_sphinx(audio)
except sr.UnknownValueError:
    b = "Sphinx could not understand audio"
except sr.RequestError as e:
    b = "Sphinx error; {0}".format(e)

with open('speech_output.csv', 'a') as f:
writer = csv.writer(f)
writer.writerow(['file_name','google',sphinx])
writer.writerow([file_name,a,b])

对代码的引用。 https://github.com/Uberi/speech_recognition/blob/master/examples/audio_transcribe.py

【问题讨论】:

    标签: python python-2.7 python-3.x


    【解决方案1】:

    你可以使用os.walk获取一个目录和子目录的所有文件,我在下面的代码中包含在get_file_paths()中,这里是一个例子:

    import speech_recognition as sr
    import csv
    import os
    
    
    DIRNAME = r'c:\path\to\directory'
    OUTPUTFILE = r'c:\path\to\outputfiledir\outputfile.csv'
    
    def get_file_paths(dirname):
        file_paths = []  
        for root, directories, files in os.walk(dirname):
            for filename in files:
                filepath = os.path.join(root, filename)
                file_paths.append(filepath)  
        return file_paths    
    
    def process_file(file):
        r = sr.Recognizer()
        a = ''
        with sr.AudioFile(file) as source:
            audio = r.record(source)    
            try:
                a =  r.recognize_google(audio)        
            except sr.UnknownValueError:
                a = "Google Speech Recognition could not understand audio"
            except sr.RequestError as e:
                a = "Could not request results from Google Speech Recognition service; {0}".format(e)  
        return a
    
    def main():
        files = get_file_paths(DIRNAME)                 # get all file-paths of all files in dirname and subdirectories
        for file in files:                              # execute for each file
            (filepath, ext) = os.path.splitext(file)    # get the file extension
            file_name = os.path.basename(file)          # get the basename for writing to output file
            if ext == '.wav':                           # only interested if extension is '.wav'
                a = process_file(file)                  # result is returned to a
                with open(OUTPUTFILE, 'a') as f:        # write results to file
                    writer = csv.writer(f)
                    writer.writerow(['file_name','google'])
                    writer.writerow([file_name, a])            
    
    
    if __name__ == '__main__':
        main()
    

    如果您想做多个识别器,这样的事情可能会起作用。请注意,这是一个未经测试的示例:

    import speech_recognition as sr
    import csv
    import os
    
    
    DIRNAME = r'c:\path\to\directory'
    OUTPUTFILE = r'c:\path\to\outputfiledir\outputfile.csv'
    
    def get_file_paths(dirname):
        file_paths = []  
        for root, directories, files in os.walk(dirname):
            for filename in files:
                filepath = os.path.join(root, filename)
                file_paths.append(filepath)  
        return file_paths    
    
    def recog_multiple(file):
        r = sr.Recognizer()
        r_types = ['recognize_google', 'recognize_sphinx']
        results = []
        for r_type in r_types:
            result = ''
            with sr.AudioFile(file) as source:
                audio = r.record(source)
                try:
                    result = r_type + ': ' + str(getattr(r, r_type)(audio))
                except sr.UnknownValueError:
                    result = r_type + ': Speech Recognition could not understand audio'
                except sr.RequestError as e:
                    result = r_type + ': Could not request results from Speech Recognition service; {0}'.format(e)        
            results.append(result)
        return results
    
    def main():
        files = get_file_paths(DIRNAME)                 # get all file-paths of all files in dirname and subdirectories
        for file in files:                              # execute for each file
            (filepath, ext) = os.path.splitext(file)    # get the file extension
            file_name = os.path.basename(file)          # get the basename for writing to output file
            if ext == '.wav':                           # only interested if extension is '.wav'
                a = recog_multiple(file)                # result is returned to a
                with open(OUTPUTFILE, 'a') as f:        # write results to file
                    writer = csv.writer(f)
                    writer.writerow(['file_name','results'])
                    writer.writerow([file_name, a])            
    
    
    if __name__ == '__main__':
        main()
    

    【讨论】:

    • 嘿,如果我想使用 2 个语音识别器,我实现了 google 和 bing 的代码。但我在def(main)中遇到问题。我将我的问题修改为两个语音识别,请看一下
    • 因为 a = process_file(file),会在 a 中添加 google 语音识别器的字符串,但我也想在 b 中添加 spinx
    • 编写一个名为def process_file_sphinx(file)的新函数,将b = process_file_sphinx(file)添加到main(),然后将writer.writerow() 函数更改为包含b
    • 是的,你可以随心所欲地编写它,在一个函数中使用多个识别器,然后返回一个字符串列表,或者一个包含字符串的字典和字符串生成的方法,无穷无尽的选项......我的建议试试看。最佳做法是将“重复代码”放入函数中,这样您就不必为每个识别器键入相同的命令......祝你好运
    • 先生,请帮我编写一个函数中的多个识别器的代码。我已经更新了我的问题代码,让你看看@Edwin
    【解决方案2】:

    包含文件夹的文本文件,用于将文本汇总到数据框然后是 csv 文件

    #Importing package and summarizer
    
    import pandas as pd
    
    import gensim
    
    #pip show gensim
    
    #pip install gensim==3.8.3
    
    from gensim.summarization import summarize 
    
    import os
    
    folder_path =  r"C:\Users\lenovo\My_Texts"
    
    dirListing = os.listdir(folder_path)
    
    total_files = len(dirListing)
    
    
    Files = []
    
    
    for i in range(0,total_files): 
    
        print(dirListing[i])
    
        file = dirListing[i]
    
        if file.endswith(".txt"):
    
            file_path = f"{folder_path}\{file}"
    
            print(file_path)
    
            f = open(file_path, "r") 
    
            info = f.read()
    
            # Summarization by ratio
    
            summary_by_ratio=summarize(info,ratio=0.1)
    
            print(summary_by_ratio)
    
            Files.append(summary_by_ratio)
    
    len(Files)   
             
    df_new = pd.DataFrame(Files) 
      
    df_new = df_new.rename(columns={0: 'text'})
    
    # to csv
    
    df_new.to_csv('Project_AI.csv', index=False)
    

    我认为这段代码可以帮助您让我知道它有效

    一切顺利

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-19
      • 2013-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-01
      相关资源
      最近更新 更多