harbor run -t xiaoboai/02-code-intelligence-task-1-sam3-inference
harbor download xiaoboai/02-code-intelligence-task-1-sam3-inference
你是一名 AI 编程专家。在工作目录下有一个 SAM3(Segment Anything Model 3)的简化代码库(sam3/ 目录),但没有详细文档、README 或示例 Notebook。你需要通过阅读源代码,理解 SAM3 的使用方法,然后编写推理脚本完成以下 4 个目标检测用例。
sam3/
请编写一个 Python 推理脚本,在测试图像上运行以下 4 个用例,并将结果保存到 output/predictions.json。
output/predictions.json
sam3/assets/images/test_image.jpg
阅读 sam3/__init__.py、sam3/model/sam3_image.py、sam3/model/sam3_image_processor.py、sam3/model/box_ops.py 来了解 API。
sam3/__init__.py
sam3/model/sam3_image.py
sam3/model/sam3_image_processor.py
sam3/model/box_ops.py
用例 1: text_shoe — 使用文本提示 "shoe" 在图像中检测鞋子,置信度阈值 0.5
text_shoe
"shoe"
用例 2: single_box — 使用一个 bounding box (xywh):[480.0, 290.0, 110.0, 360.0],置信度阈值 0.5
single_box
[480.0, 290.0, 110.0, 360.0]
用例 3: multi_box — 使用两个 bounding box:[[480.0, 290.0, 110.0, 360.0], [370.0, 280.0, 115.0, 375.0]],对应正/负标签 [True, False],置信度阈值 0.5
multi_box
[[480.0, 290.0, 110.0, 360.0], [370.0, 280.0, 115.0, 375.0]]
[True, False]
用例 4: text_box_combined — 使用文本提示 "child" 和一个 bounding box:[480.0, 290.0, 110.0, 360.0],置信度阈值 0.5
text_box_combined
"child"
predictions.json:
predictions.json
{ "image": "test_image.jpg", "image_size": [width, height], "cases": { "text_shoe": {"boxes_xyxy": [[x1,y1,x2,y2], ...], "scores": [...]}, "single_box": {"boxes_xyxy": [[x1,y1,x2,y2], ...], "scores": [...]}, "multi_box": {"boxes_xyxy": [[x1,y1,x2,y2], ...], "scores": [...]}, "text_box_combined": {"boxes_xyxy": [[x1,y1,x2,y2], ...], "scores": [...]} } }
boxes_xyxy 是 [x_min, y_min, x_max, y_max] 格式(像素),scores 是对应的置信度分数。
boxes_xyxy
[x_min, y_min, x_max, y_max]
scores