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

App: Use RecyclerView and map to support various model #37

Merged
merged 1 commit into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ml_inference_offloading/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ dependencies {
implementation(libs.androidx.ui.tooling.preview)
implementation(libs.androidx.material3)
implementation(project(":nnstreamer-api"))
implementation("androidx.recyclerview:recyclerview:1.3.2")
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import android.content.ServiceConnection
import android.os.Bundle
import android.os.IBinder
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.activity.ComponentActivity
Expand All @@ -17,17 +19,56 @@ import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.ui.Modifier
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.io.OutputStream

// todo: Define DTO with generality and extensibility
data class ModelInfo(
val name: String,
val filter: String
)

class MainActivity : ComponentActivity() {
private val TAG = "MainActivity"
private var mService: MainService? = null

private val connection = object : ServiceConnection {
inner class ModelViewHolder(itemView: View) :
RecyclerView.ViewHolder(itemView) {
fun bind(info: ModelInfo) {
val start = itemView.findViewById<Button>(R.id.start)
val stop = itemView.findViewById<Button>(R.id.stop)
val port = itemView.findViewById<TextView>(R.id.port)

start.setOnClickListener(View.OnClickListener {
val serverPort = mService?.startServer(info.name, info.filter)
port.text = "Listening on port: " + serverPort.toString();
})
stop.setOnClickListener(View.OnClickListener {
mService?.stopServer(info.name)
})
}
}

inner class ModelAdapter(private val modelInfos: ArrayList<ModelInfo>) : RecyclerView.Adapter<ModelViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ModelViewHolder {
val inflater = LayoutInflater.from(parent.context)
val view = inflater.inflate(R.layout.models, parent, false)

return ModelViewHolder(view)
}

override fun onBindViewHolder(holder: ModelViewHolder, position: Int) {
holder.bind(modelInfos[position])
}

override fun getItemCount(): Int = modelInfos.size
}

private val connection = object : ServiceConnection {
override fun onServiceConnected(className: ComponentName, service: IBinder) {
val binder = service as MainService.LocalBinder
mService = binder.getService()
Expand Down Expand Up @@ -90,22 +131,20 @@ class MainActivity : ComponentActivity() {
copyFilesToExternalDir()
startForegroundService(Intent(this, MainService::class.java))

val start = findViewById<Button>(R.id.start)
start.setOnClickListener(View.OnClickListener {
val path = getExternalFilesDir(null)!!.absolutePath
val model = File("$path/mobilenet_v1_1.0_224_quant.tflite")
val filter = "other/tensor,format=static,dimension=(string)3:224:224:1,type=uint8,framerate=0/1 ! " +
"tensor_filter framework=tensorflow-lite model=" + model.getAbsolutePath() + " ! " +
"other/tensor,format=static,dimension=(string)1001:1,type=uint8,framerate=0/1"
val serverPort = mService?.startServer(filter)
val portTextView = findViewById<TextView>(R.id.port)
portTextView.text = "Listening on port: " + serverPort.toString();
})

val stop = findViewById<Button>(R.id.stop)
stop.setOnClickListener(View.OnClickListener {
mService?.stopServer()
})
// todo: Use database instead of just ArrayList
val modelList = ArrayList<ModelInfo>()
val path = getExternalFilesDir(null)!!.absolutePath
val mobileNet = File("$path/mobilenet_v1_1.0_224_quant.tflite")
val filter = "other/tensor,format=static,dimension=(string)3:224:224:1,type=uint8,framerate=0/1 ! " +
"tensor_filter framework=tensorflow-lite model=" + mobileNet.getAbsolutePath() + " ! " +
"other/tensor,format=static,dimension=(string)1001:1,type=uint8,framerate=0/1"

val mobileNetInfo = ModelInfo("MobileNet", filter)
modelList.add(mobileNetInfo)

val recyclerView = findViewById<RecyclerView>(R.id.model_list)
recyclerView.adapter = ModelAdapter(modelList)
recyclerView.layoutManager = LinearLayoutManager(this, )
}

override fun onStart() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class MainService : Service() {
private lateinit var handlerThread: HandlerThread
private var initialized = false
private var port = -1
private lateinit var tensorQueryServer: Pipeline
private var serverInfo = mutableMapOf<String,Pipeline>()

private fun startForeground() {
// Get NotificationManager
Expand Down Expand Up @@ -145,6 +145,9 @@ class MainService : Service() {
}

override fun onDestroy() {
serverInfo.values.forEach { pipeline ->
pipeline.close()
}
Toast.makeText(this, "The MainService has been gone", Toast.LENGTH_SHORT).show()
}

Expand Down Expand Up @@ -225,21 +228,22 @@ class MainService : Service() {
return port
}

fun startServer(filter: String): Int {
fun startServer(name:String, filter: String): Int {
val hostAddress = getIpAddress()
if (!isPortAvailable(port)) {
port = findPort()
}

val desc = "tensor_query_serversrc host=" + hostAddress + " port=" + port.toString() + " ! " +
filter + " ! tensor_query_serversink async=false"
tensorQueryServer = Pipeline(desc, null)
val desc = "tensor_query_serversrc host=" + hostAddress + " port=" + port.toString() +
" ! " + filter + " ! tensor_query_serversink async=false"
val tensorQueryServer = Pipeline(desc, null)
serverInfo[name] = tensorQueryServer
tensorQueryServer.start()

return port
}

fun stopServer() {
tensorQueryServer.close()
fun stopServer(name:String) {
serverInfo[name]?.close()
}
}
18 changes: 4 additions & 14 deletions ml_inference_offloading/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start" />
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop" />
<TextView
android:id="@+id/port"
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/model_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
android:layout_height="wrap_content" />
</LinearLayout>
21 changes: 21 additions & 0 deletions ml_inference_offloading/src/main/res/layout/models.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start" />
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop" />
<TextView
android:id="@+id/port"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>