Google Cloud使用筆記(五):使用 Google Vision API進行影像辨識

Due to the frequently version update of Google Cloud, I recommend you can refer to the official documentation to make sure you can set up quickly.

Step 1

Setting up the environment

pip install --upgrade google-cloud-storage

Installation instructions

#Download and unzip:google-cloud-sdk-281.0.0-windows-x86_64-bundled-python.zipgoogle-cloud-sdk/install.bat

initialize the SDK

./google-cloud-sdk/bin/gcloud init

Step 2

Setting up the API

Step 3

Try to Use API with Python

import io
import os
# Imports the Google Cloud client library
from google.cloud import vision
from google.cloud.vision import types
# Importantance:set your json file in this part, I try to follow official guide but it didn't work, use below instead.T
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]=r"yourAPI.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(output the possible label without score)
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(I guess that the method just like google image search but capture the text from related site as output)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(This method can output the annotation info, including bounding box location)response = client.object_localization(image=image)
target = response.localized_object_annotations
for item in target:
print(item.name)
print(item.score)

For more API usage, please refer to the official docs from Google:

補充資料:

主要功能:

web_detection API傳回的資料:

fullMatchingImages:其他網站有出現的一模一樣的圖

pagesWithMatchingImages:一樣的圖,其出現的網址

visuallySimilarImages:類似的圖

bestGuessLabels:API預測這張的最符合的標籤

關於score:

官方Example:

這個程式可以進行圖片內容辨識,並提供score

Written by

Machine Learning / Deep Learning / Python / Flutter cakeresume.com/yanwei-liu

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store