LoginSignup
0
1

More than 5 years have passed since last update.

Python の requests で、Watoson の画像認識を使う (日本語出力)

Last updated at Posted at 2017-09-06

次のページの出力を、日本語にしました。
Python の requests で、Watoson の画像認識を使う

実行したときの様子

visual_sep0601.png

get_url_visual.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
#   get_url_visual.py
#
#                   Sep/06/2017
#
# --------------------------------------------------------------------
import sys
import json
import requests
#
# --------------------------------------------------------------------
def get_url_visual_proc(url_jpg):
    api_key="31b9172e4832c2339dc9e6d2e89cfadc5110692"
    version="2016-05-20"
#
    payload = {
        'api_key': api_key,
        'version': version,
        'url': url_jpg,
        'owners': 'IBM',
        'classifier_ids': 'default'
        }

    headers = {
        'Accept': 'application/json',
        'Accept-Language': 'ja'
        }

    url_api = "https://gateway-a.watsonplatform.net/visual-recognition/api/v3/classify"
    rr = requests.get(url_api,params=payload,headers=headers)
#
    print(rr)
    print()
#   print(rr.text)
#   print()
    dict_aa=json.loads(rr.text)
#
    rvalue = []
    for it in range(3):
        unit=dict_aa["images"][0]["classifiers"][0]["classes"][it]
        rvalue.append(unit)
#
    return rvalue
#
# --------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
url_jpg="https://github.com/watson-developer-cloud/doc-tutorial-downloads/raw/master/visual-recognition/fruitbowl.jpg"
#
rvalue=get_url_visual_proc(url_jpg)
#
for unit in rvalue:
    print (unit["class"],unit["score"])
#
sys.stderr.write("*** 終了 ***\n")
# --------------------------------------------------------------------
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1