Last time, I spoke the information obtained by web scraping. This time, let Raspberry Pi speak the weather information using openJtalk!
--Get weather information using API --Let the Raspberry Pi speak the weather
・ Python 3 and OpenJtalk can be used on Raspberry Pi (Installing OpenJtalk is explained in this article, so if you haven't done so already!)
・ Raspberry Pi3 model B ・ OS: Raspbian ・ Python ver3.7
In this article, https://openweathermap.org/ Use the API provided by. As for how to use this, my predecessor has done it for me, so please refer to the following article and register. Article 1: https://qiita.com/syunyo/items/b408b8d61f9f73b21da7 Article 2: https://qiita.com/nownabe/items/aeac1ce0977be963a740
From here, we will proceed on the assumption that API registration of Open weather map has been completed. Create a directory as you like.
$ mkdir weatherapi
$ cd weatherapi
First, write the following code so that openJtalk can work with a Python script.
$ vi jtalk.py
Press i to enter edit mode and then copy and paste the following.
jtalk.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import subprocess
from datetime import datetime
def jtalk(t):
open_jtalk=['open_jtalk']
mech=['-x','/var/lib/mecab/dic/open-jtalk/naist-jdic']
htsvoice=['-m','/usr/share/hts-voice/mei/mei_normal.htsvoice']
speed=['-r','1.0']
outwav=['-ow','open_jtalk.wav']
cmd=open_jtalk+mech+htsvoice+speed+outwav
c = subprocess.Popen(cmd,stdin=subprocess.PIPE)
c.stdin.write(t.encode())
c.stdin.close()
c.wait()
aplay = ['aplay','-q','open_jtalk.wav']
wr = subprocess.Popen(aplay)
def say_datetime():
d = datetime.now()
text = '%s month%s day,%s time%s minutes%s seconds' % (d.month, d.day, d.hour, d.minute, d.second)
jtalk(text)
if __name__ == '__main__':
say_datetime()
The code above uses a female voice that is not the default for openJtalk. For those who only install openJtalk
htsvoice=['-m','/usr/share/hts-voice/mei/mei_normal.htsvoice']
To
htsvoice=['-m','/usr/share/hts-voice/nitech-jp-atr503-m001/nitech_jp_atr503_m001.htsvoice]
Please change to.
Next, write the code to hit the API in the same directory to speak. In the same way
$ vi weather_api_jtalk.py
Press i to enter edit mode and then copy and paste the following.
weather_api_jtalk.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#open weather map &Audio output weather using openJtalk
#Reference: https://qiita.com/syunyo/items/b408b8d61f9f73b21da7
#API import
import requests
import json
### jtalk import
import jtalk
import time
# API key
apikey = "your api key" #Put your api key here
#List of cities for which you want to check the weather
#cities = ["Sendai,JP", "London,UK", "New York,US"]
cities = ["Sendai,JP"] #Only Sendai this time
#API template
api = "http://api.openweathermap.org/data/2.5/weather?q={city}&APPID={key}"
#Temperature conversion(Kelvin → Celsius)
k2c = lambda k: k - 273.15
#Get the temperature of each city
for name in cities:
#Get the API URL
url = api.format(city=name, key=apikey)
#Actually send a request to the API and get the result
r = requests.get(url)
#The result is in JSON format, so decode it json-> dictionary
data = json.loads(r.text)
#Output result
jtalk.jtalk("Kokon'nichiwa, weather today")
time.sleep(2)
print("+city=", data["name"])
print("|weather=", data["weather"][0]["description"])
if "rain" in data["weather"][0]["description"]:
jtalk.jtalk("It's rain.")
else:
jtalk.jtalk("It's not rain")
time.sleep(2)
print("|Lowest Temperature=", k2c(data["main"]["temp_min"]))
jtalk.jtalk("The lowest temperature is in Celsius"+str(k2c(data["main"]["temp_min"]))+"How is it?")
time.sleep(4)
print("|Highest temperature=", k2c(data["main"]["temp_max"]))
jtalk.jtalk("The maximum temperature is in degrees Celsius"+str(k2c(data["main"]["temp_max"]))+"How is it?")
#print("|Humidity=", data["main"]["humidity"])
#print("|Barometric pressure=", data["main"]["pressure"])
#print("|Wind direction=", data["wind"]['deg'])
#print("|Wind speed=", data["wind"]["speed"])
#print("")
#print("missing some requested data or some error occurs")
$ python weather_api_jtalk.py
How about that? I always commute by bicycle, so I just want to know if it's raining, so if it's raining, I say "it's raining", otherwise I say "it's not raining". Next, it will inform you of the minimum and maximum temperatures.
I'm a little addicted to it here, so I'll emphasize it.
jtalk.jtalk("Kokon'nichiwa, weather today")
time.sleep(2)
As you can see here, the sleep function is used to stop the execution while uttering with jtalk. If you do not do this, the voice information to be output next will come in while you are talking, and the utterance will be interrupted. So, after jtalk.jtalk (), there is a downtime similar to the time it takes to talk. time.sleep (about the time it will take to speak) Please sandwich.
This time, I hit the API to get the weather and output it as voice using OpenJtalk. If you use the natural dialogue API, you will be able to have conversations. Then!
Recommended Posts