加载中

Claude的视觉能力让您可以在消息中包含图像,并要求Claude以复杂的方式分析这些图像。您可以要求Claude描述图像内容、比较多张图像、计数物体,或执行复杂的视觉分析任务。

图像处理基础

一张幻灯片,展示下方所列图像限制和令牌计算公式的旧版本,旁边是下方同样展示的base64图像代码块。

在Claude中处理图像时,您需要了解几个关键限制:

  • 单个请求中所有消息最多可包含100张图像
  • 每张图像最大10MB(按base64编码后的大小计算)
  • 发送单张图像时:最大高度/宽度为8000px
  • 发送多张图像时:最大高度/宽度为2000px
  • 图像只能以base64编码方式包含
  • 每张图像根据尺寸计算令牌数: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的视觉能力很强大,但它们需要与任何复杂任务一样的精心提示工程。应投入时间创建详细、结构化的提示,而不是依赖简单的问题。