Caldarie / flutter_tflite_audio

Audio classification Tflite package for flutter (iOS & Android). Can support Google Teachable Machine models
MIT License
64 stars 26 forks source link

iOS issue with Background service plugin / outputRawScores #30

Closed bobosette closed 2 years ago

bobosette commented 2 years ago

Hi @Caldarie. I'm testing the app on iOS but the package doesn't work. I have followed the guidelines for the implementation but it still doesn't work. This is the exception:

`Unhandled Exception: MissingPluginException(No implementation found for method loadModel on channel tflite_audio)

0 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:165:7)

══╡ EXCEPTION CAUGHT BY SERVICES LIBRARY ╞══════════════════════════════════════════════════════════ The following MissingPluginException was thrown while activating platform stream on channel AudioRecognitionStream: MissingPluginException(No implementation found for method listen on channel AudioRecognitionStream) When the exception was thrown, this was the stack: #0 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:165:7) #1 EventChannel.receiveBroadcastStream. (package:flutter/src/services/platform_channel.dart:506:9) ════════════════════════════════════════════════════════════════════════════════════════════════════` How can I fix it??? Thank you.
bobosette commented 2 years ago

Ok. At the moment i could be satisfied even if the app works on ios without background service. I will let you know with the latest update

Il Mar 1 Feb 2022, 17:11 Michael Nguyen @.***> ha scritto:

I think you best talk to the creator of background_service about this issue. As I have almost no knowledge about background service, he or she may have an insight to your problem.

— Reply to this email directly, view it on GitHub https://github.com/Caldarie/flutter_tflite_audio/issues/30#issuecomment-1027014135, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADEWB4NBZRQCRGHXK7FUWX3UZAA27ANCNFSM5MKLXLCA . You are receiving this because you were mentioned.Message ID: @.***>

bobosette commented 2 years ago

Nothing to do man, also with the latest version I have the outputrawscores bug. Could it be the OS version? On my iPhone 6 I have the 12.5.5. Maybe is too old? Which version do you have when you debug?

Caldarie commented 2 years ago

iPhone 7, version 15.3.

Could be your iphone.

bobosette commented 2 years ago

Hi @Caldarie . Yesterday I got deep inside your code and I found the problem. Inside SwiftTfliteAudioPlugin.swift, line 546, if the result of the recognition is Raw Label Scores: ▿ 6 elements

the app crashes due to this error: "Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid number value (NaN) in JSON write' ". When I run both my app or your example only the FIRST recognition gives always back that result (6 nan) and of course the app always crashes. Could you please fix it by catching that NSInvalidArgumentException??

Caldarie commented 2 years ago

Not much i can do when i cannot reproduce it. However, the code is open to the public, so you are more than welcome to fix it. The most likely problem for your situation is:

let data = try? JSONSerialization.data(withJSONObject: scores)
let stringValue = String(data: data!, encoding: String.Encoding.utf8)
finalResults = Result(recognitionResult: stringValue, inferenceTime: roundInterval, hasPermission: true)
bobosette commented 2 years ago

Yes. This three line. I can do it by myself but as I told you I'm not so comfortable with Swift. How do you handle that exception?

Il giorno gio 3 feb 2022 alle ore 10:15 Michael Nguyen < @.***> ha scritto:

Not much i can do when i cannot reproduce it. However, the code is open to the public, so you are more than welcome to fix it. The most likely problem for your situation is:

let data = try? JSONSerialization.data(withJSONObject: scores) let stringValue = String(data: data!, encoding: String.Encoding.utf8) finalResults = Result(recognitionResult: stringValue, inferenceTime: roundInterval, hasPermission: true)

— Reply to this email directly, view it on GitHub https://github.com/Caldarie/flutter_tflite_audio/issues/30#issuecomment-1028767911, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADEWB4KZ5EHIP3BFYFADT2DUZJBSVANCNFSM5MKLXLCA . You are receiving this because you were mentioned.Message ID: @.***>

bobosette commented 2 years ago

Finally. I fixed the outputRawScores bug. I changed this

let data = try? JSONSerialization.data(withJSONObject: scores) let stringValue = String(data: data!, encoding: String.Encoding.utf8) finalResults = Result(recognitionResult: stringValue, inferenceTime: roundInterval, hasPermission: true)

with this

var realScores = [Float32]() for var score in scores { if (score.isNaN) { score = 0.0 print("catch nan exception") } realScores.append(score) }

        //convert array to exact string value
        let data = try? JSONSerialization.data(withJSONObject: realScores)
        let stringValue = String(data: data!, encoding: String.Encoding.utf8)
        finalResults = Result(recognitionResult: stringValue, inferenceTime: roundInterval, hasPermission: true)

I know that is not a perfect writing, but it works

Caldarie commented 2 years ago

I’m glad to hear that.

You can also try this to handle NaNs let realScores = scores.map { $0.isNaN ? 0 : $0 }

bobosette commented 2 years ago

you're right, thank you. Do you think you can add it in an update?

Il giorno ven 4 feb 2022 alle ore 01:30 Michael Nguyen < @.***> ha scritto:

I’m glad to hear that. A more elegant solution would be:

let realScore = score.isFinite ? score : 0.0

— Reply to this email directly, view it on GitHub https://github.com/Caldarie/flutter_tflite_audio/issues/30#issuecomment-1029528382, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADEWB4KB7GRHSGKNKE5N5KDUZMMZVANCNFSM5MKLXLCA . You are receiving this because you were mentioned.Message ID: @.***>

Caldarie commented 2 years ago

No problems, I will provide an update soon

bobosette commented 2 years ago

thank you. let me know please

Il giorno ven 4 feb 2022 alle ore 09:36 Michael Nguyen < @.***> ha scritto:

No problems, I will provide an update soon

— Reply to this email directly, view it on GitHub https://github.com/Caldarie/flutter_tflite_audio/issues/30#issuecomment-1029760799, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADEWB4NIT6OFFH2JODC5I3TUZOFX7ANCNFSM5MKLXLCA . You are receiving this because you were mentioned.Message ID: @.***>

Caldarie commented 2 years ago

Done. Please update to 0.2.2+4 and let me know if there are any problems.

If there are no problems, you can close the issue. Thanks

bobosette commented 2 years ago

Sorry for the late reply. It works fine, thank you

Il Gio 10 Feb 2022, 17:03 Michael Nguyen @.***> ha scritto:

Closed #30 https://github.com/Caldarie/flutter_tflite_audio/issues/30.

— Reply to this email directly, view it on GitHub https://github.com/Caldarie/flutter_tflite_audio/issues/30#event-6043481677, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADEWB4O3IDDUVKPZPUODRWLU2POU7ANCNFSM5MKLXLCA . You are receiving this because you were mentioned.Message ID: @.***>