qinxiaonan_branch 5 months ago
commit ce5652c8e2

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

@ -1,32 +1,32 @@
#common makefile header #common makefile header
DIR_INC = ../../include 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 =
CFLAGS = -g -Wall -I$(DIR_INC) CFLAGS = -g -Wall -I$(DIR_INC)
ifdef LINUX64 ifdef LINUX64
LDFLAGS := -L$(DIR_LIB)/x64 LDFLAGS := -L$(DIR_LIB)/x64
else else
LDFLAGS := -L$(DIR_LIB)/x86 LDFLAGS := -L$(DIR_LIB)/x86
endif endif
LDFLAGS += -lmsc -lrt -ldl -lpthread -lstdc++ LDFLAGS += -lmsc -lrt -ldl -lpthread -lstdc++
OBJECTS := $(patsubst %.c,%.o,$(wildcard *.c)) OBJECTS := $(patsubst %.c,%.o,$(wildcard *.c))
$(BIN_TARGET) : $(OBJECTS) $(BIN_TARGET) : $(OBJECTS)
$(CROSS_COMPILE)gcc $(CFLAGS) $^ -o $@ $(LDFLAGS) $(CROSS_COMPILE)gcc $(CFLAGS) $^ -o $@ $(LDFLAGS)
%.o : %.c %.o : %.c
$(CROSS_COMPILE)gcc -c $(CFLAGS) $< -o $@ $(CROSS_COMPILE)gcc -c $(CFLAGS) $< -o $@
clean: clean:
@rm -f *.o $(BIN_TARGET) @rm -f *.o $(BIN_TARGET)
.PHONY:clean .PHONY:clean
#common makefile foot #common makefile foot

