tfjs: cameraWithTensors Error: Argument 'x' passed to 'cast' must be a Tensor or TensorLike, but got 'Tensor'

I’m trying to run TensorCamera with React Native. Follwing the example code but I get an error that says the ImageTensors are ‘Tensor’ not Tensor or TensorLike

import React from 'react'
import { Camera } from 'expo-camera'
import { cameraWithTensors } from '@tensorflow/tfjs-react-native'
import * as tf from '@tensorflow/tfjs'
import '@tensorflow/tfjs-react-native'

const TensorCamera = cameraWithTensors(Camera)

function CameraView() {
  handleCameraStream(images, updatePreview, gl) {
      const loop = async () => {
        const nextImageTensor = images.next().value;
  
        nextImageTensor.toFloat();
        // throws [Error: Argument 'x' passed to 'cast' must be a Tensor or TensorLike, but got 'Tensor']
  
        nextImageTensor.expandDims(0);
        // throws [Error: Argument 'x' passed to 'expandDims' must be a Tensor or TensorLike, but got 'Tensor']
  
        // Solved both of it using tf.func but my model is giving a similar error now
        model.predict( tf.expandDims( tf.cast(nextImageTensor, 'float32'), 0) );
        // throws [Error: Argument 'x' passed to 'stridedSlice' must be a Tensor or TensorLike, but got 'Tensor']
      }
      loop();
    }
   
    return (
       <View>
         <TensorCamera
          // Standard Camera props
          style={styles.camera}
          type={Camera.Constants.Type.back}
          // Tensor related props
          cameraTextureHeight={textureDims.height}
          cameraTextureWidth={textureDims.width}
          resizeHeight={640}
          resizeWidth={640}
          resizeDepth={3}
          onReady={handleCameraStream}
          autorender={true}
         />
       </View>
     )
   }

I’m tried front & back camera, same issue with both. Tried printing out the image tensor and it looks alright

console.log(tf.tensor4d([[
          [[1, 3], [2, 8]],
          [[3, 9], [4, 2]]
      ]]))
// {"dataId": {"id": 247}, "dtype": "float32", "id": 253, "isDisposedInternal": false, "kept": false, "rankType": "4", "shape": [1, 2, 2, 2], "size": 8, "strides": [8, 4, 2]}

console.log( tf.expandDims( tf.cast(nextImageTensor, 'float32'), 0))
// {"dataId": {"id": 246}, "dtype": "float32", "id": 255, "isDisposedInternal": false, "kept": false, "rankType": "4", "scopeId": 14, "shape": [1, 640, 640, 3], "size": 1228800, "strides": [1228800, 1920, 3]}

Am I missing something? Seems like a simple usecase for cameraWithTensors. Please help

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 32 (1 by maintainers)

Most upvoted comments

Thanks for the update @sarons. Sorry for this tedious process… Hope you find the root cause soon.

I found this working example for mobile. I’ll try to downgrade all the packages to what this example uses, maybe that will work 🤞