Try using GCP's Vision API

Hello.
I'm Mandai, in charge of Wild on the development team.
I previously played around with Microsoft's Emotion API, but Google Cloud Platform (GCP) also has a similar image detection API that uses machine learning.
This time, I've put together the preparations for playing around with the Vision API.
A Google Account is required
To use GCP, you need a Google account and a credit card.
GCP's API has a free tier, which is enough for just getting started, but there are some features that require you to register a credit card.
The Vision API is exactly that type of API, and if you don't have a registered credit card, a dialog box will appear saying "Charge required."
When you're told you need to pay, it can be hard to bring yourself to press the button, but don't worry, you'll be asked for confirmation before you're charged beyond the free limit!
This psychological barrier is quite large.
To enable billing, go to the billing account creation page.
If you sign up as a new user, you will receive a $300 credit that can be used for 60 days after registration.
There is also a free tier for using the Vision API itself, so as long as you don't overuse it, you can enjoy it for free.
Enable the Vision API on GCP
Create a suitable project and open the API Manager from the hamburger in the top left
Click the "Enable API" link next to the dashboard title
Click "Vision API" in the "Google Cloud Machine Learning" list in the library
Click "Enable" next to the Google Cloud Vision API title to enable the API
Obtaining an API key
Once you have enabled the API, you will need to create an API key to access the API
Select "Credentials" from the left side menu and click the "Create Credentials" button
Select "API Key" from the drop-down menu
A dialog will appear showing the API key, so copy and paste it and save it locally
When you create an API key, anyone can use it by adding it to the request parameters, so you can restrict its use by clicking the "Restrict Key" link.
There are four ways to restrict the key.
HTTP Referrer
This type restricts API usage using referrer information embedded in the HTTP header.
This is an effective method when used in a browser.
IP address
Restrictions by IP address (IPv4/IPv6) are used when running batch processing on a server.
You can register multiple IP addresses to allow access from multiple servers, and you can also specify a range using a subnet mask.
Android app
To restrict usage from Android apps, obtain the SHA-1 signing certificate fingerprint in your development environment and register it along with the package name
iOS app
If you want to restrict usage from an iOS app, you will need to register a bundle ID
Even if you are only using it for testing purposes, it is better to impose restrictions, but if you absolutely must, you can create an API key as many times as you like, so we strongly recommend deleting it immediately after you have finished using it
Now you're ready to use the API
Test Code
Now that we're ready, let's test the API from PHP
the basic information is available in Google Cloud Vision API Documentation | Google Cloud Vision API | Google Cloud Platform Getting Started | API Client Library for PHP (Beta) | Google Developers , or do it brute force using curl or something similar.
This time, I'll take this opportunity to write it using curl
Since the Vision API is an image recognition API, you need to include image information in the request data
The following image information is available:
- Local image data
- Image data stored on Google Cloud Storage
As an aside, when analyzing local image data sequentially, the traffic can be a bit of a concern.
When using it in a full-scale production environment, you may want to pay attention to bandwidth usage.
In this case, you also need to be careful about server storage, so you might want to consider storing images in Google Cloud Storage.
Public Internet URIs are not accepted as external sources, so if you want to integrate with AWS S3 or similar, you'll need to do a little ingenuity.
Using the Vision API with an API key is very easy, so let's quickly post the code
<?php $path = '/path/to/image.jpg'; $url = 'https://vision.googleapis.com/v1/images:annotate?key='. 'YOUR_SECRET_API_KEY'; $body = []; $res = []; file_exists($path) or die("file not found. path:". $path); $image = base64_encode(file_get_contents($path)); $body = [ 'requests' => [ [ 'image' => ['content' => $image], 'features' => [ ['type' => 'LABEL_DETECTION'], ['type' => 'TEXT_DETECTION'], ['type' => 'FACE_DETECTION'], ['type' => 'LANDMARK_DETECTION'], ['type' => 'LOGO_DETECTION'], ['type' => 'SAFE_SEARCH_DETECTION'], ['type' => 'IMAGE_PROPERTIES'], ], ], ], ]; $ch = curl_init($url); curl_setopt_array($ch, [ CURLOPT_HTTPHEADER => [ 'Content-Type: application/json', ], CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => true, CURLOPT_VERBOSE => true, CURLOPT_POSTFIELDS => json_encode($body), ]); try { $response = curl_exec($ch); } catch (Exception $ex){ var_dump($ex->getMessage()); } $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); echo "-- header --\n". substr($response, 0, $header_size). "\n"; echo "-- body --\n". substr($response, $header_size). "\n";
Specify the absolute path of the local image file in $path and enter your API key in the "YOUR_SECRET_API_KEY" section to analyze it.
(Tested in my PHP 5.6 environment)
One of the biggest problems I had was forgetting to include the Content-Type header, which with Microsoft's Motion API it worked without it.
I'm tempted to say that you can tell just by looking at the request, but Google is really strict when it comes to specifications in this area...
Well, did you receive a response?
When you played it on the console, did the image change at a rapid pace?
This API is easy to analyze, but it is the most difficult API to organize the response data
There's too much data returned!
Looking at the response data seems like it would be a lot of work, so I'll try again later
That's it.
0





