2016-10-06 12 views
0

現在、ディレクトリから1つのオーディオファイルを取得していますが、出力を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

答えて

1

あなたは、私は以下のコードでget_file_paths()に含まれているos.walkとディレクトリとサブディレクトリのすべてのファイルを取得することができ、ここでの例です:

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() 
+0

よろしくお願いします。 –

+0

2つの音声認識ツールを使用したい場合はどうすればいいですか 私はgoogleとbingのコードを実装しました。 しかし、私は def(main)の問題に直面しています。私は2つの音声認識のために私の質問を修正しました。 –

+0

を見てください= process_file(ファイル)は、speecの認識プログラムの文字列を追加しますが、私もspinxをbに追加したい –

関連する問題