有没有一种流畅的方法可以在 Raspberry Pi 5 上集成两个 Python 脚本?第一个脚本涉及语音识别,将口语单词转换为文本。第二个脚本将文本转换为 G 代码,专门用于手写格式。
语音识别的代码如下:
import sys
import typing as t
import time
import whispercpp as w
class StreamTranscriber:
def __init__(self, model_name: str):
self.transcriber = w.Whisper.from_pretrained(model_name)
self.paused = False
def store_transcript_handler(self, ctx, n_new, data):
segment = ctx.full_n_segments() - n_new
cur_segment = ""
while segment < ctx.full_n_segments():
cur_segment = ctx.full_get_segment_text(segment)
data.append(cur_segment)
segment += 1
if "hey toaster" in cur_segment.lower() and not self.paused:
self.transcriber.pause_audio()
self.paused = True
print("Stopping")
time.sleep(5)
self.transcriber.resume_audio()
self.paused = False
print("Resumed")
def main(self, **kwargs: t.Any):
transcription: t.Iterator[str] | None = None
try:
transcription = self.transcriber.stream_transcribe(callback=self.store_transcript_handler, **kwargs)
finally:
assert transcription is not None, "Something went wrong!"
sys.stderr.writelines(
["\nTranscription (line by line):\n"] + [f"{it}\n" for it in transcription]
)
sys.stderr.flush()
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument(
"--model_name", default="tiny.en", choices=list(w.utils.MODELS_URL)
)
parser.add_argument(
"--device_id", type=int, help="Choose the audio device", default=0
)
parser.add_argument(
"--length_ms",
type=int,
help="Length of the audio buffer in milliseconds",
default=5000,
)
parser.add_argument(
"--sample_rate",
type=int,
help="Sample rate of the audio device",
default=w.api.SAMPLE_RATE,
)
parser.add_argument(
"--n_threads",
type=int,
help="Number of threads to use for decoding",
default=3,
)
parser.add_argument(
"--step_ms",
type=int,
help="Step size of the audio buffer in milliseconds",
default=0,
)
parser.add_argument(
"--keep_ms",
type=int,
help="Length of the audio buffer to keep in milliseconds",
default=200,
)
parser.add_argument(
"--max_tokens",
type=int,
help="Maximum number of tokens to decode",
default=32,
)
parser.add_argument("--audio_ctx", type=int, help="Audio context", default=512)
parser.add_argument(
"--list_audio_devices",
action="store_true",
default=False,
help="Show available audio devices",
)
args = parser.parse_args()
if args.list_audio_devices:
w.utils.available_audio_devices()
sys.exit(0)
transcriber = StreamTranscriber(args.model_name)
transcriber.main(**vars(args))
我们在上面的程序中哪里可以添加文本到g代码转换程序代码,以便这两个程序流畅运行而没有时间延迟?
我建议您使用其中一个脚本作为模块。
如果我没记错的话,你可以在上面的代码中导入这个模块(G代码生成器脚本)并使用该模块的所有功能。
这应该给你一个起点。
有关模块的更多信息可以在这里找到:https://docs.python.org/3/tutorial/modules.html