載入中

Claude 的視覺能力讓您可以在訊息中包含圖像,並要求 Claude 以複雜的方式分析這些圖像。您可以要求 Claude 描述圖像內容、比較多張圖像、計算物件數量,或執行複雜的視覺分析任務。

圖像處理基礎

一張投影片,顯示下方所列圖像限制與 token 計算公式的舊版本,旁邊是同樣顯示於下方的 base64 圖像區塊程式碼。

在 Claude 中處理圖像時,您需要了解幾個關鍵限制:

  • 單一請求中所有訊息最多可包含 100 張圖像
  • 每張圖像最大為 10MB(以 base64 編碼後的大小計算)
  • 傳送單張圖像時:最大高度/寬度為 8000px
  • 傳送多張圖像時:最大高度/寬度為 2000px
  • 圖像只能以 base64 編碼方式包含
  • 每張圖像依尺寸計算 token 數:tokens = ⌈width / 28⌉ × ⌈height / 28⌉

若要包含圖像,您需要在使用者訊息中加入圖像區塊,與文字區塊並列。以下是其結構:

python
with open("image.png", "rb") as f:
    image_bytes = base64.standard_b64encode(
        f.read()
    ).decode("utf-8")

add_user_message(messages, [
    # 圖片區塊
    {
        "type": "image",
        "source": {
            "type": "base64",
            "media_type": "image/png",
            "data": image_bytes,
        }
    },
    # 文字區塊
    {
        "type": "text",
        "text": "What do you see in this image?"
    }
])

訊息流程

使用者訊息包含原始圖像資料的圖像區塊與文字區塊傳送給 Claude,Claude 的回覆開頭為「I see a tall tree」。

對話方式與純文字互動相同。您的伺服器會傳送包含圖像與文字區塊的使用者訊息給 Claude,而 Claude 會以分析該圖像的文字訊息回應。

提示技巧

使用者訊息包含一張有 12 顆彈珠的照片與計數問題,Claude 的回覆為 13 顆彈珠,並以紅色 X 標記。

關於 Claude 視覺能力,最重要的一點是:良好的提示技巧絕對至關重要。即使圖像清晰,簡單的提示往往會產生不佳的結果。

舉例來說,若詢問「這張圖像中有多少顆彈珠?」,而圖像中實際有 12 顆彈珠,可能會得到錯誤的計數結果 13 顆。您可以透過套用與文字提示相同的提示工程技巧,大幅提升準確度:

  • 提供詳細的指引與分析步驟
  • 使用單次(one-shot)或多次(multi-shot)範例
  • 將複雜任務拆解為較小的步驟

逐步分析

一張彈珠照片與逐步計數提示傳送給 Claude,Claude 的回覆「I count 12 marbles in this image」正確無誤。

與其提出簡單的問題,不如為 Claude 提供一套方法論:

Analyze this image of marbles and determine the exact count using this methodology:
1. Begin by identifying each unique marble one at a time. Assign each a number as you identify it.
2. Verify your result by counting with a different method. Start from the bottom-left corner and work row by row, from left to right.

What is the exact, verified number of marbles in this image?

這種結構化的方法有助於 Claude 得出正確的 12 顆彈珠計數結果。

單次範例

一張標示為 11 顆彈珠的範例照片,接著是目標照片;Claude 正確回覆「I count 12 marbles in this image」。

您也可以在單一訊息中包含多組圖像與文字配對,使用單次提示法:

[Image of 11 marbles]
The image above has 11 marbles in it.

[Image of 12 marbles]  
How many marbles are in this image?

提供範例能顯著提升 Claude 在目標圖像上的準確度。

實際案例:火災風險評估

一棟被茂密樹木環繞的房屋的標註衛星照片,箭頭標示出下方列出的三項風險特徵。

以下是一個實際應用:為住宅保險自動化火災風險評估。保險公司通常要求屋主修剪房屋周圍的樹木,以降低野火風險。您可以使用衛星影像搭配 Claude,而不必派遣檢查員前往每個物業。

該系統分析衛星影像以辨識:

  • 住宅附近密集、緊密排列的樹木
  • 緊急服務難以進入的通道
  • 懸垂於住宅上方的樹枝

與其使用像「提供火災風險評分」這樣簡單的提示,不如建立一套詳細的分析框架:

Analyze the attached satellite image of a property with these specific steps:

1. Residence identification: Locate the primary residence on the property by looking for:
   - The largest roofed structure
   - Typical residential features (driveway connection, regular geometry)
   - Distinction from other structures (garages, sheds, pools)

2. Tree overhang analysis: Examine all trees near the primary residence:
   - Identify any trees whose canopy extends directly over any portion of the roof
   - Estimate the percentage of roof covered by overhanging branches (0-25%, 25-50%, 50-75%, 75%+)
   - Note particularly dense areas of overhang

3. Fire risk assessment: For any overhanging trees, evaluate:
   - Potential wildfire vulnerability (ember catch points, continuous fuel paths to structure)
   - Proximity to chimneys, vents, or other roof openings if visible
   - Areas where branches create a "bridge" between wildland vegetation and the structure

4. Defensible space identification: Assess the property's overall vegetative structure:
   - Identify if trees connect to form a continuous canopy over or near the home
   - Note any obvious fuel ladders (vegetation that can carry fire from ground to tree to roof)

5. Fire risk rating: Based on your analysis, assign a Fire Risk Rating from 1-4:
   - Rating 1 (Low Risk): No tree branches overhanging the roof, good defensible space around
   - Rating 2 (Moderate Risk): Minimal overhang (<25% of roof), some separation between tree canopies
   - Rating 3 (High Risk): Significant overhang (25-50% of roof), connected tree canopies, multiple points of vulnerability
   - Rating 4 (Severe Risk): Extensive overhang (>50% of roof), dense vegetation against structure

For each item above (1-5), write one sentence summarizing your findings, with your final response being the numerical rating.

這個全面的提示引導 Claude 進行系統化分析,從而得出準確且可行動的火災風險評估結果。在測試一處樹木覆蓋嚴重的物業時,Claude 正確地將其判定為「3(高風險)」,原因是建築物周圍有顯著的樹木懸垂與相連的樹冠。

關鍵要點是:Claude 的視覺能力雖然強大,但仍需要與處理任何複雜任務時相同的謹慎提示工程。請投入時間建立詳細、結構化的提示,而不要僅依賴簡單的問題。