Tutorial: Google Vision API with Python and Heroku | by Evan Fang | Towards Data Science

As we learned before, Google Vision AI could be divided into two parts, AutoML Vision and Vision API. For the Vision API reference, here is the previous post talking about what could Vision API does, how to create an API key, and then query it with curl.
towardsdatascience.com
Today, we are going to learn how to use it with Python. Then we could use it in our projects in the future.
Before we get started, I would like to share the official tutorial with you.
codelabs.developers.google.com
There is a quick tutorial in the following paragraph, but if you want to know more detail after reading it, you still can learn it from the Google Codelabs. Great, now let’s begin.
Establish a Vision API project.Enable the Vision API.Authenticate API requests and download the keyFile.json.Set GOOGLE_APPLICATION_CREDENTIALS with keyFile.json.Install the Google Client Vision API client library.Write Python code to query the Vision API.Push the code to Heroku.
Establish a Vision API project.
Enable the Vision API.
Authenticate API requests and download the keyFile.json.
Set GOOGLE_APPLICATION_CREDENTIALS with keyFile.json.
Install the Google Client Vision API client library.
Write Python code to query the Vision API.
Push the code to Heroku.
Sign-in to Google Cloud Platform Console and create a new project.
Sign-in to Google Cloud Platform Console and create a new project.
2. Name the project and click the CREATE button.
3. Click Active Cloud Shell.
4. Then a console shell will appear at the bottom.Run $ gcloud auth list to confirm that you are authenticated.And also run $ gcloud config list project to confirm the project id.
Run command line $ gcloud services enable vision.googleapis.com
According to the Google Codelabs tutorial, we need to create a Service Account and a key for accessing the Vision API.
In order to make requests to the Vision API, you need to use a Service Account. A Service Account is an account, belonging to your project, that is used by the Google Client Python library to make Vision API requests.
Click the link below and follow the Setting up authentication GCP CONSOLE steps.
cloud.google.com
GO TO THE CREATE SERVICE ACCOUNT KEY PAGEFrom the Service account list, select New service account.In the Service account name field, enter a name.Don’t select a value from the Role list. No role is required to access this service.
GO TO THE CREATE SERVICE ACCOUNT KEY PAGE
From the Service account list, select New service account.
In the Service account name field, enter a name.
Don’t select a value from the Role list. No role is required to access this service.
If all the settings are correctly set up, the UI would be like this:
5. Click Create. A note appears, warning that this service account has no role.
6. Click Create without role. A JSON file that contains your key downloads to your computer.
Rename the JSON file as keyFile.json. Then set GOOGLE_APPLICATION_CREDENTIALS with the file.
export GOOGLE_APPLICATION_CREDENTIALS=~/Desktop/keyFile.json
Run pip freeze | grep google-cloud-vision to check if the library is already installed.
Run pip freeze | grep google-cloud-vision to check if the library is already installed.
2. If you’re not setting the environment, please run pip install --upgrade google-cloud-vision.
Copy and save the following code as a Python file.
Run the Python file to test if all the environment settings are correctly set up.
/usr/local/opt/python/bin/python3.7 [path/to/your/filename.py]
We upload two superstar’s photos and use Face Detection in Vision API to get their face bounds and emotion.
The input data:
Here is the output:
=========================================================File: {pic}Face surprised: VERY_UNLIKELYFace bounds: (207,54),(632,54),(632,547),(207,547)=========================================================File: {pic}Face surprised: LIKELYFace bounds: (16,38),(323,38),(323,394),(16,394)
Congratulations! It means we have queried the Vision API successfully. Try other Vision APIs from the official tutorials.
cloud.google.com
Note: As the suggestion from Mandar Vaze, we should not upload our personal keyFile.json to the public space. If you have concern about the security issue, please refer to this post for the solution.
If you still don’t know much about Heroku, please read the previous post first.
medium.com
In Step 6, the project is running well locally, but we need to build it as an online service for apps or web to use.
Here is the code sample, please download it and learn how to push it online in the following steps.
Here is the code sample, please download it and learn how to push it online in the following steps.
https://github.com/mutant0113/Google_Vision_API_sample/blob/master/medium-google-vision-api.zip
2. Add your own Google Service Account keyFile.json to folder medium-google-vision-api/config/
3. Create a new Heroku project.
heroku loginheroku create [your/project/name] --buildpack heroku/python
4. Link the project and push code to the master branch.
cd [path/to/your/local/project/download/from/step1]heroku git:remote -a [your/project/name]git push heroku master
5. Set GOOGLE_APPLICATION_CREDENTIALS with keyFile.json on Heroku.
heroku config:set GOOGLE_APPLICATION_CREDENTIALS='config/keyFile.json'
6. Ensure that at least one instance of the app is running.
heroku ps:scale web=1
7. Open Heroku website
heroku open
If everything is set properly, you should see the website with content like the image.
8. You can edit the code in hello/views.py and hello/vision_api.py
That’s all for today. We can now start to write code using Google Vision API. Hope this post helps you to know more about the Google Vision API.
If you have any questions or suggestions, welcome to leave a comment under this article. See you.
Cloud Vision API pricing
cloud.google.com
Our Heroku code is based on the official sample code.
devcenter.heroku.com
Another tutorial for Node.js version.