Making a silly game using Google Cloud Vision and Instagram

I was recently invited to the first alpha release of Google Cloud Vision, which is a game changing new API from Google. I can programmatically upload any picture and get this kind of stuff back:

  • Face detection, with pixel annotations of the corner of the mouth etc, plus unexpected values like sorrowLikelihood
  • Landmark detection, with LatLon polygon boundaries to real world landmarks
  • Logo detection
  • Text detection
  • Safe search detection, which can detect medical, nude and violent content

I wanted to dig right in, and decided to build a really silly little game. I call it Game Of Cats, and has nothing to do with Game of Thrones at all.

A user logs in using their Instagram credentials. My game engine starts polling all recent pictures from the people the user follows, and awards a point for every cat picture that occurs. The game goes on forever, without any interaction.

Screenshot 2015-12-08 22.35.28

I won’t bore you with details about how Instagram handles OAUTH, but you might be interested in how Google Cloud Vision wants to talk to an app like this? I built this quick and dirty as it should be, in PHP.

function fetch_tags_for_image($path){
$tags = [];
$payload = '{"requests": [{"image": {"content": "'.base64_encode(file_get_contents($path)).'"}, "features": [{"type": "LABEL_DETECTION", "maxResults": "10"}]}]}';
file_put_contents('payload.json', $payload);
$cmd = 'curl -k -s -H "Content-Type: application/json" https://vision.googleapis.com/v1alpha1/images:annotate?key=GOOGLEAPIKEY --data-binary @payload.json';
exec($cmd, $result);
if($json = @json_decode(join($result, "\n"))){
foreach($json->responses[0]->labelAnnotations as $tag){
$tags[] = $tag->description;
}
}
return $tags;
}

I would love to have the energy to build a WordPress plugin that adds a taxonomy to the media library, and automatically tags all uploaded images using Google Cloud Vision, but I don’t. Someone else will, and they write better code than I do anyway.

/Peder

Note: I can’t post the URL to the actual game, yet. The alpha’s terms and conditions prohibits me from deploying to production.