• Stars
    star
    381
  • Rank 112,502 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 4 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

FaceMeshFaceGeometry for FaceMesh

FaceMeshFaceGeometry

Three.js helper for FaceMesh https://github.com/tensorflow/tfjs-models/tree/master/facemesh


Demo with textured mask: https://spite.github.io/FaceMeshFaceGeometry/examples/mask/index.html

Demo with remapped video: https://spite.github.io/FaceMeshFaceGeometry/examples/video/index.html

Demo with instanced geometry: https://spite.github.io/FaceMeshFaceGeometry/examples/instanced/index.html

Demo of texture mapping from an image: https://spite.github.io/FaceMeshFaceGeometry/examples/face_transfer/index.html

Demo of face switching: https://spite.github.io/FaceMeshFaceGeometry/examples/face_off/index.html

FaceMeshFaceGeometry

How to use

After following the code in the FaceMesh repo, you end up with a face estimation.

First import the class.

import { FaceMeshFaceGeometry } from './face.js';

Create a new geometry helper.

const faceGeometry = new FaceMeshFaceGeometry();

On the update loop, after the model returns some faces:

const faces = await model.estimateFaces(video);
if(faces.length) {
  faceGeometry.update(faces[0]);
}

You have to call FaceMeshFaceGeometry::setSize with the width and height of the video to normalise the coordinates so they align with the video source.

That's all there is. You can use faceGeometry as any other BufferGeometry:

const mask = new Mesh(faceGeometry, material);
scene.add(mask);

Mirrored video

This works for the video as it comes. If your source is a webcam, you might want to flip it horizontally.

In that case -besides flipping the video element, by using transform: scaleX(-1), for instance- you'll have to pass a flag to estimateFaces and update:

const faces = await model.estimateFaces(video, false, true);
if(faces.length) {
  faceGeometry.update(faces[0], true);
}

How to use the video/input as a texture for the face

You can use the input to the FaceMesh model to texture the 3d mesh of the face. Constuct the helper with:

const faceGeometry = new FaceMeshFaceGeometry({useVideoTexture: true});

That will remap the UV coordinates of the geometry to fit the input. The vertex coordinates from the estimation will be projected every frame into the UV space. That means that the UV coordinates for the shader won't work for texture mapping (i.e,, alpha mask, ao map, etc. will be mapped differently and probably wrong).

There seem to be issues with instanced video in macOs Chrome and Safari.

How to use as a 3d mesh, independently of the camera

The range of the vertices is based on the resolution of the input feed, so it changes depending on the chosen video or image input. Construct the FaceGeometry helper with the option normalizeCoords set to true and the mesh will be in a resonable consistent size.

const faceGeometry = new FaceMeshFaceGeometry({normalizeCoords: true});

How to update my threejs camera

FaceMesh data works better with an Orthographic camera. First create a camera:

const camera = new OrthographicCamera(1, 1, 1, 1, -1000, 1000);

and then when the video is ready, or the video dimensions change (width, height), run:

camera.left = -.5 * width;
camera.right = .5 * width;
camera.top = .5 * height;
camera.bottom = -.5 * height;
camera.updateProjectionMatrix();

Track points in geometry

After faceGeometry.update() you can use faceGeometry.track() to place objects relative to the surface of the face.

dummy.position.copy(track.position);
dummy.rotation.setFromRotationMatrix(track.rotation);

It will calculate a triangle defined by the three provided vertex ids, and return a position, a normal, and an orthogonal basis rotation that cane be used to rotate and object along the correct normal of that triangle.

Use this image as a reference for vertex Ids.

API

FaceMeshFaceGeometry::update(face: FaceEstimation, cameraIsFlipped: boolean): void

Updates the vertices and recalculates normals.

FaceMeshFaceGeometry::setSize(width: number, height: number): void

Sets the internal values to reframe the coordinates.

