@dehciuy

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

APIの統一

二つのAPIを統合したい

visionAPIの出力結果を二つ目のソースコードのqueryのところにインプットしたい

例)
visionAPIとcaloriesNinjaを使用して食品画像認識でカロリー成分表示をできるようにしたいです

発生している問題・エラー

二つのプログラムの統一方法がわからない

//ひとつ目のプログラム

from base64 import b64encode
from sys import argv
import json
import requests

API_URL = 'https://vision.googleapis.com/v1/images:annotate'

if name == 'main':
api_key = argv[1]
filename = argv[2]

img_request = []
with open(filename, 'rb') as f:
    ctxt = b64encode(f.read()).decode()
    img_request.append({
            'image': {'content': ctxt},
            'features': [{
                'type': 'LABEL_DETECTION',
                'maxResults': 10
            }]
    })

request_data = json.dumps({"requests": img_request }).encode()

response = requests.post(API_URL,
                        data=request_data,
                        params={'key': api_key},
                        headers={'Content-Type': 'application/json'})

if response.status_code != 200 or response.json().get('error'):
    print(response.text)
else:
    for resp in enumerate(response.json()['responses']):
        print (json.dumps(resp, indent=2))

//二つ目のプログラム

import requests

api_url = 'https://api.calorieninjas.com/v1/nutrition?query='
query = 'oyster'
response = requests.get(api_url + query, headers={'X-Api-Key': 'T6ELSQrGhspgDN7BstS7Tg==A84xkAxXqfG1k0ql'})
if response.status_code == requests.codes.ok:
print(response.text)
else:
print("Error:", response.status_code, response.text)

0 likes

2Answer

二つ目のプログラムのoysterに相当する情報が、一つ目のプログラムのどこで得られるのでしょうか?
その位置が分かれば、二つ目のプログラムをその位置に置いて、query = xxxと書けばよいと思います。

0Like

