当主线程在Python中继续执行时,如何从主线程中生成子线程

瓦尔丹·阿加瓦尔

我正在制作一个将麦克风录制的音频转换为文本的应用程序。录音的时间可能会很长,例如3个小时,所以我想将其转换为持续时间较短(例如一分钟左右)的波形文件,然后产生一个子线程来执行音频到文本操作会更好,而主线程可以在下一分钟开始记录。音频到文本的操作比录制部分要快得多,因此定时不会成为问题。

流程图

这是我认为应如何工作的流程图。

我正在pyaudio用来录制音频。它的代码是:

import pyaudio
import wave
import time

def read_audio(stream):
    chunk = 1024  # Record in chunks of 1024 samples
    sample_format = pyaudio.paInt16  # 16 bits per sample
    channels = 2
    fs = 44100  # Record at 44100 samples per second
    seconds = 10
    filename = 'record.wav'
    frames = []  # Initialize array to store frames
    # Store data in chunks for 3 seconds
    
    for i in range(0, int(fs / chunk * seconds)):
        data = stream.read(chunk)
        frames.append(data)
    
    # Save the recorded data as a WAV file
    wf = wave.open(filename, 'wb')
    wf.setnchannels(channels)
    wf.setsampwidth(p.get_sample_size(sample_format))
    wf.setframerate(fs)
    wf.writeframes(b''.join(frames))
    wf.close()
    
    # Stop and close the stream
    stream.stop_stream()
    stream.close()

    

p = pyaudio.PyAudio()  # Create an interface to PortAudio
chunk = 1024  # Record in chunks of 1024 samples
sample_format = pyaudio.paInt16  # 16 bits per sample
channels = 2
fs = 44100
stream = p.open(format=sample_format,channels=channels,rate=fs,
                frames_per_buffer=chunk,input=True)
read_audio(stream)
p.terminate() # Terminate the PortAudio interface

对于语音识别,使用Google的API speech_recognition其代码:

import speech_recognition as sr

def convert():
    sound = "record.wav"
 
    r = sr.Recognizer()
 
 
    with sr.AudioFile(sound) as source:
        r.adjust_for_ambient_noise(source)
        print("Converting Audio To Text and saving to file..... ") 
        audio = r.listen(source)
    try:

        value = r.recognize_google(audio) ##### API call to google for speech recognition

        if str is bytes: 
            result = u"{}".format(value).encode("utf-8")

        else: 
            result = "{}".format(value)

        with open("test.txt","a") as f:
            f.write(result)
        print("Done !\n\n")

    except sr.UnknownValueError:
        print("")
    except sr.RequestError as e:
        print("{0}".format(e))
    except KeyboardInterrupt:
        pass
 
convert()
杰里米·巴尔(Jeremy Bare)

由于GIL,Python从来就不是真正的多线程,但是在您的情况下这可能并不重要,因为您正在使用api调用来为您进行语音识别。

因此,您可以尝试启动线程进行转换

from threading import Thread
t = Thread(target=convert)
t.start()

在尝试转换下一分钟之前,您可以尝试加入最后一个线程以确保它已完成

t.join()

您可能还可以使用asyncio库

尽管这可能是过大的,但我可能会使用多处理库。在您的情况下,您可能有一个监听程序工作进程正在不断记录和保存新的声音文件,而一个转换工作程序进程正在不断寻找新文件并进行转换。

如果需要的话,这将允许您编写一个更强大的系统。例如,如果您失去了互联网连接,并且在几分钟之内无法通过Google api转换声音文件,录音机工作人员将继续保存声音文件而无需关心,当恢复互联网连接时,声音文件将得到处理。

无论如何,这是您可以使用的转换工作程序流程的一个小示例。

import multiprocessing as mp
import os
from pathlib import Path
from time import sleep

class ConversionWorker:

    def __init__(self, sound_file_directory_path: str, text_save_filepath: str):
        self.sound_directory_path = Path(sound_file_directory_path)
        self.text_filepath = Path(text_save_filepath)

    def run(self):
        while True:

            # find and convert all wav files in the target directory
            filepaths = self.sound_directory_path.glob('*.wav')
            for path in filepaths:
                # convert from path
                # save to self.text_filepath
                convert()

                # we can delete the sound file after converting it
                os.remove(path)

            # sleep for a bit since we are only saving files once a minute or so
            sleep(5)

def main():
    conversion_worker = ConversionWorker(sound_file_directory_path='path/to/sounds', text_save_filepath='path/to/text')
    p = mp.Process(target=conversion_worker.run)
    p.start()

    # do the recording and saving for as long as you want

    p.terminate()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章