增加运行项目代码和更改voice文件

master
liuyitao 5 months ago
parent b705dba19c
commit 578197dc3b

File diff suppressed because it is too large Load Diff

@ -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()

@ -0,0 +1,2 @@
#!/bin/bash
/usr/bin/python3 /home/pi/Desktop/al/run.py

@ -4,7 +4,7 @@ DIR_INC = ../../include
DIR_BIN = ../../bin DIR_BIN = ../../bin
DIR_LIB = ../../libs DIR_LIB = ../../libs
TARGET = output TARGET = error
BIN_TARGET = $(DIR_BIN)/$(TARGET) BIN_TARGET = $(DIR_BIN)/$(TARGET)
CROSS_COMPILE = CROSS_COMPILE =

@ -1,13 +1,8 @@
/*
* Text To SpeechTTS
*
* 便
*/
#include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <errno.h>
#include <string.h>
#include "../../include/qtts.h" #include "../../include/qtts.h"
#include "../../include/msp_cmn.h" #include "../../include/msp_cmn.h"
#include "../../include/msp_errors.h" #include "../../include/msp_errors.h"
@ -147,8 +142,8 @@ int main(int argc, char* argv[])
* *
*/ */
const char* session_begin_params = "engine_type = local,voice_name=xiaoyan, text_encoding = UTF8, tts_res_path = fo|res/tts/xiaoyan.jet;fo|res/tts/common.jet, sample_rate = 16000, speed = 50, volume = 50, pitch = 50, rdn = 2"; const char* session_begin_params = "engine_type = local,voice_name=xiaoyan, text_encoding = UTF8, tts_res_path = fo|res/tts/xiaoyan.jet;fo|res/tts/common.jet, sample_rate = 16000, speed = 50, volume = 50, pitch = 50, rdn = 2";
const char* filename = "output.wav"; //合成的语音文件名称 const char* filename = "/home/pi/Desktop/al/error.wav"; //合成的语音文件名称
const char* text = "欢迎使用轻便型导盲杖,此产品主要适用于盲人,为盲人出行进行安全的指导,解决盲人出现问题"; //合成文本 const char* text = " 程序发生错误。"; //合成文本
/* 用户登录 */ /* 用户登录 */
ret = MSPLogin(NULL, NULL, login_params); //第一个参数是用户名第二个参数是密码第三个参数是登录参数用户名和密码可在http://www.xfyun.cn注册获取 ret = MSPLogin(NULL, NULL, login_params); //第一个参数是用户名第二个参数是密码第三个参数是登录参数用户名和密码可在http://www.xfyun.cn注册获取
if (MSP_SUCCESS != ret) if (MSP_SUCCESS != ret)
@ -157,12 +152,6 @@ int main(int argc, char* argv[])
goto exit ;//登录失败,退出登录 goto exit ;//登录失败,退出登录
} }
printf("\n###########################################################################\n");
printf("## 语音合成Text To SpeechTTS技术能够自动将任意文字实时转换为连续的 ##\n");
printf("## 自然语音,是一种能够在任何时间、任何地点,向任何人提供语音信息服务的 ##\n");
printf("## 高效便捷手段,非常符合信息时代海量数据、动态更新和个性化查询的需求。 ##\n");
printf("###########################################################################\n\n");
/* 文本合成 */ /* 文本合成 */
printf("开始合成 ...\n"); printf("开始合成 ...\n");
ret = text_to_speech(text, filename, session_begin_params); ret = text_to_speech(text, filename, session_begin_params);
@ -173,12 +162,6 @@ int main(int argc, char* argv[])
printf("合成完毕\n"); printf("合成完毕\n");
exit: exit:
printf("###########################################################################\n\n");
printf("###########################################################################\n\n");
printf("###########################################################################\n\n");
printf("###########################################################################\n\n");
printf("按任意键退出 ...\n");
getchar();
MSPLogout(); //退出登录 MSPLogout(); //退出登录
return 0; return 0;
Loading…
Cancel
Save