Skip to content

External Data Recorder - SAM2 AOI Tracking

With SAM2 AOI tracking, you draw your own areas of interest (AOIs) on the session recording during postprocessing, using brush, polygon, and box tools. SAM2 (Segment Anything Model 2, from Meta) then follows each AOI through the rest of the video. The gaze point is matched to your AOIs the same way it's matched to detected objects in AI Object Detection, with dwell and fixation data for each AOI.

The overlay video of a flight simulator cockpit, with six AOIs drawn as colored areas labeled with their names, such as "orange 1" and "handle i think", and the gaze point circled in red on the throttle AOI

Use AOI tracking when the things you care about aren't objects a detector can name: individual instruments in a cockpit, a region of a screen, or parts of a control panel. AOI tracking and AI object detection can't run in the same postprocessing run, so choose one for each run.

Setup

The External Data Recorder installer includes a SAM2 AOI tracking component, selected by default. It:

  • Downloads SAM2 into SightLab's sightlab_utils\third_party\sam2 folder and installs it into Vizard 8's Python. You don't need git.
  • Downloads the SAM2.1 tiny model, sam2.1_hiera_tiny.pt, into sightlab_utils\third_party.
  • Checks that SAM2 loads, and reports whether it will run on your graphics card.

This is about a 210 MB download. SAM2 uses the PyTorch that the installer's Python packages component installs, which runs first.

If you've already installed the External Data Recorder, run External_Data_Recorder_Installer.exe from the installer folder again, or click Run Installer in the SightLab Dashboard. Click Yes when Windows asks for permission, and select only SAM2 AOI tracking. Running it again is safe: any step that's already done is skipped. The installer needs administrator rights because it writes to Vizard's folder in C:\Program Files.

See Installer for the other components.

Manual Setup

These steps assume Vizard 8's default location. Run them as an administrator, since they write to C:\Program Files.

  1. Download SAM2 into C:\Program Files\WorldViz\Vizard8\bin\Lib\site-packages\sightlab_utils\third_party\sam2. Either clone https://github.com/facebookresearch/sam2.git there, or download the repository as a ZIP and extract it so that setup.py is directly inside the sam2 folder.
  2. In the Vizard Package Manager (Tools > Package Manager), open the CMD tab and run:

    install --no-build-isolation -e "C:\Program Files\WorldViz\Vizard8\bin\Lib\site-packages\sightlab_utils\third_party\sam2"
    

    --no-build-isolation makes pip use the PyTorch you already have, instead of downloading a second copy just to build SAM2.
    3. Download the SAM2.1 tiny model into the sightlab_utils\third_party folder.

PyTorch and torchvision must be CUDA builds that match your graphics card. The External Data Recorder's requirements.txt installs them. We have only tested AOI tracking with CUDA.

Using a Larger Model

AOI tracking uses SAM2.1's tiny model, which used under 1 GB of graphics memory in our tests. If your graphics card has room, a larger model may track more accurately, but it runs more slowly:

  1. Download sam2.1_hiera_small.pt, sam2.1_hiera_base_plus.pt, or sam2.1_hiera_large.pt from https://dl.fbaipublicfiles.com/segment_anything_2/092824/ into sightlab_utils\third_party.
  2. At the top of AOI_Util/SAM2.py in the External Data Recorder folder, change CONFIG and CHECKPOINT to match. For the small model:
# AOI_Util/SAM2.py
CONFIG = "configs/sam2.1/sam2.1_hiera_s.yaml"
CHECKPOINT = os.path.join(_THIRD_PARTY, "sam2.1_hiera_small.pt")

The configs for the other models are sam2.1_hiera_b+.yaml and sam2.1_hiera_l.yaml.

Configure Postprocessing

Change these settings in Postprocess_Config.py:

Setting Set to Default Description
USE_AOI_TRACKING True False Turns on AOI drawing and SAM2 tracking
USE_OBJECT_DETECTION False True Required: postprocessing stops with an error if both are on
BATCH_SIZE 1 16 Required: SAM2 tracks the video one frame at a time, so frames can't be processed in batches. Postprocessing stops with an error otherwise.

