Training into a singular dataset file

This commit is contained in:
watsonb8 2020-11-27 01:15:46 -05:00
parent 53c1b162ae
commit 6268efe517
2 changed files with 39 additions and 38 deletions

View File

@ -1,5 +1,5 @@
import { Rtsp } from "rtsp-stream/lib";
import { FaceMatcher, nets } from "@vladmandic/face-api";
import { nets } from "@vladmandic/face-api";
import * as faceapi from "@vladmandic/face-api";
import canvas from "canvas";
import fs from "fs";
@ -31,28 +31,23 @@ const main = async () => {
path.join(__dirname, "../weights")
);
const files = fs.readdirSync(modelDir);
const matchers: Array<FaceMatcher> = [];
for (const file of files) {
const raw = fs.readFileSync(path.join(modelDir, file), "utf-8");
const content = JSON.parse(raw);
matchers.push(FaceMatcher.fromJSON(content));
}
const raw = fs.readFileSync(path.join(modelDir, "data.json"), "utf-8");
const content = JSON.parse(raw);
const matcher = faceapi.FaceMatcher.fromJSON(content);
rtsp.on("data", async (data: Buffer) => {
const img = new Image();
img.src = data.toString("base64");
const input = await canvas.loadImage(data, "base64");
const input = ((await canvas.loadImage(data)) as unknown) as ImageData;
const out = faceapi.createCanvasFromMedia(input);
// fs.writeFileSync(path.join(__dirname, "image.jpg"), data, "base64");
const resultsQuery = await faceapi
.detectAllFaces(input, getFaceDetectorOptions(faceDetectionNet))
.detectAllFaces(out, getFaceDetectorOptions(faceDetectionNet))
.withFaceLandmarks()
.withFaceDescriptors();
for (const res of resultsQuery) {
for (const matcher of matchers) {
const bestMatch = matcher.findBestMatch(res.descriptor);
console.log(bestMatch.label);
}
const bestMatch = matcher.findBestMatch(res.descriptor);
console.log(bestMatch.label);
}
});

View File

@ -2,7 +2,7 @@ import * as faceapi from "@vladmandic/face-api";
import canvas from "canvas";
import fs, { lstatSync } from "fs";
import * as path from "path";
import { TNetInput } from "@vladmandic/face-api";
import { LabeledFaceDescriptors, TNetInput } from "@vladmandic/face-api";
import * as mime from "mime-types";
import dotenv from "dotenv-extended";
import { getFaceDetectorOptions } from "../src/common";
@ -33,6 +33,7 @@ const main = async () => {
const dirs = fs.readdirSync(inputDir);
const refs: Array<LabeledFaceDescriptors> = [];
for (const dir of dirs) {
if (!lstatSync(path.join(inputDir, dir)).isDirectory()) {
continue;
@ -54,11 +55,15 @@ const main = async () => {
)) as unknown;
const descriptor = await faceapi
.detectAllFaces(referenceImage as TNetInput, options)
.detectSingleFace(referenceImage as TNetInput, options)
.withFaceLandmarks()
.withFaceDescriptors();
.withFaceDescriptor();
if (!descriptor || !descriptor.descriptor) {
throw new Error("No face found");
}
return descriptor.length > 0 ? descriptor : undefined;
const faceDescriptors = [descriptor.descriptor];
return new faceapi.LabeledFaceDescriptors(dir, faceDescriptors);
} catch (err) {
console.log(
"An error occurred loading image at path: " +
@ -69,26 +74,27 @@ const main = async () => {
})
);
const items = [];
for (const item of referenceResults) {
if (item) {
items.push(...item);
}
if (referenceResults) {
refs.push(
...(referenceResults.filter((e) => e) as LabeledFaceDescriptors[])
);
}
const faceMatcher = new faceapi.FaceMatcher(items);
fs.writeFile(
path.join(outDir, dir + ".json"),
JSON.stringify(faceMatcher.toJSON()),
"utf8",
(err) => {
if (err) {
console.log(`An error occurred while writing ${dir} model to file`);
}
console.log(`Successfully wrote ${dir} model to file`);
}
);
}
const faceMatcher = new faceapi.FaceMatcher(refs);
fs.writeFile(
path.join(outDir, "data.json"),
JSON.stringify(faceMatcher.toJSON()),
"utf8",
(err) => {
if (err) {
console.log(`An error occurred while writing data model to file`);
}
console.log(`Successfully wrote data model to file`);
}
);
};
main();