Skip to content

Instantly share code, notes, and snippets.

@jwulff
Last active July 15, 2016 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jwulff/54acc16aada777db72c8 to your computer and use it in GitHub Desktop.
Save jwulff/54acc16aada777db72c8 to your computer and use it in GitHub Desktop.
Google Cloud Vision API Example
# Get a server key https://cloud.google.com/vision/docs/getting-started
# $ ruby google-cloud-vision-api-example.rb KEY https://www.whitehouse.gov/sites/default/files/whitehouse_files/image/west_wing_main_photo.jpg
require 'net/https'
require 'json'
require 'base64'
require 'open-uri'
key = ARGV[0]
image_url = ARGV[1]
url = "https://vision.googleapis.com/v1/images:annotate?key=#{key}"
payload = {
requests: [
{
image: {
content: Base64.encode64(open(image_url).read)
},
features: [
{ type: 'FACE_DETECTION' },
{ type: 'LANDMARK_DETECTION' },
{ type: 'LOGO_DETECTION' },
{ type: 'LABEL_DETECTION' },
{ type: 'TEXT_DETECTION' },
{ type: 'SAFE_SEARCH_DETECTION' },
{ type: 'IMAGE_PROPERTIES' }
]
}
]
}
uri = URI url
request = Net::HTTP::Post.new uri
request.content_type = 'application/json'
request.body = payload.to_json
http = Net::HTTP.new uri.hostname, uri.port
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE # Ewwwwwww!
#http.set_debug_output $stderr
response = http.request request
puts response.body
@jwulff
Copy link
Author

jwulff commented Feb 20, 2016

Replace KEY with a "server key" https://cloud.google.com/vision/docs/getting-started

Kapalua

$ ruby google-cloud-vision-api-example.rb KEY http://www.aloha-hawaii.com/wp-content/uploads/2010/08/kapalua-beach.jpg
{
  "responses": [
    {
      "labelAnnotations": [
        {
          "mid": "/m/0b3yr",
          "description": "beach",
          "score": 0.94665974
        },
        {
          "mid": "/m/06npx",
          "description": "sea",
          "score": 0.93214893
        },
        {
          "mid": "/m/02jwqh",
          "description": "vacation",
          "score": 0.88489515
        },
        {
          "mid": "/m/01r3xn",
          "description": "shoal",
          "score": 0.85252696
        },
        {
          "mid": "/m/03ktm1",
          "description": "body of water",
          "score": 0.78492069
        }
      ],
      "safeSearchAnnotation": {
        "adult": "VERY_UNLIKELY",
        "spoof": "VERY_UNLIKELY",
        "medical": "VERY_UNLIKELY",
        "violence": "VERY_UNLIKELY"
      },
      "imagePropertiesAnnotation": {
        "dominantColors": {
          "colors": [
            {
              "color": {
                "red": 120,
                "green": 174,
                "blue": 170
              },
              "score": 0.20442095,
              "pixelFraction": 0.092809364
            },
            {
              "color": {
                "red": 60,
                "green": 133,
                "blue": 159
              },
              "score": 0.16427764,
              "pixelFraction": 0.057971016
            },
            {
              "color": {
                "red": 196,
                "green": 191,
                "blue": 162
              },
              "score": 0.078156434,
              "pixelFraction": 0.095503531
            },
            {
              "color": {
                "red": 242,
                "green": 227,
                "blue": 174
              },
              "score": 0.058972564,
              "pixelFraction": 0.13991082
            },
            {
              "color": {
                "red": 40,
                "green": 55,
                "blue": 28
              },
              "score": 0.020918924,
              "pixelFraction": 0.028149387
            },
            {
              "color": {
                "red": 17,
                "green": 22,
                "blue": 11
              },
              "score": 0.0035301843,
              "pixelFraction": 0.018952062
            },
            {
              "color": {
                "red": 89,
                "green": 161,
                "blue": 173
              },
              "score": 0.10494472,
              "pixelFraction": 0.045429207
            },
            {
              "color": {
                "red": 157,
                "green": 189,
                "blue": 175
              },
              "score": 0.10479397,
              "pixelFraction": 0.036696397
            },
            {
              "color": {
                "red": 55,
                "green": 132,
                "blue": 170
              },
              "score": 0.041868147,
              "pixelFraction": 0.02703456
            },
            {
              "color": {
                "red": 214,
                "green": 196,
                "blue": 143
              },
              "score": 0.033141714,
              "pixelFraction": 0.069583796
            }
          ]
        }
      }
    }
  ]
}

