2020-11-09 03:14:45 +00:00
|
|
|
import * as faceapi from "@vladmandic/face-api";
|
|
|
|
import * as path from "path";
|
|
|
|
import fs from "fs";
|
|
|
|
|
|
|
|
// SsdMobilenetv1Options
|
2020-12-11 18:39:30 +00:00
|
|
|
export const minConfidence = 0.4;
|
2020-11-09 03:14:45 +00:00
|
|
|
|
|
|
|
// TinyFaceDetectorOptions
|
2020-12-12 16:01:23 +00:00
|
|
|
export const inputSize = 416;
|
2020-11-09 03:14:45 +00:00
|
|
|
export const scoreThreshold = 0.5;
|
|
|
|
|
|
|
|
export const getFaceDetectorOptions = (net: faceapi.NeuralNetwork<any>) => {
|
|
|
|
return net === faceapi.nets.ssdMobilenetv1
|
|
|
|
? new faceapi.SsdMobilenetv1Options({ minConfidence })
|
|
|
|
: new faceapi.TinyFaceDetectorOptions({ inputSize, scoreThreshold });
|
|
|
|
};
|
|
|
|
|
2020-12-12 05:14:56 +00:00
|
|
|
export const saveFile = async (
|
2020-12-11 05:02:10 +00:00
|
|
|
basePath: string,
|
|
|
|
fileName: string,
|
|
|
|
buf: Buffer
|
2020-12-12 05:14:56 +00:00
|
|
|
): Promise<void> => {
|
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
try {
|
|
|
|
//Create directory if it does not exist
|
|
|
|
await makeDirectory(basePath);
|
|
|
|
} catch (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
//Write file to directory
|
|
|
|
try {
|
|
|
|
const asdf = fs.writeFileSync(
|
|
|
|
path.join(basePath, fileName),
|
|
|
|
buf,
|
|
|
|
"base64"
|
|
|
|
);
|
|
|
|
} catch (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
return resolve();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const makeDirectory = (path: string): Promise<void> => {
|
2020-12-11 05:02:10 +00:00
|
|
|
return new Promise(async (resolve, reject) => {
|
2020-12-12 05:14:56 +00:00
|
|
|
if (!fs.existsSync(path)) {
|
|
|
|
fs.mkdir(path, async (err) => {
|
2020-12-11 05:02:10 +00:00
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
2020-12-12 05:14:56 +00:00
|
|
|
|
|
|
|
return resolve();
|
2020-12-11 05:02:10 +00:00
|
|
|
});
|
|
|
|
}
|
2020-12-12 05:14:56 +00:00
|
|
|
|
|
|
|
return resolve();
|
2020-12-11 05:02:10 +00:00
|
|
|
});
|
2020-12-12 05:14:56 +00:00
|
|
|
};
|
2020-11-27 17:31:53 +00:00
|
|
|
|
|
|
|
export const delay = (ms: number): Promise<void> => {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
setTimeout(() => {
|
|
|
|
resolve();
|
|
|
|
}, ms);
|
|
|
|
});
|
|
|
|
};
|