GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - alineacms/focuspoint

BranchesTags
Open more actions menu

Latest commit

 

History

17 Commits
17 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
demo
demo
 
 
eval
eval
 
 
scripts
scripts
 
 
site
site
 
 
src
src
 
 
test
test
 
 
train
train
 
 
.gitignore
.gitignore
 
 
README.md
README.md
 
 
TRAINING_PROMPT.md
TRAINING_PROMPT.md
 
 
bun.lock
bun.lock
 
 
package.json
package.json
 
 
tsconfig.build.json
tsconfig.build.json
 
 
tsconfig.json
tsconfig.json
 
 

Repository files navigation

focuspoint

Find the focus point of an image: the (x, y) position that every crop centred on it should keep, such as the face people look at first.

  • Knows what matters: a tiny learned model (35 kB gzipped) that knows a head matters more than a bright sign.
  • Fast: 4–6 ms per image.
  • Runs anywhere: browser, Node, Bun, Deno, workers. No dependencies, no WebGPU or WASM. It only needs RGBA pixels.

Try it: alineacms.github.io/focuspoint shows 150 photos cropped live, with every setting adjustable.

import {focusPoint} from '@alinea/focuspoint'

const {x, y, box, confidence} = focusPoint({data, width, height})
// x, y: focus point, 0..1 from the top left
// box: bounding box of the subject, 0..1
// confidence: how clearly a subject stands out, 0..1

Usage

Browser

Draw the image onto a small canvas so the browser does the downscaling, then pass its pixels:

import {focusPoint} from '@alinea/focuspoint'

const img = new Image()
img.src = '/photo.jpg'
await img.decode()
const scale = Math.min(1, 256 / Math.max(img.naturalWidth, img.naturalHeight))
const width = Math.round(img.naturalWidth * scale)
const height = Math.round(img.naturalHeight * scale)
const ctx = new OffscreenCanvas(width, height).getContext('2d')!
ctx.drawImage(img, 0, 0, width, height)
const {x, y} = focusPoint(ctx.getImageData(0, 0, width, height))

Node / Bun with sharp

Pass any {data, width, height} RGBA buffer. Decoding and shrinking the image first keeps things fast. Around 256 px on the longest side is plenty.

import sharp from 'sharp'
import {focusPoint} from '@alinea/focuspoint'

const {data, info} = await sharp(file)
  .rotate()
  .resize(256, 256, {fit: 'inside'})
  .ensureAlpha()
  .raw()
  .toBuffer({resolveWithObject: true})
const point = focusPoint({data, width: info.width, height: info.height})

Importance map

saliency(image) returns the importance map behind the point ({width, height, data: Float32Array}, values 0..1). locate(map, options) turns any map into a focus point, so you can also bring your own.

Applying the point

The point is meant for centre-and-clamp cropping: centre the crop window on the point, then shift it back inside the image where it would overflow. This is what image CDNs and most crop tools do:

function crop(point, imageWidth, imageHeight, cropWidth, cropHeight) {
  const clamp = (v: number, max: number) => Math.min(max, Math.max(0, v))
  return {
    x: clamp(point.x * imageWidth - cropWidth / 2, imageWidth - cropWidth),
    y: clamp(point.y * imageHeight - cropHeight / 2, imageHeight - cropHeight),
    width: cropWidth,
    height: cropHeight
  }
}

In CSS, object-position: x% y% aligns proportionally rather than centring. To get the same crop, convert the clamped window: object-position is cropX / (imageWidth - cropWidth) (or 50% when nothing is cropped on that axis).

How it works

  1. Score how important each pixel is. The image is squashed to 64×64 and run through a 55k-parameter convolutional network (depthwise-separable encoder, global context, decoder with skips, 32×32 output). Its weights are 4-bit, stored as text. It was distilled from a larger network trained on where people look (SALICON), on subject masks (DUTS) and on 30,000 extra photos, with detected heads marked as the most important part. Transparent areas count as background. See train/README.md.
  2. Find the subject: threshold the map, take the strongest region, and settle on the centre of the importance around it.
  3. Protect the top spot: the area around the map's peak, usually a face, must stay whole in every crop. Only points that achieve that are considered; when none can (a face taller than a 3:1 banner), the one that cuts least of it wins. Set protect: 0 to favour more of the whole subject instead.
  4. Place the point for cropping: search x and y for the position that keeps the most importance in view when the image is cropped to common container shapes (3:1, 16:9, 1:1, 4:5, 9:16), each crop centred on the point and clamped. Positions within a small tolerance of the best count as equal, and the one closest to the subject wins.

Evaluation

Every image is cropped to five container shapes (3:1, 16:9, 1:1, 4:5, 9:16) around the point, with centre-and-clamp. All numbers are on held-out data: SALICON val, DUTS-TE, ECSSD, PASCAL-S and MSRA10K were never used for training or tuning the model. See eval/README.md.

Heads kept whole: on photos where a face detector finds a face, the share of crops that keep the main head (forehead to chin, with hair) entirely in view. Cutting through a head is the worst crop there is, so this comes first. "Ideal" centres on the head itself. "heuristic" is the hand-written importance map focuspoint used before the model (minimum barrier distance, skin tone and a centre prior), kept in eval/heuristic.ts as a baseline.

Main head kept SALICON (1704) DUTS-TE (952) ECSSD (201) PASCAL-S (149)
ideal 98.5% 97.1% 94.3% 94.8%
focuspoint 79.3% 83.8% 77.7% 80.1%
focuspoint, protect: 0 77.2% 81.9% 75.3% 78.4%
heuristic 61.3% 65.5% 58.8% 60.7%
centre 59.0% 63.7% 57.1% 58.3%

Top spot: how often the single most important spot survives the crop. On SALICON that is where most people looked. On the subject-mask sets it is the middle of the subject, which focuspoint trades for the head.

Top spot SALICON DUTS-TE ECSSD PASCAL-S MSRA10K*
oracle 93.8% 99.4% 99.3% 97.9% 99.7%
focuspoint 89.4% 92.7% 92.7% 90.0% 94.7%
focuspoint, protect: 0 90.8% 93.9% 94.4% 91.9% 96.3%
heuristic 87.7% 95.3% 97.9% 95.7% 98.7%
centre 87.4% 94.2% 97.1% 94.4% 94.8%
smartcrop, as Alinea uses it 86.6% 93.1% 94.0% 92.8% 95.5%

Kept: how much of what matters stays in view, averaged over the crops.

Kept SALICON DUTS-TE ECSSD PASCAL-S MSRA10K*
oracle 85.7% 92.2% 88.7% 85.6% 90.8%
focuspoint 81.1% 85.8% 83.1% 78.6% 86.6%
focuspoint, protect: 0 82.5% 86.4% 84.0% 79.6% 87.6%
heuristic 82.9% 86.4% 85.1% 80.9% 88.1%
centre 82.6% 84.9% 83.6% 79.7% 83.3%
smartcrop, as Alinea uses it 81.9% 84.3% 82.0% 78.8% 85.0%

* MSRA10K: an evenly spaced sample of 2000 images.

Hand-marked points: on 55 of the website's photos, a person marked where the focus should be. focuspoint keeps that point in 93.8% of crops, against 84.4% for the heuristic and 83.6% for the centre.

"smartcrop, as Alinea uses it" is the centre of smartcrop.js's best 100×100 crop.

What it trades: protecting the head moves the point away from the middle of large subjects, so it keeps 1–2 points less of the whole subject than the heuristic. protect: 0 keeps nearly as much as the heuristic and still keeps far more heads.

Viewer and website

  • demo/focuspoint-viewer.html: open it in a browser to try your own images, with the importance map and crops.
  • site/: the website, built with bun scripts/site.ts and published to GitHub Pages on every push to main.

Development

bun install
bun test          # unit tests, including the PyTorch/TypeScript parity test
bun run check     # typecheck (TypeScript 7)
bun run build     # emit dist/
bun run size      # bundle sizes
bun scripts/bench.ts <images...>  # speed
bun eval/run.ts <dataset-dir> --methods oracle,center,heuristic,model
bun eval/faces.ts <dataset-dir>   # heads kept whole
bun scripts/site.ts --serve       # build and serve the website, with point marking
bun run demo      # rebuild demo/focuspoint-viewer.html

A dataset directory holds images/*.jpg and masks/*.png (or fixations/*.png) with matching names. See eval/README.md for how to get the datasets, and train/README.md for training the model.

About

No description, website, or topics provided.

Resources

Readme
Activity
Custom properties

Stars

0 stars

Watchers

0 watching

Forks

0 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.