human/demo/typescript/index.ts

110 lines
3.6 KiB
TypeScript
Raw Normal View History

2021-10-27 14:16:06 +02:00
/**
* Human demo for browsers
2021-10-27 15:45:38 +02:00
* @default Human Library
* @summary <https://github.com/vladmandic/human>
* @author <https://github.com/vladmandic>
* @copyright <https://github.com/vladmandic>
* @license MIT
2021-10-27 14:16:06 +02:00
*/
2021-10-31 14:06:33 +01:00
/// <reference path="../../types/src//human.d.ts" />
2021-10-30 18:21:54 +02:00
import Human from '../../dist/human.esm.js'; // equivalent of @vladmandic/human
2021-10-27 14:16:06 +02:00
const config = {
modelBasePath: '../../models',
backend: 'humangl',
2021-10-27 15:45:38 +02:00
async: true,
2021-10-31 14:06:33 +01:00
face: { enabled: true },
body: { enabled: true },
hand: { enabled: true },
object: { enabled: false },
gesture: { enabled: true },
2021-10-27 14:16:06 +02:00
};
const human = new Human(config);
2021-10-31 14:06:33 +01:00
human.env.perfadd = false;
2021-10-27 14:16:06 +02:00
let result;
2021-10-27 15:45:38 +02:00
const dom = {
video: document.getElementById('video') as HTMLVideoElement,
canvas: document.getElementById('canvas') as HTMLCanvasElement,
log: document.getElementById('log') as HTMLPreElement,
fps: document.getElementById('status') as HTMLPreElement,
perf: document.getElementById('performance') as HTMLDivElement,
};
const fps = { detect: 0, draw: 0 };
2021-10-27 14:16:06 +02:00
2021-10-27 15:45:38 +02:00
const log = (...msg) => {
dom.log.innerText += msg.join(' ') + '\n';
// eslint-disable-next-line no-console
console.log(...msg);
};
const status = (msg) => {
dom.fps.innerText = msg;
};
const perf = (msg) => {
dom.perf.innerText = 'performance: ' + JSON.stringify(msg).replace(/"|{|}/g, '').replace(/,/g, ' | ');
};
2021-10-27 14:16:06 +02:00
async function webCam() {
status('starting webcam...');
const options = { audio: false, video: { facingMode: 'user', resizeMode: 'none', width: { ideal: document.body.clientWidth } } };
const stream: MediaStream = await navigator.mediaDevices.getUserMedia(options);
2021-10-27 15:45:38 +02:00
const ready = new Promise((resolve) => { dom.video.onloadeddata = () => resolve(true); });
dom.video.srcObject = stream;
dom.video.play();
2021-10-27 14:16:06 +02:00
await ready;
2021-10-27 15:45:38 +02:00
dom.canvas.width = dom.video.videoWidth;
dom.canvas.height = dom.video.videoHeight;
2021-10-27 14:16:06 +02:00
const track: MediaStreamTrack = stream.getVideoTracks()[0];
2021-10-28 23:25:50 +02:00
const capabilities: MediaTrackCapabilities | string = track.getCapabilities ? track.getCapabilities() : '';
const settings: MediaTrackSettings | string = track.getSettings ? track.getSettings() : '';
const constraints: MediaTrackConstraints | string = track.getConstraints ? track.getConstraints() : '';
2021-10-27 15:45:38 +02:00
log('video:', dom.video.videoWidth, dom.video.videoHeight, track.label, { stream, track, settings, constraints, capabilities });
dom.canvas.onclick = () => {
if (dom.video.paused) dom.video.play();
else dom.video.pause();
2021-10-27 14:16:06 +02:00
};
}
async function detectionLoop() {
const t0 = human.now();
2021-10-27 15:45:38 +02:00
if (!dom.video.paused) {
result = await human.detect(dom.video);
}
2021-10-27 14:16:06 +02:00
const t1 = human.now();
fps.detect = 1000 / (t1 - t0);
requestAnimationFrame(detectionLoop);
}
async function drawLoop() {
const t0 = human.now();
2021-10-27 15:45:38 +02:00
if (!dom.video.paused) {
2021-10-27 14:16:06 +02:00
const interpolated = await human.next(result);
2021-10-27 15:45:38 +02:00
await human.draw.canvas(dom.video, dom.canvas);
await human.draw.all(dom.canvas, interpolated);
perf(interpolated.performance);
2021-10-27 14:16:06 +02:00
}
const t1 = human.now();
fps.draw = 1000 / (t1 - t0);
2021-10-27 15:45:38 +02:00
status(dom.video.paused ? 'paused' : `fps: ${fps.detect.toFixed(1).padStart(5, ' ')} detect / ${fps.draw.toFixed(1).padStart(5, ' ')} draw`);
2021-10-27 14:16:06 +02:00
requestAnimationFrame(drawLoop);
}
async function main() {
2021-10-27 15:45:38 +02:00
log('human version:', human.version, 'tfjs:', human.tf.version_core);
log('platform:', human.env.platform, 'agent:', human.env.agent);
2021-10-27 14:16:06 +02:00
status('loading...');
await human.load();
status('initializing...');
2021-10-27 15:45:38 +02:00
log('backend:', human.tf.getBackend(), 'available:', human.env.backends);
2021-10-27 14:16:06 +02:00
await human.warmup();
await webCam();
await detectionLoop();
await drawLoop();
}
window.onload = main;