FaceMeshFaceGeometry::track(id0: number, id1: number, id2: number): { position: Vector3, normal: Vector3, rotation: Matrix4 }

Calculates a triangle defined by vertices id0, id1 and id2, return its center, normal and orthogonal basis.

More Repositories

1

ccapture.js

A library to capture canvas-based animations at a fixed framerate
JavaScript
3,419
star
2

THREE.MeshLine

Mesh replacement for THREE.Line
JavaScript
2,147
star
3

Wagner

Effects composer for three.js
JavaScript
1,041
star
4

looper

Looperepool
JavaScript
954
star
5

polygon-shredder

The polygon shredder that takes many cubes and turns them into confetti
JavaScript
807
star
6

ShaderEditorExtension

Google Chrome DevTools extension to live edit WebGL GLSL shaders
JavaScript
607
star
7

WebVR-Extension

Chrome DevTools extension to emulate WebVR API
JavaScript
301
star
8

android-lens-blur-depth-extractor

JavaScript
291
star
9

virtual-webcam

JavaScript
268
star
10

rstats

rStats
JavaScript
243
star
11

sketch

Explorations on cross-hatching, engraving, and similar non-photorealistic rendering.
JavaScript
222
star
12

codevember-2021

JavaScript
203
star
13

THREE.CubemapToEquirectangular

Export an equirectangular panorama image from a three.js scene
JavaScript
188
star
14

WebAudioExtension

Google Chrome DevTools extension to view and hopefully interact with the routing graph of Web Audio API
JavaScript
183
star
15

ThreeJSEditorExtension

Three.js Editor Extension for Google Chrome
JavaScript
182
star
16

THREE.DecalGeometry

Decals for three.js
JavaScript
149
star
17

fuck-2020

A browser demo to send away 2020.
JavaScript
140
star
18

codevember-2017

JavaScript
139
star
19

GSVPano.js

Google Street View Panorama Util
JavaScript
136
star
20

THREE.AugmentedConsole.js

Augmented methods for console.log
JavaScript
134
star
21

THREE.FBOHelper

FrameBuffer Object inspector for three.js
JavaScript
127
star
22

genuary-2022

JavaScript
114
star
23

cross-hatching

HTML
108
star
24

codevember-2016

JavaScript
90
star
25

cruciform

cruΒ·ciΒ·form
JavaScript
88
star
26

PanomNom.js

Panorama fetching tools in JavaScript
JavaScript
86
star
27

codevember-2022

JavaScript
83
star
28

spherical-environment-mapping

Spherical Environment Mapping GLSL Shader
JavaScript
79
star
29

CSS3DClouds

CSS 3D Clouds
HTML
78
star
30

Maf.js

Maf
JavaScript
77
star
31

Storyline.js

Storyline - generic sequencer for JavaScript projects
JavaScript
75
star
32

THREE.UpdatableTexture

JavaScript
71
star
33

depth-player

Depth player for Android Lens Blur images
JavaScript
61
star
34

SolariDisplay

A Split-Flap Display with CSS and JavaScript
JavaScript
57
star
35

THREE.EquirectangularToCubemap

Convert an equirectangular panorama into a cubemap texture
JavaScript
57
star
36

ShaderEditor

Live GLSL WebGL Shader Editor
JavaScript
55
star
37

vertex-displacement-noise-3d-webgl-glsl-three-js

JavaScript
47
star
38

webxr-trails

JavaScript
41
star
39

ConcatSoundLoader

Concatenated Sound Files Loader | Typed Arrays and Web Audio API
HTML
35
star
40

perlin-experiments

HTML
33
star
41

sdf-physics

SDF Physics
JavaScript
33
star
42

npr-shading

JavaScript
28
star
43

infinite-gift

Christmas Experiment 2018
JavaScript
28
star
44

THREE.ConstantSpline

Constant-time B-Spline
HTML
27
star
45

serviceworker-svg

ServiceWorker-SVG
HTML
26
star
46

