From 57eb43c4bc81a767e14850efeb0da074399e0a82 Mon Sep 17 00:00:00 2001 From: watsonb8 Date: Fri, 27 Nov 2020 12:31:53 -0500 Subject: [PATCH] Consistently able to stream and detect --- package-lock.json | 21 ++++++++++-- package.json | 3 ++ scripts/streamAndDetect.ts | 15 +++++---- src/common.ts | 10 +++++- tsconfig.face-location.json | 64 ------------------------------------- tsconfig.json | 19 +++++------ 6 files changed, 46 insertions(+), 86 deletions(-) delete mode 100644 tsconfig.face-location.json diff --git a/package-lock.json b/package-lock.json index ac033d6..b87c5ed 100644 --- a/package-lock.json +++ b/package-lock.json @@ -42,6 +42,13 @@ "@types/webgl-ext": "0.0.30", "@types/webgl2": "0.0.4", "seedrandom": "2.4.3" + }, + "dependencies": { + "@types/webgl2": { + "version": "0.0.4", + "resolved": "https://registry.npmjs.org/@types/webgl2/-/webgl2-0.0.4.tgz", + "integrity": "sha512-PACt1xdErJbMUOUweSrbVM7gSIYm1vTncW2hF6Os/EeWi6TXYAYMPp+8v6rzHmypE5gHrxaxZNXgMkJVIdZpHw==" + } } }, "@tensorflow/tfjs-converter": { @@ -60,6 +67,13 @@ "@types/webgl2": "0.0.4", "node-fetch": "~2.6.1", "seedrandom": "2.4.3" + }, + "dependencies": { + "@types/webgl2": { + "version": "0.0.4", + "resolved": "https://registry.npmjs.org/@types/webgl2/-/webgl2-0.0.4.tgz", + "integrity": "sha512-PACt1xdErJbMUOUweSrbVM7gSIYm1vTncW2hF6Os/EeWi6TXYAYMPp+8v6rzHmypE5gHrxaxZNXgMkJVIdZpHw==" + } } }, "@tensorflow/tfjs-data": { @@ -127,9 +141,10 @@ "integrity": "sha512-LKVgNmBxN0BbljJrVUwkxwRYqzsAEPcZOe6S2T6ZaBDIrFp0qu4FNlpc5sM1tGbXUYFgdVQIoeLk1Y1UoblyEg==" }, "@types/webgl2": { - "version": "0.0.4", - "resolved": "https://registry.npmjs.org/@types/webgl2/-/webgl2-0.0.4.tgz", - "integrity": "sha512-PACt1xdErJbMUOUweSrbVM7gSIYm1vTncW2hF6Os/EeWi6TXYAYMPp+8v6rzHmypE5gHrxaxZNXgMkJVIdZpHw==" + "version": "0.0.5", + "resolved": "https://registry.npmjs.org/@types/webgl2/-/webgl2-0.0.5.tgz", + "integrity": "sha512-oGaKsBbxQOY5+aJFV3KECDhGaXt+yZJt2y/OZsnQGLRkH6Fvr7rv4pCt3SRH1somIHfej/c4u7NSpCyd9x+1Ow==", + "dev": true }, "@vladmandic/face-api": { "version": "0.8.8", diff --git a/package.json b/package.json index 4815b4a..acccbf4 100644 --- a/package.json +++ b/package.json @@ -31,5 +31,8 @@ "rtsp-stream": "file:../rtsp-stream", "ts-node": "^9.0.0", "typescript": "^4.0.5" + }, + "devDependencies": { + "@types/webgl2": "0.0.5" } } diff --git a/scripts/streamAndDetect.ts b/scripts/streamAndDetect.ts index 95f6cc2..e8dedfb 100644 --- a/scripts/streamAndDetect.ts +++ b/scripts/streamAndDetect.ts @@ -5,7 +5,7 @@ import canvas from "canvas"; import fs from "fs"; import * as path from "path"; import dotenv from "dotenv-extended"; -import { getFaceDetectorOptions, saveFile } from "../src/common"; +import { delay, getFaceDetectorOptions, saveFile } from "../src/common"; require("@tensorflow/tfjs-node"); const { Canvas, Image, ImageData } = canvas; @@ -21,7 +21,9 @@ const main = async () => { const modelDir = process.env.TRAINED_MODEL_DIR as string; const rtsp = new Rtsp("rtsp://brandon:asdf1234@192.168.1.229/live", { - rate: 10, + rate: 0.5, + image: true, + codec: "copy", }); const faceDetectionNet = nets.ssdMobilenetv1; @@ -38,21 +40,20 @@ const main = async () => { rtsp.on("data", async (data: Buffer) => { const input = ((await canvas.loadImage(data)) as unknown) as ImageData; const out = faceapi.createCanvasFromMedia(input); - - // fs.writeFileSync(path.join(__dirname, "image.jpg"), data, "base64"); + await saveFile("image.jpg", data); const resultsQuery = await faceapi .detectAllFaces(out, getFaceDetectorOptions(faceDetectionNet)) .withFaceLandmarks() .withFaceDescriptors(); for (const res of resultsQuery) { - const bestMatch = matcher.findBestMatch(res.descriptor); - console.log(bestMatch.label); + const bestMatch = matcher.matchDescriptor(res.descriptor); + console.log("Face Detected: " + bestMatch.label); } }); rtsp.on("error", (err) => { - console.log(err); + // console.log(err); }); rtsp.start(); diff --git a/src/common.ts b/src/common.ts index 87332b5..50d2541 100644 --- a/src/common.ts +++ b/src/common.ts @@ -21,5 +21,13 @@ export function saveFile(fileName: string, buf: Buffer) { fs.mkdirSync(baseDir); } - fs.writeFileSync(path.resolve(baseDir, fileName), buf); + fs.writeFileSync(path.resolve(baseDir, fileName), buf, "base64"); } + +export const delay = (ms: number): Promise => { + return new Promise((resolve) => { + setTimeout(() => { + resolve(); + }, ms); + }); +}; diff --git a/tsconfig.face-location.json b/tsconfig.face-location.json deleted file mode 100644 index 28c576d..0000000 --- a/tsconfig.face-location.json +++ /dev/null @@ -1,64 +0,0 @@ -{ - "compilerOptions": { - /* Basic Options */ - // "incremental": true, /* Enable incremental compilation */ - "target": "es5" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */, - "module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */, - // "lib": [], /* Specify library files to be included in the compilation. */ - // "allowJs": true, /* Allow javascript files to be compiled. */ - // "checkJs": true, /* Report errors in .js files. */ - // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */ - // "declaration": true, /* Generates corresponding '.d.ts' file. */ - // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ - "sourceMap": true /* Generates corresponding '.map' file. */, - // "outFile": "./", /* Concatenate and emit output to single file. */ - "outDir": "./bin" /* Redirect output structure to the directory. */, - "rootDir": "./src" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */, - "composite": true /* Enable project compilation */, - // "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */ - // "removeComments": true, /* Do not emit comments to output. */ - // "noEmit": true, /* Do not emit outputs. */ - // "importHelpers": true, /* Import emit helpers from 'tslib'. */ - // "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */ - // "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */ - /* Strict Type-Checking Options */ - "strict": true /* Enable all strict type-checking options. */, - "noImplicitAny": true /* Raise error on expressions and declarations with an implied 'any' type. */, - // "strictNullChecks": true, /* Enable strict null checks. */ - "strictFunctionTypes": true /* Enable strict checking of function types. */, - // "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */ - // "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */ - // "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */ - // "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */ - /* Additional Checks */ - // "noUnusedLocals": true, /* Report errors on unused locals. */ - // "noUnusedParameters": true, /* Report errors on unused parameters. */ - "noImplicitReturns": true /* Report error when not all code paths in function return a value. */, - "noFallthroughCasesInSwitch": true /* Report errors for fallthrough cases in switch statement. */, - /* Module Resolution Options */ - // "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ - "baseUrl": "./" /* Base directory to resolve non-absolute module names. */, - "paths": { - "rtsp/*": ["./node_modules/rtsp-stream/lib/*"] - } /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */, - // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */ - // "typeRoots": [], /* List of folders to include type definitions from. */ - // "types": [], /* Type declaration files to be included in compilation. */ - // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */ - "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */, - // "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */ - // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ - /* Source Map Options */ - // "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */ - // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ - // "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */ - // "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */ - /* Experimental Options */ - // "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */ - // "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */ - /* Advanced Options */ - "skipLibCheck": true, - "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */ - }, - "include": ["./src"] -} diff --git a/tsconfig.json b/tsconfig.json index a97b4e1..b5b8cc8 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,14 +1,4 @@ { - "references": [ - { - "path": "./node_modules/rtsp-stream/tsconfig.json" - }, - { - "path": "./tsconfig.face-location.json" - } - ], - "files": [], - "exclude": ["node_modules/*"], "compilerOptions": { /* Basic Options */ // "incremental": true, /* Enable incremental compilation */ @@ -69,5 +59,12 @@ /* Advanced Options */ "skipLibCheck": true, "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */ - } + }, + "include": ["./src"], + "exclude": ["node_modules"], + "references": [ + { + "path": "./node_modules/rtsp-stream/tsconfig.json" + } + ] }