Gemini Robotics ER 模型可以编写和执行 Python 代码,以便在回答之前处理图片和应用逻辑。本页介绍了代码执行示例:使用缩放和裁剪进行对象检测、仪表读数、流体测量、电路板读数和图片注解。
如需根据自己的用例调整这些示例,请将提示文本和上传的图片文件替换为您自己的内容。您还可以调整提示中请求的 JSON 架构,以匹配应用所需的输出结构,或添加 system_instruction 来强制执行输出格式和精度。
如需查看完整的可运行代码,请参阅 机器人技术 Cookbook。
思考等级
您可以控制思考等级,以在延迟时间和准确率之间进行权衡。对于对象检测等空间任务,较低的思考等级效果良好。对于计数或重量估计等复杂任务,较高的思考等级效果更好。
以下示例将复杂计数任务的思考等级设置为 high:
Python
from google import genai
from google.genai import types
client = genai.Client()
with open('scene.jpeg', 'rb') as f:
image_bytes = f.read()
response = client.models.generate_content(
model="gemini-robotics-er-2-preview",
contents=[
types.Part.from_bytes(
data=image_bytes,
mime_type='image/jpeg',
),
"Identify and count all objects on the table."
],
config=types.GenerateContentConfig(
thinking_config=types.ThinkingConfig(thinking_level="high")
)
)
print(response.text)
如需了解详情,请参阅思考。
对象检测(缩放和裁剪)
以下示例演示了如何在检测对象和返回边界框时使用代码执行来缩放和裁剪图片,以便更清晰地查看。
Python
from google import genai
from google.genai import types
client = genai.Client()
# Load your image
with open('sorting.jpeg', 'rb') as f:
image_bytes = f.read()
prompt = """
Return JSON in the format {label: val, y: val, x: val, y2: val, x2: val} for
the compostable objects in this scene. Please Zoom and crop the image for a
clearer view. Return an annotated image of the final result with the bounding
boxes drawn on it to the API caller as a part of your process.
"""
response = client.models.generate_content(
model="gemini-robotics-er-2-preview",
contents=[
types.Part.from_bytes(
data=image_bytes,
mime_type='image/jpeg',
),
prompt
],
config = types.GenerateContentConfig(
tools=[types.Tool(code_execution=types.ToolCodeExecution)],
)
)
print(response.text)
模型输出类似于以下 JSON 响应:
[
{"label": "compostable", "y": 256, "x": 482, "y2": 295, "x2": 546},
{"label": "compostable", "y": 317, "x": 478, "y2": 350, "x2": 542},
{"label": "compostable", "y": 586, "x": 556, "y2": 668, "x2": 595},
{"label": "compostable", "y": 463, "x": 669, "y2": 511, "x2": 718},
{"label": "compostable", "y": 178, "x": 565, "y2": 250, "x2": 609}
]
下图显示了模型返回的框。
读取模拟仪表并应用逻辑
以下示例演示了如何使用模型读取模拟仪表并执行时间计算。它使用系统指令来强制执行 JSON 输出。
Python
from google import genai
from google.genai import types
client = genai.Client()
with open('gauge.jpeg', 'rb') as f:
image_bytes = f.read()
system_instruction = "Be precise. When JSON is requested, reply with ONLY that JSON (no preface, no code block)."
response = client.models.generate_content(
model="gemini-robotics-er-2-preview",
contents=[
types.Part.from_bytes(
data=image_bytes,
mime_type='image/jpeg',
),
"""Read the current value from this gauge. Then, calculate how long
it will take at the current rate for the value to reach maximum.
Reply in JSON: {"current_value": val, "max_value": val,
"time_to_max_minutes": val}"""
],
config = types.GenerateContentConfig(
system_instruction=system_instruction,
tools=[types.Tool(code_execution=types.ToolCodeExecution)],
)
)
print(response.text)
测量容器中的流体
以下示例演示了如何使用代码执行来测量容器中的流体液位。
Python
from google import genai
from google.genai import types
client = genai.Client()
with open('fluid.jpeg', 'rb') as f:
image_bytes = f.read()
system_instruction = "Be precise. When JSON is requested, reply with ONLY that JSON (no preface, no code block)."
response = client.models.generate_content(
model="gemini-robotics-er-2-preview",
contents=[
types.Part.from_bytes(
data=image_bytes,
mime_type='image/jpeg',
),
"""Measure the amount of fluid in the container. Reply in JSON:
{"fluid_level_ml": val, "container_capacity_ml": val,
"percentage_full": val}"""
],
config = types.GenerateContentConfig(
system_instruction=system_instruction,
tools=[types.Tool(code_execution=types.ToolCodeExecution)],
)
)
print(response.text)
读取电路板上的标记
以下示例演示了如何使用代码执行来读取电路板上的标记。
Python
from google import genai
from google.genai import types
client = genai.Client()
with open('circuit_board.jpeg', 'rb') as f:
image_bytes = f.read()
system_instruction = "Be precise. When JSON is requested, reply with ONLY that JSON (no preface, no code block)."
response = client.models.generate_content(
model="gemini-robotics-er-2-preview",
contents=[
types.Part.from_bytes(
data=image_bytes,
mime_type='image/jpeg',
),
"""Read all visible component labels and markings on this circuit
board. Reply in JSON: {"components": [{"label": val,
"location": [y, x]}]}"""
],
config = types.GenerateContentConfig(
system_instruction=system_instruction,
tools=[types.Tool(code_execution=types.ToolCodeExecution)],
)
)
print(response.text)
图片注解
以下示例演示了如何使用代码执行来注解图片(例如,绘制箭头以提供处置说明)并返回修改后的图片。
Python
from google import genai
from google.genai import types
client = genai.Client()
# Load your image
with open('sorting.jpeg', 'rb') as f:
image_bytes = f.read()
prompt = """
Look at this image and return it as an annotated version using arrows of
different colors to represent which items should go in which bins for
disposal. You must return the final image to the API caller.
"""
response = client.models.generate_content(
model="gemini-robotics-er-2-preview",
contents=[
types.Part.from_bytes(
data=image_bytes,
mime_type='image/jpeg',
),
prompt
],
config = types.GenerateContentConfig(
tools=[types.Tool(code_execution=types.ToolCodeExecution)],
)
)
print(response.text)
以下是输入图片示例。
模型输出类似于以下内容:
The annotated image shows the suggested disposal locations for the items on the table:
- **Green bin (Compost/Organic)**: Green chili, red chili, grapes, and cherries.
- **Blue bin (Recycling)**: Yellow crushed can and plastic container.
- **Black bin (Trash)**: Chocolate bar wrapper, Welch's packet, and white tissue.
后续步骤
- 任务编排 - 使用自定义机器人 API 的长时程任务。
- 使用流式传输的机器人技术 - 实时双向流式传输(仅限 Gemini Robotics ER 2)。
- 视频理解 - 查找时刻和进度分类(仅限 Gemini Robotics ER 2)。