vladmandic / face-api

FaceAPI: AI-powered Face Detection & Rotation Tracking, Face Description & Recognition, Age & Gender & Emotion Prediction for Browser and NodeJS using TensorFlow/JS
https://vladmandic.github.io/face-api/demo/webcam.html
MIT License
824 stars 149 forks source link

Node face compare demo not working with TS #151

Closed AnthonyLzq closed 1 year ago

AnthonyLzq commented 1 year ago

Issue Description

I'm trying to run the demo/node-face-compare.js script, but I have migrated it to TS. However, TypeScript is complaining about the type of the tensor with the following error:

Argument of type 'Tensor3D | Tensor4D' is not assignable to parameter of type 'TNetInput'.
  Type 'Tensor3D' is not assignable to type 'TNetInput'.
    Type 'Tensor<Rank.R3>' is not assignable to type 'Tensor4D'.
      Types of property 'shape' are incompatible.
        Type '[number, number, number]' is not assignable to type '[number, number, number, number]'.
          Source has 3 element(s) but target requires 4.ts(2345)

Image for reference:

image

Steps to Reproduce

Try to run this script with ts-node or something.

Expected Behavior

To work, I guess, I suppose this is an issue with the types.

Environment

vladmandic commented 1 year ago

i'm going to assume this is similar to other issue you've just created - https://github.com/vladmandic/human/issues/328 make sure that models are actually correctly loaded, not just last error message.

AnthonyLzq commented 1 year ago

In fact, this isn't similar, this is just a type error.

vladmandic commented 1 year ago

ok, i'll take a look

vladmandic commented 1 year ago

i just tried to reproduce using your script and it works just fine here:

node_modules/.bin/ts-node --esm test.ts
input images: vlado1.jpg vlado2.jpg
distance between most prominant detected faces: 0.4469153942760356
similarity between most prominant detected faces: 0.5530846057239645

i guess it up to project setup
please share full reproduction project that i can just clone (tsconfig.json & package.json are key)

vladmandic commented 1 year ago

any updates?

AnthonyLzq commented 1 year ago

Hi there, last week was crazy for me, did not have time to check it, I will review it right away. Sorry for the delay.

AnthonyLzq commented 1 year ago

Here is the repo with the issue.

vladmandic commented 1 year ago

ok, i see what the issue is, but i don't see a way to resolve it without major change on how face-api is built.

the error is misleading, the core issue is that Tensor type signatures are not the same in @tensorflow/tfjs (browser package) and @tensorflow/tfjs-node (nodejs package)

you can confirm by basically doing:

const tensor = tf.node.decodeImage(buffer, 3) as faceApi.tf.Tensor3D

easiest temporary solution is to relax that typecheck:

const faces = await faceApi.detectAllFaces(tensor as any, optionsSSDMobileNet)

extracting and bundling different typedefs for different flavors of tfjs during faceapi build would be out-of-scope for now. i did restructure some typedefs, so do a git pull anyhow.

btw, off-topic, but if you're building for node and using esm, you may want to import node flavor of faceapi explictly as its auto import resolver may pick browser esm version:

import faceApi from '@vladmandic/face-api/dist/face-api.node'
vladmandic commented 1 year ago

as per previous note, i'm closing the issue as its cosmetic only and full fix would be out of scope for now.