human/demo/typescript/index.js

103 lines
3.2 KiB
JavaScript
Raw Normal View History

2021-10-27 14:16:06 +02:00
/*
Human
homepage: <https://github.com/vladmandic/human>
author: <https://github.com/vladmandic>'
*/
// demo/typescript/index.ts
import Human from "../../dist/human.custom.esm.js";
var config = {
modelBasePath: "../../models",
2021-10-27 15:45:38 +02:00
backend: "humangl",
async: true
2021-10-27 14:16:06 +02:00
};
var human = new Human(config);
var result;
2021-10-27 15:45:38 +02:00
var dom = {
video: document.getElementById("video"),
canvas: document.getElementById("canvas"),
log: document.getElementById("log"),
fps: document.getElementById("status"),
perf: document.getElementById("performance")
};
var fps = { detect: 0, draw: 0 };
var log = (...msg) => {
dom.log.innerText += msg.join(" ") + "\n";
console.log(...msg);
};
2021-10-27 14:16:06 +02:00
var status = (msg) => {
2021-10-27 15:45:38 +02:00
dom.fps.innerText = msg;
};
var 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 = await navigator.mediaDevices.getUserMedia(options);
const ready = new Promise((resolve) => {
2021-10-27 15:45:38 +02:00
dom.video.onloadeddata = () => resolve(true);
2021-10-27 14:16:06 +02:00
});
2021-10-27 15:45:38 +02:00
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 = stream.getVideoTracks()[0];
const capabilities = track.getCapabilities();
const settings = track.getSettings();
const constraints = 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();
2021-10-27 14:16:06 +02:00
else
2021-10-27 15:45:38 +02:00
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 = 1e3 / (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 = 1e3 / (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);
human.env.perfadd = true;
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;
2021-10-27 15:45:38 +02:00
/**
* Human demo for browsers
* @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
//# sourceMappingURL=index.js.map