一度に全て処理しpygame で読み上げ
wavファイルの読み上げを gygame にすることでエラー対処
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | import requests import json from datetime import datetime import deepl def get_weather_forecast(latitude, longitude, API_key, deepl_auth_key): # DeepL Translatorのインスタンスを生成 translator = deepl.Translator(deepl_auth_key) # OpenWeather APIのURL url = "https://api.openweathermap.org/data/2.5/onecall?lat={lat}&lon={lon}&exclude=hourly,minutely&units=metric&lang=ja&appid={API_key}" url = url. format (lat=latitude, lon=longitude, API_key=API_key) # APIリクエスト response = requests.get(url) jsondata = response.json() # 今日の日付を取得 today = datetime.now(). date () # 今日の天気予報を探す for daily_forecast in jsondata[ "daily" ]: date = datetime.fromtimestamp(daily_forecast[ "dt" ]). date () if date == today: min_temp = daily_forecast[ "temp" ][ "min" ] max_temp = daily_forecast[ "temp" ][ "max" ] weather = daily_forecast[ "weather" ][0][ "main" ] description = daily_forecast[ "weather" ][0][ "description" ] break # 天気をdeeplで日本語に翻訳 weather_japanese = translator.translate_text(weather, target_lang= "JA" ).text # 今日の天気予報をまとめる today_weather_repo = f "今日の天気は{weather_japanese}、予想最高気温は{max_temp}度、予想最低気温は{min_temp}度です" return today_weather_repo # 関数を使用して天気予報を取得 latitude = "緯度" longitude = "経度" API_key = "weather map apiキー" deepl_auth_key = "deeplのAPIキー" weather_report = get_weather_forecast(latitude, longitude, API_key, deepl_auth_key) # 天気予報をテキストファイルに保存 with open ( 'weather.txt' , 'w' ) as file : file .write(weather_report) |
として関数にして
weather_forecast.py
として保存
次に
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | import subprocess import pygame import time from weather_forecast import get_weather_forecast # 天気予報を取得してファイルに保存 latitude = "緯度" longitude = "経度" API_key = "weather map apiキー" deepl_auth_key = "DeepL APIキー" weather_report = get_weather_forecast(latitude, longitude, API_key, deepl_auth_key) with open ( 'weather.txt' , 'w' ) as file : file .write(weather_report) # JSONファイルを作成するためのcurlコマンド command_json = [ "curl" , "-s" , "-X" , "POST" , "192.168.1.69:50021/audio_query?speaker=1" , "--get" , "--data-urlencode" , "text@weather.txt" ] # 音声ファイルを作成するためのcurlコマンド command_audio = [ "curl" , "-s" , "-H" , "Content-Type: application/json" , "-X" , "POST" , "-d" , "@query.json" , "192.168.1.69:50021/synthesis?speaker=1" ] # 最初のコマンドを実行してJSONファイルを作成 with open ( 'query.json' , 'w' ) as file : subprocess.run(command_json, stdout= file ) # 第二のコマンドを実行して音声ファイルを作成 with open ( 'test_audio.wav' , 'wb' ) as file : subprocess.run(command_audio, stdout= file ) # Pygameの初期化 pygame.init() pygame.mixer.init() # WAVファイルを読み込む sound = pygame.mixer.Sound( "test_audio.wav" ) # 再生 sound.play() # 再生が終了するまで待機 while pygame.mixer.get_busy(): time . sleep (0.1) |
として作成したファイルを元に
Subprocess で
Curl で音声ファイルを作成
それをpygame で読み上げるようにした
とりあえず動作確認はできたので
次に顔認識と合わせてみる