homebridge-face-location/src/common.ts

68 lines
1.5 KiB
TypeScript
Raw Normal View History

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;
// TinyFaceDetectorOptions
2020-12-12 16:01:23 +00:00
export const inputSize = 416;
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 (
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> => {
return new Promise(async (resolve, reject) => {
2020-12-12 05:14:56 +00:00
if (!fs.existsSync(path)) {
fs.mkdir(path, async (err) => {
if (err) {
return reject(err);
}
2020-12-12 05:14:56 +00:00
return resolve();
});
}
2020-12-12 05:14:56 +00:00
return resolve();
});
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);
});
};