Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

tflite audio plugin for flutter

audio classification tflite package for flutter (ios & android). can support google teachable machine models.

if you are a complete newbie to audio classification, you can read the tutorial here. credit to carolina for writing a comprehensive article.

to keep this project alive, please consider being a contributor. a star is also appreciated.

recording inference result
finish start

about this plugin

the plugin can support several model types:

  1. (beginner) google teachable machine, which requires little ml knowledge and coding.
  2. supports models with raw audio inputs. for more information on how to train your own model, take a look here.
  3. supports models with decoded wave inputs. for more information on how to train your own model:
    • detailed guide here
    • to train a decoded wave with mfcc, take a look here
  4. (future feature) adjustable input size
  5. (future feature) spectogram as an input type. will support model from this tutorial.
  6. (future feature) model with mutliple outputs
  7. (future feature) audio embeddings

known issues

a) model won’t load

you need to configures permissions and dependencies to use this plugin. please follow the steps below:

b) inference isn’t accurate

its possible that your device doesn’t have enough time to record. simply adjust the buffersize to a lower value. likewise, if your buffersize is too low, the recording length will be too long and your model may possibly register it as background noise. simply adjust the buffersize to a higher value.

c) tensorflow lite error: regular tensorflow ops are not supported by this interpreter. make sure you apply/link the flex delegate before inference

please make sure that you have enabled ops-select on your podfile – step 4 & xcode – step 5 and build gradle – step 3

if you tried above, please run the example on a device (not emulator). if you still recieved this error, its very likely that theres an issue with cocoapod or xcode configuration. please check the issue #7

if you recieved this error from your custom model (not gtm), its likely that you’re using unsupported tensorflow operators for tflite, as found in issue #5. for more details on which operators are supported, look at the official documentation here

d) (ios) app crashes when running google’s teachable machine model

please run your simulation on actual ios device. as of this moment, there’s limited support for x86_64 architectures from the tensorflow lite select-ops framework. if you absolutely need to run it on an emulator, you can consider building the select ops framework yourself. instructions can be found here

e) (android) fatal signal 11 (sigsegv), code 1 (segv_maperr), fault addr 0xfffffff4 in tid 5403

it seems like the latest tflite package for android is causing this issue. until this issue is fixed, please run this package on an actual android device.

f) failed to invoke the interpreter with error: provided data count (number) must match the required count (number).

please make that your recording length matches your model input size. for example, google’s teachable machine requires recording length is 44032

if you have found any other issues not listed above, please create a new issue.

please read if you are using google’s teachable machine. otherwise skip.

be aware: google’s teachable machine requires select tensorflow operators to work. this feature is experimental and will cause the following issues:

  1. increase the overall size of your app. if this is unnacceptable for you, it’s recommended that you build your own custom model. tutorials can be found in the about this plugin section
  2. emulators for ios do not work due to limited support for x86_64 architectures. you need to run your simulation on an actual device. issue can be found here
  3. you will need to manually implement ops-select on your podfile – step 4 & xcode – step 5 and build gradle – step 3

how to add tflite model and label to flutter:

  1. place your custom tflite model and labels into the asset folder.
  2. in pubsec.yaml, link your tflite model and label under ‘assets’. for example:
  assets:
    - assets/decoded_wav_model.tflite
    - assets/decoded_wav_label.txt

how to use this plugin

please look at the example on how to implement these futures.

  1. import the plugin. for example:
import 'package:tflite_audio/tflite_audio.dart';
  1. to load your model:
   tfliteaudio.loadmodel(
        model: 'assets/conv_actions_frozen.tflite',
        label: 'assets/conv_actions_labels.txt',
        numthreads: 1,
        isasset: true);
  1. to start and listen to the stream for inference results:

example for google’s teachable machine models

tfliteaudio.startaudiorecognition(
  numofinferences: 1,
  inputtype: 'rawaudio',
  samplerate: 44100,
  recordinglength: 44032,
  buffersize: 22016,
  )
    .listen(
      //do something here to collect data
      )
    .ondone(
       //do something here when stream closes
      );

example for decodedwav models

tfliteaudio.startaudiorecognition(
  numofinferences: 1,
  inputtype: 'decodedwav',
  samplerate: 16000,
  recordinglength: 16000,
  buffersize: 2000,
  )
    .listen(
      //do something here to collect data
      )
    .ondone(
       //do something here when stream closes
      );
  1. to forcibly cancel the stream and recognition while executing:
tfliteaudio.stopaudiorecognition();

rough guide on the parameters

  • numthreads – higher threads will reduce inferencetime. however, cpu usage will be higher.
  • numofinferences – determines how many times you want to repeat the inference.
  • samplerate – a higher sample rate will improve accuracy. recommened values are 16000, 22050, 44100
  • recordinglength – determines the size of your tensor input. if the value is not equal to your tensor input, it will crash.
  • buffersize – make sure this value is equal or below your recording length. be aware that a higher value may not allow the recording enough time to capture your voice. a lower value will give more time, but it’ll be more cpu intensive. remember that the optimal value varies depending on the device.

android installation & permissions

  1. add the permissions below to your androidmanifest. this could be found in <yourapp>/android/app/src folder. for example:
<uses-permission android:name="android.permission.record_audio" />
<uses-permission android:name="android.permission.write_external_storage" />
  1. edit the following below to your build.gradle. this could be found in <yourapp>/app/src/for example:
aaptoptions {
        nocompress 'tflite'

(android) if you are using google’s teachable machine. otherwise skip.

  1. enable select-ops under dependencies in your build gradle.
dependencies {
    compile 'org.tensorflow:tensorflow-lite-select-tf-ops:+'
}

ios installation & permissions

  1. add the following key to info.plist for ios. this ould be found in <yourapp>/ios/runner
<key>nsmicrophoneusagedescription</key>
<string>record audio for playback</string>
  1. change the deployment target to at least 12.0. this could be done by:

    a. open your project workspace on xcode

    b. select root runner on the left panel

    c. under the info tab, change the ios deployment target to 12.0

  2. open your podfile in your ios folder and change platform ios to 12.
platform :ios, '12.0'

(ios) if you are using google’s teachable machine model. otherwise skip.

  1. add `pod ‘tensorflowliteselecttfops’ under target.
target 'runner' do
  use_frameworks! 
  use_modular_headers!
  pod 'tensorflowliteselecttfops' #add this line here. 

  flutter_install_all_ios_pods file.dirname(file.realpath(__file__))
end
  1. force load select ops for tensorflow. to do that:

    a. open your project on xcode

    b. click on runner under “targets”

    c. click on “build settings” tab

    d. click on “all” tab

    e. click on the empty space which is on the right side of “other links flag”

    f. add: -force_load $(srcroot)/pods/tensorflowliteselecttfops/frameworks/tensorflowliteselecttfops.framework/tensorflowliteselecttfops

tflite-select-ops-installation

  1. install the ops-select package to pod. to do this:

    a. cd into ios folder

    b. run flutter pub get on terminal

    c. run pod install on terminal

    d. run flutter clean on terminal


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

Top