-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a copy of TFLite sound classification app for compatibility pu…
…rpose PiperOrigin-RevId: 373307415
- Loading branch information
1 parent
fa9dd93
commit eec6470
Showing
17 changed files
with
1,042 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
lite/examples/sound_classification/android_legacy/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Sound Classifier Android sample. | ||
|
||
## Important notice | ||
|
||
* See this sample for how to integrate a sound classification model into an | ||
Android app using | ||
[TFLite Task Library](https://github.com/tensorflow/examples/tree/master/lite/examples/sound_classification/android) | ||
* This copy of the Android sound classification sample is to demonstrate how | ||
to integrate sound classification models trained on Teachable Machine. | ||
* This sample is only for compatibility purpose and will be removed in a near | ||
future when Teachable Machine exports models that are compatible with Task | ||
Library. | ||
|
||
## Requirements | ||
|
||
* Android Studio 4.1 (installed on a Linux, Mac or Windows machine) | ||
* An Android device, or an Android Emulator | ||
|
||
## Build and run | ||
|
||
### Step 1. Clone the TensorFlow examples source code | ||
|
||
Clone the TensorFlow examples GitHub repository to your computer to get the demo | ||
application. | ||
|
||
``` | ||
git clone https://github.com/tensorflow/examples | ||
``` | ||
|
||
### Step 2. Import the sample app to Android Studio | ||
|
||
Open the TensorFlow source code in Android Studio. To do this, open Android | ||
Studio and select `Import Projects (Gradle, Eclipse ADT, etc.)`, setting the | ||
folder to `examples/lite/examples/sound_classification/android` | ||
|
||
### Step 3. Run the Android app | ||
|
||
Connect the Android device to the computer and be sure to approve any ADB | ||
permission prompts that appear on your phone. Select `Run -> Run app.` Select | ||
the deployment target in the connected devices to the device on which the app | ||
will be installed. This will install the app on the device. | ||
|
||
To test the app, open the app called `TFL Sound Classifier` on your device. | ||
Re-installing the app may require you to uninstall the previous installations. | ||
|
||
## Resources used: | ||
|
||
* [TensorFlow Lite](https://www.tensorflow.org/lite) | ||
* [Teachable Machine Audio Project](https://teachablemachine.withgoogle.com/train/audio) |
66 changes: 66 additions & 0 deletions
66
lite/examples/sound_classification/android_legacy/app/build.gradle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
apply plugin: 'com.android.application' | ||
apply plugin: 'kotlin-android' | ||
apply plugin: 'de.undercouch.download' | ||
|
||
android { | ||
compileSdkVersion 30 | ||
defaultConfig { | ||
applicationId "org.tensorflow.lite.examples.soundclassifier" | ||
minSdkVersion 23 | ||
targetSdkVersion 30 | ||
versionCode 1 | ||
versionName "1.0" | ||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | ||
} | ||
|
||
aaptOptions { | ||
noCompress "tflite" | ||
} | ||
|
||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
|
||
buildFeatures { | ||
viewBinding true | ||
} | ||
|
||
compileOptions { | ||
sourceCompatibility 1.8 | ||
targetCompatibility 1.8 | ||
} | ||
|
||
kotlinOptions { | ||
jvmTarget = "1.8" | ||
} | ||
} | ||
|
||
// import DownloadModels task | ||
project.ext.ASSET_DIR = projectDir.toString() + '/src/main/assets' | ||
|
||
// Download default models; if you wish to use your own models then | ||
// place them in the "assets" directory and comment out below line. | ||
apply from: 'download_model.gradle' | ||
|
||
dependencies { | ||
implementation fileTree(dir: "libs", include: ["*.jar"]) | ||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" | ||
implementation "androidx.core:core-ktx:1.3.1" | ||
implementation "androidx.appcompat:appcompat:1.2.0" | ||
implementation "androidx.lifecycle:lifecycle-common-java8:2.2.0" | ||
implementation "androidx.constraintlayout:constraintlayout:2.0.1" | ||
implementation "androidx.recyclerview:recyclerview:1.1.0" | ||
implementation "com.google.android.material:material:1.2.1" | ||
|
||
implementation "org.tensorflow:tensorflow-lite:2.3.0" | ||
implementation "org.tensorflow:tensorflow-lite-select-tf-ops:2.3.0" | ||
implementation "org.tensorflow:tensorflow-lite-support:0.1.0" | ||
implementation "org.tensorflow:tensorflow-lite-metadata:0.1.0" | ||
|
||
testImplementation "junit:junit:4.13" | ||
androidTestImplementation "androidx.test.ext:junit:1.1.2" | ||
androidTestImplementation "androidx.test.espresso:espresso-core:3.3.0" | ||
} |
11 changes: 11 additions & 0 deletions
11
lite/examples/sound_classification/android_legacy/app/download_model.gradle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
task downloadSoundClassificationModelFile(type: Download) { | ||
src 'https://storage.googleapis.com/download.tensorflow.org/models/tflite/sound_classification/snap_clap.tflite' | ||
dest project.ext.ASSET_DIR + '/sound_classifier.tflite' | ||
overwrite false | ||
} | ||
|
||
tasks.whenTaskAdded { task -> | ||
if ((task.name == 'assembleDebug') || (task.name == 'assembleRelease')) { | ||
task.dependsOn 'downloadSoundClassificationModelFile' | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
lite/examples/sound_classification/android_legacy/app/proguard-rules.pro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
25 changes: 25 additions & 0 deletions
25
lite/examples/sound_classification/android_legacy/app/src/main/AndroidManifest.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="org.tensorflow.lite.examples.soundclassifier"> | ||
|
||
<uses-permission android:name="android.permission.RECORD_AUDIO" /> | ||
|
||
<application | ||
android:allowBackup="true" | ||
android:icon="@mipmap/ic_launcher" | ||
android:label="@string/app_name" | ||
android:roundIcon="@mipmap/ic_launcher_round" | ||
android:supportsRtl="true" | ||
android:theme="@style/AppTheme"> | ||
<activity | ||
android:name=".MainActivity" | ||
android:exported="true"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
|
||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
</activity> | ||
</application> | ||
|
||
</manifest> |
3 changes: 3 additions & 0 deletions
3
lite/examples/sound_classification/android_legacy/app/src/main/assets/labels.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
0 snap | ||
1 _background_noise_ | ||
2 clap |
129 changes: 129 additions & 0 deletions
129
...oid_legacy/app/src/main/java/org/tensorflow/lite/examples/soundclassifier/MainActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/* | ||
* Copyright 2020 The TensorFlow Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.tensorflow.lite.examples.soundclassifier | ||
|
||
import android.Manifest | ||
import android.content.pm.PackageManager | ||
import android.os.Build | ||
import android.os.Bundle | ||
import android.util.Log | ||
import android.view.WindowManager | ||
import androidx.annotation.RequiresApi | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.core.content.ContextCompat | ||
import org.tensorflow.lite.examples.soundclassifier.databinding.ActivityMainBinding | ||
|
||
class MainActivity : AppCompatActivity() { | ||
private val probabilitiesAdapter by lazy { ProbabilitiesAdapter() } | ||
|
||
private lateinit var soundClassifier: SoundClassifier | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
||
val binding = ActivityMainBinding.inflate(layoutInflater) | ||
setContentView(binding.root) | ||
|
||
soundClassifier = SoundClassifier(this, SoundClassifier.Options()).also { | ||
it.lifecycleOwner = this | ||
} | ||
|
||
with(binding) { | ||
recyclerView.apply { | ||
setHasFixedSize(true) | ||
adapter = probabilitiesAdapter.apply { | ||
labelList = soundClassifier.labelList | ||
} | ||
} | ||
|
||
keepScreenOn(inputSwitch.isChecked) | ||
inputSwitch.setOnCheckedChangeListener { _, isChecked -> | ||
soundClassifier.isPaused = !isChecked | ||
keepScreenOn(isChecked) | ||
} | ||
|
||
overlapFactorSlider.value = soundClassifier.overlapFactor | ||
overlapFactorSlider.addOnChangeListener { _, value, _ -> | ||
soundClassifier.overlapFactor = value | ||
} | ||
} | ||
|
||
soundClassifier.probabilities.observe(this) { resultMap -> | ||
if (resultMap.isEmpty() || resultMap.size > soundClassifier.labelList.size) { | ||
Log.w(TAG, "Invalid size of probability output! (size: ${resultMap.size})") | ||
return@observe | ||
} | ||
probabilitiesAdapter.probabilityMap = resultMap | ||
probabilitiesAdapter.notifyDataSetChanged() | ||
} | ||
|
||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | ||
requestMicrophonePermission() | ||
} else { | ||
soundClassifier.start() | ||
} | ||
} | ||
|
||
override fun onTopResumedActivityChanged(isTopResumedActivity: Boolean) { | ||
// Handles "top" resumed event on multi-window environment | ||
if (isTopResumedActivity) { | ||
soundClassifier.start() | ||
} else { | ||
soundClassifier.stop() | ||
} | ||
} | ||
|
||
override fun onRequestPermissionsResult( | ||
requestCode: Int, | ||
permissions: Array<out String>, | ||
grantResults: IntArray | ||
) { | ||
if (requestCode == REQUEST_RECORD_AUDIO) { | ||
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) { | ||
Log.i(TAG, "Audio permission granted :)") | ||
soundClassifier.start() | ||
} else { | ||
Log.e(TAG, "Audio permission not granted :(") | ||
} | ||
} | ||
} | ||
|
||
@RequiresApi(Build.VERSION_CODES.M) | ||
private fun requestMicrophonePermission() { | ||
if (ContextCompat.checkSelfPermission( | ||
this, | ||
Manifest.permission.RECORD_AUDIO | ||
) == PackageManager.PERMISSION_GRANTED | ||
) { | ||
soundClassifier.start() | ||
} else { | ||
requestPermissions(arrayOf(Manifest.permission.RECORD_AUDIO), REQUEST_RECORD_AUDIO) | ||
} | ||
} | ||
|
||
private fun keepScreenOn(enable: Boolean) = | ||
if (enable) { | ||
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) | ||
} else { | ||
window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) | ||
} | ||
|
||
companion object { | ||
const val REQUEST_RECORD_AUDIO = 1337 | ||
private const val TAG = "AudioDemo" | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
...cy/app/src/main/java/org/tensorflow/lite/examples/soundclassifier/ProbabilitiesAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright 2020 The TensorFlow Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.tensorflow.lite.examples.soundclassifier | ||
|
||
import android.animation.ObjectAnimator | ||
import android.content.res.ColorStateList | ||
import android.view.LayoutInflater | ||
import android.view.ViewGroup | ||
import android.view.animation.AccelerateDecelerateInterpolator | ||
import androidx.recyclerview.widget.RecyclerView | ||
import org.tensorflow.lite.examples.soundclassifier.databinding.ItemProbabilityBinding | ||
|
||
internal class ProbabilitiesAdapter : RecyclerView.Adapter<ProbabilitiesAdapter.ViewHolder>() { | ||
var labelList = emptyList<String>() | ||
var probabilityMap = mapOf<String, Float>() | ||
|
||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { | ||
val binding = | ||
ItemProbabilityBinding.inflate(LayoutInflater.from(parent.context), parent, false) | ||
return ViewHolder(binding) | ||
} | ||
|
||
override fun onBindViewHolder(holder: ViewHolder, position: Int) { | ||
val label = labelList[position] | ||
val probability = probabilityMap[label] ?: 0f | ||
holder.bind(position, label, probability) | ||
} | ||
|
||
override fun getItemCount() = labelList.size | ||
|
||
class ViewHolder(private val binding: ItemProbabilityBinding) : | ||
RecyclerView.ViewHolder(binding.root) { | ||
fun bind(position: Int, label: String, probability: Float) { | ||
with(binding) { | ||
labelTextView.text = label | ||
progressBar.progressBackgroundTintList = progressColorPairList[position % 3].first | ||
progressBar.progressTintList = progressColorPairList[position % 3].second | ||
|
||
val newValue = (probability * 100).toInt() | ||
// If you don't want to animate, you can write like `progressBar.progress = newValue`. | ||
val animation = | ||
ObjectAnimator.ofInt(progressBar, "progress", progressBar.progress, newValue) | ||
animation.duration = 100 | ||
animation.interpolator = AccelerateDecelerateInterpolator() | ||
animation.start() | ||
} | ||
} | ||
|
||
companion object { | ||
/** List of pairs of background tint and progress tint */ | ||
private val progressColorPairList = listOf( | ||
ColorStateList.valueOf(0xfff9e7e4.toInt()) to ColorStateList.valueOf(0xffd97c2e.toInt()), | ||
ColorStateList.valueOf(0xfff7e3e8.toInt()) to ColorStateList.valueOf(0xffc95670.toInt()), | ||
ColorStateList.valueOf(0xffecf0f9.toInt()) to ColorStateList.valueOf(0xff714Fe7.toInt()), | ||
) | ||
} | ||
} | ||
} |
Oops, something went wrong.