Make a camera filter #

A TensorChain camera filter (a lens) is data, not code: the models it needs and an ordered list of effects, each with checked settings. Because a lens carries no code, anyone can send you one as a pass, and trying it cannot run anything on your device. It runs in your wallet, on your camera, and the frames never leave the page.

Every example here ran when the page was built, against the package (@tensorchain/wallet-kit/lens/kit.mjs).

The five effects #

EffectWhat it doesNeedsSettings
background_blurblurs the background by depth band; the person stays sharpsegment (and depth for the bands)near_px 0-40, far_px 0-60
background_replaceputs a colour or an image behind the personsegmentcolor (#rrggbb) or image (an asset's sha256)
color_gradebrightness, contrast, saturation and warmth, on the whole frame or the personnone, or segment for the personeach -1 to 1; target frame or person
hair_tinttints the hairhaircolor, strength 0-1
stickeran image pinned to a face point, sized to the face, turning with the eyesfaceasset, landmark 0-477, size_of_face_width 0.05-2, rotate_with_eyes

Effects apply in order, each on the result of the one before. The models are named by role (face, segment, hair, depth) and must be on-device models that passed review (they are badged); a lens naming any other model is refused.

A lens from scratch #

makeSpec builds a lens and checks it against the manifest of badged models (in the wallet, the manifest comes from the lens bucket at tensorchain.io/lens/, checked by its sha256):

import assert from "node:assert/strict";
import { makeSpec, checkSpec, EFFECTS, LensRefused } from "@tensorchain/wallet-kit/lens/kit.mjs";

const manifest = { models: { "face-landmarker": {}, "selfie-segmenter": {}, "hair-segmenter": {}, "da-v2-small": {} } };
const lens = makeSpec({
  name: "teal room, warm light",
  models: { segment: "selfie-segmenter" },
  effects: [
    { type: "background_replace", color: "#1f7a7a" },
    { type: "color_grade", warmth: 0.4, brightness: 0.1, target: "person" },
  ],
}, manifest);
assert.equal(lens.format, "lens.spec.v2");
assert.deepEqual(Object.keys(EFFECTS).sort(), ["background_blur", "background_replace", "color_grade", "hair_tint", "sticker"]);
console.log(JSON.stringify(lens, null, 1));
Output (captured at build)
{
 "format": "lens.spec.v2",
 "name": "teal room, warm light",
 "models": {
  "segment": "selfie-segmenter"
 },
 "effects": [
  {
   "type": "background_replace",
   "color": "#1f7a7a"
  },
  {
   "type": "color_grade",
   "warmth": 0.4,
   "brightness": 0.1,
   "target": "person"
  }
 ]
}

What is refused #

Every setting is checked before the lens runs, and so is every model:

const why = (spec) => { try { checkSpec(spec, manifest); return "ok"; } catch (e) { return e instanceof LensRefused ? e.code : String(e); } };
const base = { format: "lens.spec.v2", models: { segment: "selfie-segmenter" } };
assert.equal(why({ ...base, effects: [{ type: "teleport" }] }), "bad_effect");                                      // not one of the five
assert.equal(why({ ...base, effects: [{ type: "background_blur", near_px: 6, far_px: 999 }] }), "bad_effect");      // out of range
assert.equal(why({ ...base, effects: [{ type: "hair_tint", color: "#7a3cff", strength: 0.8 }] }), "missing_model");  // no hair model
assert.equal(why({ ...base, models: { depth: "da3-metric-large" }, effects: [{ type: "background_blur" }] }), "not_badged");
console.log("refused: bad_effect, bad_effect, missing_model, not_badged");
Output (captured at build)
refused: bad_effect, bad_effect, missing_model, not_badged

A sticker names its image by sha256, so the image it draws is exactly the one you were sent:

const sticker = makeSpec({
  models: { face: "face-landmarker" },
  effects: [{ type: "sticker", asset: "d184e225f159f3131fff40ad0dacef5cc376789e0879b4eec89975bd6d31919a", landmark: 1,
              size_of_face_width: 0.35, rotate_with_eyes: true }],
}, manifest);
assert.equal(sticker.effects[0].landmark, 1);                    // landmark 1: the tip of the nose
console.log(sticker.effects[0]);
Output (captured at build)
{
  type: 'sticker',
  asset: 'd184e225f159f3131fff40ad0dacef5cc376789e0879b4eec89975bd6d31919a',
  landmark: 1,
  size_of_face_width: 0.35,
  rotate_with_eyes: true
}

Send it, try it #

A lens travels as a filter pass. The service checks it too: the format, the effect types, and that every model is in the manifest it serves.

await w.result.send("filter", "#bob", 0, JSON.stringify(lens));

In Bob's wallet the pass waits in the Inbox with Try, Accept and Refuse. Try runs it on his camera:

import { startLens } from "@tensorchain/wallet-kit/lens/view.mjs";
const S = startLens({ base: "https://tensorchain.io/lens/", spec: lens, video, canvas });
// S.ready, S.frames, S.faces; S.error is set if anything is refused (a changed file is "changed")

The models come from the lens bucket, each named by its sha256 and checked byte for byte before it is used; a file that differs by one byte is refused and never cached. The extension wallet carries the runtime itself and pins the manifest it was built with.

What runs where #