flutter_tflite_audio icon indicating copy to clipboard operation
flutter_tflite_audio copied to clipboard

Output with predicated score and label

Open SanaSizmic opened this issue 2 years ago • 4 comments

Hi, @Caldarie can I add the predicted label with score or value as shown in the image below Can you please help with this, it would be appreciated

screenshot-developers google com-2022 07 28-17_15_29

SanaSizmic avatar Jul 28 '22 11:07 SanaSizmic

Hi, yes it’s possible to get the raw score. All you need to do is set outputRawScores to true. For example:


TfliteAudio.loadModel(
    model: 'assets/conv_actions_frozen.tflite',
    label: 'assets/conv_actions_label.txt',
    inputType: 'decodedWav',
    outputRawScores: true,  //change value here
    numThreads: 1,
    isAsset: true,
  );


Caldarie avatar Jul 28 '22 12:07 Caldarie

You need to get the scores from the stream. Sample code is from the example


   result = TfliteAudio.startAudioRecognition(
      sampleRate: sampleRate,
      bufferSize: bufferSize,
      numOfInferences: numOfInferences,
    );

    result
        ?.listen((event) =>
            print("Recognition Result: " + event["recognitionResult"].toString()));
  

Caldarie avatar Jul 28 '22 12:07 Caldarie

Yes, I am using the exact same audio code, but when I set outputRawScores to true it's not detecting anything and when I set it to false it works fine without Scores.

SanaSizmic avatar Jul 28 '22 13:07 SanaSizmic

Hi,

Of course it will not match the labels as you are returning the raw scores. If you use the code that i have pasted above, it should print out the raw score on the terminal.

If you wish to match the raw scores to the labels, you'll need to implement the code yourself. For an example of an implementation, please take a the code found here


 //model initialization
  TfliteAudio.loadModel(
      inputType: 'rawAudio', //'decodedWav', //'rawAudio',
      numThreads: 1,
      outputRawScores: true,
      isAsset: true,
      model:
          'assets/cough_model.tflite', 
      label:
          'assets/cough_label.txt' 
      );


  Stream<Map<dynamic, dynamic>>? result;

  result = TfliteAudio.startAudioRecognition(
    sampleRate: 44100, 
    //recordingLength: 44032, //16000, //44032,
    bufferSize: 11008, //to get 2-3 secs of analisys instead of 1
    numOfInferences: 99999,
    detectionThreshold: 0.7,
    averageWindowDuration: 1500,
    //minimumTimeBetweenSamples: this.minimumTimeBetweenSamples,
    //suppressionTime: this.suppressionTime,
  );


  ///Logs the results and assigns false when stream is finished.
  result.listen((audioEvent) {
    var scores = audioEvent['recognitionResult'];
    try {
      //handle Unexpected character error
      scores = jsonDecode(scores.replaceAll('"', ''));
    } catch (e) {
      Exception(e.toString());
    }

    double last = 0;
    double max = 1;
    if (scores is List) {
      List<double> l = scores.cast<double>();
      last = l.last;
      max = l.reduce(math.max);
    }

    //if cough has the higher percentage and this is higher than 0.72 > IS COUGH
    if (last == max && max > 0.72) {
      count = count + 1;
      FlutterBackgroundService().setNotificationInfo(
        title: "Background",
        content: "$count",
      );
      FlutterBackgroundService().sendData(
        {"result": count},
      );
     
    }
  }).onDone(() {});

Caldarie avatar Jul 28 '22 14:07 Caldarie