makio-torus

JavaScript
26
star
47

baubles

generative baubles
JavaScript
24
star
48

AugmentedCompileShader.js

Augmented WebGLRenderingContext.prototype.compileShader
JavaScript
24
star
49

css-filters-devtools-extension

CSS Filters DevTools Extension
JavaScript
22
star
50

PerfMeter

JavaScript
22
star
51

FloatingShinyKnot

JavaScript
22
star
52

futurejs-2014

Web Audio presentation for Future JS Barcelona 2014
HTML
22
star
53

chrome-sketchtab

New tab extension with a code editor
JavaScript
21
star
54

THREE.GamepadControls

Camera controls for three.js using the Gamepad API
JavaScript
21
star
55

THREE.Text

THREE.TextMesh for three.js
JavaScript
19
star
56

solskogen-2020

JavaScript
18
star
57

resonate-2015

Workshop contents | Resonate 2015
JavaScript
18
star
58

shadow-mapping

Shadow mapping in GLSL
JavaScript
17
star
59

scotlandjs-2014

JavaScript
16
star
60

snipsnap

SnipSnap: Snippet Snapshot tool
CSS
16
star
61

GLStateCache.js

State cache for WebGL Rendering Context
JavaScript
15
star
62

unmute

UI for Chrome autoplay policy
JavaScript
15
star
63

mira-lab-2013

Contents for MIRA Lab 2013 Workshop
15
star
64

THREE.ShaderTexture

ShaderTexture, a helper to use procedurally generated texture
JavaScript
13
star
65

lines-on-sphere

JavaScript
13
star
66

genuary-2023

JavaScript
12
star
67

explore-with-me

Explore with me - Christmas Experiments 2017
HTML
12
star
68

halloween-2016

JavaScript
10
star
69

shield-vfx

JavaScript
10
star
70

dual-boids

Dual Boids
JavaScript
10
star
71

3dwebfest-2016

Ambient effect for 3DWebFest
JavaScript
9
star
72

halloween-2022

JavaScript
8
star
73

relooper

more loops, old loops, new loops
JavaScript
8
star
74

WebVR-Recorder

WebVR motion recorder and player
JavaScript
7
star
75

BlockyEarth

JavaScript
7
star
76

speech-jammer

Speech Jammer using getUserMedia and Web Audio API
JavaScript
7
star
77

graphicalweb-2016

JavaScript
6
star
78

linkognito

JavaScript
5
star
79

voxel-volume

JavaScript
5
star
80

getting-started-threejs

JavaScript
5
star
81

google-images-view-image-button

Adds a View Image button to Google Images results page
JavaScript
5
star
82

WebVR-Physics

WebVR Physics experiment
JavaScript
5
star
83

FullStackFest-2015

The Road to WebVR - FullStackFest 2015
JavaScript
5
star
84

list-issues

List all issues from Github repos from the logged in user
JavaScript
5
star
85

audio-cover

JavaScript
4
star
86

scotlandjs-2015

ScotlandJS 2015 - Slides and Demo
HTML
4
star
87

bumpy-metaballs

JavaScript
4
star
88

HexViewerExtension

JavaScript
3
star
89

adventofcode2021

JavaScript
3
star
90

powermate-input

Exploration using a Powermate as input device in the browser
JavaScript
3
star
91

wu2021-python

Python
2
star
92

adventofcode2023

JavaScript
2
star
93

floating-shiny-knot

Floating Shiny Knot
JavaScript
2
star
94

canvas-capture

JavaScript
2
star
95

service-worker-require

require() support with service worker
HTML
2
star
96

AdventOfCode2019

Advent of Code 2019
JavaScript
2
star
97

soundcloud-pi

Solution to souncloud challenge
PHP
2
star
98

webxr

JavaScript
2
star
99

nonocache-sw.js

JavaScript
2
star
100

get-into

JavaScript
2
star