语音识别程序偶尔工作



我不确定为什么,但它只是偶尔查找,转换和转录目录中的所有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))

编辑:我犯了一个非常愚蠢的错误,我用相同的名称命名所有输出的文本文件,所以它们被覆盖,我确保通过下降到毫秒作为名称来给它们一个唯一的名称,然后在文件名中添加一个随机数以获得良好的衡量标准。

当你使用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)

最新更新