![](/img/trans.png)
[英]Python: Can you directly pass an audio file as user input command to Google Assistant SDK?
[英]How to send text input to google assistant sdk in python
將文本轉換為音頻然后發送到谷歌助手 pushtalk 並沒有給出准確的結果。 所以我需要建議,如何將文本數據發送到谷歌助手文本輸入以獲得更准確的結果。 或任何其他可以提高我在 python 中的准確性的建議
這是我目前安裝的
“pip install --upgrade setuptools wheel \\ google-assistant-library \\ google-assistant-sdk[samples] \\ google-auth-oauthlib[tool]”
這是我目前用來將文本轉換為音頻然后發送到 google-assistant
def post(self):
args = request.get_json()
text_received = str(args['assitant_text'])
file_name = str(args['google_file_name'])
project_id = str(args['project_id'])
device_model_id = str(args['device_model_id'])
refresh_token = str(args['refresh_token'])
client_id = str(args['client_id'])
client_secret = str(args['client_secret'])
voice = str(args['voice'])
text_received = "WHat is the capital of karnataka"
logger.emit("Received request parameters from api",
{
"api": "/google",
"args": args
}
)
token = {"refresh_token": refresh_token, "token_uri": "https://accounts.google.com/o/oauth2/token", "client_id": client_id, "client_secret": client_secret, "scopes": ["https://www.googleapis.com/auth/assistant-sdk-prototype"]}
with open('credentials.json', 'w') as outfile:
json.dump(token, outfile)
status = subprocess.call('aws polly synthesize-speech --output-format mp3 --voice-id %s --text "%s" %s.mp3' %(voice, text_received, file_name), shell=True, stdout=subprocess.PIPE)
logger.emit("Calling aws polly",
{
"api": "/google",
"status_code": status
}
)
tf = open("%s.mp3" % file_name, 'r')
_input = AudioSegment.from_file(tf.name)
tf = tempfile.NamedTemporaryFile(suffix=".wav", delete=True)
output = _input.set_channels(1).set_frame_rate(16000)
f = output.export(tf.name, format="wav")
status = subprocess.call('googlesamples-assistant-pushtotalk --credentials credentials.json --device-config device_config.json --project-id %s --device-model-id %s -i %s -o %s_output.wav' %(project_id, device_model_id, f.name, file_name), shell=True, stdout=subprocess.PIPE)
logger.emit("Calling google samples assistant pushtotalk",
{
"api": "/google",
"status": status
}
)
as_output = AudioSegment.from_file("%s_output.wav" %file_name)
kf = tempfile.NamedTemporaryFile(suffix=".wav")
output = as_output.set_channels(2)
f = output.export(kf.name, format="wav")
r = sr.Recognizer()
with sr.AudioFile(f.name) as source:
output_audio = r.record(source)
text = r.recognize_google(output_audio)
f.close()
kf.close()
os.remove("%s_output.wav" %file_name)
os.remove("%s.mp3" %file_name)
logger.emit("api completed successfully",
{
"api": "/google",
"status": "completed"
}
)
return jsonify({"response": text})
您可以直接使用 API 發送文本查詢,而不是嘗試轉換音頻。
如果您下載示例項目,您應該可以訪問文本輸入示例:
cd google-assistant-sdk/googlesamples/assistant/grpc
python -m textinput --device-id 'my-device-identifier' --device-model-id 'my-model-identifier'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.