【发布时间】:2021-05-06 16:18:29
【问题描述】:
这是我第一次使用 python,我不了解过程:
我正在使用 Pocketsphinx 进行语音转文本,但函数 createAudio(videoclip): 存在问题。该脚本不会运行整个transcript() 函数,或者只运行几秒钟,直到下一个函数启动。如何在不设置计时器的情况下使transcript() 函数运行直到它完成并且下一个可以启动/跟随?因为视频文件以后会有更大的不同尺寸。
用于测试:.wav 文件大小为 300MB,文本输出只有 4 个字
def createAudio(videoclip):
pathOfSameFolder=str(pathOfFolder) dirCreated = True try: newDir=createDirectory(pathOfSameFolder) except OSError: print ('Error: Creating directory. ' + str(pathOfSameFolder) + ' may be existing!') dirCreated = False if dirCreated : audioclip = videoclip.audio audioclip.write_audiofile(mp3_file) audioclip.close() videoclip.close() print('Converting audio transcripts into text ...') transcript() directoryMove(newDir)
这是整个代码:
import moviepy.editor as mp
from moviepy.editor import *
import speech_recognition as sr
import shutil
from random import random
import threading
import time
import asyncio
import os
from pocketsphinx import AudioFile, get_model_path, get_data_path
from sphinxbase.sphinxbase import *
mp4_file = r'/Users/younesyaakoubi/Desktop/5min.mp4'
mp3_file = r'/Users/younesyaakoubi/Desktop/audio_only.wav'
newMethodmp3_file = r'/Users/younesyaakoubi/Desktop/AUDIO_FILE/audio_only.wav'
model_path = get_model_path()
data_path = get_data_path()
path = os.getcwd()
config = {
'verbose': False,
'audio_file': os.path.join(data_path, str(mp3_file)),
'buffer_size': 2048,
'no_search': False,
'full_utt': False,
# 'hmm': os.path.join(model_path, 'en-us'),
# 'lm': os.path.join(model_path, 'en-us.lm.bin'),
# 'dict': os.path.join(model_path, 'cmudict-en-us.dict')
}
r = sr.Recognizer()
pathOfFolder= "/Users/younesyaakoubi/Desktop/AUDIO_FILE"
audioFileName= "audio_only.wav"
scriptName="script.txt"
#Save Videofile into object to be handled by next function
def convert():
videoclip = VideoFileClip(mp4_file)
createAudio(videoclip)
#Convert video to audio
def createAudio(videoclip):
pathOfSameFolder=str(pathOfFolder)
dirCreated = True
try:
newDir=createDirectory(pathOfSameFolder)
except OSError:
print ('Error: Creating directory. ' + str(pathOfSameFolder) + ' may be existing!')
dirCreated = False
if dirCreated :
audioclip = videoclip.audio
audioclip.write_audiofile(mp3_file)
audioclip.close()
videoclip.close()
print('Converting audio transcripts into text ...')
transcript()
directoryMove(newDir)
#Checks first if path exists and if not it creates one File
def createDirectory(pathOfFolder):
sum = 0
directory=" "
#In Range wird die Maximale Anzahl der möglichen Ordner definiert
for num in range(5):
if num==range:
print("Not More Possible. Change Range !")
exit()
if not os.path.exists(pathOfFolder+str(num)):
print("Directory or File name is: ", pathOfFolder+str(num) )
#Make a new Folder or Directory
os.makedirs(pathOfFolder+str(num))
directory=pathOfFolder+str(num)
sum =+num
return directory
break
#Move first Audiofile to Folder and change directory to continue
def directoryMove(directory):
shutil.move('/Users/younesyaakoubi/Desktop/'+str(audioFileName), directory)
shutil.move('/Users/younesyaakoubi/Desktop/'+str(scriptName), directory)
#DOES NOTHING YET !!!! - Downsample 44.1kHz to 8kH
def downSample():
# Load into PyDub
print("Downsampling of Audio succesful")
#createFolder('./AudioInput/')
#os.chdir("/Users/younesyaakoubi/Desktop/AUDIO_FILE")
#f.write(audioFile)
def transcript():
with sr.AudioFile(str(audioFileName)) as source:
audio_text = r.listen(source)
#recoginize_() method will throw a request error if the API is unreachable, hence using exception handling
try:
# using Sphinx speech recognition
text = r.recognize_sphinx(audio_text)
f = open(str(scriptName),"w+")
f.write(text)
f.close()
print("Converting succesful")
except:
print('Sorry.. run again...')
#keyWordSearch()
def keyWordSearch():
audio = AudioFile(**config)
for phrase in audio:
#print(phrase)
print("Find keywords...")
f= open(str(scriptName),"a")
f.write(" "+str(phrase))
print("Keywords found")
f.close()
#keyWordOrder()
def keyWordOrder():
print("Classify Keywords")
with open(str(scriptName)) as file:
# reading each line
for line in file:
# reading each word
for word in line.split():
# displaying the words
print(word)
with open(str(scriptName)) as file:
# reading each line
for line in file:
# reading each word
for word in line.split():
# displaying the words
print(word)
#See in which Directory the path is described
print ("The current working directory is %s" % path)
convert()
print("Thanks for using xxxx")
【问题讨论】:
-
你怎么知道
the next function starts而不是等待transcript()完成? -
@quamrana 因为文本文件或者
transcript()函数的输出不正确 -
所以,
transcript()可能不起作用。 -
@quamrana 如果我使用 50MB 的较小的 .wav 文件,那么“transcript()”会返回更多文本。但也许你是对的。我会检查它
标签: python python-3.x speech-recognition speech-to-text