如何解决Python - 如何在不设置计时器的情况下等待函数完成进程
这是我第一次使用 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")
解决方法
Python 是(除非指定)一种同步语言,这意味着在前一个进程未完成时,进程不会/无法启动。
适用于您的情况,如果您的 directoryMove()
函数被执行,则意味着您的 transcript()
函数以一种或另一种方式完全完成(可能失败了)。
您可以在这两个函数中打印更多调试日志,以说服自己并帮助您进一步调查问题所在。
,了解转录的功能会很有用吗?我想它正在启动一个线程。在这种情况下,您必须捕获一个线程 id 并 join() 它以等待线程结束
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。