為了在 Google Vision API 的高階影像理解功能中從註釋回應物件中提取地標資訊來偵測地標,我們需要利用 API 提供的相關欄位和方法。註釋回應物件是一個 JSON 結構,包含與影像分析結果相關的各種屬性和值。
首先,我們需要確保 API 已成功處理圖像,並且回應物件包含必要的資訊。這可以透過檢查響應對象的“狀態”欄位來完成。如果狀態為“OK”,則表示影像分析成功,我們可以繼續擷取地標資訊。
可以從回應物件的「landmarkAnnotations」欄位存取地標資訊。此欄位是註釋數組,其中每個註釋代表影像中偵測到的地標。每個地標註釋包含多個屬性,包括位置、描述和分數。
“位置”屬性提供偵測到的地標的邊界框座標。這些座標指定圖像中地標的位置和大小。透過分析這些座標,我們可以確定地標的準確位置。
「描述」屬性提供了地標的文字描述。此描述可用於識別地標並向使用者提供附加上下文。例如,如果 API 偵測到圖像中的艾菲爾鐵塔,則表示屬性可能包含文字「艾菲爾鐵塔」。
「score」屬性表示 API 在偵測地標時的置信度分數。此分數是 0 到 1 之間的值,其中分數越高表示置信度越高。透過分析這個分數,我們可以評估偵測到的地標的可靠性。
要從註釋回應物件中提取地標訊息,我們可以迭代「landmarkAnnotations」陣列並存取每個註釋的相關屬性。然後,我們可以根據需要儲存或處理這些信息,以進行進一步分析或顯示。
以下是 Python 中的範例程式碼片段,示範如何使用 Google Cloud Vision API 用戶端程式庫從註釋回應物件中提取地標資訊:
python from google.cloud import vision def extract_landmark_info(response): if response.status == 'OK': for annotation in response.landmark_annotations: location = annotation.location description = annotation.description score = annotation.score # Process the landmark information as needed print(f"Landmark: {description}") print(f"Location: {location}") print(f"Score: {score}n") else: print('Image analysis failed.') # Assuming you have already authenticated and created a client client = vision.ImageAnnotatorClient() # Assuming you have an image file 'image.jpg' to analyze with open('image.jpg', 'rb') as image_file: content = image_file.read() image = vision.Image(content=content) response = client.landmark_detection(image=image) extract_landmark_info(response)
在此範例中,「extract_landmark_info」函數將註釋回應物件作為輸入,並迭代「landmark_annotations」陣列。然後,它提取並列印每個註釋的地標訊息,包括描述、位置和分數。
透過遵循這種方法,我們可以有效地從 Google Vision API 的高級圖像理解功能提供的註釋響應對像中提取地標信息,以檢測地標。
最近的其他問題和解答 高級圖像理解:
- Google Vision API 中的物件辨識有哪些預先定義類別?
- 將安全搜尋偵測功能與其他審核技術結合使用的建議方法是什麼?
- 我們如何存取並顯示安全搜尋註釋中每個類別的似然值?
- 我們如何在Python中使用Google Vision API獲得安全的搜尋註解?
- 安全搜尋偵測功能包含哪五個類別?
- Google Vision API 的安全搜尋功能如何偵測影像中的露骨內容?
- 我們如何使用枕頭庫直觀地識別並突出顯示圖像中檢測到的對象?
- 我們如何使用 pandas 資料框以表格格式組織提取的物件資訊?
- 我們如何從 API 回應中提取所有物件註解?
- 使用哪些函式庫和程式語言來示範 Google Vision API 的功能?