shaqian / tflite-react-native

React Native library for TensorFlow Lite
https://www.npmjs.com/package/tflite-react-native
MIT License
291 stars 106 forks source link

TypeError: null is not an object (evaluating 'TfliteReactNative.loadModel') #34

Open nitsanasraf opened 2 years ago

nitsanasraf commented 2 years ago

I am developing an app for ios with react-native CLI, and I am facing a problem loading the model. I've tried every solution possible on the internet, but it still doesn't work, unfortunately. Linking is working properly, I've tried manual linking and automatic. I have my model in src/models. And in Xcode, it's under MyProject > models.

This is my code:

const initModel = () => {
    const tflite = new Tflite();
    console.log('started initialization of TENSOR FLOW LITE');
    tflite.loadModel(
      {
        model: 'models/model.tflite', // required
        labels: 'models/labels.txt', // required
        numThreads: 1,
        async: false, // defaults to 1
      },
      (err, res) => {
        if (err) {
          console.log(err);
        } else {
          res.runModelOnImage(
            {
              path: pictureUrl, // required
              imageMean: 128.0, // defaults to 127.5
              imageStd: 128.0, // defaults to 127.5
              numResults: 3, // defaults to 5
              threshold: 0.05, // defaults to 0.1
            },
            (err, res) => {
              if (err) console.log(err);
              else setPrediction(res);
            },
          );
        }
      },
    );
  };

  useEffect(() => {
    initModel();
  }, []);

Podfile:

require_relative '../node_modules/react-native/scripts/react_native_pods'
require_relative '../node_modules/@react-native-community/cli-platform-ios/native_modules'

platform :ios, '11.0'

pod 'RNVectorIcons', :path => '../node_modules/react-native-vector-icons'

target 'ai_app' do
  config = use_native_modules!
  use_react_native!(
    :path => config[:reactNativePath],
    # to enable hermes on iOS, change `false` to `true` and then install pods
    :hermes_enabled => false
    )

  pod 'TensorFlowLite', '1.12.0'
  pod 'TfliteReactNative', :path => '../node_modules/tflite-react-native/ios'

  # target 'ai_appTests' do
  #   inherit! :complete
  #   # Pods for testing
  # end

  # Enables Flipper.
  #
  # Note that if you have use_frameworks! enabled, Flipper will not work and
  # you should disable the next line.
  use_flipper!()

  post_install do |installer|
    react_native_post_install(installer)
    __apply_Xcode_12_5_M1_post_install_workaround(installer)
  end
end

Metro.config:

/**
 * Metro configuration for React Native
 * https://github.com/facebook/react-native
 *
 * @format
 */

module.exports = {
  resolver: {
    assetExts: ['tflite', 'txt','png'],
  },
};

Building is successful on Xcode.

Thanks in advance.