question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:26

github_iconTop GitHub Comments

1reaction
jinjingforevercommented, Jan 12, 2022

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

1reaction
saronscommented, Jan 3, 2022

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Argument 'x' passed to 'cast' must be a Tensor or TensorLike ...
I wonder if tensorflow/qna is suitable for mobile applications? Error screen. Error: Argument 'x' passed to 'cast' must be a Tensor or ...
Read more >
Error: Argument "x" passed to 'conv2d' must be a Tensor or ...
Have I written custom code (as opposed to using a stock example script provided in TensorFlow.js): no; OS Platform for development: Windows 10,...
Read more >
Argument 'x' passed to 'slice' must be a Tensor, but got object.
Error : Argument 'x' passed to 'slice' must be a Tensor, but got object. ... While I if I use tf = require('@tensorflow/tfjs-node');...
Read more >
Creates a tf.Tensor with the provided values, shape and dtype.
Pass an array of values to create a vector. tf.tensor([1, 2, 3, 4]).print(); ... The input tensors real and imag must have the...
Read more >
50 TensorFlow.js API Explained in 5 Minutes
I know this article should be 5 minutes long but don't worry it ... Tensor [1, 2, 3, 4]// Pass a flat array...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found