Finding the Focal Point: Faces, Then People, Then Saliency (Part 2 of 2)
The server half of face-aware image framing, where a chain of open detectors finds the point worth keeping and the client keeps it in view with one CSS property. This is part two of a two-part series. If you missed it,
The server half of face-aware image framing, where a chain of open detectors finds the point worth keeping and the client keeps it in view with one CSS property.
This is part two of a two-part series. If you missed it, start with part one to see how the page keeps faces in the frame.
In 2018 Twitter let a saliency model decide which part of every photo stayed in the preview. Three years later its own researchers measured whose faces that crop kept, and recommended removing it. Thatβs why the chain in this article looks for faces first and only falls back to saliency when thereβs nothing else to aim at.
In part one I stopped a hero banner cutting peopleβs heads off without cropping a single file. The client stores one focal point per image, converts it for the box it lands in, and sets object-position. That works with any point. This article is about producing a good one automatically, for thousands of images nobody will ever hand-edit, and doing it inside a server that renders pages without slowing them down.
The contract hasnβt changed. For each image URL the server returns a point as percentages of the image, the analysed imageβs width and height, and the name of the detector that produced it. Brand images return a box and a background colour instead. Everything is cached, and a miss falls back to the default centre crop rather than blocking the page.
Faces, then people, then saliency
Each image goes through a chain of detectors, starting with the one I trust most. The first one that finds something useful wins. Brand images skip the chain and go to the logo check. Every result is cached along with the name of the detector that produced it.

Saliency only gets a turn when nobody else claims the photo.

Figure 1. How the focal-point pipeline chooses a detector
As Figure 1 shows, after one cache lookup the pipeline tries faces, then the main personβs head, then saliency, while logos take their own path.
Ours runs in Node, in the same process that renders the page, using TensorFlow.js on its WebAssembly backend. Thereβs no GPU to provision and no vision API to pay for, though nothing below depends on that choice. The samples are TypeScript, and every piece has an equivalent on other stacks.
- Decoding uses sharp, which wraps libvips. libvips has bindings for most server languages, and Pillow or ImageMagick also work.
- Faces come from MediaPipe, which also runs in Python and the browser. OpenCVβs YuNet, or any detector that returns boxes, will slot in.
-
People need any COCO-trained detector with a
personclass. -
Saliency is libvipsβs
attentioncrop, which every binding can reach. smartcrop.js and smartcrop.py are close alternatives. - The cache is whatever you already have, keyed by URL.
- The client is one CSS property, set inline or from a data attribute in any framework.
Images are first decoded and shrunk to fit 512Γ512. Thatβs all the detail a face detector needs, and it keeps the work small. Helpers such as fetchImage and decodeForAnalysis, and the FaceBox, PersonBox and RawImage types, are left out for brevity.
type ImageFocus = Focus & { source: 'face' | 'person' | 'attention' };
// A broken model must not take the later fallbacks down with it.
const safely = <T>(name: string, run: Promise<T[]>) =>
run.catch((err) => {
console.warn(`${name} detector failed`, err);
return [] as T[];
});
const computeImageFocus = async (url: string): Promise<ImageFocus | null> => {
const image = await decodeForAnalysis(await fetchImage(url)); // RGB, max 512px
if (!image) return null;
// the client needs the analysed size to know which axis will be cropped
const size = { imageWidth: image.width, imageHeight: image.height };
const faces = await safely('face', detectFaces(image.data, image.width, image.height));
const face = facesFocus(faces, image.width, image.height);
if (face) return { ...face, ...size, source: 'face' };
const people = await safely('person', detectPeople(image.data, image.width, image.height));
const head = personFocus(people, image.width, image.height);
if (head) return { ...head, ...size, source: 'person' };
return { ...(await attentionFocus(image)), ...size, source: 'attention' };
};
Faces first
I use MediaPipeβs full-range face detector through @tensorflow-models/face-detection. The full-range model finds the smaller faces in wide shots, which the short-range selfie model misses. Two rules turn a list of boxes into one point.
- Ignore faces smaller than 0.5% of the image. In a film still or a crowd, they belong to background people.
- Keep every face at least half the size of the largest, and aim at the centre of that group. A band photo is framed around the middle of the band, and an extra in the corner canβt drag the crop away. In a narrow box, members at the edges can still be cut off.
const MIN_FACE_AREA = 0.005;
const GROUP_FACE_RATIO = 0.5;
export const facesFocus = (faces: FaceBox[], width: number, height: number) => {
const area = (f: FaceBox) => f.width * f.height;
const subjects = faces.filter((f) => area(f) >= MIN_FACE_AREA * width * height);
if (!subjects.length) return null;
const largest = Math.max(...subjects.map(area));
const group = subjects.filter((f) => area(f) >= largest * GROUP_FACE_RATIO);
const cx = group.reduce((s, f) => s + f.xMin + f.width / 2, 0) / group.length;
const cy = group.reduce((s, f) => s + f.yMin + f.height / 2, 0) / group.length;
return { x: (cx / width) * 100, y: (cy / height) * 100 };
};
Cloudflare centres the box that holds every face instead. That works for group portraits, but a single background face can pull it off the subject. The size filter avoids that.
The main personβs head
A face detector misses racing drivers in helmets and masked characters, along with people in profile and some stage shots. For those, I run the COCO-SSD detector built on SSDLite MobileNet v2 and keep only the person class. The pipeline takes the largest person and aims at their head, which sits about 10% down from the top of a standing or half-length box.

