LoginSignup
11

More than 5 years have passed since last update.

MicrosoftのComputer Vision API使ってみたらガラパゴス標識「止まれ」を認識した

Last updated at Posted at 2016-07-28

最近の画像認識の実力~MS の最先端の研究成果 Computer Vision API を Python で使ってみた

細かいところは上記の記事を読んでいただければ大体どういうものかわかるかと思います。

#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Microsoft Computer Vision API のデモを
# Python 2 / 3 両系統で動作するようにしたもの
#
#
# Usage: python ms_cv_api_exp.py (image_url)
#
# 参考
# http://qiita.com/kosfuji/items/621cbedfad0eb68b2f5d
# https://dev.projectoxford.ai/docs/services/56f91f2d778daf23d8ec6739/operations/56f91f2e778daf14a499e1fa
#

from __future__ import print_function

import sys
PY3 = sys.version_info[0] == 3

if PY3:
    from urllib.parse import urlencode
    from http.client import HTTPSConnection
else:
    from urllib import urlencode
    from httplib import HTTPSConnection

def main(image_url):
    headers = {
        'Content-Type': 'application/json',
        'Ocp-Apim-Subscription-Key': '(ここにsubscription keyを入れる)',
    }
    params = urlencode({'visualFeatures': 'Description'})
    try:
        conn = HTTPSConnection('api.projectoxford.ai')
        conn.request("POST", "/vision/v1.0/analyze?%s" % params,
                     "{'url': '%s'}" % image_url,
                     headers)
        response = conn.getresponse()
        data = response.read()
        print(data)
        conn.close()
    except Exception as e:
        print("[Errno {0}] {1}".format(e.errno, e.strerror))

if __name__ == '__main__':
    if len(sys.argv) < 2:
        print("Usage: {} url".format(sys.argv[0]))
    main(sys.argv[1])

Subscription keyがあれば上記のツールでURLに対しての解析は結構色々遊べると思います。

こちらはモネの「印象・日の出」

Impression_Sunrise.png

結果は……

{
  "description": {
    "tags": [
      "building",
      "water",
      "street",
      "red",
      "light",
      "sunset",
      "painting",
      "large",
      "city",
      "white",
      "bus",
      "standing",
      "rain",
      "walking",
      "traffic",
      "colorful",
      "man",
      "blurry",
      "riding",
      "parked",
      "people",
      "river",
      "night"
    ],
    "captions": [
      {
        "text": "a painting of a river",
        "confidence": 0.6266185233006358
      }
    ]
  },
  "requestId": "f4ead5ca-0c3c-4e41-97ef-df5d8e2e566d",
  "metadata": {
    "width": 1000,
    "height": 776,
    "format": "Png"
  }
}

すげぇ印象派の絵画でも判定したぞ

こちらは同じくモネの「キャピュシーヌ大通り」

The_Boulevard_des_Capucines.jpg

結果は……

{
  "description": {
    "tags": [
      "outdoor",
      "tree",
      "snow",
      "mountain",
      "covered",
      "group",
      "standing",
      "large",
      "water",
      "riding",
      "field"
    ],
    "captions": [
      {
        "text": "a view of a mountain",
        "confidence": 0.41440203405943216
      }
    ]
  },
  "requestId": "a41ea71a-6e1d-416f-b34e-aa19b98c03e0",
  "metadata": {
    "width": 736,
    "height": 1000,
    "format": "Jpeg"
  }
}

え、山……?(´・ω・`)

ちょっとおちゃめなAPIですが個人的に「すげぇ」と思ったのが以下

「止まれ」の標識
Tomare.png

結果は……

{
  "description": {
    "tags": [
      "building",
      "sign",
      "outdoor",
      "red",
      "stop",
      "street",
      "pole",
      "front",
      "traffic",
      "sitting",
      "black",
      "city",
      "white",
      "close",
      "side",
      "large",
      "blue",
      "standing",
      "train"
    ],
    "captions": [
      {
        "text": "a red stop sign sitting on the side of a building",
        "confidence": 0.8758533311779192
      }
    ]
  },
  "requestId": "2b687702-9442-45cd-bd5c-7de6be37440d",
  "metadata": {
    "width": 1000,
    "height": 1334,
    "format": "Png"
  }
}

当たってる〜

他の人と話していて知ったこととして、日本の「止まれ」の標識ってある種ガラパゴス的なものなんだそうです。

国際標準は八角形の「止まれ(一時停止)」の道路標識 なぜ日本では逆三角形になった?

なので、この図をもって「a red stop sign」と判定するには学習データに日本語のそういうのが入ってないとだめということ。
学習データにちゃんとあるってことなんでしょう。

今回はPreviewということで無料でした。説明によると「5,000 transactions per month, 20 per minute.」。
お仕事で本気で使うには厳しいですがこのくらいで遊ぶのには十分といったところ。

お値段の話はこちらにあります。
https://www.microsoft.com/cognitive-services/en-us/pricing

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
11