Python: 使用 Google Cloud Vision API 的完整教學(含憑證設定); pip install google-cloud-vision; from google.cloud import vision

加入好友
加入社群
Python: 使用 Google Cloud Vision API 的完整教學(含憑證設定); pip install google-cloud-vision; from google.cloud import vision - 儲蓄保險王

Google Cloud Vision API 是一個功能強大的影像分析服務,可以用來做 文字辨識 (OCR)物件偵測標籤分類臉部偵測 等。
這篇文章會帶你從 下載憑證檔 開始,到用 Python 成功呼叫 Vision API。


🔑 一、建立憑證檔(Service Account Key)

  1. 登入 Google Cloud Console
  2. 建立一個專案(如果還沒有的話)
  3. 啟用 Vision API:
    • 前往 API 與服務 > 程式庫
    • 搜尋 Vision API → 點選啟用
  4. 建立 Service Account:
    • 前往 IAM 與管理 > 服務帳戶
    • 建立新服務帳號,給它一個名稱(例如 vision-sa
    • 權限選擇 專案 > 編輯者(測試用,正式環境應給最小權限)
  5. 建立金鑰:
    • 點選剛剛的服務帳號 → 金鑰 > 新增金鑰 > JSON
    • 下載下來的檔案就是 憑證檔(例如 vision-key.json

⚠️ 請務必保護好這個檔案,不要上傳到 GitHub 或公開分享。

前往 IAM 與管理 > 服務帳戶

Python: 使用 Google Cloud Vision API 的完整教學(含憑證設定); pip install google-cloud-vision; from google.cloud import vision - 儲蓄保險王

建立服務帳戶後:

Python: 使用 Google Cloud Vision API 的完整教學(含憑證設定); pip install google-cloud-vision; from google.cloud import vision - 儲蓄保險王

🐍 二、安裝 Python SDK

pip install google-cloud-vision

📂 三、設定憑證來源

有兩種方式可以使用憑證檔:

方式一:用環境變數指定檔案路徑(建議)

import os
from google.cloud import vision

# 指定憑證檔路徑
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/absolute/path/to/vision-key.json"

client = vision.ImageAnnotatorClient()

方式二:直接在程式中指定金鑰檔案

from google.cloud import vision

client = vision.ImageAnnotatorClient.from_service_account_file("vision-key.json")

方式三:將檔案讀取為dict,再使用dict的內容:

from google.cloud import vision
import json

service_account_info = json.loads("你的json金鑰字串")
client = vision.ImageAnnotatorClient.from_service_account_info(service_account_info)

service_account_info:

Python: 使用 Google Cloud Vision API 的完整教學(含憑證設定); pip install google-cloud-vision; from google.cloud import vision - 儲蓄保險王

vision.ImageAnnotatorClient.from_service_account_info

import os
import json
# pip install google-cloud-vision
from google.cloud import vision

dir_api = r"D:\user\Python\GPT\json"
basename_api = "gen-lang-client.json"
path_api = os.path.join(dir_api, basename_api)

dirname = r"D:\Temp"
basename = "sample.png"
path = os.path.join(dirname, basename)

with open(path_api, "r", encoding="utf-8") as f:
    service_account_info = json.load(f)

# client = vision.ImageAnnotatorClient(credentials=service_account_info)

# 使用 from_service_account_info 方法
client = vision.ImageAnnotatorClient.from_service_account_info(service_account_info)
# 讀取圖片
with open(path, "rb") as image_file:
    content = image_file.read()

image = vision.Image(content=content)

# 呼叫 Vision API OCR
response = client.text_detection(image=image)
texts = response.text_annotations

print("=== OCR 結果 ===")
for text in texts:
    print(text.description)

輸出:

Python: 使用 Google Cloud Vision API 的完整教學(含憑證設定); pip install google-cloud-vision; from google.cloud import vision - 儲蓄保險王

from google.oauth2 import service_account
建立正確的認證物件
credentials = service_account.Credentials.from_service_account_file(path_api)

from google.oauth2 import service_account
from google.cloud import vision
import os
import json

# 讀取服務帳號金鑰檔案
dir_api = r"D:\user\Python\GPT\json"
basename_api = "gen-lang-client.json"
path_api = os.path.join(dir_api, basename_api)

# 圖片路徑
dirname = r"D:\Temp"
basename = "sample.png"
path_image = os.path.join(dirname, basename)

# 建立正確的認證物件
credentials = service_account.Credentials.from_service_account_file(path_api)

# 或者如果您已經載入了JSON:
# with open(path_api, "r", encoding="utf-8") as f:
#     service_account_info = json.load(f)
# credentials = service_account.Credentials.from_service_account_info(service_account_info)

# 使用認證創建客戶端
client = vision.ImageAnnotatorClient(credentials=credentials)

# 讀取圖片
with open(path_image, "rb") as image_file:
    content = image_file.read()

image = vision.Image(content=content)

# 呼叫 Vision API OCR
response = client.text_detection(image=image)
texts = response.text_annotations

print("=== OCR 結果 ===")
if texts:
    print(texts[0].description)  # 完整文本
    print("\n--- 詳細文字項目 ---")
    for text in texts[1:]:  # 個別單詞/區塊
        print(f"文字: '{text.description}'")
else:
    print("未檢測到文字")

輸出:

Python: 使用 Google Cloud Vision API 的完整教學(含憑證設定); pip install google-cloud-vision; from google.cloud import vision - 儲蓄保險王
Python: 使用 Google Cloud Vision API 的完整教學(含憑證設定); pip install google-cloud-vision; from google.cloud import vision - 儲蓄保險王

📝 四、範例程式:OCR 文字辨識

假設我們有一張圖片 sample.jpg,想要辨識裡面的文字:

from google.cloud import vision

client = vision.ImageAnnotatorClient.from_service_account_file("vision-key.json")

# 讀取圖片
with open("sample.jpg", "rb") as image_file:
    content = image_file.read()

image = vision.Image(content=content)

# 呼叫 Vision API OCR
response = client.text_detection(image=image)
texts = response.text_annotations

print("=== OCR 結果 ===")
for text in texts:
    print(text.description)

# 錯誤處理
if response.error.message:
    raise Exception(f"Vision API Error: {response.error.message}")

執行後,你會看到圖片中的文字被完整輸出 🎉


📌 五、其他 Vision API 功能

  • 標籤分類(幫圖片加標籤):
response = client.label_detection(image=image)
for label in response.label_annotations:
    print(label.description, label.score)

臉部偵測

response = client.face_detection(image=image)
print("臉部數量:", len(response.face_annotations))

Logo 偵測

response = client.logo_detection(image=image)
for logo in response.logo_annotations:
    print(logo.description)

🎯 六、總結

  1. 在 GCP 建立專案 → 啟用 Vision API
  2. 建立 Service Account → 下載 JSON 憑證檔
  3. 安裝 google-cloud-vision
  4. 用 Python 載入憑證 → 呼叫 API

只要跑通這幾步,你就能開始玩轉各種 Vision API 的影像分析功能 🚀

推薦hahow線上學習python: https://igrape.net/30afN

加入好友
加入社群
Python: 使用 Google Cloud Vision API 的完整教學(含憑證設定); pip install google-cloud-vision; from google.cloud import vision - 儲蓄保險王

儲蓄保險王

儲蓄險是板主最喜愛的儲蓄工具,最喜愛的投資理財工具則是ETF,最喜愛的省錢工具則是信用卡

You may also like...

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *