2020-08-18 13:54:53 +02:00
|
|
|
import { FaceDetection } from '../classes/FaceDetection';
|
|
|
|
import { FaceLandmarks } from '../classes/FaceLandmarks';
|
|
|
|
import { FaceLandmarks68 } from '../classes/FaceLandmarks68';
|
|
|
|
import { isWithFaceDetection, WithFaceDetection } from './WithFaceDetection';
|
|
|
|
|
|
|
|
export type WithFaceLandmarks<
|
|
|
|
TSource extends WithFaceDetection<{}>,
|
2020-12-23 17:26:55 +01:00
|
|
|
TFaceLandmarks extends FaceLandmarks = FaceLandmarks68 > = TSource & {
|
|
|
|
landmarks: TFaceLandmarks
|
|
|
|
unshiftedLandmarks: TFaceLandmarks
|
|
|
|
alignedRect: FaceDetection
|
|
|
|
}
|
2020-08-18 13:54:53 +02:00
|
|
|
|
|
|
|
export function isWithFaceLandmarks(obj: any): obj is WithFaceLandmarks<WithFaceDetection<{}>, FaceLandmarks> {
|
|
|
|
return isWithFaceDetection(obj)
|
2020-12-23 17:26:55 +01:00
|
|
|
// eslint-disable-next-line dot-notation
|
2020-08-18 13:54:53 +02:00
|
|
|
&& obj['landmarks'] instanceof FaceLandmarks
|
2020-12-23 17:26:55 +01:00
|
|
|
// eslint-disable-next-line dot-notation
|
2020-08-18 13:54:53 +02:00
|
|
|
&& obj['unshiftedLandmarks'] instanceof FaceLandmarks
|
2020-12-23 17:26:55 +01:00
|
|
|
// eslint-disable-next-line dot-notation
|
|
|
|
&& obj['alignedRect'] instanceof FaceDetection;
|
2020-08-18 13:54:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function extendWithFaceLandmarks<
|
|
|
|
TSource extends WithFaceDetection<{}>,
|
2020-12-23 17:26:55 +01:00
|
|
|
TFaceLandmarks extends FaceLandmarks = FaceLandmarks68 >(sourceObj: TSource, unshiftedLandmarks: TFaceLandmarks): WithFaceLandmarks<TSource, TFaceLandmarks> {
|
|
|
|
const { box: shift } = sourceObj.detection;
|
|
|
|
const landmarks = unshiftedLandmarks.shiftBy<TFaceLandmarks>(shift.x, shift.y);
|
2020-08-18 13:54:53 +02:00
|
|
|
|
2020-12-23 17:26:55 +01:00
|
|
|
const rect = landmarks.align();
|
|
|
|
const { imageDims } = sourceObj.detection;
|
|
|
|
const alignedRect = new FaceDetection(sourceObj.detection.score, rect.rescale(imageDims.reverse()), imageDims);
|
2020-08-18 13:54:53 +02:00
|
|
|
|
|
|
|
const extension = {
|
|
|
|
landmarks,
|
|
|
|
unshiftedLandmarks,
|
2020-12-23 17:26:55 +01:00
|
|
|
alignedRect,
|
|
|
|
};
|
2020-08-18 13:54:53 +02:00
|
|
|
|
2020-12-23 17:26:55 +01:00
|
|
|
return { ...sourceObj, ...extension };
|
|
|
|
}
|