|
|
|
@ -0,0 +1,152 @@
|
|
|
|
|
import sys
|
|
|
|
|
import os
|
|
|
|
|
import subprocess
|
|
|
|
|
import time
|
|
|
|
|
import logging
|
|
|
|
|
import glob
|
|
|
|
|
# 设置日志配置,将日志输出到log.txt文件中,并附加运行的时间
|
|
|
|
|
logging.basicConfig(level=logging.INFO,
|
|
|
|
|
format='%(asctime)s - %(levelname)s - %(message)s',
|
|
|
|
|
filename='/home/pi/Desktop/al/run/log.txt',
|
|
|
|
|
filemode='a')
|
|
|
|
|
|
|
|
|
|
# 添加其他文件夹的路径到sys.path
|
|
|
|
|
sys.path.append('/home/pi/Desktop/voice_assistant')
|
|
|
|
|
sys.path.append('/home/pi/Desktop/Guide_stick_system')
|
|
|
|
|
sys.path.append('/home/pi/Desktop/manage/itt')
|
|
|
|
|
|
|
|
|
|
import New
|
|
|
|
|
import wenzi
|
|
|
|
|
import main
|
|
|
|
|
# 设置文件路径
|
|
|
|
|
text_file = "/home/pi/Desktop/manage/demo.txt"
|
|
|
|
|
output_file = "/home/pi/Desktop/Guide_stick_system/run/output.mp3"
|
|
|
|
|
vlc_path = 'vlc'
|
|
|
|
|
remind_path = 'remind.wav'
|
|
|
|
|
error_path = 'error.wav'
|
|
|
|
|
directory = '/home/pi/Desktop/manage/*.txt'
|
|
|
|
|
img_path = '/home/pi/Desktop/manage/img'
|
|
|
|
|
marker = 0
|
|
|
|
|
consecutive_errors = 0
|
|
|
|
|
|
|
|
|
|
def get_latest_message(file_path, marker):
|
|
|
|
|
try:
|
|
|
|
|
with open(file_path, "r", encoding="utf-8") as f:
|
|
|
|
|
f.seek(marker) # 移动到上次读取的位置
|
|
|
|
|
latest_message = f.read()
|
|
|
|
|
new_marker = f.tell() # 获取新的位置标记
|
|
|
|
|
return latest_message, new_marker
|
|
|
|
|
except FileNotFoundError:
|
|
|
|
|
logging.error(f"文件未找到: {file_path}")
|
|
|
|
|
return "", 0
|
|
|
|
|
except PermissionError:
|
|
|
|
|
logging.error(f"没有权限读取文件: {file_path}")
|
|
|
|
|
return "", 0
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logging.error(f"读取文件时发生错误: {e}")
|
|
|
|
|
return "", 0
|
|
|
|
|
|
|
|
|
|
def log_demo_text():
|
|
|
|
|
if os.path.isfile(text_file):
|
|
|
|
|
with open(text_file, 'r') as file:
|
|
|
|
|
text = file.read()
|
|
|
|
|
logging.info(text)
|
|
|
|
|
else:
|
|
|
|
|
logging.error('txt文件不存在')
|
|
|
|
|
|
|
|
|
|
def is_file_empty(file_path):
|
|
|
|
|
return os.path.getsize(file_path) == 0
|
|
|
|
|
|
|
|
|
|
def is_new_message(file_path, marker):
|
|
|
|
|
with open(file_path, "r", encoding="utf-8") as f:
|
|
|
|
|
f.seek(0)
|
|
|
|
|
current_message = f.read()
|
|
|
|
|
current_marker = f.tell()
|
|
|
|
|
return current_message != ""
|
|
|
|
|
|
|
|
|
|
def clear_text_content(file_path):
|
|
|
|
|
with open(file_path, "w") as file:
|
|
|
|
|
file.truncate()
|
|
|
|
|
|
|
|
|
|
def get_latest_txt_filename(directory_path):
|
|
|
|
|
# 获取目录下所有的 .txt 文件
|
|
|
|
|
file_list = glob.glob(os.path.join(directory_path, '*.txt'))
|
|
|
|
|
|
|
|
|
|
# 如果文件列表不为空
|
|
|
|
|
if file_list:
|
|
|
|
|
# 按修改时间排序,获取最新的文件
|
|
|
|
|
latest_file = max(file_list, key=os.path.getmtime)
|
|
|
|
|
return latest_file
|
|
|
|
|
else:
|
|
|
|
|
logging.error("没有找到 .txt 文件")
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
wenzi.ensure_pytesseract_installed()
|
|
|
|
|
wenzi.ensure_tesseract_executable_configured()
|
|
|
|
|
|
|
|
|
|
while True:
|
|
|
|
|
try:
|
|
|
|
|
# 初始化检测器
|
|
|
|
|
config = main.load_config()
|
|
|
|
|
detector = main.initialize_detector(config)
|
|
|
|
|
if detector is None:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# 检测图像中的对象
|
|
|
|
|
img_path = main.config['img_path']
|
|
|
|
|
detections = main.detect_objects(detector, img_path)
|
|
|
|
|
|
|
|
|
|
# 可视化结果
|
|
|
|
|
main.visualize_results(img_path, detections)
|
|
|
|
|
|
|
|
|
|
#图像转文本
|
|
|
|
|
if not os.path.exists(img_path):
|
|
|
|
|
logging.error(f"图片 {img_path} 不存在,等待下一张...")
|
|
|
|
|
time.sleep(1)
|
|
|
|
|
continue
|
|
|
|
|
text = wenzi.image_to_text(img_path)
|
|
|
|
|
if text:
|
|
|
|
|
with open(text_file, 'w', encoding='utf-8') as file:
|
|
|
|
|
file.write(text)
|
|
|
|
|
logging.info(f"图片的识别结果已保存到 {text_file}")
|
|
|
|
|
else:
|
|
|
|
|
logging.info(f"无法识别图片中的文字。")
|
|
|
|
|
|
|
|
|
|
#读取文本信息
|
|
|
|
|
content = get_latest_txt_filename(directory)
|
|
|
|
|
if content:
|
|
|
|
|
logging.info(content)
|
|
|
|
|
latest_message, marker = get_latest_message(content, marker)
|
|
|
|
|
if is_file_empty(text_file):
|
|
|
|
|
time.sleep(2)
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
#文本转语音
|
|
|
|
|
log_demo_text()
|
|
|
|
|
logging.info(f"转换文本到语音: {text_file}")
|
|
|
|
|
New.text_to_speech(latest_message, output_file)
|
|
|
|
|
logging.info(f"语音文件已生成: {output_file}")
|
|
|
|
|
clear_text_content(text_file)
|
|
|
|
|
logging.info(f"播放音频文件: {remind_path}")
|
|
|
|
|
subprocess.call([vlc_path, '--play-and-exit', remind_path])
|
|
|
|
|
time.sleep(1)
|
|
|
|
|
logging.info(f"播放音频文件: {output_file}")
|
|
|
|
|
subprocess.call([vlc_path, '--play-and-exit', output_file])
|
|
|
|
|
time.sleep(1)
|
|
|
|
|
marker = 0
|
|
|
|
|
consecutive_errors = 0
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logging.error(f"发生错误: {e}")
|
|
|
|
|
time.sleep(1)
|
|
|
|
|
consecutive_errors += 1
|
|
|
|
|
time.sleep(1)
|
|
|
|
|
if consecutive_errors >= 3:
|
|
|
|
|
logging.info(f"播放音频文件: {error_path}")
|
|
|
|
|
subprocess.call([vlc_path, '--play-and-exit', error_path])
|
|
|
|
|
consecutive_errors = 0
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main()
|