No face, but definitely a head.
Two checks stop it guessing badly.
- A person under 3% of the image is a background figure, so it returns nothing.
- A box taller than 75% of the image is a close-up whose top edge is usually hair or a hat, so it returns nothing and lets saliency decide.
const MIN_PERSON_AREA = 0.03;
const MAX_PERSON_HEIGHT = 0.75;
const HEAD_OFFSET = 0.1;
export const personFocus = (people: PersonBox[], width: number, height: number) => {
const area = (p: PersonBox) => p.width * p.height;
const main = [...people].sort((a, b) => area(b) - area(a))[0];
if (!main || area(main) < MIN_PERSON_AREA * width * height) return null;
if (main.height > MAX_PERSON_HEIGHT * height) return null;
return {
x: ((main.xMin + main.width / 2) / width) * 100,
y: ((main.yMin + main.height * HEAD_OFFSET) / height) * 100,
};
};
Saliency from libvips
When thereβs no person, sharpβs attention strategy makes the guess using the saliency scoring built into libvips, which weighs luminance frequency and saturation along with skin tones. It returns the offset of a crop along the cropped axis instead of a point, so the code asks twice with a half-width window and a half-height one and reads the centre of each window.

Saliency, finding the most interesting thing in the photo.
export const attentionFocus = async ({ data, width, height }: RawImage) => {
const raw = { raw: { width, height, channels: 3 as const } };
const offset = async (w: number, h: number) => {
const { info } = await sharp(data, raw)
.resize(w, h, { fit: 'cover', position: sharp.strategy.attention })
.toBuffer({ resolveWithObject: true });
// sharp reports attention crop offsets as negative numbers
return { left: -(info.cropOffsetLeft ?? 0), top: -(info.cropOffsetTop ?? 0) };
};
const hw = Math.round(width / 2);
const hh = Math.round(height / 2);
const [{ left }, { top }] = await Promise.all([offset(hw, height), offset(width, hh)]);
return { x: ((left + hw / 2) / width) * 100, y: ((top + hh / 2) / height) * 100 };
};
The result records which detector produced it (face, person or attention), and the client uses that. A hero banner crops hard, so it only moves for a face or a person and keeps the default centre for a saliency guess. Small cards crop gently and can use any of the results.
Logos need their own method
A logo has no face, and saliency tends to pick one letter of a wordmark. Part one showed how the client frames the whole mark on its own background colour. The serverβs job is to find the markβs box and that colour.
Most logos sit on a flat or transparent background, so plain pixel checks do the job without any model.
- Read the border pixels and take their median colour, then give up if fewer than 95% of them are within a small RGB distance of it, since that means the background isnβt flat.
- Mark every pixel that differs from the background, or is opaque on a transparent image. A transparent image has no background colour to return, so the client uses the page background.
- The first and last rows and columns with at least two marked pixels define the logoβs box, and the two-pixel rule stops JPEG speckles from stretching it.
The result is stored as a box and a colour rather than a point. If the border isnβt flat the image goes through the normal chain instead, and the client falls back to a plain cover crop.
Making it safe to run on every page view
Getting the detectors to work was the easy part. The hard part was running all of this during server rendering without slowing pages down.
- Repeat work. Cache the result per image URL for about 30 days in whatever store you already use, and version the cache key so a better detector re-analyses everything.
- Concurrent misses. Keep a map of in-flight promises, so concurrent requests for one image share a single analysis.
- Cold starts. Load the image and ML libraries lazily, only on a cache miss.
- Slow pages. Give each batch of images a 1.5 s budget, and render anything not ready by then with the default crop while its result is cached for next time.
- One CPU, one backend. Run the models through a single promise queue, because the WASM backend runs single-threaded in this setup.
- Huge originals. Analyse a ~500px thumbnail instead of a multi-megabyte original when the source offers one, as Wikimedia does, and cap downloads at 10 MB.
-
Bad URLs. Accept only public
httpshostnames and follow redirects by hand, checking every hop, so a stored image URL can never reach internal addresses through SSRF. Resolve each hostname yourself, reject private, loopback and link-local addresses, and connect to the address you checked, or DNS rebinding can slip past the check. - Model failures. A throwing face or person model is logged and skipped, so the chain falls through to saliency and the page still renders.
-
Flaky upstreams. Throw on timeouts and on 5xx or 429 responses so they stay out of the cache, and return
nullfor missing or broken images since that is a stable answer worth caching.
Three more things will catch you off guard, especially on serverless.

