This commit is contained in:
isp
2024-09-13 22:53:43 -06:00
parent 3f701fefd3
commit c5824c9439
13 changed files with 182 additions and 104 deletions

View File

@@ -15,6 +15,8 @@ import com.google.android.gms.wearable.Wearable
class MainActivity : AppCompatActivity() {
// private lateinit var soundClassifier: SoundClassifier
val REQUEST_PERMISSIONS = 1337
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@@ -28,20 +30,22 @@ class MainActivity : AppCompatActivity() {
}
}
)
Downloader.downloadModels(this);
requestPermissions();
Downloader.downloadModels(this)
requestPermissions()
soundClassifier = SoundClassifier(this, SoundClassifier.Options())
Location.requestLocation(this, soundClassifier)
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}
}
companion object {
const val REQUEST_PERMISSIONS = 1337
@SuppressLint("StaticFieldLeak")
lateinit var soundClassifier: SoundClassifier
var soundClassifier: SoundClassifier? = null
// fun getSoundClassifier(): SoundClassifier? {
// return soundClassifier
// }
}
private fun requestPermissions() {

View File

@@ -18,16 +18,18 @@ class MessageListenerService : WearableListenerService() {
override fun onMessageReceived(p0: MessageEvent) {
super.onMessageReceived(p0)
// MainActivity
val soundclassifier = MainActivity.soundClassifier
if (soundclassifier == null) {
Log.w(tag, "Have invalid sound classifier")
return
} else {
Log.w(tag, "Have valid classifier")
}
val short_array = ShortArray(48000 * 3);
val short_array = ShortArray(48000 * 3)
var tstamp_bytes = p0.data.copyOfRange(0, Long.SIZE_BYTES)
var audio_bytes = p0.data.copyOfRange(Long.SIZE_BYTES, p0.data.size)
var string_send: String = ""
ByteBuffer.wrap(audio_bytes).order(
ByteOrder.LITTLE_ENDIAN
@@ -39,9 +41,13 @@ class MessageListenerService : WearableListenerService() {
val score = sorted_list[i].value
val index = sorted_list[i].index
val species_name = soundclassifier.labelList[index]
Log.w(tag, species_name + ", " + score.toString());
Log.w(tag, species_name + ", " + score.toString())
string_send+= species_name
string_send+=','
string_send+=score.toString()
string_send+=';'
}
MessageSenderFromPhone.sendMessage("/audio", tstamp_bytes, this)
MessageSenderFromPhone.sendMessage("/audio", tstamp_bytes + string_send.toByteArray(), this)
// Log.i(tag , short_array.map( { abs(it)}).sum().toString())
// Log.i(tag, short_array[0].toString())
// Log.i(tag, p0.data.toString(Charsets.US_ASCII))

View File

@@ -5,6 +5,7 @@ import android.location.Location
import android.os.SystemClock
import android.preference.PreferenceManager
import android.util.Log
import androidx.annotation.Nullable
import org.tensorflow.lite.Interpreter
import java.io.BufferedReader
import java.io.File
@@ -26,6 +27,7 @@ import java.nio.ShortBuffer
import kotlin.math.round
import kotlin.math.sin
class SoundClassifier(
context: Context,
private val options: Options = Options()
@@ -60,10 +62,10 @@ class SoundClassifier(
/** Names of the model's output classes. */
public lateinit var labelList: List<String>
lateinit var labelList: List<String>
/** Names of the model's output classes. */
public lateinit var assetList: List<String>
lateinit var assetList: List<String>
/** How many milliseconds between consecutive model inference calls. */
private var inferenceInterval = 800L
@@ -315,10 +317,10 @@ class SoundClassifier(
private const val NANOS_IN_MILLIS = 1_000_000.toDouble()
}
public fun executeScoring(
fun executeScoring(
short_array: ShortArray
): List<IndexedValue<Float>> {
val highPass = 0;
val highPass = 0
val butterworth = Butterworth()
butterworth.highPass(6, 48000.0, highPass.toDouble())
@@ -372,7 +374,7 @@ class SoundClassifier(
// Load new audio samples
// val sampleCounts = loadAudio(recordingBuffer)
val sampleCounts = 0;
val sampleCounts = 0
if (sampleCounts == 0) {
return@task
}