2.Image support
此文档由 学习AI的1000天 翻译制作(抖音,B站,YouTube)
邮箱: szqshan@gmail.com | 微信: szqshan
网址: www.xueai.org
==================================================
Claude的视觉能力让您可以在消息中包含图像,并要求Claude以无数种方式分析这些图像。您可以要求Claude描述图像中的内容、比较多个图像、计算对象数量,或执行复杂的视觉分析任务。
图像处理基础

在处理图像时,需要记住几个重要的限制:
- 单次请求中所有消息最多可包含100张图像
- 每张图像最大尺寸为5MB
- 发送单张图像时:最大高度/宽度为8000px
- 发送多张图像时:最大高度/宽度为2000px
- 图像可以通过base64编码或图像URL的方式包含
- 每张图像根据其尺寸计算token数:token数 = (宽度px × 高度px) / 750
要向Claude发送图像,您需要在用户消息中与文本块一起包含图像块。结构如下:
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": "你在这张图片中看到了什么?"
}
])
Message Flow

对话的工作方式与纯文本交互完全相同。你的服务器向Claude发送包含图像和文本块的用户消息,Claude会用包含其分析结果的文本块进行回应。
提示技巧

获得良好图像处理结果的关键是应用与文本处理相同的提示工程技术。简单的提示往往会导致糟糕的结果。例如,询问"这张图片中有多少个弹珠?"可能会返回错误的计数。
你可以通过以下方式显著提高Claude的准确性:
- 提供详细的指导原则和分析步骤
- 使用单次示例或多次示例
- 将复杂任务分解为更小的步骤
逐步分析

不要只是提出一个简单的问题,而是为Claude提供一套方法论:
分析这张弹珠图像,并使用以下方法论确定准确的数量:
1. 首先逐个识别每颗独特的弹珠。在识别时为每颗弹珠分配一个编号。
2. 通过使用不同的方法来验证你的结果。从左下角开始,按行计数,从左到右进行。
这张图像中弹珠的准确、经过验证的数量是多少?
One-Shot Examples

你还可以通过在消息中提供示例来提高准确性。包含一张已知数量的图像,说明正确答案,然后询问你的目标图像。这为Claude提供了你想要的分析类型的参考点。
实际应用案例:火灾风险评估

这里有一个实际应用:为家庭保险自动化火灾风险评估。保险公司可以使用卫星图像和Claude的分析,而不是派遣检查员到每个物业。
该系统分析卫星图像以识别:
- 靠近住宅的密集、紧密排列的树木
- 紧急服务难以到达的通道
- 悬垂在住宅上方的树枝
与简单的提示词如"提供火灾风险评分"不同,结构良好的提示词将分析分解为具体步骤:
按照以下具体步骤分析所附的房产卫星图像:
1. 住宅识别:通过寻找以下特征在房产中定位主要住宅:
- The largest roofed structure
- Typical residential features (driveway connection, regular geometry)
- Distinction from other structures (garages, sheds, pools)
2. 树木悬垂分析:检查主要住宅附近的所有树木:
- 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. 火灾风险评估:对于任何悬垂的树木,评估:
- 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. 可防御空间识别:评估房产的整体植被结构:
- 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. 火灾风险等级:基于您的分析,分配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
对于上述每个项目(1-5),写一句话总结您的发现,最终回复为数字等级。
这个详细的提示词引导Claude进行系统性分析,相比简单的请求,能够产生更加准确和有用的评估结果。
请记住:适用于文本的提示词技巧同样适用于图像。如果你想要可靠的结果,请投入时间制作详细、结构化的提示词,而不是依赖简单的问题。
==================================================
此文档由 学习AI的1000天 翻译制作(抖音,B站,YouTube)
邮箱: szqshan@gmail.com | 微信: szqshan
网址: www.xueai.org