首页 > 解决方案 > 无法从 React Native 上的 Tensorflow.js Tensorcamera 读取图像

问题描述

我正在尝试在 React Native 上使用 Tensorflow.jsposenet,但我无法获得“图像”。
目前世博相机工作正常,但图像未定义,并且还显示

找不到变量:反应

请告知如何迭代从 TensorCamera 获取图像。
谢谢

import * as React from 'react';
import { Text, View, StyleSheet, Image, Platform } from 'react-native';
import { Camera } from 'expo-camera';
import { cameraWithTensors } from '@tensorflow/tfjs-react-native';
import * as posenet from '@tensorflow-models/posenet';
import * as tf from '@tensorflow/tfjs';

const TensorCamera = cameraWithTensors(Camera);

export default class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.handleCameraStream = this.handleCameraStream.bind(this);
  }

  async estimatePoseOnImage(imageElement) {
    const net = await posenet.load();
    const pose = await net.estimateSinglePose(imageElement, {
      flipHorizontal: false,
    });
    this.setState({ pose: pose });
    return pose;
  }

  handleCameraStream( images ) {
    const loop = async () => {
      const nextImageTensor = await images.next().value;
      await this.estimatePoseOnImage(nextImageTensor);
      requestAnimationFrame(loop);
    };
    loop();
  }

  render() {
    return (
      <View>
        <TensorCamera
          type={Camera.Constants.Type.front}
          onReady={this.handleCameraStream}
          autorender={true}
        />
      </View>
    );
  }
}

标签: reactjsreact-nativetensorflowexpotensorflow.js

解决方案


<TensorCamera
    ref={(ref) => { this.camera = ref }}
    type={Camera.Constants.Type.front}
    resizeHeight={64}
    resizeWidth={64}
    resizeDepth={3}
    onReady={this.handleCameraStream}
    style={{width: width, height: height}}
/>

您必须通过 resizeHeight、resizeWidth、resizeDepth 来获取张量。它对我有用。


推荐阅读