@ -1,186 +1,169 @@
/* #include <stdlib.h>
* Text To SpeechTTS #include <stdio.h>
* #include <unistd.h>
* 便 #include <errno.h>
*/ #include <string.h>
#include <string.h> #include "../../include/qtts.h"
#include <stdlib.h> #include "../../include/msp_cmn.h"
#include <stdio.h> #include "../../include/msp_errors.h"
#include <unistd.h> typedef int SR_DWORD;
#include <errno.h> typedef short int SR_WORD ;
#include "../../include/qtts.h"
#include "../../include/msp_cmn.h" /* wav音频头部格式 */
#include "../../include/msp_errors.h" typedef struct _wave_pcm_hdr
typedef int SR_DWORD; {
typedef short int SR_WORD ; char riff[4]; // = "RIFF"
int size_8; // = FileSize - 8
/* wav音频头部格式 */ char wave[4]; // = "WAVE"
typedef struct _wave_pcm_hdr char fmt[4]; // = "fmt "
{ int fmt_size; // = 下一个结构体的大小 : 16
char riff[4]; // = "RIFF"
int size_8; // = FileSize - 8 short int format_tag; // = PCM : 1
char wave[4]; // = "WAVE" short int channels; // = 通道数 : 1
char fmt[4]; // = "fmt " int samples_per_sec; // = 采样率 : 8000 | 6000 | 11025 | 16000
int fmt_size; // = 下一个结构体的大小 : 16 int avg_bytes_per_sec; // = 每秒字节数 : samples_per_sec * bits_per_sample / 8
short int block_align; // = 每采样点字节数 : wBitsPerSample / 8
short int format_tag; // = PCM : 1 short int bits_per_sample; // = 量化比特数: 8 | 16
short int channels; // = 通道数 : 1
int samples_per_sec; // = 采样率 : 8000 | 6000 | 11025 | 16000 char data[4]; // = "data";
int avg_bytes_per_sec; // = 每秒字节数 : samples_per_sec * bits_per_sample / 8 int data_size; // = 纯数据长度 : FileSize - 44
short int block_align; // = 每采样点字节数 : wBitsPerSample / 8 } wave_pcm_hdr;
short int bits_per_sample; // = 量化比特数: 8 | 16
/* 默认wav音频头部数据 */
char data[4]; // = "data"; wave_pcm_hdr default_wav_hdr =
int data_size; // = 纯数据长度 : FileSize - 44 {
} wave_pcm_hdr; { 'R', 'I', 'F', 'F' },
0,
/* 默认wav音频头部数据 */ {'W', 'A', 'V', 'E'},
wave_pcm_hdr default_wav_hdr = {'f', 'm', 't', ' '},
{ 16,
{ 'R', 'I', 'F', 'F' }, 1,
0, 1,
{'W', 'A', 'V', 'E'}, 16000,
{'f', 'm', 't', ' '}, 32000,
16, 2,
1, 16,
1, {'d', 'a', 't', 'a'},
16000, 0
32000, };
2, /* 文本合成 */
16, int text_to_speech(const char* src_text, const char* des_path, const char* params)
{'d', 'a', 't', 'a'}, {
0 int ret = -1;
}; FILE* fp = NULL;
/* 文本合成 */ const char* sessionID = NULL;
int text_to_speech(const char* src_text, const char* des_path, const char* params) unsigned int audio_len = 0;
{ wave_pcm_hdr wav_hdr = default_wav_hdr;
int ret = -1; int synth_status = MSP_TTS_FLAG_STILL_HAVE_DATA;
FILE* fp = NULL;
const char* sessionID = NULL; if (NULL == src_text || NULL == des_path)
unsigned int audio_len = 0; {
wave_pcm_hdr wav_hdr = default_wav_hdr; printf("params is error!\n");
int synth_status = MSP_TTS_FLAG_STILL_HAVE_DATA; return ret;
}
if (NULL == src_text || NULL == des_path) fp = fopen(des_path, "wb");
{ if (NULL == fp)
printf("params is error!\n"); {
return ret; printf("open %s error.\n", des_path);
} return ret;
fp = fopen(des_path, "wb"); }
if (NULL == fp) /* 开始合成 */
{ sessionID = QTTSSessionBegin(params, &ret);
printf("open %s error.\n", des_path); if (MSP_SUCCESS != ret)
return ret; {
} printf("QTTSSessionBegin failed, error code: %d.\n", ret);
/* 开始合成 */ fclose(fp);
sessionID = QTTSSessionBegin(params, &ret); return ret;
if (MSP_SUCCESS != ret) }
{ ret = QTTSTextPut(sessionID, src_text, (unsigned int)strlen(src_text), NULL);
printf("QTTSSessionBegin failed, error code: %d.\n", ret); if (MSP_SUCCESS != ret)
fclose(fp); {
return ret; printf("QTTSTextPut failed, error code: %d.\n",ret);
} QTTSSessionEnd(sessionID, "TextPutError");
ret = QTTSTextPut(sessionID, src_text, (unsigned int)strlen(src_text), NULL); fclose(fp);
if (MSP_SUCCESS != ret) return ret;
{ }
printf("QTTSTextPut failed, error code: %d.\n",ret); printf("正在合成 ...\n");
QTTSSessionEnd(sessionID, "TextPutError"); fwrite(&wav_hdr, sizeof(wav_hdr) ,1, fp); //添加wav音频头使用采样率为16000
fclose(fp); while (1)
return ret; {
} /* 获取合成音频 */
printf("正在合成 ...\n"); const void* data = QTTSAudioGet(sessionID, &audio_len, &synth_status, &ret);
fwrite(&wav_hdr, sizeof(wav_hdr) ,1, fp); //添加wav音频头使用采样率为16000 if (MSP_SUCCESS != ret)
while (1) break;
{ if (NULL != data)
/* 获取合成音频 */ {
const void* data = QTTSAudioGet(sessionID, &audio_len, &synth_status, &ret); fwrite(data, audio_len, 1, fp);
if (MSP_SUCCESS != ret) wav_hdr.data_size += audio_len; //计算data_size大小
break; }
if (NULL != data) if (MSP_TTS_FLAG_DATA_END == synth_status)
{ break;
fwrite(data, audio_len, 1, fp); }
wav_hdr.data_size += audio_len; //计算data_size大小 printf("\n");
} if (MSP_SUCCESS != ret)
if (MSP_TTS_FLAG_DATA_END == synth_status) {
break; printf("QTTSAudioGet failed, error code: %d.\n",ret);
} QTTSSessionEnd(sessionID, "AudioGetError");
printf("\n"); fclose(fp);
if (MSP_SUCCESS != ret) return ret;
{ }
printf("QTTSAudioGet failed, error code: %d.\n",ret); /* 修正wav文件头数据的大小 */
QTTSSessionEnd(sessionID, "AudioGetError"); wav_hdr.size_8 += wav_hdr.data_size + (sizeof(wav_hdr) - 8);
fclose(fp);
return ret; /* 将修正过的数据写回文件头部,音频文件为wav格式 */
} fseek(fp, 4, 0);
/* 修正wav文件头数据的大小 */ fwrite(&wav_hdr.size_8,sizeof(wav_hdr.size_8), 1, fp); //写入size_8的值
wav_hdr.size_8 += wav_hdr.data_size + (sizeof(wav_hdr) - 8); fseek(fp, 40, 0); //将文件指针偏移到存储data_size值的位置
fwrite(&wav_hdr.data_size,sizeof(wav_hdr.data_size), 1, fp); //写入data_size的值
/* 将修正过的数据写回文件头部,音频文件为wav格式 */ fclose(fp);
fseek(fp, 4, 0); fp = NULL;
fwrite(&wav_hdr.size_8,sizeof(wav_hdr.size_8), 1, fp); //写入size_8的值 /* 合成完毕 */
fseek(fp, 40, 0); //将文件指针偏移到存储data_size值的位置 ret = QTTSSessionEnd(sessionID, "Normal");
fwrite(&wav_hdr.data_size,sizeof(wav_hdr.data_size), 1, fp); //写入data_size的值 if (MSP_SUCCESS != ret)
fclose(fp); {
fp = NULL; printf("QTTSSessionEnd failed, error code: %d.\n",ret);
/* 合成完毕 */ }
ret = QTTSSessionEnd(sessionID, "Normal");
if (MSP_SUCCESS != ret) return ret;
{ }
printf("QTTSSessionEnd failed, error code: %d.\n",ret);
} int main(int argc, char* argv[])
{
return ret; int ret = MSP_SUCCESS;
} const char* login_params = "appid = ee5efae8, work_dir = .";//登录参数,appid与msc库绑定,请勿随意改动
/*
int main(int argc, char* argv[]) * rdn:
{ * volume:
int ret = MSP_SUCCESS; * pitch:
const char* login_params = "appid = ee5efae8, work_dir = .";//登录参数,appid与msc库绑定,请勿随意改动 * speed:
/* * voice_name:
* rdn: * sample_rate:
* volume: * text_encoding:
* pitch: *
* speed: */
* voice_name: 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";
* sample_rate: const char* filename = "/home/pi/Desktop/al/error.wav"; //合成的语音文件名称
* text_encoding: const char* text = " 程序发生错误。"; //合成文本
* /* 用户登录 */
*/ ret = MSPLogin(NULL, NULL, login_params); //第一个参数是用户名第二个参数是密码第三个参数是登录参数用户名和密码可在http://www.xfyun.cn注册获取
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"; if (MSP_SUCCESS != ret)
const char* filename = "output.wav"; //合成的语音文件名称 {
const char* text = "欢迎使用轻便型导盲杖,此产品主要适用于盲人,为盲人出行进行安全的指导,解决盲人出现问题"; //合成文本 printf("MSPLogin failed, error code: %d.\n", ret);
/* 用户登录 */ goto exit ;//登录失败,退出登录
ret = MSPLogin(NULL, NULL, login_params); //第一个参数是用户名第二个参数是密码第三个参数是登录参数用户名和密码可在http://www.xfyun.cn注册获取 }
if (MSP_SUCCESS != ret)
{ /* 文本合成 */
printf("MSPLogin failed, error code: %d.\n", ret); printf("开始合成 ...\n");
goto exit ;//登录失败,退出登录 ret = text_to_speech(text, filename, session_begin_params);
} if (MSP_SUCCESS != ret)
{
printf("\n###########################################################################\n"); printf("text_to_speech failed, error code: %d.\n", ret);
printf("## 语音合成Text To SpeechTTS技术能够自动将任意文字实时转换为连续的 ##\n"); }
printf("## 自然语音,是一种能够在任何时间、任何地点,向任何人提供语音信息服务的 ##\n"); printf("合成完毕\n");
printf("## 高效便捷手段,非常符合信息时代海量数据、动态更新和个性化查询的需求。 ##\n");
printf("###########################################################################\n\n"); exit:
MSPLogout(); //退出登录
/* 文本合成 */
printf("开始合成 ...\n"); return 0;
ret = text_to_speech(text, filename, session_begin_params); }
if (MSP_SUCCESS != ret)
{
printf("text_to_speech failed, error code: %d.\n", ret);
}
printf("合成完毕\n");
exit:
printf("###########################################################################\n\n");
printf("###########################################################################\n\n");
printf("###########################################################################\n\n");
printf("###########################################################################\n\n");
printf("按任意键退出 ...\n");
getchar();
MSPLogout(); //退出登录
return 0;
}
Loading…
Cancel
Save