LoginSignup
5
4

More than 5 years have passed since last update.

Google Cloud Vison のロゴ判定はOCRっぽかった

Last updated at Posted at 2016-04-13

みんな大好き、Google Cloud Vison。その中の機能に「ロゴ判定」がある。これは大変だ。テレビを見せているだけで、どんどん「企業の露出」が分かっちゃう。「バッグ」と「ヴィトンのバッグ」とは大違い。

ということで、ロゴ判定でどこまでできるかを試しましたよ!

ソース

#cloud vision api を使ってロゴ判定

require 'base64'
require 'json'
require 'net/http'
require 'uri'
require 'open-uri'
require 'find'

#ディレクトリ名を入力
DIR=ARGV

VISION_API_URL = "https://vision.googleapis.com/v1/images:annotate"
API_KEY        = "********"  #発行したAPIキー
URL            = "#{VISION_API_URL}?key=#{API_KEY}"

begin
  #ディレクトリ内の画像を入力
  Dir.glob("image/*.jpg").each {|imgfile|
    input = Base64.encode64(File.open(imgfile,"rb").read)

    uri           = URI.parse(URL)
    https         = Net::HTTP.new(uri.host, uri.port)
    https.use_ssl = true

    req                 = Net::HTTP::Post.new(uri.request_uri)
    req["Content-Type"] = "application/json"
    param               = {
      "requests" =>
      [
        {
          "image" =>
          {
            "content" => input
          },
          "features" =>
          [
            {
              "type"       => "LOGO_DETECTION",
              "maxResults" => 10
            }
          ]
        }
      ]
    }
    req.body = param.to_json
    res      = https.request(req)

    case res
    when Net::HTTPSuccess
      logname="#{File.basename(imgfile).split('.').first}.txt"
      File.open("log/#{logname}.txt","w"){|f|
        f.puts res.body  
      }
    else
      res.error!
    end
  }
rescue => e
  puts "error : #{e.message}"
end

imageフォルダにある、jpeg画像を片っ端から、ロゴ判定にかけて、そのJSONをテキストファイルに吐いてる。

結論

結論からいうと、文字が入ってると、判定できる。マークだけだとダメっぽい。ロゴ判定ってそういうものなのか? じゃぁ単なるOCRかというと、「CHANEL」は、"Coco Chanel" と判定してるから、ブランドとして認識はしている。日本の企業とかはしらないかと思ったら、「SSK」は、"SSK Baseball" って判定してる。そこは賢い。

写真とイラストの違いかと思いきや、アップルのリンゴマーク単体は認識されてないから、写真かどうかは影響しなさそう。

ここから先は、画像と判定結果のJSONがひたすら並びます。

シャネル文字なし

chanel_1.jpg

{
  "responses": [
    {}
  ]
}

シャネル文字あり

chanel_2.jpg

{
  "responses": [
    {
      "logoAnnotations": [
        {
          "mid": "/m/037g0",
          "description": "Coco Chanel",
          "score": 0.61861169,
          "boundingPoly": {
            "vertices": [
              {
                "x": 43,
                "y": 73
              },
              {
                "x": 820,
                "y": 73
              },
              {
                "x": 820,
                "y": 640
              },
              {
                "x": 43,
                "y": 640
              }
            ]
          }
        }
      ]
    }
  ]
}

ミズノ文字なし

mizuno_1.jpg

{
  "responses": [
    {}
  ]
}

ミズノ文字あり

mizuno_2.jpg

{
  "responses": [
    {
      "logoAnnotations": [
        {
          "mid": "/m/060wbl",
          "description": "Mizuno",
          "score": 0.57299244,
          "boundingPoly": {
            "vertices": [
              {
                "x": 82,
                "y": 16
              },
              {
                "x": 335,
                "y": 16
              },
              {
                "x": 335,
                "y": 194
              },
              {
                "x": 82,
                "y": 194
              }
            ]
          }
        }
      ]
    }
  ]
}

SSK文字なし

ssk_1.jpg

{
  "responses": [
    {}
  ]
}

SSK文字あり

ssk_2.jpg

{
  "responses": [
    {
      "logoAnnotations": [
        {
          "description": "SSK Baseball",
          "score": 0.22743617,
          "boundingPoly": {
            "vertices": [
              {
                "x": 12,
                "y": 16
              },
              {
                "x": 201,
                "y": 16
              },
              {
                "x": 201,
                "y": 39
              },
              {
                "x": 12,
                "y": 39
              }
            ]
          }
        }
      ]
    }
  ]
}

ヴィトン文字なし

vuitton_1.jpg

{
  "responses": [
    {}
  ]
}

ヴィトン文字あり

vuitton_2.jpg

{
  "responses": [
    {
      "logoAnnotations": [
        {
          "mid": "/m/03h90x",
          "description": "Louis Vuitton",
          "score": 0.42544109,
          "boundingPoly": {
            "vertices": [
              {
                "x": 13,
                "y": 54
              },
              {
                "x": 174,
                "y": 54
              },
              {
                "x": 174,
                "y": 293
              },
              {
                "x": 13,
                "y": 293
              }
            ]
          }
        }
      ]
    }
  ]
}

