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
Installing the Cloud Client Libraries for Python
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
###Make sure you have enabled the Cloud Vision API###
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) (舊版寫法)
image = vision.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_annotationsprint('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_entitiesfor 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:
補充資料:
主要功能:
label detection
可以告訴你這圖片是什麼web detection
在網路上搜尋相似圖片,並從這些搜尋結果中提取內容,回傳圖片更多相關資訊。OCR (Optical Character Recognition)
解析你圖片中的文字內容,告訴你圖片中哪裡有文字,甚至可以告訴你這是什麼語言。Logo detection
專門辨識公司logo用的Face detection
指出臉部的喜怒哀樂、特徵、位置、是否有戴眼鏡、是否模糊等。Landmark detection
如果這圖片包含著常見的地標,他可以告訴你是什麼地標。 同時提供對應地標的經度、緯度Crop hints
可以幫助你裁剪照片,以符合你想要做的主題。Explicit Content Detection
檢測圖片中是否有不適當的內容。 adult(成人)、spoof(詐騙)、medical(藥物)、violence(暴力)、racy(性刺激)landmark
地標偵測,圖片中的地點在哪裡
web_detection API傳回的資料:
webEntities:有類似圖樣內容的Entity清單
fullMatchingImages:其他網站有出現的一模一樣的圖
pagesWithMatchingImages:一樣的圖,其出現的網址
visuallySimilarImages:類似的圖
bestGuessLabels:API預測這張的最符合的標籤
關於score:
目前在網路上所查到的資訊,score有點像是信心分數,指的是有多少%的信心,認為該圖片中的內容是OOO的可能
官方Example:
這個程式可以進行圖片內容辨識,並提供score