読み込み中

画像サポート

レッスン 4410 分
読み込み中

Claudeのビジョン機能を使えば、メッセージに画像を含めて、Claudeに高度な方法で分析させることができます。画像の内容を説明させたり、複数の画像を比較させたり、オブジェクトの数を数えさせたり、複雑な視覚分析タスクを実行させたりすることが可能です。

画像処理の基本

以下に示す画像制限とトークン計算式の古いバージョンが記載されたスライド。その横にはbase64画像ブロックのコードも示されている。

Claudeで画像を扱う際には、いくつかの重要な制限事項を理解しておく必要があります。

  • 1回のリクエスト内の全メッセージを通じて最大100枚の画像
  • 画像1枚あたりの最大サイズは10MB(base64エンコード後の値)
  • 画像を1枚送信する場合: 最大高さ/幅は8000px
  • 複数の画像を送信する場合: 最大高さ/幅は2000px
  • 画像はbase64エンコードでのみ含めることができる
  • 各画像は寸法に基づいてトークンとしてカウントされる: tokens = ⌈width / 28⌉ × ⌈height / 28⌉

画像を含めるには、テキストブロックと並べて画像ブロックをユーザーメッセージに追加します。構造は以下の通りです。

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の返答は「I see a tall tree」で始まる。

会話はテキストのみのやり取りと全く同じように機能します。サーバーは画像ブロックとテキストブロックの両方を含むユーザーメッセージをClaudeに送信し、Claudeは画像を分析したテキストメッセージで応答します。

プロンプティング技法

12個のビー玉の写真とカウントに関する質問を含むユーザーメッセージ、その後Claudeの返答は13個のビー玉(赤い×印付き)。

Claudeのビジョン機能について理解すべき最も重要な点は、優れたプロンプティング技法が絶対に不可欠だということです。単純なプロンプトでは、たとえ明瞭な画像であっても、しばしば不十分な結果しか得られません。

例えば、12個のビー玉が写っている画像に対して「この画像には何個のビー玉がありますか?」と尋ねると、13個という誤ったカウントが返ってくることがあります。テキストに対して使用するのと同じプロンプトエンジニアリング技法を適用することで、精度を劇的に改善できます。

  • 詳細なガイドラインと分析手順を提供する
  • ワンショットまたはマルチショットの例を使用する
  • 複雑なタスクをより小さなステップに分解する

ステップバイステップ分析

ビー玉の写真とステップバイステップのカウントプロンプトが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」と返答する。

1つのメッセージ内に複数の画像とテキストのペアを含めることで、ワンショットプロンプティングを使用することもできます。

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

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

例を提供することで、対象画像に対するClaudeの精度が大幅に向上します。

実世界の例: 火災リスク評価

密集した木々に囲まれた家の注釈付き衛星写真。以下に挙げる3つのリスク要因が矢印で示されている。

これは実践的な応用例です: 住宅保険のための火災リスク評価を自動化します。保険会社はしばしば、山火事のリスクを軽減するために、住宅所有者に物件周辺の木を剪定するよう求めます。各物件に検査員を派遣する代わりに、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のビジョン機能は強力であるものの、あらゆる複雑なタスクに必要とされるのと同じ入念なプロンプトエンジニアリングが求められるということです。単純な質問に頼るのではなく、詳細で構造化されたプロンプトの作成に時間を投資してください。