前言
python語音播報?手上有一臺樹莓派,平時在運行一些服務,現在天氣熱了導致樹莓派嚴重發燙,使用python實現溫度過高時播報當前溫度,還可以通過樹莓派的gpio引腳自動啟動風扇進行散熱等玩法。
環境安裝
python讀取文件編碼錯誤。首先pyttsx3是基于python3以及pip3,正常樹莓派自帶(pip3沒有自帶),沒有則手動搜索引擎安裝
安裝pip3可以參照
安裝espeak
?先安裝語音轉換引擎(重要)
sudo apt-get install espeak
安裝pyttsx3
sudo pip3 install pyttsx3
問題
如果失敗則更新下pip3
sudo pip3 install --upgrade pip
繼續失敗則嘗試降低下版本
sudo pip3 install pyttsx3==2.71
代碼
import os
import time
import pyttsx3
# Return CPU temperature as a character string
def getCPUtemperature():
res = os.popen('vcgencmd measure_temp').readline()
return(res.replace("temp=","").replace("'C\n","").replace(".","")[0:2])
if __name__ == '__main__':
# CPU informatiom
CPU_temp = getCPUtemperature()
# 判斷溫度大于50度
if(int(CPU_temp) > 50):
print('CPU Temperature = '+CPU_temp)
engine = pyttsx3.init() # 創建對象
rate = engine.getProperty('rate') # 獲取當前語速(默認值)
#print (rate) # 打印當前語速(默認值)
engine.setProperty('rate', 135) # 設置一個新的語速
volume = engine.getProperty('volume') # 獲取當前的音量 (默認值)(min=0 and max=1)
#print (volume) # 打印當前音量(默認值)
engine.setProperty('volume',1.0) # 設置一個新的音量(0 < volume < 1)
voices = engine.getProperty('voices') # 獲取當前的音色信息
engine.setProperty('voice', voices[0].id) # 改變中括號中的值,0為男性,1為女性
engine.setProperty('voice','zh') #將音色中修改音色的語句替換
engine.say("警告 溫度:"+CPU_temp)
engine.runAndWait()
保存文件為temp.py
執行
python3 temp.py
定時任務
定時后臺監控溫度播報,這里使用linux自帶的定時任務crontab
crontab -e
* * * * * python3 /home/pi/python/temp.py
以上一分鐘檢測一次
本文由 SAn 創作,采用 知識共享署名4.0 國際許可協議進行許可
本站文章除注明轉載/出處外,均為本站原創或翻譯,轉載前請務必署名
最后編輯時間為:
2020/10/16 16:22
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态