載入中
載入中

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

圖像處理基礎

一張投影片,顯示較舊版本的圖像限制與權杖公式,旁邊是下方展示的圖像區塊與文字區塊程式碼。

處理圖像時,有幾個重要限制需要牢記:

  • 單一請求中所有訊息最多可包含 600 張圖像(對於上下文視窗為 200k 權杖的模型則為 100 張)
  • 每張圖像最大 10MB(以 base64 編碼後測量)
  • 傳送單張圖像時:最大高度/寬度為 8000px
  • 傳送多張圖像時:最大高度/寬度為 2000px
  • 圖像可以以 base64 編碼或圖像 URL 的形式包含
  • 每張圖像依其尺寸計算權杖數:tokens = ⌈width / 28⌉ × ⌈height / 28⌉

若要將圖像傳送給 Claude,您需要在使用者訊息中加入一個圖像區塊,並搭配文字區塊。以下是其結構:

python
import base64

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,Claude 則以包含其分析結果的文字區塊作為回應。

提示技巧

一張標記「圖像中有 12 顆彈珠」並附有計數問題的照片,以及 Claude 回答 13 顆彈珠,旁邊標示紅色 X 表示錯誤。

要從圖像中獲得良好結果,關鍵在於運用與文字相同的提示工程技巧。簡單的提示往往會導致不佳的結果。例如,詢問「這張圖像中有多少顆彈珠?」可能會得到不正確的計數結果。

您可以透過以下方式大幅提升 Claude 的準確度:

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

逐步分析

搭配彈珠照片與下方的方法論提示,Claude 回覆「我在這張圖像中數到 12 顆彈珠」,旁邊標示綠色勾號。

與其提出簡單的問題,不如為 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?

單樣本範例

提示說明一張彈珠照片有 11 顆彈珠,接著詢問第二張照片;Claude 回答 12 顆,旁邊標示綠色勾號。

您也可以透過在訊息中提供範例來提升準確度。先附上一張已知數量的圖像,說明正確答案,再詢問您的目標圖像。這能為 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 the home
   - 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 vulnerability points
   - 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 進行系統性分析,相較於簡單的請求,能產生更準確且更實用的評估結果。

請記住:適用於文字的提示技巧同樣適用於圖像。如果您想獲得可靠的結果,請投入時間打造詳細且結構化的提示,而不要依賴簡單的問題。