Google Gemini 2.5 零样本检测与分割实战:从 JSON 结果到可视化

Google Gemini 2.5 零样本检测与分割实战:从 JSON 结果到可视化


这篇教程是我根据近期学习 Gemini 2.5 多模态能力、零样本目标检测和实例分割的复现过程整理出来的。重点演示如何用 Gemini 2.5 Flash 直接根据提示词返回检测框或分割 mask,并通过 supervision 解析和可视化结果。

Gemini 2.5 的优势是不用训练就能通过自然语言描述视觉任务。本教程保留模型 prompt 和 API 参数,适合用来理解 VLM 输出 JSON、再把 JSON 转成可视化检测结果的完整链路。

本文会重点跑通以下流程:

  • 配置 GOOGLE_API_KEY 并初始化 Gemini API 客户端
  • 用 prompt 控制模型输出检测框或分割 mask
  • supervision 解析 VLM JSON 并可视化
  • 保留英文类别名和 JSON 字段,避免影响模型输出格式

如果你正在系统学习计算机视觉、多模态模型或 Roboflow 项目复现,建议收藏本文;配套 notebook、示例图片和运行环境说明后续会继续整理。如果环境配置卡住,可以在评论区说明具体报错。


⚙️ 环境准备

先配置 Google API Key、安装依赖,并下载两张示例图片。

import os
from google.colab import userdata

os.environ["GOOGLE_API_KEY"] = userdata.get("GOOGLE_API_KEY")
!pip install google-genai supervision
!wget -q https://media.roboflow.com/notebooks/examples/dog-2.jpeg
!wget -q https://media.roboflow.com/notebooks/examples/dog-3.jpeg

🔌 初始化 Gemini API 客户端

这里创建 Gemini 客户端,并设置安全策略。

from google import genai
from google.genai import types

client = genai.Client(api_key=os.environ["GOOGLE_API_KEY"])

safety_settings = [
    types.SafetySetting(
        category="HARM_CATEGORY_DANGEROUS_CONTENT",
        threshold="BLOCK_ONLY_HIGH",
    ),
]

🎯 目标检测 Prompt

先让 Gemini 根据提示词检测狗尾巴,并要求模型返回包含 box_2dlabel 的 JSON。

MODEL_NAME = "gemini-2.5-flash-preview-05-20"
TEMPERATURE = 0.5

IMAGE_PATH = "/content/dog-3.jpeg"
PROMPT = "Detect dogs tail. " + \
"Output a JSON list of bounding boxes where each entry contains the 2D bounding box in the key \"box_2d\", " + \
"and the text label in the key \"label\". Use descriptive labels."
from PIL import Image

image = Image.open(IMAGE_PATH)
width, height = image.size
target_height = int(1024 * height / width)
resized_image = image.resize((1024, target_height), Image.Resampling.LANCZOS)

response = client.models.generate_content(
    model=MODEL_NAME,
    contents=[resized_image, PROMPT],
    config = types.GenerateContentConfig(
        temperature=TEMPERATURE,
        safety_settings=safety_settings,
        thinking_config=types.ThinkingConfig(
          thinking_budget=0
        )
    )
)

response.text

🖼️ 解析检测结果并可视化

使用 supervision.Detections.from_vlm 将 Gemini 返回的 JSON 转换为标准检测结果。

import supervision as sv

resolution_wh = image.size

detections = sv.Detections.from_vlm(
    vlm=sv.VLM.GOOGLE_GEMINI_2_5,
    result=response.text,
    resolution_wh=resolution_wh
)

thickness = sv.calculate_optimal_line_thickness(resolution_wh=resolution_wh)
text_scale = sv.calculate_optimal_text_scale(resolution_wh=resolution_wh)

box_annotator = sv.BoxAnnotator(thickness=thickness)
label_annotator = sv.LabelAnnotator(
    smart_position=True,
    text_color=sv.Color.BLACK,
    text_scale=text_scale,
    text_position=sv.Position.CENTER
)

annotated = image
for annotator in (box_annotator, label_annotator):
    annotated = annotator.annotate(scene=annotated, detections=detections)

sv.plot_image(annotated)

在这里插入图片描述

🧩 实例分割 Prompt

下面让 Gemini 返回 pepper 和 salt 的分割 mask。prompt 中的类别和 JSON 字段会影响输出,保持英文原样。

MODEL_NAME = "gemini-2.5-flash-preview-05-20"
TEMPERATURE = 0.5

IMAGE_PATH = "/content/dog-2.jpeg"
PROMPT = "Give the segmentation masks peper, salt. " + \
"Output a JSON list of segmentation masks where each entry contains the 2D bounding box in the key \"box_2d\", " + \
"the segmentation mask in key \"mask\", and the text label in the key \"label\". Use descriptive labels."
from PIL import Image

image = Image.open(IMAGE_PATH)
width, height = image.size
target_height = int(1024 * height / width)
resized_image = image.resize((1024, target_height), Image.Resampling.LANCZOS)

response = client.models.generate_content(
    model=MODEL_NAME,
    contents=[resized_image, PROMPT],
    config = types.GenerateContentConfig(
        temperature=TEMPERATURE,
        safety_settings=safety_settings,
        thinking_config=types.ThinkingConfig(
          thinking_budget=0
        )
    )
)

response.text

🎨 解析分割结果并可视化

实例分割结果会额外包含 mask,这里使用 MaskAnnotator 叠加到原图。

import supervision as sv

resolution_wh = image.size

detections = sv.Detections.from_vlm(
    vlm=sv.VLM.GOOGLE_GEMINI_2_5,
    result=response.text,
    resolution_wh=resolution_wh
)

thickness = sv.calculate_optimal_line_thickness(resolution_wh=resolution_wh)
text_scale = sv.calculate_optimal_text_scale(resolution_wh=resolution_wh)

box_annotator = sv.BoxAnnotator(thickness=thickness)
label_annotator = sv.LabelAnnotator(
    smart_position=True,
    text_color=sv.Color.BLACK,
    text_scale=text_scale,
    text_position=sv.Position.CENTER
)
masks_annotator = sv.MaskAnnotator()

annotated = image
for annotator in (box_annotator, label_annotator, masks_annotator):
    annotated = annotator.annotate(scene=annotated, detections=detections)

sv.plot_image(annotated)

在这里插入图片描述


📌 小结

这篇教程完整整理了 Zero-Shot Object Detection and Segmentation with Google Gemini 2.5 的核心复现流程。实际复现时,建议先确认 API Key、GPU、依赖版本和数据集路径,再逐段运行 notebook。

  • 配置 GOOGLE_API_KEY 并初始化 Gemini API 客户端
  • 用 prompt 控制模型输出检测框或分割 mask
  • supervision 解析 VLM JSON 并可视化
  • 保留英文类别名和 JSON 字段,避免影响模型输出格式

后续我会继续按源项目顺序整理 Roboflow Notebooks 中的目标检测、实例分割、OCR、多目标跟踪和视觉大模型教程。

📚 同系列教程汇总

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值