fix(lint) make sure eslint also runs on TypeScript files (#11777)

Co-authored-by: robertpin <robert.pin9@gmail.com>
Co-authored-by: Gabriel Borlea <gabriel.borlea@8x8.com>
This commit is contained in:
Saúl Ibarra Corretgé
2022-07-11 14:30:37 +02:00
committed by GitHub
parent 61a6ce2a2e
commit b0deb9ec0c
49 changed files with 1063 additions and 200 deletions

View File

@@ -36,7 +36,7 @@ export interface FaceLandmarksHelper {
}
/**
* Helper class for human library
* Helper class for human library.
*/
export class HumanHelper implements FaceLandmarksHelper {
protected human: Human | undefined;
@@ -44,6 +44,7 @@ export class HumanHelper implements FaceLandmarksHelper {
protected baseUrl: string;
private detectionInProgress = false;
private lastValidFaceBox: FaceBox | undefined;
/**
* Configuration for human.
*/
@@ -66,7 +67,7 @@ export class HumanHelper implements FaceLandmarksHelper {
},
mesh: { enabled: false },
iris: { enabled: false },
emotion: {
emotion: {
enabled: false,
modelPath: 'emotion.json'
},
@@ -78,12 +79,23 @@ export class HumanHelper implements FaceLandmarksHelper {
segmentation: { enabled: false }
};
/**
* Constructor function for the helper which initialize the helper.
*
* @param {InitInput} input - The input for the helper.
* @returns {void}
*/
constructor({ baseUrl, detectionTypes }: InitInput) {
this.faceDetectionTypes = detectionTypes;
this.baseUrl = baseUrl;
this.init();
}
/**
* Initializes the human helper with the available tfjs backend for the given detection types.
*
* @returns {Promise<void>}
*/
async init(): Promise<void> {
if (!this.human) {
@@ -95,7 +107,7 @@ export class HumanHelper implements FaceLandmarksHelper {
}
if (this.faceDetectionTypes.length > 0 && this.config.face) {
this.config.face.enabled = true
this.config.face.enabled = true;
}
if (this.faceDetectionTypes.includes(DETECTION_TYPES.FACE_BOX) && this.config.face?.detector) {
@@ -107,16 +119,24 @@ export class HumanHelper implements FaceLandmarksHelper {
}
const initialHuman = new Human(this.config);
try {
await initialHuman.load();
} catch (err) {
console.error(err);
}
this.human = initialHuman;
}
}
/**
* Gets the face box from the detections, if there is no valid detections it will return undefined..
*
* @param {Array<FaceResult>} detections - The array with the detections.
* @param {number} threshold - Face box position change threshold.
* @returns {FaceBox | undefined}
*/
getFaceBox(detections: Array<FaceResult>, threshold: number): FaceBox | undefined {
if (this.getFaceCount(detections) !== 1) {
return;
@@ -127,18 +147,24 @@ export class HumanHelper implements FaceLandmarksHelper {
left: Math.round(detections[0].boxRaw[0] * 100),
right: Math.round((detections[0].boxRaw[0] + detections[0].boxRaw[2]) * 100)
};
faceBox.width = Math.round(faceBox.right - faceBox.left);
if (this.lastValidFaceBox && threshold && Math.abs(this.lastValidFaceBox.left - faceBox.left) < threshold) {
return;
}
this.lastValidFaceBox = faceBox;
return faceBox;
}
/**
* Gets the face expression from the detections, if there is no valid detections it will return undefined.
*
* @param {Array<FaceResult>} detections - The array with the detections.
* @returns {string | undefined}
*/
getFaceExpression(detections: Array<FaceResult>): string | undefined {
if (this.getFaceCount(detections) !== 1) {
return;
@@ -149,6 +175,12 @@ export class HumanHelper implements FaceLandmarksHelper {
}
}
/**
* Gets the face count from the detections, which is the number of detections.
*
* @param {Array<FaceResult>} detections - The array with the detections.
* @returns {number}
*/
getFaceCount(detections: Array<FaceResult> | undefined): number {
if (detections) {
return detections.length;
@@ -157,44 +189,56 @@ export class HumanHelper implements FaceLandmarksHelper {
return 0;
}
/**
* Gets the detections from the image captured from the track.
*
* @param {ImageBitmap | ImageData} image - The image captured from the track,
* if OffscreenCanvas available it will be ImageBitmap, otherwise it will be ImageData.
* @returns {Promise<Array<FaceResult>>}
*/
async getDetections(image: ImageBitmap | ImageData): Promise<Array<FaceResult>> {
if (!this.human || !this.faceDetectionTypes.length) {
return [];
}
this.human.tf.engine().startScope();
const imageTensor = this.human.tf.browser.fromPixels(image);
const { face: detections } = await this.human.detect(imageTensor, this.config);
this.human.tf.engine().endScope();
return detections.filter(detection => detection.score > FACE_DETECTION_SCORE_THRESHOLD);
}
return detections.filter(detection => detection.score > FACE_DETECTION_SCORE_THRESHOLD);
}
/**
* Gathers together all the data from the detections, it's the function that will be called in the worker.
*
* @param {DetectInput} input - The input for the detections.
* @returns {Promise<DetectOutput>}
*/
public async detect({ image, threshold } : DetectInput): Promise<DetectOutput> {
let detections;
let faceExpression;
let faceBox;
this.detectionInProgress = true;
detections = await this.getDetections(image);
const detections = await this.getDetections(image);
if (this.faceDetectionTypes.includes(DETECTION_TYPES.FACE_EXPRESSIONS)) {
faceExpression = this.getFaceExpression(detections);
}
if (this.faceDetectionTypes.includes(DETECTION_TYPES.FACE_BOX)) {
//if more than one face is detected the face centering will be disabled.
if (this.getFaceCount(detections) > 1 ) {
// if more than one face is detected the face centering will be disabled.
if (this.getFaceCount(detections) > 1) {
this.faceDetectionTypes.splice(this.faceDetectionTypes.indexOf(DETECTION_TYPES.FACE_BOX), 1);
//face-box for re-centering
// face-box for re-centering
faceBox = {
left: 0,
right: 100,
width: 100,
width: 100
};
} else {
faceBox = this.getFaceBox(detections, threshold);
@@ -204,14 +248,19 @@ export class HumanHelper implements FaceLandmarksHelper {
this.detectionInProgress = false;
return {
faceExpression,
return {
faceExpression,
faceBox,
faceCount: this.getFaceCount(detections)
}
};
}
/**
* Returns the detection state.
*
* @returns {boolean}
*/
public getDetectionInProgress(): boolean {
return this.detectionInProgress;
}
}
}

View File

@@ -59,4 +59,4 @@ export const DETECTION_TYPES = {
/**
* Threshold for detection score of face.
*/
export const FACE_DETECTION_SCORE_THRESHOLD = 0.6;
export const FACE_DETECTION_SCORE_THRESHOLD = 0.6;

View File

@@ -1,5 +1,5 @@
import { FaceLandmarksHelper, HumanHelper } from './FaceLandmarksHelper';
import { DETECT_FACE, INIT_WORKER } from './constants';
import { FaceLandmarksHelper, HumanHelper }from './FaceLandmarksHelper';
let helper: FaceLandmarksHelper;