Python—如何在不设置计时器的情况下等待函数完成进程
这是我第一次使用python,我不了解流程: 我正在使用Pocketsphinx进行语音到文本转换,功能Python—如何在不设置计时器的情况下等待函数完成进程,python,python-3.x,speech-recognition,speech-to-text,Python,Python 3.x,Speech Recognition,Speech To Text,这是我第一次使用python,我不了解流程: 我正在使用Pocketsphinx进行语音到文本转换,功能createAudio(videoclip):有问题。脚本不会运行整个transcript()函数,或者只运行几秒钟,直到下一个函数启动。如何使transcript()函数运行到完成,并且下一个函数可以启动/跟随,而无需设置计时器?因为视频文件稍后会有更大和不同的大小 用于测试:该.wav文件的大小为300MB,文本输出仅为4个字 def createAudio(视频剪辑): 这是全部代码:
createAudio(videoclip):
有问题。脚本不会运行整个transcript()
函数,或者只运行几秒钟,直到下一个函数启动。如何使transcript()
函数运行到完成,并且下一个函数可以启动/跟随,而无需设置计时器?因为视频文件稍后会有更大和不同的大小
用于测试:该.wav文件的大小为300MB,文本输出仅为4个字
def createAudio(视频剪辑):
这是全部代码:
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并将其连接()以等待线程结束您如何知道下一个函数启动,而不是等待
transcript()
完成?@quamrana因为textfile或transcript()
函数的输出不正确所以,可能transcript()
不起作用。@quamrana如果我使用50MB的较小的.wav文件,则“transcript()”会返回更多文本。但也许你是对的。我会检查它,我会尝试线程。你有一个简单易懂的初学者源代码吗?python线程已经足够了。否,如果函数前面有Noasync
关键字,它将不会启动异步线程。默认情况下,它是同步的,在同一个线程中。
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")