As a memorandum because I forget how to do it a little.
This time, I will use this for the image I want to analyze.
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.
#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)
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"!
# ~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