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
852 stars 153 forks source link

Create tensor - how I convert it to old face api #80

Closed danies8 closed 2 years ago

danies8 commented 2 years ago

Hi,

  let arr = imageStrEncodedBase64.split(',');
  let bstr = atob(arr[1]);
  let n = bstr.length;
  let u8arr = new Uint8Array(n);
  while (n--) {
    u8arr[n] = bstr.charCodeAt(n);
  }
  const tensor = tf.node.decodeImage(u8arr, 3);

Error:tf.node.decodeImage is not a function Thanks,

vladmandic commented 2 years ago

if you're using old face-api, post issues in that forum - i'm not going to troubleshoot something that works in my port of face-api and doesnt in the old one.

danies8 commented 2 years ago

It will be very great if you help how to create tensor in old api js

vladmandic commented 2 years ago

its been there for a while :) https://github.com/vladmandic/face-api/blob/master/demo/node-image.js

danies8 commented 2 years ago

I used your code:

const tf = require('@tensorflow/tfjs-node');
const canvas = await image.imageFromBuffer(Buffer.from(imageStrEncodedBase64.split(',')[1], 'base64')); // decode to canvas
   const imageData = image.getImageData(canvas);

   const tensor = tf.tidy(() => { // create tensor from image data
        *const data = tf.tensor(Array.from(imageData.data || []), [canvas.height, canvas.width, 4], 'int32'); // create rgba image tensor from flat array and flip to height x width
        const channels = tf.split(data, 4, 2); // split rgba to channels
        const rgb = tf.stack([channels[0], channels[1], channels[2]], 2); // stack channels back to rgb
        const reshape = tf.reshape(rgb, [1, canvas.height, canvas.width, 3]); // move extra dim from the end of tensor and use it as batch number instead
        return reshape;
      });

Get error in *: TypeError: tfc.ENV.findBackend is not a function What do I miss ?

vladmandic commented 2 years ago

very weird, but it again points to bad tfjs-node installation as it use even basic API.

danies8 commented 2 years ago

I think I succeded to create tensor: Create tensor done successfully(tensor shape:1,512,383,3, tensor size:588288 but when I reached to this code (*) I got: UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'backend' of undefined at Engine.moveData \node_modules\tfjs-image-recognition-base\node_modules\@tensorflow\tfjs-core\dist\engine.js:349:31) at DataStorage.get node_modules\tfjs-image-recognition-base\node_modules\@tensorflow\tfjs-core\dist\backends\backend.js:31:28)

const ssdOptions = { minConfidence: parseFloat(config.ssdOptionsMinConfidence), maxResults: parseInt(config.ssdOptionsMaxResults) };
      const optionsSSDMobileNet = new faceapi.SsdMobilenetv1Options(ssdOptions);
     * const faces = await faceapi
        .detectAllFaces(tensor, optionsSSDMobileNet)
        .withFaceLandmarks()
        .withFaceDescriptors();

What do I miss ?

vladmandic commented 2 years ago

it means that faceapi is using wrong instance of tf. it has browser version of tfjs embedded (even when used in node),
but that doesnt work in node

i dont remember anymore if const tf = require('@tensorflow/tfjs-node') should be done before or after face-api import so it basically overwrites its internal registrations - try both.

i don't know why are you spending so much time trying old face-api? is there a problem with the new one?

danies8 commented 2 years ago

I used in both places and got same error,

vladmandic commented 2 years ago

something is really wrong with installation of tfjs-node on your system and i have no idea why...