human/src/face/faceboxes.ts

67 lines
2.8 KiB
TypeScript
Raw Normal View History

2022-05-30 14:58:54 +02:00
// https://github.com/TropComplique/FaceBoxes-tensorflow
2022-10-17 02:28:57 +02:00
import * as tf from 'dist/tfjs.esm.js';
2022-05-30 14:58:54 +02:00
import { log } from '../util/util';
2022-07-02 09:39:40 +02:00
import { loadModel } from '../tfjs/load';
2022-05-30 14:58:54 +02:00
import type { GraphModel, Tensor } from '../tfjs/types';
import type { Config } from '../config';
type Box = [number, number, number, number];
export class FaceBoxes {
enlarge: number;
model: GraphModel;
config: Config;
inputSize: 0;
constructor(model, config: Config) {
this.enlarge = 1.1;
this.model = model;
this.config = config;
this.inputSize = model.inputs[0].shape ? model.inputs[0].shape[2] : 0;
}
async estimateFaces(input, config) {
if (config) this.config = config;
2022-08-21 19:34:51 +02:00
const results: { confidence: number, box: Box, boxRaw: Box, image: Tensor }[] = [];
2022-05-30 14:58:54 +02:00
const resizeT = tf.image.resizeBilinear(input, [this.inputSize, this.inputSize]);
const castT = resizeT.toInt();
const [scoresT, boxesT, numT] = await this.model.executeAsync(castT) as Tensor[];
2022-06-02 16:39:53 +02:00
const scores = await scoresT.data();
2022-05-30 14:58:54 +02:00
const squeezeT = tf.squeeze(boxesT);
2022-10-17 02:28:57 +02:00
const boxes = squeezeT.arraySync() as number[][];
2022-05-30 14:58:54 +02:00
scoresT.dispose();
boxesT.dispose();
squeezeT.dispose();
numT.dispose();
castT.dispose();
resizeT.dispose();
2022-10-17 02:28:57 +02:00
for (let i = 0; i < boxes.length; i++) {
2022-08-21 19:34:51 +02:00
if (scores[i] && scores[i] > (this.config.face.detector?.minConfidence || 0.1)) {
2022-05-30 14:58:54 +02:00
const crop = [boxes[i][0] / this.enlarge, boxes[i][1] / this.enlarge, boxes[i][2] * this.enlarge, boxes[i][3] * this.enlarge];
const boxRaw: Box = [crop[1], crop[0], (crop[3]) - (crop[1]), (crop[2]) - (crop[0])];
const box: Box = [
parseInt((boxRaw[0] * input.shape[2]).toString()),
parseInt((boxRaw[1] * input.shape[1]).toString()),
parseInt((boxRaw[2] * input.shape[2]).toString()),
parseInt((boxRaw[3] * input.shape[1]).toString())];
const resized = tf.image.cropAndResize(input, [crop], [0], [this.inputSize, this.inputSize]);
const image = tf.div(resized, [255]);
resized.dispose();
results.push({ confidence: scores[i], box, boxRaw, image });
// add mesh, meshRaw, annotations,
}
}
return results;
}
}
2022-08-21 21:23:03 +02:00
export async function load(config: Config) {
2022-07-02 09:39:40 +02:00
const model = await loadModel(config.face.detector?.modelPath);
2022-08-21 21:23:03 +02:00
if (config.face.detector?.modelPath && config.debug) log(`load model: ${config.face.detector.modelPath?.match(/\/(.*)\./)?.[1] || ''}`);
2022-05-30 14:58:54 +02:00
const faceboxes = new FaceBoxes(model, config);
2022-08-21 21:23:03 +02:00
if (config.face.mesh?.enabled && config.face.mesh?.modelPath && config.debug) log(`load model: ${config.face.mesh.modelPath.match(/\/(.*)\./)?.[1] || ''}`);
if (config.face.iris?.enabled && config.face.iris?.modelPath && config.debug) log(`load model: ${config.face.iris.modelPath?.match(/\/(.*)\./)?.[1] || ''}`);
2022-05-30 14:58:54 +02:00
return faceboxes;
}