tensorflow / tfjs

A WebGL accelerated JavaScript library for training and deploying ML models.
https://js.tensorflow.org
Apache License 2.0
18.36k stars 1.92k forks source link

TypeError: undefined is not an object (evaluating 'env().platform.encode') #8349

Closed gameveloster closed 2 days ago

gameveloster commented 1 month ago

I'm having issues using tfjs in Expo app.

Getting the error:

TypeError: undefined is not an object (evaluating 'env().platform.encode')

when trying to use tf.util.encodeString:

import * as tf from "@tensorflow/tfjs";
import * as ImageManipulator from "expo-image-manipulator";

export default function MyScreen() {
    const resizedImage = await ImageManipulator.manipulateAsync(
        imageUri,
        [{ resize: { width: 640, height: 640 } }],
        { format: ImageManipulator.SaveFormat.PNG, base64: true }
    );

    const imgBuffer = tf.util.encodeString(
        resizedImage.base64,
        "base64"
    ).buffer;

    // ...
}

Anyone encountered this before?

My app has installed expo@51.0.13, @tensorflow/tfjs@4.20.0

shmishra99 commented 2 weeks ago

Hi @gameveloster,

Apologies for the delayed response.

I was able to successfully convert the base64 image string into a base64 Uint8Array using the code snippet you provided. Here is the code I used:

const resizedImage = await ImageManipulator.manipulateAsync(
  selectedImage,
  [{ resize: { width: 640, height: 640 } }],
  { format: ImageManipulator.SaveFormat.PNG, base64: true }
);
const resizedImagBase = resizedImage.base64 ??  ''; 
console.log("resizedImage..", resizedImagBase)
const imgBuffer = tf.util.encodeString( resizedImagBase
  , 'base64').buffer;
console.log("imgBuffer",imgBuffer)

This code results in a Uint8Array, as shown in the attached image.

image

The error suggests that the env() function might not be properly initialized, as referenced in environment.ts.

Please try the following troubleshooting steps:

 1) Delete your node_modules folder.
 2) Remove the package-lock.json or yarn.lock file.
 3) Reinstall all dependencies using `npm install`.

Let me know if this resolves the issue.

Thank you!

github-actions[bot] commented 1 week ago

This issue has been marked stale because it has no recent activity since 7 days. It will be closed if no further activity occurs. Thank you.

github-actions[bot] commented 2 days ago

This issue was closed due to lack of activity after being marked stale for past 7 days.

google-ml-butler[bot] commented 2 days ago

Are you satisfied with the resolution of your issue? Yes No