参考代码:
如果是1.0版本的遇到以上问题 可以参考以下方案
AttributeError: 'Sequential' object has no attribute 'predict_classes'
如何解决AttributeError: module 'tensorflow' has no attribute 'placeholder'
Original: https://blog.csdn.net/Gabrielle_OyO/article/details/120204514
Author: ML_GearYe
Title: tensorflow 2.6
相关阅读1
Title: TF2.x的keras模型保存与加载
Keras模型包含多个组件:
- 模型的结构或者配置文件,表明模型包含哪些网络层以及各层之间的连接方式。
- 当前状态模型的参数。
- 模型的optimizer,在complie里进行定义的。
- 模型的损失函数和度量函数(在complile函数中定义的或者通过
add_loss()
、add_metric()
函数添加)。
通过Keras的API可以将上述的所有组件保存成一个文件或者选择性的保存其中某些组件:
- 以
Tensorflow SavedModel格式
或者Keras H5格式
将整个模型保存为一个文件。 - 以
JSON
文件形式 保存模型的结构或者配置。 - 只保留模型的权重,通常在训练模型的过程中使用。
保存加载
整个模型的保存与加载
- 模型的结构和配置
- 通过训练学习到的权重
-
模型的编译信息(如果保存前有调用
model.compile
) -
model.save()
或者tf.keras.models.save_model()
tf.keras.models.load_model()
此种方式可以 Keras H5格式
或者 Tensorflow SavedModel格式
保存整个模型,在 TF2.x
版本中默认以 SavedModel格式
保存,如果想要使用 Keras H5格式
,可以通过以下形式进行保存:
- 在
model.save()
函数中传递参数saved_format='h5'
; - 在
model.save()
函数传递文件名参数时以.h5
或者.keras
结尾。
import tensorflow as tf
model = tf.keras.applications.ResNet50()
model.save("saved_model")
model = tf.keras.models.load_model('saved_model')
import tensorflow as tf
model = tf.keras.applications.ResNet50()
model.save("my_h5_model.hdf5", include_optimizer=False, save_format="h5")
model = tf.keras.models.load_model("my_h5_model.h5")
保存模型结构
模型的配置或者结构表明模型包含哪些网络层,以及这些网络层的连接方式,通过模型的配置文件或者结构文件可以生成一个新的具有初始权重的模型,但不包含编译信息,例如损失函数、度量函数或者优化器函数。
get_config()
和from_config()
tf.keras.models.model_to_json()
和tf.keras.models.model_from_json()
调用 config=model.get_config()
将会以 Python Dict
的形式返回一个模型的配置,通过调用 Sequential.from_config(config)
(Sequential模型)或者 Model.from_config(config)
(Functional API模型)生成一个新的模型。
import tensorflow as tf
model = tf.keras.Sequential([keras.Input((32,)), keras.layers.Dense(1)])
config = model.get_config()
new_model = tf.keras.Sequential.from_config(config)
json_config = model.to_json()
new_model = keras.models.model_from_json(json_config)
import tensorflow as tf
inputs = tf.keras.Input((32,))
outputs = tf.keras.layers.Dense(1)(inputs)
model = tf.keras.Model(inputs, outputs)
config = model.get_config()
new_model = tf.keras.Model.from_config(config)
json_config = model.to_json()
new_model = keras.models.model_from_json(json_config)
保存模型权重
适合只使用模型进行推理后者进行迁移学习,可保存为 Tensorflow Checkpoint
或者 Keras H5
格式, 默认格式为 H5
。
model.save_weights()
import tensorflow as tf
model = tf.keras.applications.ResNet50()
model.save_weights("weights.h5", save_format="h5")
model.load_weights("weights.h5")
model.save_weights("model.ckpt", save_format="tf")
model.load_weights("model.ckpt")
keras
模型保存成 SavedModel
格式,虽可以使用 keras.models.load_model
或者 tf.saved_model.load
进行加载,但都会报类似下面的 warning
,并且会严重拖慢模型加载的速度,但 h5
格式的模型通过 keras.models.load_model
不会出现此类问题,很奇怪。
WARNING:tensorflow:Importing a function (__inference_block2a_expand_activation_layer_call_and_return_conditional_losses_xxxxx) with ops with custom gradients. Will likely fail if a gradient is requested.
Original: https://blog.csdn.net/weixin_44899143/article/details/120045726
Author: 努力躺平
Title: TF2.x的keras模型保存与加载
相关阅读2
Title: TensorFlow DLL文件缺失解决方案
前言:
最近在用TensorFlow做车牌识别的项目,在训练模型时发现了如下问题,那就是 xxx.dll not found.
(本人是 windwos
电脑,可能 Windows
电脑问题比较多,如果cuda环境变量一项配置有问题就可能导致程序无法执行),今天就以 cudnn64_7.dll
文件 not found
为例,讲解如何解决这一类问题。
问题描述和分析
在用gpu训练模型时,为了验证gpu是否可用,执行了如下程序。
import tensorflow as tf
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
print(tf.__version__)
a = tf.constant(1.)
b = tf.constant(2.)
print(a + b)
print('GPU:', tf.test.is_gpu_available())
然后执行以上的代码后,发现报了如下错误:
D:\Users\Administrator\Anaconda3\python.exe E:/hyperlpr-train/tensorflowGpu.py
2021-05-14 18:50:06.283947: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
2.2.0
2021-05-14 18:50:13.818450: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library nvcuda.dll
2021-05-14 18:50:13.835941: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1561] Found device 0 with properties:
pciBusID: 0000:01:00.0 name: GeForce RTX 2070 SUPER computeCapability: 7.5
coreClock: 1.815GHz coreCount: 40 deviceMemorySize: 8.00GiB deviceMemoryBandwidth: 417.29GiB/s
2021-05-14 18:50:13.836128: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
2021-05-14 18:50:13.839458: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cublas64_10.dll
2021-05-14 18:50:13.841704: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cufft64_10.dll
2021-05-14 18:50:13.842960: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library curand64_10.dll
2021-05-14 18:50:13.845417: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusolver64_10.dll
2021-05-14 18:50:13.847722: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusparse64_10.dll
2021-05-14 18:50:13.850513: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudnn64_7.dll'; dlerror: cudnn64_7.dll not found
2021-05-14 18:50:13.850618: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1598] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform.
Skipping registering GPU devices...
2021-05-14 18:50:14.143759: I tensorflow/core/platform/cpu_feature_guard.cc:143] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
2021-05-14 18:50:14.154386: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x25d93ccb3c0 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2021-05-14 18:50:14.154570: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version
2021-05-14 18:50:14.154697: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1102] Device interconnect StreamExecutor with strength 1 edge matrix:
2021-05-14 18:50:14.154785: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1108]
tf.Tensor(3.0, shape=(), dtype=float32)
WARNING:tensorflow:From E:/hyperlpr-train/tensorflowGpu.py:9: is_gpu_available (from tensorflow.python.framework.test_util) is deprecated and will be removed in a future version.
Instructions for updating:
Use `tf.config.list_physical_devices('GPU')` instead.
2021-05-14 18:50:15.852497: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1561] Found device 0 with properties:
pciBusID: 0000:01:00.0 name: GeForce RTX 2070 SUPER computeCapability: 7.5
coreClock: 1.815GHz coreCount: 40 deviceMemorySize: 8.00GiB deviceMemoryBandwidth: 417.29GiB/s
2021-05-14 18:50:15.853123: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
2021-05-14 18:50:15.853210: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cublas64_10.dll
2021-05-14 18:50:15.853294: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cufft64_10.dll
2021-05-14 18:50:15.853383: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library curand64_10.dll
2021-05-14 18:50:15.853471: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusolver64_10.dll
2021-05-14 18:50:15.853560: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusparse64_10.dll
2021-05-14 18:50:16.070937: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudnn64_7.dll'; dlerror: cudnn64_7.dll not found
2021-05-14 18:50:16.071049: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1598] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform.
Skipping registering GPU devices...
2021-05-14 18:50:16.138353: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1102] Device interconnect StreamExecutor with strength 1 edge matrix:
2021-05-14 18:50:16.138450: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1108] 0
2021-05-14 18:50:16.138505: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1121] 0: N
2021-05-14 18:50:16.139480: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x25da4d20b10 initialized for platform CUDA (this does not guarantee that XLA will be used). Devices:
2021-05-14 18:50:16.139596: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): GeForce RTX 2070 SUPER, Compute Capability 7.5
GPU: False
报错的日志很长,我们做一下过滤,你会发现核心提示错误如下,图中我已近标出。
认真看图中标记的1,2,3部分,你会发现,在第一部分中, cudart64_101.dll,cublas64_10.dll,cufft64_10.dll,curand64_10.dll,cusolver64_10.dll,cusparse64_10.dll
文件都存在,并且 Successfully opened dynamic library
,唯独第二部分的 cudnn64_7.dll
打开失败,所以导致GPU也无法使用,所以在第三步中显示了 GPU: False
。打开失败原因吗,在第二部分中已经显示的非常清楚, Could not load dynamic library 'cudnn64_7.dll'; dlerror: cudnn64_7.dll not found
,随之也给出了参考解决方案: Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform.Skipping registering GPU devices...
既然知道是cudnn64_7.dll文件缺失,那好办,只需要下载cudnn64_7.dll文件就好了。
解决方案
1. 推荐解决方案,重新下载CUDA和与之对应的CUDNN.
- CUDA下载地址:https://developer.nvidia.com/zh-cn/cuda-downloads,可以查看自己电脑cuda版本去官网下载对应的CUDA和对应的显卡驱动。
- CUDNN下载地址:https://developer.nvidia.com/cudnn
一般情况下,如下问题基本都能解决,如果你的window和我一样变态,我推荐你可以尝试一下第二种方案。
2. 搜索缺失DLL系列并安装
比如我这里缺失的是 cudnn64_7.dll,那么我就下载cudnn64_7.dll文件即可。下面是我在网上搜罗的好心人分享的各种dll文件的链接地址:
- DLL文件夹,链接:https://pan.baidu.com/s/1iO9ojtf-oPBtzG9wbQSxpw提取码:
koj7
- cusolver64_10.dll,链接:https://pan.baidu.com/s/1om_-n18iKZU_YRjJ1JUwyQ 提取码:
7tdl
- cudnn64_7.dll,链接:https://pan.baidu.com/s/1qRr9HSFgF8mT8vty9m1rXA,提取码:
p09o
- cudnn64_8.dll,cudnn-11.1-windows-x64-v8.0.4.30.zip,https://pan.baidu.com/s/1cqCINlm0_ZaK_otBwybM8w,提取码:
1kge
此外,以下四个cndnn6/7/8/9版本都有,链接为https://pan.baidu.com/s/10Bm48r7-qruAAzh9-wgPig#list/path=%2F,提取码: bsir
下载完毕之后,我们只需把对应的xxx.dll文件放在对应的目录下即可。一般都是放在如下俩个目录:
- C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1\bin
- C:\Windows\System32
好了,执行一下第一步中的代码(验证代码更具自己的实际情况而定,我这里就是验证GPU是否可用),果然,GPU是可以用的,看看成功后的日志如下:
D:\Users\Administrator\Anaconda3\python.exe E:/hyperlpr-train/tensorflowGpu.py
2021-05-14 19:27:19.226930: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
2.2.0
2021-05-14 19:27:20.746048: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library nvcuda.dll
2021-05-14 19:27:20.761813: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1561] Found device 0 with properties:
pciBusID: 0000:01:00.0 name: GeForce RTX 2070 SUPER computeCapability: 7.5
coreClock: 1.815GHz coreCount: 40 deviceMemorySize: 8.00GiB deviceMemoryBandwidth: 417.29GiB/s
2021-05-14 19:27:20.762000: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
2021-05-14 19:27:20.764550: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cublas64_10.dll
2021-05-14 19:27:20.766331: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cufft64_10.dll
2021-05-14 19:27:20.767209: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library curand64_10.dll
2021-05-14 19:27:20.769296: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusolver64_10.dll
2021-05-14 19:27:20.771242: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusparse64_10.dll
2021-05-14 19:27:20.824569: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudnn64_7.dll
2021-05-14 19:27:20.999286: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1703] Adding visible gpu devices: 0
2021-05-14 19:27:20.999775: I tensorflow/core/platform/cpu_feature_guard.cc:143] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
2021-05-14 19:27:21.005884: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x29e89bb5040 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2021-05-14 19:27:21.006029: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version
2021-05-14 19:27:21.006383: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1561] Found device 0 with properties:
pciBusID: 0000:01:00.0 name: GeForce RTX 2070 SUPER computeCapability: 7.5
coreClock: 1.815GHz coreCount: 40 deviceMemorySize: 8.00GiB deviceMemoryBandwidth: 417.29GiB/s
2021-05-14 19:27:21.006567: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
2021-05-14 19:27:21.006661: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cublas64_10.dll
2021-05-14 19:27:21.006756: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cufft64_10.dll
2021-05-14 19:27:21.006846: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library curand64_10.dll
2021-05-14 19:27:21.006938: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusolver64_10.dll
2021-05-14 19:27:21.007029: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusparse64_10.dll
2021-05-14 19:27:21.007119: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudnn64_7.dll
2021-05-14 19:27:21.007403: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1703] Adding visible gpu devices: 0
2021-05-14 19:28:38.869252: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1102] Device interconnect StreamExecutor with strength 1 edge matrix:
2021-05-14 19:28:38.869782: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1108] 0
2021-05-14 19:28:38.870114: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1121] 0: N
2021-05-14 19:28:38.966406: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1247] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 6620 MB memory) -> physical GPU (device: 0, name: GeForce RTX 2070 SUPER, pci bus id: 0000:01:00.0, compute capability: 7.5)
2021-05-14 19:28:39.014094: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x29ec10ed830 initialized for platform CUDA (this does not guarantee that XLA will be used). Devices:
2021-05-14 19:28:39.014219: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): GeForce RTX 2070 SUPER, Compute Capability 7.5
tf.Tensor(3.0, shape=(), dtype=float32)
WARNING:tensorflow:From E:/hyperlpr-train/tensorflowGpu.py:9: is_gpu_available (from tensorflow.python.framework.test_util) is deprecated and will be removed in a future version.
Instructions for updating:
Use `tf.config.list_physical_devices('GPU')` instead.
2021-05-14 19:28:40.552702: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1561] Found device 0 with properties:
pciBusID: 0000:01:00.0 name: GeForce RTX 2070 SUPER computeCapability: 7.5
coreClock: 1.815GHz coreCount: 40 deviceMemorySize: 8.00GiB deviceMemoryBandwidth: 417.29GiB/s
2021-05-14 19:28:40.552888: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
2021-05-14 19:28:40.552980: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cublas64_10.dll
2021-05-14 19:28:40.553066: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cufft64_10.dll
2021-05-14 19:28:40.553149: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library curand64_10.dll
2021-05-14 19:28:40.553236: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusolver64_10.dll
2021-05-14 19:28:40.553325: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusparse64_10.dll
2021-05-14 19:28:40.553411: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudnn64_7.dll
2021-05-14 19:28:40.553693: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1703] Adding visible gpu devices: 0
2021-05-14 19:28:40.553790: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1102] Device interconnect StreamExecutor with strength 1 edge matrix:
2021-05-14 19:28:40.553879: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1108] 0
2021-05-14 19:28:40.553934: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1121] 0: N
2021-05-14 19:28:40.554219: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1247] Created TensorFlow device (/device:GPU:0 with 6620 MB memory) -> physical GPU (device: 0, name: GeForce RTX 2070 SUPER, pci bus id: 0000:01:00.0, compute capability: 7.5)
GPU: True
这里我提供一下各种dll文件下载的地址,进入官网,自己自行搜索自己缺少的dll文件。直接在网站https://www.dll-files.com/search/下载:
友情提示:
需要特别提示的是,有些缺少DLL文件并不一定是安装了相应的DLL文件就能解决的。有时候需要降低自己的tensorflow版本使tensorflow与其它的函数库相匹配才可以。
Original: https://blog.csdn.net/ljx1400052550/article/details/116801643
Author: 不羁少年!
Title: TensorFlow DLL文件缺失解决方案
相关阅读3
Title: 基于树莓派语音合成小白避坑
文章目录
前言
如何利用树莓派能够实现语音识别、语音合成适合小白的入门小设计。
参考文章:
https://blog.csdn.net/weixin_44897649/article/details/103173247?utm_source=app&app_version=4.8.1
1.硬件要求
树莓派3b+运行环境配置好以及sd卡、tf卡等基本器件,声音输出可以选择耳机,usb声卡(免驱动)+普通麦克风(内部没有声卡3.5mm)或者选择usb麦克风内置声卡(免驱动),我们选择的是后者。
; 2.麦克风检测
参考文章:https://blog.csdn.net/weixin_43892158/article/details/113787727
2.1细节注意
网上很多提到对树莓派对于默认声卡的修改,其实这是有前提的,如果你能够实现通过麦克风声音的输入与树莓派声音的输出,没有必要进行默认声卡的修改。
其次如果感觉耳机输出声音太小可通过以下,进行调节
pi@raspberrypi:~ $ alsamixer
3.python-sdk模块
首先完成账号的申请与产品的创建,要注意语音识别、语音合成是不同的产品,要分别进行创建。其次安装该模块有两种方式安装,以语音识别库下载为例代码如下
示例1:
首先官网下载到树莓派pi文件夹下,先解压后安装
unzip aip-python-sdk-4.15.4.zip
sudo pip install baidu-aip
示例2:
直接终端运行
pip install baidu-aip
4.代码部分改进
参考原来的文章实现相对比较智能化的在线语音合成,实现直接将输入的文本合成音频直接输出。
#_*_ coding:UTF-8 _*_
# @author: zdl
# 百度云语音合成Demo,实现对本地文本的语音合成。
# 需安装好python-SDK,待合成文本不超过1024个字节
# 合成成功返回audio.mp3 否则返回错误代码
# 导入AipSpeech AipSpeech是语音识别的Python SDK客户端
import playsound
from aip import AipSpeech
import os
''' 你的APPID AK SK 参数在申请的百度云语音(合成)服务的控制台查看'''
APP_ID = 'xxx'
API_KEY = 'xxx'
SECRET_KEY = 'xxx'
# 新建一个AipSpeech
client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
# 将本地文件进行语音合成
demo = raw_input('请输入你要合成的文字:')
print('输入的文字为:%s' %demo)
def tts(demo):
if len(demo) != 0:
word = demo
result = client.synthesis(word,'zh',1, {
'vol': 5,'per':0,
})
# 合成正确返回audio.mp3,错误则返回dict
if not isinstance(result, dict):
with open('audio.mp3', 'wb') as f:
f.write(result)
print ('tts successful')
os.system("mplayer /home/pi/audio.mp3")
# main
if __name__ == '__main__':
tts(demo)
总结
第一次写博客,实践过程中有小伙伴们的支持,老师的提议,很多大神的帮助,少走了很多弯路,希望这篇文章同样可以帮到大家,不足的地方希望大家批评指正。
Original: https://blog.csdn.net/qq_43447941/article/details/117573396
Author: 努力进步一点点
Title: 基于树莓派语音合成小白避坑