python3 でのAPI呼び出し例

#!/usr/bin/env python3
# -*-coding: utf-8 -*-

import requests
import json
import base64  # これがドキュメントでの例では抜けてた

URL = 'https://xxxxx.dl.sony.com/v1/serverless/classifiers/dasdfadfd/inference'
KEY = '6626b3e4-1316-4b1a-b10b-6dsaf3dfsdab995d8' PIC = './sample33-55.png' headers = { 'Content-Type' : 'application/json', 'x-api-key' : KEY } b64 = base64.encodebytes(open(PIC, 'rb').read()).decode('utf8') data = { "executor": "Executor", "inputs": [{ "name": "x", "type": "png", "data": b64 }] } r = requests.post(URL, data=json.dumps(data), headers=headers) print(r.text)


# 実行結果の例:
# {"version": "20201002_180604020502", "outputs": [{"name": "y'", "data": "0.9957193732261658, 0.004280652850866318"}]}

Last modified: Sunday, 4 October 2020, 11:24 PM