Every cold start, downloading the same 18 MB from someone elseβs server.
-
Vendor the model weights. Most detector libraries download them from a model hub at runtime by default, so every cold function depends on that hub being up. Ship the weights with the code and load them from disk, with
tf.io.fromMemoryin TensorFlow.js or a local path in Python. -
Bundle native and WASM binaries yourself. On a serverless host the install-time path to
.wasmfiles or libvipsβs shared libraries is often missing from the deployed bundle. Copy them into the project and point the loader at them, usingsetWasmPathsin TensorFlow.js. Then list them in your platformβs file-tracing or include config, such asoutputFileTracingIncludesin Next.js orincludeFileson Vercel. A Lambda layer or a container image also works. -
Load models once per process. Bundlers can give each route its own copy of your module while the ML library stays shared, so a module-level
let detectorloads the model once per route. Park the loading promise on a process-wide object such asglobalThisin Node so it loads once. In a long-running Python or Go service, a plain module-level singleton is enough.
Why faces come before saliency
Saliency alone is the obvious shortcut, and it has a public failure on record.
In 2018 Twitter moved its image previews to a saliency model that cropped around the point people were most likely to look at. Its own team later measured systematic disparities in who that crop kept in view and found that cropping around the single most salient point amplified them. They recommended removing saliency cropping altogether. An independent audit that also covered Googleβs and Appleβs croppers raised similar concerns.
Thatβs why saliency is the last thing the chain tries, and why a saliency guess never moves a hero banner. The rules are also explicit and easy to inspect: every face at least half the size of the largest counts equally towards the point. A face detector can still miss people, so test it on a sample that looks like your real catalogue, covering a range of skin tones and ages, varied lighting and headwear.
Limitations
- MediaPipe and COCO-SSD find people without knowing which one matters. In a poster with two actors the crop frames both, even when the page is about only one of them.
- The logo heuristic needs a flat or transparent background. A logo on a photo or gradient falls back to the normal crop.
- The 1.5 s budget means the first visitor to an unseen image gets the default crop. Warming the cache when images are ingested would fix that.
- One point per image canβt serve two dissimilar subjects at two far-apart aspect ratios. A focal box, which the logo path already stores, is the natural next step.
If you build this
- Start with the detector you trust most, faces, and only fall back to saliency when everything else has given up.
- Filter out small faces and frame faces of similar size together. Detectors return background people too.
- Cache per URL and load models lazily. Put a time budget on the page and vendor the weights and WASM files.
- Record which detector produced each point. A hero banner should only move for a face or a person, never for a saliency guess.
The whole thing is a few hundred lines of TypeScript plus two open models, roughly 1 MB and 18 MB. I run it inside the server that renders the page, but it would work just as well as a small service in front of your renderer that returns a point per URL.
Sources
- Twitter Engineering, Smart Auto-Cropping of Images (2018)
- Yee, Tantipongpipat and Mishra, Image Cropping on Twitter: Fairness Metrics, their Limitations, and the Importance of Representation, Design, and Agency (CSCW 2021)
- Birhane, Prabhu and Whaley, Auditing Saliency Cropping Algorithms (WACV 2022)
- Cloudflare, AI face cropping for Images (2025)
- Appwrite, Introducing AutoGravity (2026)
- sharp, MediaPipe face detection for TensorFlow.js, COCO-SSD
Which detector would you put first in your own chain, and what kind of images would break it? Iβd like to hear what youβve run into.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.