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

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