Comments

  1. @dehciuy

    Questioner

    ご回答ありがとうございます。二つ目のプログラムのoysterの部分は一つ目のプログラムの結果から持ってきたいのですが、その方法がわかりません...
    可能なのでしょうか。

  2. 一つ目のプログラムの最後の行、 print (json.dumps(resp, indent=2))の出力結果を見せてください。

  3. @dehciuy

    Questioner

    ひとつ目のプログラムの実行結果は以下になります。
    {
    "mid": "/m/02wbm",
    "description": "Food",
    "score": 0.9773263,
    "topicality": 0.9773263
    },
    {
    "mid": "/m/04brg2",
    "description": "Tableware",
    "score": 0.95438135,
    "topicality": 0.95438135
    },
    {
    "mid": "/m/0p57p",
    "description": "Recipe",
    "score": 0.848017,
    "topicality": 0.848017
    },
    {
    "mid": "/m/07xgrh",
    "description": "Ingredient",
    "score": 0.8437752,
    "topicality": 0.8437752
    },
    {
    "mid": "/m/04bcr3",
    "description": "Table",
    "score": 0.7981406,
    "topicality": 0.7981406
    },
    {
    "mid": "/m/01ykh",
    "description": "Cuisine",
    "score": 0.7886198,
    "topicality": 0.7886198
    },
    {
    "mid": "/m/0j62f",
    "description": "Rectangle",
    "score": 0.78735787,
    "topicality": 0.78735787
    },
    {
    "mid": "/m/0h8nvk9",
    "description": "Dishware",
    "score": 0.7470004,
    "topicality": 0.7470004
    },
    {
    "mid": "/m/0dxrf",
    "description": "Frying",
    "score": 0.73139924,
    "topicality": 0.73139924
    },
    {
    "mid": "/m/050gv4",
    "description": "Plate",
    "score": 0.72654116,
    "topicality": 0.72654116
    }
    ]
    }

  4. 上の結果に oysterがありませんが、つなげる意味があるのでしょうか?
    oysterで無くても、カロリー計算ができるような物が見当たりませんが)

  5. oysterで無くても、カロリー計算ができるような物が見当たりませんが

    一旦、それは見ている対象物が違うとして・・・

    以下のコード(二つ目のプログラム)を、一つのプログラムの続きに書いてください。
    インデントが重要ですから、このままをコピペでつなげてください。
    もし上手く動かないようであれば、コード全体を「コードブロック」にコピペして見せてください。

        for resps in response.json()['responses']:
            for item in resps:
                import requests
                api_url = 'https://api.calorieninjas.com/v1/nutrition?query='
                query = item['description']
                response = requests.get(api_url + query, headers={'X-Api-Key': 'T6ELSQrGhspgDN7BstS7Tg==A84xkAxXqfG1k0ql'})
                if response.status_code == requests.codes.ok:
                    print(response.text)
                else:
                    print("Error:", response.status_code, response.text)
    
  6. @dehciuy

    Questioner

    お返事遅れてしまい申し訳ありません。
    上記の結果は寿司を認識した際の結果でした。oysterを認識した際の結果は以下になります。

    {
    "mid": "/m/02wbm",
    "description": "Food",
    "score": 0.98131233,
    "topicality": 0.98131233
    },
    {
    "mid": "/m/04brg2",
    "description": "Tableware",
    "score": 0.91486394,
    "topicality": 0.91486394
    },
    {
    "mid": "/m/06nwz",
    "description": "Seafood",
    "score": 0.8607072,
    "topicality": 0.8607072
    },
    {
    "mid": "/m/07xgrh",
    "description": "Ingredient",
    "score": 0.85549706,
    "topicality": 0.85549706
    },
    {
    "mid": "/m/04klfb",
    "description": "Meyer lemon",
    "score": 0.8330374,
    "topicality": 0.8330374
    },
    {
    "mid": "/m/09k_b",
    "description": "Lemon",
    "score": 0.81302017,
    "topicality": 0.81302017
    },
    {
    "mid": "/m/0p57p",
    "description": "Recipe",
    "score": 0.8101637,
    "topicality": 0.8101637
    },
    {
    "mid": "/m/050gv4",
    "description": "Plate",
    "score": 0.8090339,
    "topicality": 0.8090339
    },
    {
    "mid": "/m/0_cp5",
    "description": "Oyster",
    "score": 0.8004514,
    "topicality": 0.8004514
    },
    {
    "mid": "/m/0dqb5",
    "description": "Citrus",
    "score": 0.79680496,
    "topicality": 0.79680496
    }
    ]
    }

  7. @dehciuy

    Questioner

    "labelAnnotations": [
    {
    "mid": "/m/02wbm",
    "description": "Food",
    "score": 0.98131233,
    "topicality": 0.98131233
    },
    {
    "mid": "/m/04brg2",
    "description": "Tableware",
    "score": 0.91486394,
    "topicality": 0.91486394
    },
    {
    "mid": "/m/06nwz",
    "description": "Seafood",
    "score": 0.8607072,
    "topicality": 0.8607072
    },
    {
    "mid": "/m/07xgrh",
    "description": "Ingredient",
    "score": 0.85549706,
    "topicality": 0.85549706
    },
    {
    "mid": "/m/04klfb",
    "description": "Meyer lemon",
    "score": 0.8330374,
    "topicality": 0.8330374
    },
    {
    "mid": "/m/09k_b",
    "description": "Lemon",
    "score": 0.81302017,
    "topicality": 0.81302017
    },
    {
    "mid": "/m/0p57p",
    "description": "Recipe",
    "score": 0.8101637,
    "topicality": 0.8101637
    },
    {
    "mid": "/m/050gv4",
    "description": "Plate",
    "score": 0.8090339,
    "topicality": 0.8090339
    },
    {
    "mid": "/m/0_cp5",
    "description": "Oyster",
    "score": 0.8004514,
    "topicality": 0.8004514
    },
    {
    "mid": "/m/0dqb5",
    "description": "Citrus",
    "score": 0.79680496,
    "topicality": 0.79680496
    }
    ]
    }
    ]
    Traceback (most recent call last):
    File "kennkyuu01.py", line 40, in
    query = item['description']

    上記の通りですと最下部に上の文が表示されてカロリーの表示ができません。この後にソースコード全体を貼らせていただきます

This answer has been deleted for violation of our Terms of Service.

Your answer might help someone💌