human/demo/demo-esm.js

403 lines
16 KiB
JavaScript
Raw Normal View History

2020-10-15 21:25:58 +02:00
/* global QuickSettings */
2020-10-12 16:08:00 +02:00
import human from '../dist/human.esm.js';
2020-10-13 15:59:21 +02:00
const ui = {
baseColor: 'rgba(255, 200, 255, 0.3)',
2020-10-15 00:22:38 +02:00
baseLabel: 'rgba(255, 200, 255, 0.8)',
2020-10-13 15:59:21 +02:00
baseFont: 'small-caps 1.2rem "Segoe UI"',
baseLineWidth: 16,
};
2020-10-12 16:08:00 +02:00
const config = {
2020-10-15 21:25:58 +02:00
backend: 'webgl',
console: true,
2020-10-12 16:08:00 +02:00
face: {
2020-10-15 00:22:38 +02:00
enabled: true,
2020-10-14 17:43:33 +02:00
detector: { maxFaces: 10, skipFrames: 10, minConfidence: 0.5, iouThreshold: 0.3, scoreThreshold: 0.7 },
2020-10-13 04:03:55 +02:00
mesh: { enabled: true },
iris: { enabled: true },
2020-10-14 17:43:33 +02:00
age: { enabled: true, skipFrames: 10 },
2020-10-13 04:03:55 +02:00
gender: { enabled: true },
2020-10-15 00:22:38 +02:00
emotion: { enabled: true, minConfidence: 0.5, useGrayscale: true },
2020-10-12 16:08:00 +02:00
},
2020-10-15 00:22:38 +02:00
body: { enabled: true, maxDetections: 10, scoreThreshold: 0.7, nmsRadius: 20 },
2020-10-14 17:43:33 +02:00
hand: { enabled: true, skipFrames: 10, minConfidence: 0.5, iouThreshold: 0.3, scoreThreshold: 0.7 },
2020-10-12 16:08:00 +02:00
};
let settings;
2020-10-15 15:43:16 +02:00
let worker;
let timeStamp;
2020-10-12 16:08:00 +02:00
2020-10-15 00:22:38 +02:00
function str(...msg) {
if (!Array.isArray(msg)) return msg;
let line = '';
for (const entry of msg) {
if (typeof entry === 'object') line += JSON.stringify(entry).replace(/{|}|"|\[|\]/g, '').replace(/,/g, ', ');
else line += entry;
}
return line;
}
2020-10-15 21:25:58 +02:00
const log = (...msg) => {
// eslint-disable-next-line no-console
if (config.console) console.log(...msg);
};
2020-10-15 14:16:34 +02:00
2020-10-13 15:59:21 +02:00
async function drawFace(result, canvas) {
2020-10-12 16:08:00 +02:00
const ctx = canvas.getContext('2d');
2020-10-13 15:59:21 +02:00
ctx.strokeStyle = ui.baseColor;
ctx.font = ui.baseFont;
2020-10-12 16:08:00 +02:00
for (const face of result) {
2020-10-15 00:22:38 +02:00
ctx.fillStyle = ui.baseColor;
2020-10-13 15:59:21 +02:00
ctx.lineWidth = ui.baseLineWidth;
2020-10-12 16:08:00 +02:00
ctx.beginPath();
2020-10-13 15:59:21 +02:00
if (settings.getValue('Draw Boxes')) {
ctx.rect(face.box[0], face.box[1], face.box[2], face.box[3]);
}
2020-10-15 00:22:38 +02:00
const labelAgeGender = `${face.gender || ''} ${face.age || ''}`;
const labelIris = face.iris ? `iris: ${face.iris}` : '';
const labelEmotion = face.emotion && face.emotion[0] ? `emotion: ${Math.trunc(100 * face.emotion[0].score)}% ${face.emotion[0].emotion}` : '';
ctx.fillStyle = ui.baseLabel;
ctx.fillText(`face ${labelAgeGender} ${labelIris} ${labelEmotion}`, face.box[0] + 2, face.box[1] + 22, face.box[2]);
2020-10-12 16:08:00 +02:00
ctx.stroke();
2020-10-13 15:59:21 +02:00
ctx.lineWidth = 1;
2020-10-12 16:08:00 +02:00
if (face.mesh) {
if (settings.getValue('Draw Points')) {
for (const point of face.mesh) {
ctx.fillStyle = `rgba(${127.5 + (2 * point[2])}, ${127.5 - (2 * point[2])}, 255, 0.5)`;
ctx.beginPath();
ctx.arc(point[0], point[1], 2, 0, 2 * Math.PI);
ctx.fill();
}
}
if (settings.getValue('Draw Polygons')) {
for (let i = 0; i < human.facemesh.triangulation.length / 3; i++) {
const points = [
human.facemesh.triangulation[i * 3 + 0],
human.facemesh.triangulation[i * 3 + 1],
human.facemesh.triangulation[i * 3 + 2],
].map((index) => face.mesh[index]);
const path = new Path2D();
path.moveTo(points[0][0], points[0][1]);
for (const point of points) {
path.lineTo(point[0], point[1]);
}
path.closePath();
2020-10-13 15:59:21 +02:00
ctx.strokeStyle = `rgba(${127.5 + (2 * points[0][2])}, ${127.5 - (2 * points[0][2])}, 255, 0.3)`;
2020-10-12 16:08:00 +02:00
ctx.stroke(path);
if (settings.getValue('Fill Polygons')) {
2020-10-13 15:59:21 +02:00
ctx.fillStyle = `rgba(${127.5 + (2 * points[0][2])}, ${127.5 - (2 * points[0][2])}, 255, 0.3)`;
2020-10-12 16:08:00 +02:00
ctx.fill(path);
}
}
}
}
}
}
2020-10-13 15:59:21 +02:00
async function drawBody(result, canvas) {
2020-10-12 16:08:00 +02:00
const ctx = canvas.getContext('2d');
2020-10-13 15:59:21 +02:00
ctx.fillStyle = ui.baseColor;
ctx.strokeStyle = ui.baseColor;
ctx.font = ui.baseFont;
ctx.lineWidth = ui.baseLineWidth;
2020-10-12 16:08:00 +02:00
for (const pose of result) {
if (settings.getValue('Draw Points')) {
for (const point of pose.keypoints) {
ctx.beginPath();
ctx.arc(point.position.x, point.position.y, 2, 0, 2 * Math.PI);
ctx.fill();
}
}
if (settings.getValue('Draw Polygons')) {
const path = new Path2D();
let part;
// torso
part = pose.keypoints.find((a) => a.part === 'leftShoulder');
path.moveTo(part.position.x, part.position.y);
part = pose.keypoints.find((a) => a.part === 'rightShoulder');
path.lineTo(part.position.x, part.position.y);
part = pose.keypoints.find((a) => a.part === 'rightHip');
path.lineTo(part.position.x, part.position.y);
part = pose.keypoints.find((a) => a.part === 'leftHip');
path.lineTo(part.position.x, part.position.y);
part = pose.keypoints.find((a) => a.part === 'leftShoulder');
path.lineTo(part.position.x, part.position.y);
// legs
part = pose.keypoints.find((a) => a.part === 'leftHip');
path.moveTo(part.position.x, part.position.y);
part = pose.keypoints.find((a) => a.part === 'leftKnee');
path.lineTo(part.position.x, part.position.y);
part = pose.keypoints.find((a) => a.part === 'leftAnkle');
path.lineTo(part.position.x, part.position.y);
part = pose.keypoints.find((a) => a.part === 'rightHip');
path.moveTo(part.position.x, part.position.y);
part = pose.keypoints.find((a) => a.part === 'rightKnee');
path.lineTo(part.position.x, part.position.y);
part = pose.keypoints.find((a) => a.part === 'rightAnkle');
path.lineTo(part.position.x, part.position.y);
// arms
part = pose.keypoints.find((a) => a.part === 'leftShoulder');
path.moveTo(part.position.x, part.position.y);
part = pose.keypoints.find((a) => a.part === 'leftElbow');
path.lineTo(part.position.x, part.position.y);
part = pose.keypoints.find((a) => a.part === 'leftWrist');
path.lineTo(part.position.x, part.position.y);
// arms
part = pose.keypoints.find((a) => a.part === 'rightShoulder');
path.moveTo(part.position.x, part.position.y);
part = pose.keypoints.find((a) => a.part === 'rightElbow');
path.lineTo(part.position.x, part.position.y);
part = pose.keypoints.find((a) => a.part === 'rightWrist');
path.lineTo(part.position.x, part.position.y);
// draw all
ctx.stroke(path);
}
}
}
2020-10-13 15:59:21 +02:00
async function drawHand(result, canvas) {
2020-10-12 16:08:00 +02:00
const ctx = canvas.getContext('2d');
2020-10-13 15:59:21 +02:00
ctx.font = ui.baseFont;
ctx.lineWidth = ui.baseLineWidth;
2020-10-12 16:08:00 +02:00
window.result = result;
for (const hand of result) {
2020-10-13 15:59:21 +02:00
if (settings.getValue('Draw Boxes')) {
ctx.lineWidth = ui.baseLineWidth;
ctx.beginPath();
2020-10-15 00:22:38 +02:00
ctx.fillStyle = ui.baseColor;
2020-10-13 15:59:21 +02:00
ctx.rect(hand.box[0], hand.box[1], hand.box[2], hand.box[3]);
2020-10-15 00:22:38 +02:00
ctx.fillStyle = ui.baseLabel;
2020-10-13 15:59:21 +02:00
ctx.fillText('hand', hand.box[0] + 2, hand.box[1] + 22, hand.box[2]);
ctx.stroke();
}
2020-10-12 16:08:00 +02:00
if (settings.getValue('Draw Points')) {
for (const point of hand.landmarks) {
ctx.fillStyle = `rgba(${127.5 + (2 * point[2])}, ${127.5 - (2 * point[2])}, 255, 0.5)`;
ctx.beginPath();
ctx.arc(point[0], point[1], 2, 0, 2 * Math.PI);
ctx.fill();
}
}
if (settings.getValue('Draw Polygons')) {
const addPart = (part) => {
2020-10-13 15:59:21 +02:00
for (let i = 1; i < part.length; i++) {
ctx.lineWidth = ui.baseLineWidth;
ctx.beginPath();
2020-10-12 16:08:00 +02:00
ctx.strokeStyle = `rgba(${127.5 + (2 * part[i][2])}, ${127.5 - (2 * part[i][2])}, 255, 0.5)`;
2020-10-13 15:59:21 +02:00
ctx.moveTo(part[i - 1][0], part[i - 1][1]);
ctx.lineTo(part[i][0], part[i][1]);
ctx.stroke();
2020-10-12 16:08:00 +02:00
}
};
addPart(hand.annotations.indexFinger);
addPart(hand.annotations.middleFinger);
addPart(hand.annotations.ringFinger);
addPart(hand.annotations.pinky);
addPart(hand.annotations.thumb);
addPart(hand.annotations.palmBase);
}
}
}
2020-10-15 15:43:16 +02:00
async function drawResults(input, result, canvas) {
// update fps
settings.setValue('FPS', Math.round(1000 / (performance.now() - timeStamp)));
// draw image from video
const ctx = canvas.getContext('2d');
ctx.drawImage(input, 0, 0, input.width, input.height, 0, 0, canvas.width, canvas.height);
// draw all results
drawFace(result.face, canvas);
drawBody(result.body, canvas);
drawHand(result.hand, canvas);
// update log
const engine = await human.tf.engine();
const memory = `${engine.state.numBytes.toLocaleString()} bytes ${engine.state.numDataBuffers.toLocaleString()} buffers ${engine.state.numTensors.toLocaleString()} tensors`;
const gpu = engine.backendInstance.numBytesInGPU ? `GPU: ${engine.backendInstance.numBytesInGPU.toLocaleString()} bytes` : '';
2020-10-15 21:25:58 +02:00
document.getElementById('log').innerText = `
TFJS Version: ${human.tf.version_core} | Backend: ${human.tf.getBackend()} | Memory: ${memory} ${gpu}
2020-10-15 15:43:16 +02:00
Performance: ${str(result.performance)} | Object size: ${(str(result)).length.toLocaleString()} bytes
`;
}
async function webWorker(input, image, canvas) {
if (!worker) {
2020-10-15 21:25:58 +02:00
log('Creating worker thread');
2020-10-15 15:43:16 +02:00
// create new webworker
worker = new Worker('demo-esm-webworker.js', { type: 'module' });
// after receiving message from webworker, parse&draw results and send new frame for processing
worker.addEventListener('message', async (msg) => {
await drawResults(input, msg.data, canvas);
// eslint-disable-next-line no-use-before-define
requestAnimationFrame(() => runHumanDetect(input, canvas)); // immediate loop
});
}
// const offscreen = image.transferControlToOffscreen();
worker.postMessage({ image, config });
}
async function runHumanDetect(input, canvas) {
2020-10-13 15:59:21 +02:00
const live = input.srcObject ? ((input.srcObject.getVideoTracks()[0].readyState === 'live') && (input.readyState > 2) && (!input.paused)) : false;
2020-10-15 15:43:16 +02:00
timeStamp = performance.now();
2020-10-13 15:59:21 +02:00
// perform detect if live video or not video at all
if (live || !(input instanceof HTMLVideoElement)) {
2020-10-15 15:43:16 +02:00
if (settings.getValue('Use Web Worker')) {
// get image data from video as we cannot send html objects to webworker
const offscreen = new OffscreenCanvas(canvas.width, canvas.height);
const ctx = offscreen.getContext('2d');
ctx.drawImage(input, 0, 0, input.width, input.height, 0, 0, canvas.width, canvas.height);
const data = ctx.getImageData(0, 0, canvas.width, canvas.height);
// perform detection
await webWorker(input, data, canvas);
} else {
2020-10-15 21:25:58 +02:00
let result = {};
try {
result = await human.detect(input, config);
} catch (err) {
log('Error during execution:', err.message);
}
2020-10-15 15:43:16 +02:00
await drawResults(input, result, canvas);
if (input.readyState) requestAnimationFrame(() => runHumanDetect(input, canvas)); // immediate loop
2020-10-13 15:59:21 +02:00
}
2020-10-12 16:08:00 +02:00
}
}
2020-10-15 21:25:58 +02:00
function setupUI() {
2020-10-13 15:59:21 +02:00
// add all variables to ui control panel
2020-10-12 16:59:55 +02:00
settings = QuickSettings.create(10, 10, 'Settings', document.getElementById('main'));
2020-10-15 00:22:38 +02:00
const style = document.createElement('style');
// style.type = 'text/css';
style.innerHTML = `
.qs_main { font: 1rem "Segoe UI"; }
.qs_label { font: 0.8rem "Segoe UI"; }
.qs_title_bar { display: none; }
.qs_content { background: darkslategray; }
.qs_container { background: transparent; color: white; margin: 6px; padding: 6px; }
.qs_checkbox_label { top: 2px; }
.qs_button { width: -webkit-fill-available; font: 1rem "Segoe UI"; cursor: pointer; }
`;
document.getElementsByTagName('head')[0].appendChild(style);
settings.addButton('Play/Pause', () => {
2020-10-14 02:52:30 +02:00
const video = document.getElementById('video');
const canvas = document.getElementById('canvas');
2020-10-15 00:22:38 +02:00
if (!video.paused) {
document.getElementById('log').innerText = 'Paused ...';
video.pause();
} else {
document.getElementById('log').innerText = 'Starting Human Library ...';
video.play();
}
2020-10-14 02:52:30 +02:00
runHumanDetect(video, canvas);
2020-10-12 16:08:00 +02:00
});
2020-10-15 21:25:58 +02:00
settings.addDropDown('Backend', ['webgl', 'wasm', 'cpu'], async (val) => config.backend = val.value);
2020-10-15 14:16:34 +02:00
settings.addHTML('title', 'Enabled Models'); settings.hideTitle('title');
2020-10-12 16:08:00 +02:00
settings.addBoolean('Face Detect', config.face.enabled, (val) => config.face.enabled = val);
settings.addBoolean('Face Mesh', config.face.mesh.enabled, (val) => config.face.mesh.enabled = val);
settings.addBoolean('Face Iris', config.face.iris.enabled, (val) => config.face.iris.enabled = val);
settings.addBoolean('Face Age', config.face.age.enabled, (val) => config.face.age.enabled = val);
settings.addBoolean('Face Gender', config.face.gender.enabled, (val) => config.face.gender.enabled = val);
2020-10-15 00:22:38 +02:00
settings.addBoolean('Face Emotion', config.face.emotion.enabled, (val) => config.face.emotion.enabled = val);
2020-10-12 16:08:00 +02:00
settings.addBoolean('Body Pose', config.body.enabled, (val) => config.body.enabled = val);
settings.addBoolean('Hand Pose', config.hand.enabled, (val) => config.hand.enabled = val);
2020-10-15 14:16:34 +02:00
settings.addHTML('title', 'Model Parameters'); settings.hideTitle('title');
2020-10-12 16:08:00 +02:00
settings.addRange('Max Objects', 1, 20, 5, 1, (val) => {
config.face.detector.maxFaces = parseInt(val);
config.body.maxDetections = parseInt(val);
});
settings.addRange('Skip Frames', 1, 20, config.face.detector.skipFrames, 1, (val) => {
config.face.detector.skipFrames = parseInt(val);
2020-10-15 00:22:38 +02:00
config.face.emotion.skipFrames = parseInt(val);
2020-10-12 16:08:00 +02:00
config.face.age.skipFrames = parseInt(val);
config.hand.skipFrames = parseInt(val);
});
settings.addRange('Min Confidence', 0.1, 1.0, config.face.detector.minConfidence, 0.05, (val) => {
config.face.detector.minConfidence = parseFloat(val);
2020-10-15 00:22:38 +02:00
config.face.emotion.minConfidence = parseFloat(val);
2020-10-12 16:08:00 +02:00
config.hand.minConfidence = parseFloat(val);
});
settings.addRange('Score Threshold', 0.1, 1.0, config.face.detector.scoreThreshold, 0.05, (val) => {
config.face.detector.scoreThreshold = parseFloat(val);
config.hand.scoreThreshold = parseFloat(val);
config.body.scoreThreshold = parseFloat(val);
});
settings.addRange('IOU Threshold', 0.1, 1.0, config.face.detector.iouThreshold, 0.05, (val) => {
config.face.detector.iouThreshold = parseFloat(val);
config.hand.iouThreshold = parseFloat(val);
});
2020-10-15 14:16:34 +02:00
settings.addHTML('title', 'UI Options'); settings.hideTitle('title');
2020-10-15 15:43:16 +02:00
settings.addBoolean('Use Web Worker', false);
2020-10-15 00:22:38 +02:00
settings.addBoolean('Draw Boxes', true);
settings.addBoolean('Draw Points', true);
settings.addBoolean('Draw Polygons', true);
settings.addBoolean('Fill Polygons', true);
settings.addHTML('line1', '<hr>'); settings.hideTitle('line1');
settings.addRange('FPS', 0, 100, 0, 1);
2020-10-12 16:08:00 +02:00
}
2020-10-13 15:59:21 +02:00
async function setupCanvas(input) {
// setup canvas object to same size as input as camera resolution may change
2020-10-12 16:08:00 +02:00
const canvas = document.getElementById('canvas');
2020-10-13 15:59:21 +02:00
canvas.width = input.width;
canvas.height = input.height;
return canvas;
2020-10-12 16:08:00 +02:00
}
2020-10-13 15:59:21 +02:00
// eslint-disable-next-line no-unused-vars
2020-10-12 16:08:00 +02:00
async function setupCamera() {
2020-10-15 21:25:58 +02:00
log('Setting up camera');
2020-10-13 15:59:21 +02:00
// setup webcam. note that navigator.mediaDevices requires that page is accessed via https
2020-10-12 16:08:00 +02:00
const video = document.getElementById('video');
2020-10-13 04:01:35 +02:00
if (!navigator.mediaDevices) {
document.getElementById('log').innerText = 'Video not supported';
2020-10-13 15:59:21 +02:00
return null;
2020-10-13 04:01:35 +02:00
}
2020-10-12 16:08:00 +02:00
const stream = await navigator.mediaDevices.getUserMedia({
audio: false,
video: { facingMode: 'user', width: window.innerWidth, height: window.innerHeight },
});
video.srcObject = stream;
return new Promise((resolve) => {
video.onloadedmetadata = () => {
video.width = video.videoWidth;
video.height = video.videoHeight;
video.play();
2020-10-15 00:22:38 +02:00
video.pause();
2020-10-14 02:52:30 +02:00
resolve(video);
2020-10-12 16:08:00 +02:00
};
});
}
2020-10-12 16:59:55 +02:00
// eslint-disable-next-line no-unused-vars
2020-10-13 15:59:21 +02:00
async function setupImage() {
2020-10-12 16:59:55 +02:00
const image = document.getElementById('image');
2020-10-13 15:59:21 +02:00
image.width = window.innerWidth;
image.height = window.innerHeight;
return new Promise((resolve) => {
image.onload = () => resolve(image);
image.src = 'sample.jpg';
});
2020-10-12 16:59:55 +02:00
}
2020-10-12 16:08:00 +02:00
async function main() {
2020-10-15 21:25:58 +02:00
log('Human starting ...');
// setup ui control panel
await setupUI();
2020-10-13 15:59:21 +02:00
// setup webcam
2020-10-15 15:43:16 +02:00
const input = await setupCamera();
2020-10-13 15:59:21 +02:00
// or setup image
2020-10-15 15:43:16 +02:00
// const input = await setupImage();
// setup output canvas from input object
await setupCanvas(input);
2020-10-15 21:25:58 +02:00
const msg = `Human ready: version: ${human.version} TensorFlow/JS version: ${human.tf.version_core}`;
document.getElementById('log').innerText = msg;
log(msg);
2020-10-13 15:59:21 +02:00
// run actual detection. if input is video, it will run in a loop else it will run only once
2020-10-15 00:22:38 +02:00
// runHumanDetect(video, canvas);
2020-10-12 16:08:00 +02:00
}
window.onload = main;
2020-10-12 16:27:22 +02:00
window.onresize = main;