相較於先前的程式庫,Python 用戶端程式庫 v0.25.1 在設計上有一些重大的變更,以下是摘要說明:
將模組合併成較少的類型
將不具類型的參數取代為強類型的類別與列舉
這個主題詳細說明您需要對 Cloud Vision API 用戶端程式庫的 Python 程式碼進行哪些變更,才能使用 v0.25.1 Python 用戶端程式庫。
執行舊版用戶端程式庫
您不需要將 Python 用戶端程式庫升級至 v0.25.1。如果您要繼續使用舊版 Python 用戶端程式庫,且不想遷移程式碼,請指定應用程式使用的 Python 用戶端程式庫版本。如要指定特定的程式庫版本,請編輯 requirements.txt
檔案,如以下所示:
google-cloud-vision==0.25
移除的模組
下列模組已從 Python 用戶端程式庫 v0.25.1 套件中移除。
google.cloud.vision.annotations
google.cloud.vision.batch
google.cloud.vision.client
google.cloud.vision.color
google.cloud.vision.crop_hint
google.cloud.vision.entity
google.cloud.vision.face
google.cloud.vision.feature
google.cloud.vision.geometry
google.cloud.vision.image
google.cloud.vision.likelihood
google.cloud.vision.safe_search
google.cloud.vision.text
google.cloud.vision.web
必要程式碼變更
匯入
包含新的 google.cloud.vision.types
模組,以存取 Python Client Library v0.25.1 中的新類型。
types
模組包含建立要求時所需的新類別,例如 types.Image
。
此外,新的 google.cloud.vision.enums
模組包含適合用於剖析與理解 API 回應的列舉,例如 enums.Likelihood.UNLIKELY
和 enums.FaceAnnotation.Landmark.Type.LEFT_EYE
。
建立用戶端
Client
類別已由 ImageAnnotatorClient
類別取代。將 Client
類別的參照替換為 ImageAnnotatorClient
。
舊版用戶端程式庫:
old_client = vision.Client()
Python 用戶端程式庫 v0.25.1:
建構代表圖片內容的物件
如要識別本機檔案、Google Cloud Storage URI 或網頁 URI 中的圖片內容,請使用新的 Image
類別。
建構表示本機檔案中圖片內容的物件
以下範例顯示表示本機檔案中圖片內容的新方式。
舊版用戶端程式庫:
with io.open(file_name, 'rb') as image_file:
content = image_file.read()
image = old_client.image(content=content)
Python 用戶端程式庫 v0.25.1:
建構表示 URI 中圖片內容的物件
以下範例顯示表示 Google Cloud Storage URI 或網路 URI 中圖片內容的新方式。uri
是 Google Cloud Storage 或網路上圖片檔案的 URI。
舊版用戶端程式庫:
image = old_client.image(source_uri=uri)
Python 用戶端程式庫 v0.25.1:
建立要求並處理回應
在 Python 用戶端程式庫 v.0.25.1 中,face_detection
等 API 方法屬於 ImageAnnotatorClient
物件,而非 Image
物件。
不同方法傳回的值不同,詳情請參閱下文。
特別是,邊界框架頂點現在會儲存在 bounding_poly.vertices
中,而非 bounds.vertices
。每個頂點的座標都儲存在 vertex.x
和 vertex.y
中,而非 vertex.x_coordinate
和 vertex.y_coordinate
。
邊界框變更會影響 face_detection
、logo_detection
、text_detection
、document_text_detection
和 crop_hints
。
提出臉部偵測要求並處理回應
情緒可能性現在會以列舉形式傳回,並儲存在 face.surprise_likelihood
中,而非 face.emotions.surprise
。匯入 google.cloud.vision.enums.Likelihood
即可復原可能性標籤的名稱。
舊版用戶端程式庫:
with io.open(file_name, 'rb') as image_file:
content = image_file.read()
image = old_client.image(content=content)
faces = image.detect_faces()
for face in faces:
print('anger: {}'.format(face.emotions.anger))
print('joy: {}'.format(face.emotions.joy))
print('surprise: {}'.format(face.emotions.surprise))
vertices = (['({},{})'.format(bound.x_coordinate, bound.y_coordinate)
for bound in face.bounds.vertices])
print('face bounds: {}'.format(','.join(vertices)))
Python 用戶端程式庫 v0.25.1:
發出標籤偵測要求並處理回應
舊版用戶端程式庫:
with io.open(file_name, 'rb') as image_file:
content = image_file.read()
image = old_client.image(content=content)
labels = image.detect_labels()
for label in labels:
print(label.description)
Python 用戶端程式庫 v0.25.1:
發出地標偵測要求並處理回應
舊版用戶端程式庫:
地標位置的經緯度現在會儲存在 location.lat_lng.latitude
和 location.lat_lng.longitude
中,而非 location.latitude
和 location.longitude
。
with io.open(file_name, 'rb') as image_file:
content = image_file.read()
image = old_client.image(content=content)
landmarks = image.detect_landmarks()
for landmark in landmarks:
print(landmark.description, landmark.score)
for location in landmark.locations:
print('Latitude'.format(location.latitude))
print('Longitude'.format(location.longitude))
Python 用戶端程式庫 v0.25.1:
提出標誌偵測要求並處理回應
舊版用戶端程式庫:
with io.open(file_name, 'rb') as image_file:
content = image_file.read()
image = old_client.image(content=content)
logos = image.detect_logos()
for logo in logos:
print(logo.description, logo.score)
Python 用戶端程式庫 v0.25.1:
提出 SafeSearch 偵測要求並處理回應
安全搜尋機率現在會以列舉方式傳回。匯入 google.cloud.vision.enums.Likelihood
即可復原可能性標籤的名稱。
舊版用戶端程式庫:
with io.open(file_name, 'rb') as image_file:
content = image_file.read()
image = old_client.image(content=content)
safe = image.detect_safe_search()
print('Safe search:')
print('adult: {}'.format(safe.adult))
print('medical: {}'.format(safe.medical))
print('spoofed: {}'.format(safe.spoof))
print('violence: {}'.format(safe.violence))
Python 用戶端程式庫 v0.25.1:
提出文字偵測要求並處理回應
舊版用戶端程式庫:
with io.open(file_name, 'rb') as image_file:
content = image_file.read()
image = old_client.image(content=content)
texts = image.detect_text()
for text in texts:
print('\n"{}"'.format(text.description))
vertices = (['({},{})'.format(bound.x_coordinate, bound.y_coordinate)
for bound in text.bounds.vertices])
print('bounds: {}'.format(','.join(vertices)))
Python 用戶端程式庫 v0.25.1:
提出文件文字偵測要求並處理回應
舊版用戶端程式庫:
with io.open(file_name, 'rb') as image_file:
content = image_file.read()
image = old_client.image(content=content)
document = image.detect_full_text()
for page in document.pages:
for block in page.blocks:
block_words = []
for paragraph in block.paragraphs:
block_words.extend(paragraph.words)
block_symbols = []
for word in block_words:
block_symbols.extend(word.symbols)
block_text = ''
for symbol in block_symbols:
block_text = block_text + symbol.text
print('Block Content: {}'.format(block_text))
print('Block Bounds:\n {}'.format(block.bounding_box))
Python 用戶端程式庫 v0.25.1:
提出圖片屬性要求並處理回應
主色資訊現在會儲存在 props.dominant_colors.colors
中,而非 props.colors
。
舊版用戶端程式庫:
with io.open(file_name, 'rb') as image_file:
content = image_file.read()
image = old_client.image(content=content)
props = image.detect_properties()
for color in props.colors:
print('fraction: {}'.format(color.pixel_fraction))
print('\tr: {}'.format(color.color.red))
print('\tg: {}'.format(color.color.green))
print('\tb: {}'.format(color.color.blue))
print('\ta: {}'.format(color.color.alpha))
Python 用戶端程式庫 v0.25.1:
提出網路偵測要求並處理回應
舊版用戶端程式庫:
with io.open(file_name, 'rb') as image_file:
content = image_file.read()
image = old_client.image(content=content)
notes = image.detect_web()
if notes.pages_with_matching_images:
print('\n{} Pages with matching images retrieved')
for page in notes.pages_with_matching_images:
print('Score : {}'.format(page.score))
print('Url : {}'.format(page.url))
if notes.full_matching_images:
print ('\n{} Full Matches found: '.format(
len(notes.full_matching_images)))
for image in notes.full_matching_images:
print('Score: {}'.format(image.score))
print('Url : {}'.format(image.url))
if notes.partial_matching_images:
print ('\n{} Partial Matches found: '.format(
len(notes.partial_matching_images)))
for image in notes.partial_matching_images:
print('Score: {}'.format(image.score))
print('Url : {}'.format(image.url))
if notes.web_entities:
print ('\n{} Web entities found: '.format(len(notes.web_entities)))
for entity in notes.web_entities:
print('Score : {}'.format(entity.score))
print('Description: {}'.format(entity.description))
Python 用戶端程式庫 v0.25.1:
提出裁剪提示要求並處理回應
舊版用戶端程式庫:
with io.open(file_name, 'rb') as image_file:
content = image_file.read()
image = old_client.image(content=content)
hints = image.detect_crop_hints(aspect_ratios=[1.77])
for n, hint in enumerate(hints):
print('\nCrop Hint: {}'.format(n))
vertices = (['({},{})'.format(bound.x_coordinate, bound.y_coordinate)
for bound in hint.bounds.vertices])
print('bounds: {}'.format(','.join(vertices)))
Python 用戶端程式庫 v0.25.1:
請注意,顯示比例必須透過 CropHintsParams
和 ImageContext
傳入。