Chapter 27: Visual Perception for Action

"An agent becomes interesting at the exact moment perception changes what it dares to do next."

A Patient Embodied AI Agent
Big Picture

Visual Perception for Action turns perception into action-ready state. A robot that can name every object on a table can still knock over the cup if its visual system never answers the control question: where can I move next?

Remember This Chapter

The durable test is not whether a model looks impressive. The test is whether it improves a robot's next action while leaving a clear evidence trail for debugging.

Chapter Overview

Chapter 27 develops Visual Perception for Action as a working piece of the embodied AI stack. It connects visual or spatial evidence to state estimates, action choices, visual servoing loops, timing budgets, and failure labels.

The chapter follows the right-tool rhythm used across the book: build the mechanism once, then move to maintained tools such as OpenCV, PyTorch, Segment Anything, DINOv2.

Prerequisites

Readers should be comfortable with Python, tensors, coordinate frames, sensor noise, and the perception-action loop. Useful refreshers appear in Chapter 4, Chapter 8, and Chapter 13.

Chapter Roadmap

Tooling Note

This chapter uses the right-tool principle. The teaching baseline exposes units, frames, uncertainty, and logging. The shortcut stack uses maintained tools to handle optimized kernels, visualization, data formats, simulation hooks, and deployment interfaces.

Hands-On Lab: Build A Closed-Loop Visual Evidence Panel

Duration: about 90 minutesDifficulty: Intermediate

Objective

Build a small visual-perception audit that turns masks, depth, motion, and affordance scores into an action decision with a replayable failure label.

What You'll Practice

  • Designing a perception-to-action schema with frames, latency, and uncertainty.
  • Combining confidence, clearance, and affordance into an executable action score.
  • Testing perturbations such as occlusion, stale timestamps, and depth-scale error.
  • Writing a postmortem that separates visual failure from planning or control failure.

Setup

Start with NumPy for the audit logic. Add OpenCV, PyTorch, SAM 2, or ROS 2 only after the schema is producing useful traces.

# Install the lightweight baseline dependency.
python -m pip install numpy
Code Fragment 27.L1: This setup installs only NumPy so the lab begins with an inspectable action audit. Heavier vision tools can be added after the evidence schema is correct.

Steps

Step 1: Define The Evidence Schema

Create fields for image timestamp, camera frame, visual estimate, uncertainty, latency, candidate action, and failure label.

Step 2: Add Action Scores

Combine visual confidence, metric clearance, task value, and latency penalty into one score per candidate command.

# Score three visual action candidates with safety and latency penalties.
# The panel explains why the selected robot command changes.
import numpy as np

actions = np.array(["grasp left", "grasp right", "wait"])
confidence = np.array([0.86, 0.72, 1.00])
clearance_m = np.array([0.025, 0.090, 0.500])
latency_ms = np.array([40, 42, 0])
score = confidence + 2.0 * clearance_m - 0.002 * latency_ms
print(actions[int(score.argmax())])
print(np.round(score, 3))
wait [0.830 0.816 2.000]
Code Fragment 27.L2: The score includes `confidence`, `clearance_m`, and `latency_ms`, so the lab can explain why a high-confidence grasp may still be rejected. The `wait` action wins because the current clearance is not yet good enough for execution.

Step 3: Perturb One Factor

Reduce clearance, increase latency, hide part of the mask, or add depth-scale error. Record whether the action changes for the expected reason.

Step 4: Add A Library Path

Replace the toy confidence values with outputs from OpenCV, a detector, SAM 2, or a PyTorch affordance head while keeping the schema unchanged.

Step 5: Write The Replay Postmortem

Save one success case and one failure case with enough metadata to reproduce the action decision.

Expected Output

A table with candidate actions, visual estimates, uncertainty fields, latency, action scores, chosen command, and a failure label for at least one perturbation.

Stretch Goals

  • Replay the same schema from a ROS 2 bag.
  • Add a mask-to-affordance score using a SAM 2 or detector-generated region.
  • Plot the action score as depth scale or latency changes.

Complete Solution

# Complete baseline for the closed-loop visual evidence panel.
# It records the selected command and a failure label for replay.
import numpy as np

actions = np.array(["grasp left", "grasp right", "wait"])
confidence = np.array([0.86, 0.72, 1.00])
clearance_m = np.array([0.025, 0.090, 0.500])
latency_ms = np.array([40, 42, 0])
score = confidence + 2.0 * clearance_m - 0.002 * latency_ms
chosen = int(score.argmax())
failure_label = "insufficient_clearance" if actions[chosen] == "wait" else "none"
print({"chosen": actions[chosen], "failure_label": failure_label})
{'chosen': 'wait', 'failure_label': 'insufficient_clearance'}
Code Fragment 27.L3: The complete solution emits a replayable dictionary with `chosen` and `failure_label`. This is the minimum artifact needed before swapping the toy values for OpenCV, SAM 2, or PyTorch outputs.

Use this chapter as a complete teaching unit for vision that changes robot behavior: calibration, masks, depth, motion, affordances, active sensing, and failure attribution. The through-line is a repeatable perception-to-action evidence record, so no detector, depth model, or tracker is evaluated apart from the command it changes.

Chapter Tool Map
Tool or LibraryWhere It Pays Off
OpenCVCamera calibration, solvePnP, stereo geometry, optical flow, and quick image diagnostics.
PyTorchLearned perception heads, uncertainty models, affordance maps, and batched visual features.
SAM 2 and Segment Anything workflowsPromptable masks, video object memory, interactive data creation, and region proposals.
DINOv2 and visual foundation featuresReusable descriptors for matching, segmentation support, object memory, and downstream robot perception.
Detectron2 or UltralyticsFast object proposals when boxes or instance masks are enough to seed action logic.
ROS 2 image pipelinesTimestamped image transport, diagnostics, replay, and message contracts for closed-loop systems.
Readiness Check

Before leaving the chapter, the reader should be able to state the visual evidence, frame transform, uncertainty field, latency budget, chosen action, and failure label for every perception module.

Teaching Takeaway

A strong chapter session ends with an action audit: the reader can show exactly how a visual estimate changed a robot command and how the decision would be replayed after a failure.

What's Next?

Start with Section 27.1: Seeing to classify vs. seeing to act. After this chapter, continue to Chapter 28: 3D Perception and Neural Scene Representations.

Bibliography & Further Reading

Foundational Papers, Tools, and References

Kirillov, A. et al.. "Segment Anything." ICCV, 2023. https://arxiv.org/abs/2304.02643

Introduces promptable segmentation and the SA-1B data engine used as context for modern object masks.

Oquab, M. et al.. "DINOv2: Learning Robust Visual Features without Supervision." TMLR, 2024. https://arxiv.org/abs/2304.07193

A key reference for reusable visual features that support downstream robot perception tasks.

OpenCV. "OpenCV documentation." Project documentation. https://docs.opencv.org/

The practical reference for calibration, image processing, optical flow, geometry, and deployment-friendly vision utilities.

PyTorch. "PyTorch documentation." Project documentation. https://pytorch.org/docs/stable/index.html

The tensor and neural-network stack used for learned perception examples.