Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to @vladmandic/human #1218

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/Service/FaceClusterAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class FaceClusterAnalyzer {
public const MIN_DETECTION_SIZE = 0.03;
public const MIN_CLUSTER_SEPARATION = 0.35;
public const MAX_CLUSTER_EDGE_LENGTH = 0.5;
public const DIMENSIONS = 128;
public const DIMENSIONS = 1024;
public const MAX_OVERLAP_NEW_CLUSTER = 0.1;
public const MIN_OVERLAP_EXISTING_CLUSTER = 0.5;

Expand Down Expand Up @@ -186,7 +186,7 @@ public function calculateClusters(string $userId, int $batchSize = 0): void {
* @return list<float>
*/
public static function calculateCentroidOfDetections(array $detections): array {
// init 128 dimensional vector
// init 1024 dimensional vector
/** @var list<float> $sum */
$sum = [];
for ($i = 0; $i < self::DIMENSIONS; $i++) {
Expand Down
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"@tensorflow/tfjs-backend-wasm": "4.x",
"@tensorflow/tfjs-node": "4.x",
"@tensorflow/tfjs-node-gpu": "4.x",
"@vladmandic/face-api": "^1.7.11",
"@vladmandic/human": "^3.3.4",
"download": "^8.0.0",
"execa": "^5.1.1",
"exifer": "^1.0.0-beta.2",
Expand Down
67 changes: 46 additions & 21 deletions src/classifier_faces.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,60 @@
const path = require('path')

Check failure on line 1 in src/classifier_faces.js

View workflow job for this annotation

GitHub Actions / NPM lint

'path' is assigned a value but never used
const fs = require('fs/promises')

let tf, faceapi, Jimp
let tf, Human, Jimp, wasm
let PUREJS = false
if (process.env.RECOGNIZE_PUREJS === 'true') {
tf = require('@tensorflow/tfjs')
require('@tensorflow/tfjs-backend-wasm')
faceapi = require('@vladmandic/face-api/dist/face-api.node-wasm.js')
wasm = require('@tensorflow/tfjs-backend-wasm')
Human = require('@vladmandic/human/dist/human.node-wasm.js')
Jimp = require('jimp')
PUREJS = true
} else {
try {
if (process.env.RECOGNIZE_GPU === 'true') {
tf = require('@tensorflow/tfjs-node-gpu')
faceapi = require('@vladmandic/face-api/dist/face-api.node-gpu.js')
Human = require('@vladmandic/human/dist/human.node-gpu.js')
} else {
tf = require('@tensorflow/tfjs-node')
faceapi = require('@vladmandic/face-api/dist/face-api.node.js')
Human = require('@vladmandic/human/dist/human.node.js')
}
} catch (e) {
console.error(e)
console.error('Trying js-only mode')
tf = require('@tensorflow/tfjs')
require('@tensorflow/tfjs-backend-wasm')
faceapi = require('@vladmandic/face-api/dist/face-api.node-wasm.js')
wasm = require('@tensorflow/tfjs-backend-wasm')
Human = require('@vladmandic/human/dist/human.node-wasm.js')
Jimp = require('jimp')
PUREJS = true
}
}

if (process.argv.length < 3) throw new Error('Incorrect arguments: node classifier_faces.js ...<IMAGE_FILES> | node classify.js -')

const config = {
cacheSensitivity: 0.01,
//modelBasePath: 'file://node_modules/@vladmandic/human/models/',

Check failure on line 35 in src/classifier_faces.js

View workflow job for this annotation

GitHub Actions / NPM lint

Expected space or tab after '//' in comment
modelBasePath: 'https://vladmandic.github.io/human-models/models/',
backend: PUREJS ? 'wasm' : 'tensorflow',
//wasmPath: 'file://node_modules/@tensorflow/tfjs-backend-wasm/dist/',

Check failure on line 38 in src/classifier_faces.js

View workflow job for this annotation

GitHub Actions / NPM lint

Expected space or tab after '//' in comment
wasmPath: `https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-backend-wasm@${tf.version_core}/dist/`,
debug: false,
async: false,
softwareKernels: true,
face: {
enabled: true,
detector: { rotation: false, maxDetected: 100 },
iris: { enabled: true },
description: { enabled: true },
antispoof: { enabled: true },
liveness: { enabled: true },
},
hand: { enabled: false },
body: { enabled: false },
object: { enabled: false },
segmentation: { enabled: false },
filter: { enabled: false },
}

/**
*
*/
Expand All @@ -43,9 +67,8 @@
paths = process.argv.slice(2)
}

await faceapi.nets.ssdMobilenetv1.loadFromDisk(path.resolve(__dirname, '..', 'node_modules/@vladmandic/face-api/model'))
await faceapi.nets.faceLandmark68Net.loadFromDisk(path.resolve(__dirname, '..', 'node_modules/@vladmandic/face-api/model'))
await faceapi.nets.faceRecognitionNet.loadFromDisk(path.resolve(__dirname, '..', 'node_modules/@vladmandic/face-api/model'))
Human.env.updateBackend();

Check failure on line 70 in src/classifier_faces.js

View workflow job for this annotation

GitHub Actions / NPM lint

Extra semicolon
const human = new Human.Human(config)

for (const path of paths) {
try {
Expand All @@ -55,19 +78,18 @@
} else {
tensor = await tf.node.decodeImage(await fs.readFile(path), 3)
}
const results = await faceapi.detectAllFaces(tensor).withFaceLandmarks().withFaceDescriptors()
const results = await human.detect(tensor)
tensor.dispose()
const vectors = results
const vectors = results.face
.map(result => ({
angle: result.angle,
vector: result.descriptor,
x: result.detection.relativeBox.x,
y: result.detection.relativeBox.y,
height: result.detection.relativeBox.height,
width: result.detection.relativeBox.width,
score: result.detection.score,
angle: result.rotation.angle,
vector: result.embedding,
x: result.boxRaw[0],
y: result.boxRaw[1],
height: result.boxRaw[3],
width: result.boxRaw[2],
score: result.score,
}))

console.log(JSON.stringify(vectors))
} catch (e) {
console.error(e)
Expand All @@ -76,6 +98,9 @@
}
}

if (PUREJS) {
wasm.setWasmPaths(config.wasmPath, true);

Check failure on line 102 in src/classifier_faces.js

View workflow job for this annotation

GitHub Actions / NPM lint

Extra semicolon
}
tf.setBackend(PUREJS ? 'wasm' : 'tensorflow')
.then(() => main())
.catch(e => {
Expand All @@ -83,7 +108,7 @@
})

/**
* @param image

Check warning on line 111 in src/classifier_faces.js

View workflow job for this annotation

GitHub Actions / NPM lint

Missing JSDoc @param "image" description

Check warning on line 111 in src/classifier_faces.js

View workflow job for this annotation

GitHub Actions / NPM lint

Missing JSDoc @param "image" type
*/
async function createTensor(image) {
const NUM_OF_CHANNELS = 3
Expand Down
Loading