Use Google Cloud Vision API from Python

Purpose

As a memorandum because I forget how to do it a little.

Things necessary

This time, I will use this for the image I want to analyze. fujisan.png

manner

Issuance of service account key

First, issue a service account key at here.

This time, I will download it in JSON format. This file gives you access to resources in the cloud, so keep it tightly managed.

Python code

#Various imports
import io 
import os
from google.protobuf.json_format import MessageToJson
import json
from google.cloud import vision
from google.cloud.vision import types

#This work directory
base_dir = r'path\to\directory'

#File name of the JSON file from earlier
credential_path = base_dir + r'File name of the JSON file from earlier.json'

#Pass the path to the service account key
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = credential_path

#vision client initialization
client = vision.ImageAnnotatorClient()

#File name of the target image
file_name = base_dir + r"\fujisan.png "

#Load image
with io.open(file_name, 'rb') as image_file:
    content = image_file.read()
image = types.Image(content=content)

#See here for the actual method name
#https://googleapis.dev/python/vision/latest/gapic/v1p4beta1/api.html

#For example, for label detection
response = client.label_detection(image=image)

#View results
print(response)

result

label_annotations {
  mid: "/m/015kp7"
  description: "Stratovolcano"
  score: 0.7824147939682007
  topicality: 0.7824147939682007
}
label_annotations {
  mid: "/m/07j7r"
  description: "Tree"
  score: 0.6869218349456787
  topicality: 0.6869218349456787
}
label_annotations {
  mid: "/g/11jwzh3_l"
  description: "Volcanic landform"
  score: 0.5413353443145752
  topicality: 0.5413353443145752
}

It's called "Volcanic Landform"!

Also try safe_search

# ~Abbreviation~
response = client.safe_search_detection(image=image)
print(response)

It's a really safe image!

safe_search_annotation {
  adult: VERY_UNLIKELY
  spoof: VERY_UNLIKELY
  medical: VERY_UNLIKELY
  violence: VERY_UNLIKELY
  racy: VERY_UNLIKELY
}

that's all!

Recommended Posts

Use Google Cloud Vision API from Python
Use Google Analytics API from Python
Google Cloud Vision API sample for python
Use e-Stat API from Python
Forcibly use Google Translate from python
Use kabu StationĀ® API from Python
Use the Flickr API from Python
How to use GCP's Cloud Vision API
Use thingsspeak from python
Use fluentd from python
Use MySQL from Python
Use MySQL from Python
Use BigQuery from python.
Use mecab-ipadic-neologd from python
Firebase: Use Cloud Firestore and Cloud Storage from Python
Text extraction with GCP Cloud Vision API (Python3.6)
I tried using the Google Cloud Vision API
[Google Cloud Platform] Use Google Cloud API using API Client Library
I tried to extract characters from subtitles (OpenCV: Google Cloud Vision API)
Detect Japanese characters from images using Google's Cloud Vision API in Python
[GCP] [Python] Deploy API serverless with Google Cloud Functions!
Use Trello API with python
Use Twitter API with Python
Use MySQL from Anaconda (python)
From python to running instance on google cloud platform
Speech transcription procedure using Python and Google Cloud Speech API
Use subsonic API with python3
Google Drive Api Tips (Python)
Easy to use Nifty Cloud API with botocore and python
Try to determine food photos using Google Cloud Vision API
Get data from analytics API with Google API Client for python
I tried the Google Cloud Vision API for the first time
Let's use Watson from Python! --How to use Developer Cloud Python SDK
Python: Extract file information from shared drive with Google Drive API
Use Stanford Core NLP from Python
[Python] Hit the Google Translation API
[Python3] Google translate google translate without using api
Read and use Python files from Python
Using Cloud Storage from Python3 (Introduction)
How to use OpenPose's Python API
Use Azure Blob Storage from Python
Get upcoming weather from python weather api
Run Ansible from Python using API
Use Cloud Datastore from Compute Engine
Use fastText trained model from Python
[Python] How to use Typetalk API
Handle SOAP API from Python (Zeep)
Until you try the Google Cloud Vision API (harmful image detection)
How to connect to Cloud Firestore from Google Cloud Functions with python code
Let's touch Google's Vision API from Python for the time being
Collecting information from Twitter with Python (Twitter API)
Google Cloud Speech API vs. Amazon Transcribe
[Python] Web application from 0! Hands-on (3) -API implementation-
Run Cloud Dataflow (Python) from App Engine
Streaming speech recognition with Google Cloud Speech API
How to update Google Sheets from Python
I want to use jar from python
Get Google Fit API data in Python
Try using Python with Google Cloud Functions
Speech transcription procedure using Google Cloud Speech API
I tried using UnityCloudBuild API from Python