homebridge-face-location/src/common.ts

80 lines
1.7 KiB
TypeScript
Raw Permalink 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;
2020-12-19 22:09:39 +00:00
export const getFaceDetectorOptions = (
net: faceapi.NeuralNetwork<any>,
options?: {
confidence?: number;
inputSize?: number;
scoreThreshold?: number;
}
) => {
return net === faceapi.nets.ssdMobilenetv1
2020-12-19 22:09:39 +00:00
? new faceapi.SsdMobilenetv1Options({
minConfidence: options?.confidence ?? minConfidence,
})
: new faceapi.TinyFaceDetectorOptions({
inputSize: options?.inputSize ?? inputSize,
scoreThreshold: options?.scoreThreshold ?? 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);
});
};