| 1234567891011121314151617181920212223242526272829303132333435 |
- import pty
- import os
- import re
- def read_with_pty(demo_path):
- pattern = re.compile(r"\('([^']*)',\s*'([^']*)'\)")
- # 模拟终端运行程序
- master, slave = pty.openpty()
- pid = os.fork()
- if pid == 0:
- # 子进程:执行程序,输出到模拟终端
- os.dup2(slave, 1)
- os.dup2(slave, 2)
- os.close(master)
- os.close(slave)
- os.execv(demo_path, [demo_path])
- else:
- # 父进程:读取模拟终端的输出
- os.close(slave)
- print("模拟终端启动,监听中...")
- with os.fdopen(master, 'r', encoding='utf-8', errors='ignore') as f:
- for line in f:
- line = line.strip()
- if not line:
- continue
- match = pattern.match(line)
- if match:
- print(f"识别文本:{match.group(1)} | 拼音:{match.group(2)}")
- else:
- print(f"【原始】:{line}")
- os.waitpid(pid, 0)
- if __name__ == "__main__":
- DEMO_PATH = os.path.abspath("../audio_check_demo")
- read_with_pty(DEMO_PATH)
|