Frame.dev
technicalarchitecturetensorflow.json-device-ai

How SafeOS Guardian checks for motion before it runs a model

Frame.dev Team8 min read

SafeOS Guardian watches a room from the browser of a phone, tablet or laptop. It compares camera frames with plain arithmetic, and on the Security and Wildlife pages it runs an object-detection model when something moves. This post goes through the code that does it.

A check first, the model second

Every frame goes through a cheap check first. A frame goes on to the detection model when its motion level crosses the threshold, so the model stays idle while the room is still.

The check is a setInterval loop that compares pixels every 200 ms, and a Web Audio AnalyserNode that reads the sound's frequencies every 100 ms. Both are arithmetic on the CPU, and neither loads a model. The intervals are constants in CameraFeed.tsx at lines 99–101:

const FRAME_INTERVAL = 1000; // Check every second
const MOTION_INTERVAL = 200; // Motion detection every 200ms
const AUDIO_INTERVAL = 100; // Audio level every 100ms

The gate

Object detection waits for the check to report a change. The gate is in person-detection.ts:

// lines 233-240: run detection when the motion level crosses its threshold
const motionTriggered = motionLevel >= this.config.motionThreshold;
if (!motionTriggered) return null;

While nothing moves, the model does not run. COCO-SSD's model.detect() runs only in Security and Wildlife, when something in the frame moved enough to cross the motion threshold, at most twice a second in Security (minDetectionInterval: 500). One exception: while Security is triggered or sounding an alert, it runs the model every five seconds whether or not anything moved, to see if the person is still there (forceDetection).

One model in the browser

Primary detector: a pre-trained COCO-SSD on SSDLite MobileNet V2, its 4.5 million weights stored as 32-bit floats, run via TensorFlow.js. 18.6 MB from Google model storage, downloaded only after you agree on the Security or Wildlife page, then kept in the browser's Cache Storage for offline use. It knows 80 kinds of object; SafeOS uses its person class and ten animal classes. The detection canvas is 320×240 regardless of the source video resolution.

For harder scenes, the optional SafeOS server you run yourself can send frames to a local Ollama install on the same network: moondream takes a first look at each frame, and llava:7b a closer one when the first finds a concern, and they describe the scene in words. If you also give that server your own OpenRouter, OpenAI or Anthropic keys, then when Ollama is down or its analysis fails it sends the frame to the first of these it has a key for and, if that one fails, to the next.

The sound check

The sound alert needs no model. The Web Audio API's AnalyserNode with fftSize = 256 gives 128 frequency bins, sampled every ~100 ms. The alert goes off when the level stays above your threshold, whatever made the sound. A crying pattern (energy around the 300–600 Hz band, where a baby's cry has its fundamental) shows in the camera overlay, but it does not raise an alert on its own.

SafeOS is not a medical device: it does not monitor breathing and cannot prevent SIDS. Safe-sleep guidance (NIH)

How Lost & Found compares

The matcher uses no trained model. Each reference photo reduces to three signatures: a 32-bin color histogram, the top-5 dominant colors via k-means, and an 8×8 grid of edge strengths taken from differences between neighbouring pixels. The matcher samples the live feed every 500 ms in Lost & Found's local processing mode, its default, and every 900 ms in its hybrid mode. The page runs one comparison at a time, so a comparison that takes longer than that holds back the next one until it ends, and the feed is sampled fewer times. Each frame is compared on average colour first, then region by region on a weighted mix that counts the histogram most, then the dominant colours, then the edges, then the size. The code is in visual-fingerprint.ts.

It compares colours and a coarse outline, so it cannot tell your dog from another dog of the same colour. It needs no model download, and it costs little enough to run on every sample. The Lost & Found loop is in monitor/page.tsx around lines 279–337.

State and storage

State is Zustand: its persist middleware writes settings, and Wildlife's sightings with their pictures, to localStorage. Alerts, alert pictures, Lost & Found's sightings and People's pictures go to IndexedDB, and People's alerts and the pets to both. The interface is Next.js 14, React 18 and Tailwind. The files that do the watching are in src/lib/.

Known limits

On iOS, installing the app goes through Safari's Share menu, and listening in the background is limited. The detection model is an 18.6 MB download, so on a slow connection the first Security or Wildlife start is slow. The Lost & Found matcher compares colours and a coarse outline, so another animal or an object of the same colour can also raise a possible sighting. Most of these are tracked in GitHub issues.

The motion gate keeps the model idle while nothing moves. Its code is in person-detection.ts.

Every part of SafeOS, with its code at one commit and its limits, is on How SafeOS watches a room in your browser.

— The Frame.dev Team

Frame.dev Team

Frame makes SafeOS and AgentOS. frame.dev