Two more settings control when you draw AOIs and what they're called:

Setting Default Description
AOI_TIMESTAMPS [] Later moments in the recording where you also want to draw AOIs, as "minutes:seconds" text such as "01:25" or frame numbers such as 320. Leave it empty to draw AOIs on the first frame only. See Adding AOIs Later in the Recording.
NAME_AOIS True If True, you're asked to name each AOI after you draw it. If False, AOIs are named AOI Object 0, AOI Object 1, and so on.
# Postprocess_Config.py
USE_AOI_TRACKING = True
USE_OBJECT_DETECTION = False
BATCH_SIZE = 1
AOI_TIMESTAMPS = ["00:45", 3200]
NAME_AOIS = True

We recommend setting AUTO_POSTPROCESS = False in Data_Recorder_Config.py. Postprocessing then doesn't start right after each trial, so you can watch the recording first and find the moments to add to AOI_TIMESTAMPS.

Running AOI Tracking

  1. Record the session as usual.
  2. Run Postprocessor.py for the session, or click Postprocessor in the SightLab Dashboard. First set EXPERIMENT_ID and CALIBRATION_ID in Postprocess_Config.py (see the Postprocessing Guide). Or, from the External Data Recorder folder, pass the experiment ID, calibration ID, and trial number on the command line:

    "C:\Program Files\WorldViz\Vizard8\bin\python.exe" Postprocessor.py 08-24-2026-11-22-21_0 08-24-2026-11-22-21_0 1
    
  3. If the session uses Interactive Mode, crop the recording and adjust the gaze point first. You draw AOIs on the cropped frame.

  4. Draw your AOIs on the first frame of the recording, then at each of your AOI_TIMESTAMPS (see below).
  5. SAM2 tracks the AOIs through every frame. The console shows the progress.

Output files overwrite the ones from earlier postprocessing of the same session. To keep them, rename them first.

Drawing AOIs

A window opens with the first frame of the recording:

The Select AOIs window showing a flight simulator cockpit, with the help bar in the top left listing the tool keys, the AOI count, and the other controls

The help bar in the top left lists the keys, and how many AOIs you've saved so far.

To draw, hold down the key for a tool while you use the mouse. Keep holding it for all of that tool's actions, including pressing n to save.

Tool Hold How to use it
Brush m Click and drag to paint the area. Right-click and drag to erase. Scroll the mouse wheel, or press [ and ], to change the brush size. Press n to save the painted area as an AOI.
Box b Click one corner of the box and drag to the opposite corner. Releasing the mouse button saves the box. Right-click to remove the last saved AOI.
Polygon p Click to place each corner of the polygon. Right-click to remove the last point. Press o to switch between ordering the points automatically and connecting them in the order you clicked. Press n to save the polygon as an AOI.

The tools work differently in SAM2:

  • Brush and polygon AOIs are followed closely as you drew them.
  • A box tells SAM2 to find the object inside it, and SAM2 decides the object's outline. A box works best around one distinct object.

You can mix tools: AOIs from all three are kept.

Other keys:

Key What it does
u Undo the last action, such as a saved AOI, a polygon point, or a brush stroke
c Clear everything and start over on this frame
Enter Accept the AOIs and continue. Space and closing the window also accept them.
Esc Skip this frame without saving any AOIs

If no AOIs are saved at all, postprocessing stops with the message No AOIs were selected.

Naming AOIs

When NAME_AOIS = True, pressing Enter shows the frame with each AOI numbered, and a dialog asks you to name each one in turn:

The Frame #0 AOIs window with two polygon AOIs over cockpit displays, a green one numbered 0 and an orange one numbered 1, and a Vizard dialog asking "Input name for AOI #0"

The number is in the top-left corner of each AOI. In this example, AOI #0 is the green area on the left.

The names appear on the overlay video and in the data files, so choose names that will make sense when you analyze the data, such as primary flight display.

