為了使用 Pillow 庫直觀地識別和突出顯示圖像中檢測到的對象,我們可以遵循逐步過程。 Pillow 函式庫是一個功能強大的 Python 影像庫,提供廣泛的影像處理功能。透過將 Pillow 庫的功能與 Google Vision API 的物件偵測功能結合,我們可以有效率地完成此任務。
以下是使用 Pillow 庫直觀地識別和突出顯示影像中偵測到的物件的步驟:
1. 安裝必要的庫:首先安裝所需的庫。使用指令“pip installpillow”安裝 Pillow。此外,您還需要設定 Google Vision API 並安裝適用於 Python 的 Google Cloud 用戶端程式庫。
2. 使用 Google Vision API 進行身份驗證:要使用 Google Vision API,您需要對您的應用程式進行身份驗證。依照 Google 提供的文件取得必要的憑證。
3. 載入並分析影像:使用Pillow庫載入要分析的影像。您可以使用“Image.open()”方法開啟映像檔。載入圖片後,將其轉換為與 Google Vision API 相容的格式,例如 JPEG 或 PNG。
4. 將影像傳送至 Google Vision API:使用適用於 Python 的 Google Cloud 用戶端庫將影像傳送至 Google Vision API 進行物件偵測。這可以透過使用圖像資料建立請求物件並呼叫適當的方法來完成,例如「image_annotator_client.object_localization().annotate_image()」。
5. 擷取物件偵測結果:從 Google Vision API 收到的回應中擷取物件偵測結果。回應將包含有關偵測到的物件的信息,例如它們的邊界框、標籤和置信度分數。
6. 在影像上繪製邊界框:使用 Pillow 庫在影像上偵測到的物件周圍繪製邊界框。您可以使用“ImageDraw.Draw()”方法建立繪圖對象,然後使用“draw.rectangle()”方法繪製邊界框。
7. 在影像上新增標籤和分數:為了增強視覺化效果,您可以為影像新增標籤和置信度分數。使用 Pillow 庫中的“draw.text()”方法將標籤和分數覆蓋在映像上。
8. 儲存並顯示已註解的影像:使用 Pillow 庫中的「Image.save()」方法儲存已註釋的影像。您可以選擇所需的格式,例如 JPEG 或 PNG。 (可選)使用“Image.show()”方法顯示帶註釋的圖像。
透過執行以下步驟,您可以使用 Pillow 庫直觀地識別並突出顯示影像中偵測到的物件。 Pillow 強大的影像處理能力與 Google Vision API 的物件偵測功能結合,可有效率且準確地分析影像。
示例:
python from PIL import Image, ImageDraw from google.cloud import vision # Load and analyze the image image_path = 'path/to/your/image.jpg' image = Image.open(image_path) image_data = image.tobytes() # Authenticate with the Google Vision API client = vision.ImageAnnotatorClient.from_service_account_json('path/to/your/credentials.json') # Send the image to the Google Vision API for object detection response = client.object_localization(image=vision.Image(content=image_data)) objects = response.localized_object_annotations # Draw bounding boxes on the image draw = ImageDraw.Draw(image) for obj in objects: bbox = obj.bounding_poly.normalized_vertices draw.rectangle([(bbox[0].x * image.width, bbox[0].y * image.height), (bbox[2].x * image.width, bbox[2].y * image.height)], outline='red', width=3) # Add labels and scores to the image label = obj.name score = obj.score draw.text((bbox[0].x * image.width, bbox[0].y * image.height - 15), f'{label} ({score:.2f})', fill='red') # Save and display the annotated image annotated_image_path = 'path/to/save/annotated_image.jpg' image.save(annotated_image_path) image.show()
在此範例中,我們首先使用 Pillow 庫載入並分析圖像。然後,我們使用 Google Vision API 進行身份驗證並發送圖像以進行物件檢測。我們檢索物件偵測結果並使用 Pillow 庫在影像上偵測到的物件周圍繪製邊界框。此外,我們還為圖像添加標籤和置信度分數。最後,我們保存並顯示帶有註釋的圖像。
最近的其他問題和解答 高級圖像理解:
- Google Vision API 中的物件辨識有哪些預先定義類別?
- 將安全搜尋偵測功能與其他審核技術結合使用的建議方法是什麼?
- 我們如何存取並顯示安全搜尋註釋中每個類別的似然值?
- 我們如何在Python中使用Google Vision API獲得安全的搜尋註解?
- 安全搜尋偵測功能包含哪五個類別?
- Google Vision API 的安全搜尋功能如何偵測影像中的露骨內容?
- 我們如何使用 pandas 資料框以表格格式組織提取的物件資訊?
- 我們如何從 API 回應中提取所有物件註解?
- 使用哪些函式庫和程式語言來示範 Google Vision API 的功能?
- Google Vision API 如何執行影像中的物件偵測和定位?