@jwulff
Copy link
Author

jwulff commented Feb 20, 2016

Replace KEY with a "server key" https://cloud.google.com/vision/docs/getting-started

Capitol

$ ruby google-cloud-vision-api-example.rb KEY http://latitudenews.wpengine.netdna-cdn.com/wp-content/uploads/2013/01/RTR3CRB5.jpg
{
  "responses": [
    {
      "landmarkAnnotations": [
        {
          "mid": "/m/07vth",
          "description": "U.S. Capitol",
          "score": 0.72988671,
          "boundingPoly": {
            "vertices": [
              {
                "x": 1234,
                "y": 201
              },
              {
                "x": 2191,
                "y": 201
              },
              {
                "x": 2191,
                "y": 1049
              },
              {
                "x": 1234,
                "y": 1049
              }
            ]
          },
          "locations": [
            {
              "latLng": {
                "latitude": 38.889804999999996,
                "longitude": -77.010673
              }
            }
          ]
        },
        {
          "score": 0.41945121,
          "boundingPoly": {
            "vertices": [
              {
                "x": 1568,
                "y": 319
              },
              {
                "x": 1931,
                "y": 319
              },
              {
                "x": 1931,
                "y": 774
              },
              {
                "x": 1568,
                "y": 774
              }
            ]
          },
          "locations": [
            {
              "latLng": {
                "latitude": 38.890398,
                "longitude": -77.008324
              }
            }
          ]
        }
      ],
      "labelAnnotations": [
        {
          "mid": "/m/01g317",
          "description": "person",
          "score": 0.85205597
        },
        {
          "mid": "/m/03qtwd",
          "description": "crowd",
          "score": 0.82822108
        },
        {
          "mid": "/m/09g5pq",
          "description": "people",
          "score": 0.81189913
        },
        {
          "mid": "/m/02p1xh",
          "description": "marching",
          "score": 0.67885125
        },
        {
          "mid": "/m/03w7hp",
          "description": "ice rink",
          "score": 0.56805277
        }
      ],
      "textAnnotations": [
        {
          "locale": "la",
          "description": "ill|開lll ll Illi\n",
          "boundingPoly": {
            "vertices": [
              {
                "x": 1474,
                "y": 989
              },
              {
                "x": 1869,
                "y": 989
              },
              {
                "x": 1869,
                "y": 1073
              },
              {
                "x": 1474,
                "y": 1073
              }
            ]
          }
        }
      ],
      "safeSearchAnnotation": {
        "adult": "VERY_UNLIKELY",
        "spoof": "VERY_UNLIKELY",
        "medical": "VERY_UNLIKELY",
        "violence": "VERY_UNLIKELY"
      },
      "imagePropertiesAnnotation": {
        "dominantColors": {
          "colors": [
            {
              "color": {
                "red": 124,
                "green": 119,
                "blue": 113
              },
              "score": 0.23088774,
              "pixelFraction": 0.10931973
            },
            {
              "color": {
                "red": 158,
                "green": 153,
                "blue": 146
              },
              "score": 0.18368539,
              "pixelFraction": 0.12353741
            },
            {
              "color": {
                "red": 90,
                "green": 85,
                "blue": 79
              },
              "score": 0.16493472,
              "pixelFraction": 0.093469389
            },
            {
              "color": {
                "red": 53,
                "green": 68,
                "blue": 48
              },
              "score": 0.0016384164,
              "pixelFraction": 0.00020408163
            },
            {
              "color": {
                "red": 56,
                "green": 50,
                "blue": 46
              },
              "score": 0.14228432,
              "pixelFraction": 0.099319726
            },
            {
              "color": {
                "red": 29,
                "green": 25,
                "blue": 21
              },
              "score": 0.079609387,
              "pixelFraction": 0.13911565
            },
            {
              "color": {
                "red": 133,
                "green": 110,
                "blue": 116
              },
              "score": 0.046034891,
              "pixelFraction": 0.0042176871
            },
            {
              "color": {
                "red": 193,
                "green": 190,
                "blue": 187
              },
              "score": 0.030681806,
              "pixelFraction": 0.22054422
            },
            {
              "color": {
                "red": 107,
                "green": 84,
                "blue": 91
              },
              "score": 0.024027826,
              "pixelFraction": 0.0035374151
            },
            {
              "color": {
                "red": 48,
                "green": 17,
                "blue": 26
              },
              "score": 0.023328064,
              "pixelFraction": 0.0073469388
            }
          ]
        }
      }
    }
  ]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment