LoginSignup
3

More than 5 years have passed since last update.

Python の requests で、Watoson の画像認識を使う

Last updated at Posted at 2017-08-14

Wastosn の VisualRecogintion を使って、画像に写っているものが何かを認識するサンプルです。

requests の GET でアクセスするサンプルです。
watson_developer_cloud を使ってません。

関連ページ
Python で、Watoson の画像認識を使う

実行したときの様子

visual_aug1401.png

get_url_visual.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
#   get_url_visual.py
#
#                   Aug/14/2017
#
# --------------------------------------------------------------------
import sys
import json
import requests
#
# --------------------------------------------------------------------
def get_url_visual_proc(url_jpg):
    api_key="31b9172e4832c2339dc9e6d2e89cfadc5110692"
    version="2016-05-19"
#
    payload = {'api_key': api_key,
        'version': version,
        'url': url_jpg}

    url_api = "https://gateway-a.watsonplatform.net/visual-recognition/api/v3/classify"
    rr = requests.get(url_api,params=payload)
#
    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")
# --------------------------------------------------------------------

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
3