[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Successor to CentOS] AlmaLinux OS server construction/migration service

[Successor to CentOS] AlmaLinux OS server construction/migration service

[For WordPress only] Cloud server “Web Speed”

[For WordPress only] Cloud server “Web Speed”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Reservation system development] EDISONE customization development service

[Reservation system development] EDISONE customization development service

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[Global exclusive service] Beyond's MSP in North America and China

[Global exclusive service] Beyond's MSP in North America and China

[YouTube] Beyond official channel “Biyomaru Channel”

[YouTube] Beyond official channel “Biyomaru Channel”

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 an image detection API that uses similar machine learning.
This time, I have summarized the preparations for playing with this Vision API.

Requires Google Account

All you need to use GCP is a Google account and a credit card.
GCP's API has a free tier, and you can play with it if you just want to try it out, but there are some that require credit card registration.

The Vision API is exactly that type of API, and if a credit card is not registered, a dialog will appear saying "Payment required".
When you are told that you need to pay, you may feel reluctant to press the button, but it is okay because you will be asked for confirmation before you are charged beyond the free limit!
This psychological barrier is quite large.

If you want to enable billing, proceed to the billing account creation page.
If you register as a new member, you will receive a $300 credit that you can use for 60 days from registration.
Additionally, there is a free tier for using the Vision API, so as long as you don't use it recklessly, you can enjoy it for free.

 

Enabling Vision API on GCP

Create a suitable project and open API Manager from the hamburger on the top left.

Click the "Enable API" link next to the dashboard title

Click "Vision API" in the list of "Google Cloud Machine Learning" in the library

Click "Enable" next to the Google Cloud Vision API title to enable the API.

 

Get API key

Once the API is enabled, create an API key to access the API.

Select "Credentials" from the left side menu and press the "Create Credentials" button

Select "API Key" from the pull-down menu.

A dialog will appear displaying the API key, so copy and paste it and save it locally.

At the time of creation, anyone can use the API key by adding it to the request parameter, so use the "Restrict key" link to restrict the use of the API key.
There are four ways to restrict keys.

 

HTTP referrer

This type restricts API usage using referrer information embedded in the HTTP header.
This method is effective when used with a browser, etc.

 

IP address

Restrictions by IP address (IPv4/IPv6) are used when executing batch processing etc. on the server.
You can register multiple IP addresses so that you can access from multiple servers, and you can also specify a range using a subnet mask.

 

Android app

If you want to restrict usage from an android app, obtain the fingerprint of the SHA-1 signature certificate in the development environment and register it along with the package name.

 

iOS app

If you want to restrict usage from iOS apps, you need to register a bundle ID.

Even if you are only using it for testing purposes, it is better to put restrictions on it, but if you absolutely have to, you can create an API key as many times as you like, so we strongly recommend that you delete it immediately when you are done using it. .

Now you are ready to use the API.

 

test code

Now that the preparations are complete, I would like to test executing the API from PHP.

The basic information is all listed in the Google Cloud Vision API Documentation | Google Cloud Vision API | Google Cloud Platform If you want to implement it with PHP, you can either install the Google API Client Libraries from Getting Started | API Client Library for PHP (Beta) | Google Developers

This time, I'm going to write it thoroughly using curl.

Vision API is an image recognition API, so it is necessary to include image information in the request data.

The following image information can be used.

  • Local image data
  • Image data placed on Google Cloud Storage

As a side note, traffic is a bit of a concern when sequentially analyzing local image data.
When implementing a full-scale production environment, it may be a good idea to pay attention to bandwidth usage.

In this case, you need to pay attention to server storage, so you may 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, etc., you will need to make some changes.

Using the Vision API using an API key is very easy, so feel free to 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";

If you specify the absolute path of the local image file in $path and enter the API key in "YOUR_SECRET_API_KEY", it will be analyzed.
(Operation confirmed in my PHP5.6 environment)

One problem I encountered was that I forgot to include the Content-Type in the header, but with Microsoft's Motion API without it, so I had a hard time noticing.
It's tempting to say that you can understand it by looking at the request, but as expected from Google, the specifications around this are very strict...

Well, did you receive the response successfully?
When playing on a console, depending on the image, the screen doesn't flow very fast?

This API is easy to analyze, but organizing the response data is the most demanding API.

Too much data is returned!

Looking at the response data seems like a pain, so I would like to do it after reorganizing.

That's it.

If you found this article helpful , please give it a like!
0
Loading...
0 votes, average: 0.00 / 10
1,136
X facebook Hatena Bookmark pocket
[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

The person who wrote this article

About the author

Yoichi Bandai

My main job is developing web APIs for social games, but I'm also fortunate to be able to do a lot of other work, including marketing.
Furthermore, my portrait rights in Beyond are treated as CC0 by him.