インターネット企業ロゴいっぱい

comp_logo_1.jpg

{
  "responses": [
    {
      "logoAnnotations": [
        {
          "description": "IREP",
          "score": 0.40736783,
          "boundingPoly": {
            "vertices": [
              {
                "x": 200,
                "y": 722
              },
              {
                "x": 392,
                "y": 722
              },
              {
                "x": 392,
                "y": 742
              },
              {
                "x": 200,
                "y": 742
              }
            ]
          }
        },
        {
          "mid": "/m/0cm98dx",
          "description": "Dena",
          "score": 0.36809859,
          "boundingPoly": {
            "vertices": [
              {
                "x": 147,
                "y": 114
              },
              {
                "x": 240,
                "y": 114
              },
              {
                "x": 240,
                "y": 142
              },
              {
                "x": 147,
                "y": 142
              }
            ]
          }
        },
        {
          "mid": "/m/0b10_w",
          "description": "Mixi",
          "score": 0.361237,
          "boundingPoly": {
            "vertices": [
              {
                "x": 160,
                "y": 536
              },
              {
                "x": 270,
                "y": 536
              },
              {
                "x": 270,
                "y": 562
              },
              {
                "x": 160,
                "y": 562
              }
            ]
          }
        },
        {
          "description": "iStyle",
          "score": 0.29449216,
          "boundingPoly": {
            "vertices": [
              {
                "x": 31,
                "y": 670
              },
              {
                "x": 88,
                "y": 670
              },
              {
                "x": 88,
                "y": 685
              },
              {
                "x": 31,
                "y": 685
              }
            ]
          }
        },
        {
          "mid": "/m/04c9bn",
          "description": "楽天",
          "score": 0.27938938,
          "boundingPoly": {
            "vertices": [
              {
                "x": 133,
                "y": 357
              },
              {
                "x": 217,
                "y": 357
              },
              {
                "x": 217,
                "y": 382
              },
              {
                "x": 133,
                "y": 382
              }
            ]
          }
        },
        {
          "mid": "/m/0bh762n",
          "description": "アメブロ",
          "score": 0.21190925,
          "boundingPoly": {
            "vertices": [
              {
                "x": 128,
                "y": 25
              },
              {
                "x": 174,
                "y": 25
              },
              {
                "x": 174,
                "y": 73
              },
              {
                "x": 128,
                "y": 73
              }
            ]
          }
        }
      ]
    }
  ]
}

消費者向けブランドいっぱい

comp_logo_2.jpg

{
  "responses": [
    {
      "logoAnnotations": [
        {
          "mid": "/m/05g_lz",
          "description": "Meiji",
          "score": 0.41433442,
          "boundingPoly": {
            "vertices": [
              {
                "x": 208,
                "y": 565
              },
              {
                "x": 294,
                "y": 565
              },
              {
                "x": 294,
                "y": 593
              },
              {
                "x": 208,
                "y": 593
              }
            ]
          }
        },
        {
          "mid": "/m/05t__z",
          "description": "Asahi",
          "score": 0.38916281,
          "boundingPoly": {
            "vertices": [
              {
                "x": 213,
                "y": 50
              },
              {
                "x": 292,
                "y": 50
              },
              {
                "x": 292,
                "y": 75
              },
              {
                "x": 213,
                "y": 75
              }
            ]
          }
        },
        {
          "mid": "/m/03595y",
          "description": "JAXA",
          "score": 0.38760966,
          "boundingPoly": {
            "vertices": [
              {
                "x": 353,
                "y": 342
              },
              {
                "x": 425,
                "y": 342
              },
              {
                "x": 425,
                "y": 376
              },
              {
                "x": 353,
                "y": 376
              }
            ]
          }
        },
        {
          "mid": "/m/04vmpm",
          "description": "Loft",
          "score": 0.30656153,
          "boundingPoly": {
            "vertices": [
              {
                "x": 96,
                "y": 116
              },
              {
                "x": 142,
                "y": 116
              },
              {
                "x": 142,
                "y": 146
              },
              {
                "x": 96,
                "y": 146
              }
            ]
          }
        },
        {
          "mid": "/m/03tb1",
          "description": "IKEA",
          "score": 0.29957649,
          "boundingPoly": {
            "vertices": [
              {
                "x": 208,
                "y": 493
              },
              {
                "x": 292,
                "y": 493
              },
              {
                "x": 292,
                "y": 517
              },
              {
                "x": 208,
                "y": 517
              }
            ]
          }
        },
        {
          "mid": "/m/0fqsd",
          "description": "Olympus",
          "score": 0.29347152,
          "boundingPoly": {
            "vertices": [
              {
                "x": 483,
                "y": 346
              },
              {
                "x": 564,
                "y": 346
              },
              {
                "x": 564,
                "y": 362
              },
              {
                "x": 483,
                "y": 362
              }
            ]
          }
        }
      ]
    }
  ]
}
5
4
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
5
4