Git Product home page Git Product logo

Comments (8)

vladmandic avatar vladmandic commented on June 13, 2024

sorry, i have no idea how nodejs GCP works, clearly its not the same are real nodejs environment.

from face-api.

luicfrr avatar luicfrr commented on June 13, 2024

GCP works with old faceapi, I've found this repository so I think your faceapi should work too.

But those erros I've mentioned, do you know how to solve them?

from face-api.

luicfrr avatar luicfrr commented on June 13, 2024

I'm following this and this examples and having canvas type errors also:

async function getImage(
      input: Buffer
      ) {
      const img = await canvas.loadImage(input)
      const canva = canvas.createCanvas(img.width, img.height)
      const ctx = canva.getContext('2d')
      ctx.drawImage(img, 0, 0, img.width, img.height)
      return canva
    }

    const path = './models'
    const { Canvas, Image, ImageData } = canvas
    faceapi.env.monkeyPatch({Canvas, Image, ImageData})

    await faceapi.nets.ssdMobilenetv1.loadFromDisk( path )
    await faceapi.nets.faceLandmark68Net.loadFromDisk( path )
    await faceapi.nets.faceRecognitionNet.loadFromDisk( path )

    const faceDetectorOptions = new faceapi.SsdMobilenetv1Options( {
      maxResults: 2
    } )

    const selfie = await getImage( uploads[ 0 ].buffer )
    const results = await faceapi.detectAllFaces(
      selfie,
      faceDetectorOptions
    ).withFaceLandmarks()
      .withFaceDescriptors()

    console.log( 'faces found', results.length )

Captura de Tela 2023-07-26 às 16 26 15

Captura de Tela 2023-07-26 às 16 31 40

from face-api.

vladmandic avatar vladmandic commented on June 13, 2024

all those messages are about type mismatach that come from GCP, i have no idea why its mismatching perfectly fine types. i never used gcp nodejs workers nor i plan to.

from face-api.

luicfrr avatar luicfrr commented on June 13, 2024

I don't think these type errors are related to GCP because they happens locally. I think this is something related to TS. Anyway, I've managed to get rid of these errors casting types as unknown.

I just have one more doubt: Is it possible to store face detection data to use them after in FaceMatcher?

E.g:

// stringfy detection data
const detections = await detectAllFaces(
      selfie as unknown as TNetInput,
      faceDetectorOptions
    ).withFaceLandmarks()
      .withFaceDescriptors()

await saveToDb(
  JSON.stringfy(detections)
)

// then parse it back 
const stringDetections = await getFromDb()
const detections = JSON.parse(stringDetections)
const faceMatcher = new FaceMatcher( detections )

I'm trying but this leads to error. Do you have a better sollution?

from face-api.

vladmandic avatar vladmandic commented on June 13, 2024

its definitely possible and quite common use-case, search previous issues.

from face-api.

luicfrr avatar luicfrr commented on June 13, 2024

@vladmandic Thanks for your help. I've managed to get this working.

If anyone needs something like this here's a little example:

async function getDetections(
  input: Buffer,
  faceDetectorOptions: SsdMobilenetv1Options
): Promise<FaceDetectionResult> {
  // needs to require, importing will set tf to undefined
  const tf = require( '@tensorflow/tfjs-node' )
  const tensor = tf.node.decodeImage( input, 3 )
  const detections = await detectAllFaces(
    tensor as unknown as TNetInput,
    faceDetectorOptions
  ).withFaceLandmarks()
    .withFaceDescriptors()

  tf.dispose( tensor )
  return detections
}

// we will save descriptors in firestore so they need to be as string
const descriptors: string[] = []

// initialize and load detections models
....

// loop through images uploaded
for await ( const { buffer } of uploads ) {
      const detections = await getDetections(
        buffer,
        faceDetectorOptions
      )

      if ( isEmpty( detections ) ) // handle 0 faces detected
      if ( detections.length > 1 ) // handle multiple faces detected

      descriptors.push( detections[ 0 ].descriptor.toString() )
}

await firestore.doc<FaceMatcherDoc>( `faces/${ label }` )
      .set( { descriptors } )

/**
 *  now when you need to validate a face just restore stored face descriptors
 */

const faceMatcherDoc = await firestore
      .doc( `faces/${ label }` )
      .get()
    const { descriptors } = faceMatcherDoc.data()!
    const labeledDescriptors = new LabeledFaceDescriptors(
      label,
      // converts string back to Float32Array[]
      descriptors.map( descriptor => Float32Array.from(
        descriptor.split( "," ),
        parseFloat
      ) )
    )
    const faceMatcher = new FaceMatcher( labeledDescriptors )
    const match = faceMatcher.findBestMatch(
      detections[ 0 ].descriptor
    )
    const confidence = 1 - match.distance
    if ( confidence < parseFloat( FACE_MIN_CONFIDENCE ) )  // handle face do not match

    const stringDescriptor = detections[ 0 ].descriptor.toString()
    // add new descriptos to current saved so detection will be more precise
    if ( !arrayContains(
      descriptors,
      stringDescriptor
    ) ) {
      await firestore.doc( `faces/${ label }` )
        .set( {
          descriptors: [
            ...descriptors,
            stringDescriptor
          ]
        } )
    }

from face-api.

snilan avatar snilan commented on June 13, 2024

@nonam4 do you happen to have a repo available showing this? Having trouble getting this to work

from face-api.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.