SAM2 / SAM3 不是问答模型,而是 Promptable Concept Segmentation(PCS) 模型——用自然语言描述一个”概念”,模型会找出图中所有匹配的实例,每个都生成一个 mask。
Prompt 写法原则
SAM3 理解的是名词短语(concept),不理解逻辑指令:
✅ 有效
question
exam question
question block
text region
❌ 无效
all questions
detect all questions
find every question on this page
写”具体描述词”,让模型自己找所有实例。
常用场景 Prompt
试卷 / 文档题目识别
# 基础
text_prompt = "question"
# 更精准
text_prompt = "exam question"
text_prompt = "question block"
text_prompt = "problem statement"
负例过滤(排除干扰区域)
positive_prompt = "exam question text region"
negative_prompt = "header, title, footer, logo, watermark"
交通标志识别
text_prompt = "traffic sign"
text_prompt = "road sign"
物体实例分割
text_prompt = "car"
text_prompt = "pedestrian"
text_prompt = "building window"
Python 调用示例(SAM2 + Grounding DINO 方案)
目前实现 text-prompted 分割的常见管线是 Grounding DINO + SAM2(SAM 官方的 text prompt 能力仍在完善中):
from groundingdino.util.inference import load_model, load_image, predict
from segment_anything import SAMPredictor
# Grounding DINO 定位
boxes, logits, phrases = predict(
model=gd_model,
image=image,
caption="exam question block",
box_threshold=0.35,
text_threshold=0.25
)
# SAM2 分割
predictor.set_image(image)
masks, scores, _ = predictor.predict(
point_coords=None,
point_labels=None,
box=boxes, # 把 DINO 的 bbox 传给 SAM
multimask_output=False
)
只做区域切割 + OCR 的方案
SAM 负责”切块”,OCR 负责”读内容”:
# Step 1: SAM 找到所有"题目区域"的 mask
masks = sam_segment(image, text_prompt="question block")
# Step 2: 按 mask 裁剪图片区域
for i, mask in enumerate(masks):
bbox = mask_to_bbox(mask)
region = image[bbox[1]:bbox[3], bbox[0]:bbox[2]]
# Step 3: OCR 识别文字
text = paddleocr.ocr(region)
print(f"题目 {i}: {text}")
OCR 推荐:PaddleOCR(中文强)、TrOCR(表格/印刷体强)、Tesseract(通用)。
SAM 的能力边界
| 能力 | SAM 支持 |
|---|---|
| 找出所有”题目”区域 | ✅ |
| 生成 mask/bbox | ✅ |
| 理解”这是第几题” | ❌ |
| 读出题目内容 | ❌(需要 OCR) |
| 判断题目难度 | ❌(需要 LLM) |
SAM 只做像素级分割,不做语义理解。完整的文档结构化提取需要 SAM + OCR + LLM 组合。
一句话总结
SAM3 文本 prompt 用具体名词短语,它会自动找出所有匹配的实例。复杂场景可以加 negative prompt 排除干扰,或者组合 Grounding DINO + SAM2 + OCR 构建完整的图像理解管线。