Adding AOIs Later in the Recording

To track things that aren't visible on the first frame, add the moments where they appear to AOI_TIMESTAMPS:

  • "minutes:seconds", such as "01:25", counts whole seconds from the start of the recording. It's converted to a frame using the recording's average frame rate, so it can land slightly off from the time your video player shows.
  • A frame number, such as 320, is exact. Frames are counted from 0, the same as the Frame column in the object detection timeline.

After the first frame, the AOI window opens at each timestamp in time order, and you name the new AOIs after each one.

  • Draw only the new AOIs. AOIs from earlier frames keep being tracked.
  • Numbering continues from the earlier AOIs. For example, if you drew #0 and #1 on the first frame, the next AOIs are #2, #3, and so on.
  • 0 and "00:00" are skipped, since you always draw on the first frame.
  • Timestamps must be within the recording.

Results

The overlay video, recordings/<id>_overlay_trial_<n>.avi, shows each AOI as a colored area with its name (see the image at the top of this page). The gaze point is drawn with the HIGHLIGHTED_GAZE_* settings when it's on an AOI, and with the GAZE_* settings otherwise.

The object detection timeline, <id>_obj_det_timeline_trial_<n>.csv, has one row per frame, with your AOIs in place of detected objects:

  • Gazed Object and All Detected Objects: AOI names
  • All Detected Classes: AOI Object. All AOIs share this one class, so Class Dwell Flag shows dwell on any AOI.
  • All Bounding Boxes: the box around each AOI's tracked area
  • Gazed Object Confidence and All Confidences: 0, since AOIs you draw have no detection confidence
  • Near Misses columns: empty

The object and class detection summary files are only written with AI object detection, not with AOI tracking.

How gaze is matched to AOIs

  • Inside the area: gaze counts as on an AOI only when the gaze point is inside the AOI's tracked area itself, not merely inside the box around it.
  • Overlapping AOIs: the smallest AOI is used.
  • No accuracy margin: USE_ANGULAR_THRESHOLD and CLOSEST_EDGE only apply to object detection, so there is no padding for the eye tracker's measured accuracy.
  • Dwell: DWELL_THRESHOLD and DWELL_GRACE_S work the same way as for detected objects.
  • Hidden AOIs: when an AOI is out of view or fully covered, it isn't drawn or counted for those frames.

Tips and Troubleshooting

  • Speed: SAM2 processes one frame at a time, so AOI tracking is slower than object detection. It ran at about 5-6 frames per second in our tests on an NVIDIA® GeForce RTX™ 4060 Laptop GPU, with a 1920x1128 recording. At that rate, a 5-minute recording at 60 FPS (18,000 frames) takes about an hour. Close graphics-heavy programs (SteamVR, Unreal, Unity) before postprocessing.
  • Pick a clear frame: draw each AOI on a frame where it's fully visible and not covered by anything.
  • Track visible things: SAM2 follows things it can see in the image. An area that doesn't match anything visible, such as an arbitrary patch of sky, may fade out of tracking after a while.
  • A CUDA graphics card is needed: without one, SAM2 runs on the CPU and is very slow. The installer warns you if PyTorch finds no CUDA graphics card.
  • UserWarning: cannot import name '_C' from 'sam2': you can ignore this. SAM2's optional CUDA extension isn't built, which skips a small cleanup step on the tracked areas.
  • Error: Please enable either only AOI Tracking or only Object Detection!: set USE_OBJECT_DETECTION = False.
  • Error: Please set BATCH_SIZE to 1 for AOI tracking.: set BATCH_SIZE = 1.
  • No AOIs were selected: brush and polygon AOIs must be saved with n before you press Enter.
  • ModuleNotFoundError: No module named 'sam2': run the installer with SAM2 AOI tracking selected, or follow Manual Setup.
  • The installer's SAM2 step fails: the installer log shows why. The most common causes are no internet connection, or the Python packages component not having been installed. Fix the cause, then run the installer again with only SAM2 AOI tracking selected.