Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output with predicated score and label #41

Open
SanaSizmic opened this issue Jul 28, 2022 · 4 comments
Open

Output with predicated score and label #41

SanaSizmic opened this issue Jul 28, 2022 · 4 comments

Comments

@SanaSizmic
Copy link

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

@Caldarie
Copy link
Owner

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
Copy link
Owner

Caldarie commented Jul 28, 2022

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()));
  

@SanaSizmic
Copy link
Author

SanaSizmic commented Jul 28, 2022

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.

@Caldarie
Copy link
Owner

Caldarie commented Jul 28, 2022

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(() {});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants