Skip to content

Instantly share code, notes, and snippets.

@e96031413
Created February 24, 2020 02:35
Show Gist options
  • Save e96031413/300c91efe5671f8f29c0771116cc303d to your computer and use it in GitHub Desktop.
Save e96031413/300c91efe5671f8f29c0771116cc303d to your computer and use it in GitHub Desktop.
Google Cloud Vision API Example
import io
import os
# Imports the Google Cloud client library
from google.cloud import vision
from google.cloud.vision import types
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]=r"yourPath.json"
# Instantiates a client
client = vision.ImageAnnotatorClient()
# The name of the image file to annotate
file_name = os.path.abspath('test.jpg')
# Loads the image into memory
with io.open(file_name, 'rb') as image_file:
content = image_file.read()
image = types.Image(content=content)
# Performs label detection on the image file
response = client.label_detection(image=image)
labels = response.label_annotations
print('Labels:')
for label in labels:
print(label.description)
# Performs web detection on the image file
response = client.web_detection(image=image,max_results=3)
target = response.web_detection.web_entities
for item in target:
print('description:',item.description)
print('score:',item.score)
print('\n')
# Performs objects detection on the image file
response = client.object_localization(image=image)
target = response.localized_object_annotations
print(target)
for item in target:
print(item.name)
print(item.score)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment