2020-11-09 03:14:45 +00:00
|
|
|
import { Rtsp } from "rtsp-stream/lib";
|
2020-11-27 06:15:46 +00:00
|
|
|
import { nets } from "@vladmandic/face-api";
|
2020-11-09 03:14:45 +00:00
|
|
|
import * as faceapi from "@vladmandic/face-api";
|
|
|
|
import canvas from "canvas";
|
|
|
|
import fs from "fs";
|
|
|
|
import * as path from "path";
|
|
|
|
import dotenv from "dotenv-extended";
|
2020-11-27 17:31:53 +00:00
|
|
|
import { delay, getFaceDetectorOptions, saveFile } from "../src/common";
|
2020-11-09 03:14:45 +00:00
|
|
|
require("@tensorflow/tfjs-node");
|
|
|
|
|
|
|
|
const { Canvas, Image, ImageData } = canvas;
|
|
|
|
//@ts-ignore
|
|
|
|
faceapi.env.monkeyPatch({ Canvas, Image, ImageData });
|
|
|
|
|
|
|
|
const main = async () => {
|
|
|
|
dotenv.load({
|
|
|
|
silent: false,
|
|
|
|
errorOnMissing: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
const modelDir = process.env.TRAINED_MODEL_DIR as string;
|
|
|
|
|
|
|
|
const rtsp = new Rtsp("rtsp://brandon:asdf1234@192.168.1.229/live", {
|
2020-11-27 17:31:53 +00:00
|
|
|
rate: 0.5,
|
|
|
|
image: true,
|
|
|
|
codec: "copy",
|
2020-11-09 03:14:45 +00:00
|
|
|
});
|
|
|
|
const faceDetectionNet = nets.ssdMobilenetv1;
|
|
|
|
|
|
|
|
await faceDetectionNet.loadFromDisk(path.join(__dirname, "../weights"));
|
|
|
|
await nets.faceLandmark68Net.loadFromDisk(path.join(__dirname, "../weights"));
|
|
|
|
await nets.faceRecognitionNet.loadFromDisk(
|
|
|
|
path.join(__dirname, "../weights")
|
|
|
|
);
|
|
|
|
|
2020-11-27 06:15:46 +00:00
|
|
|
const raw = fs.readFileSync(path.join(modelDir, "data.json"), "utf-8");
|
|
|
|
const content = JSON.parse(raw);
|
|
|
|
const matcher = faceapi.FaceMatcher.fromJSON(content);
|
2020-11-09 03:14:45 +00:00
|
|
|
|
|
|
|
rtsp.on("data", async (data: Buffer) => {
|
2020-11-27 06:15:46 +00:00
|
|
|
const input = ((await canvas.loadImage(data)) as unknown) as ImageData;
|
|
|
|
const out = faceapi.createCanvasFromMedia(input);
|
2020-11-27 17:31:53 +00:00
|
|
|
await saveFile("image.jpg", data);
|
2020-11-09 03:14:45 +00:00
|
|
|
const resultsQuery = await faceapi
|
2020-11-27 06:15:46 +00:00
|
|
|
.detectAllFaces(out, getFaceDetectorOptions(faceDetectionNet))
|
2020-11-09 03:14:45 +00:00
|
|
|
.withFaceLandmarks()
|
|
|
|
.withFaceDescriptors();
|
|
|
|
|
|
|
|
for (const res of resultsQuery) {
|
2020-11-27 17:31:53 +00:00
|
|
|
const bestMatch = matcher.matchDescriptor(res.descriptor);
|
|
|
|
console.log("Face Detected: " + bestMatch.label);
|
2020-11-09 03:14:45 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
rtsp.on("error", (err) => {
|
2020-11-27 17:31:53 +00:00
|
|
|
// console.log(err);
|
2020-11-09 03:14:45 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
rtsp.start();
|
|
|
|
};
|
|
|
|
|
|
|
|
main();
|