Skip to content

Commit

Permalink
App: Use ListView and map to support various model
Browse files Browse the repository at this point in the history
This patch replaces buttons and textView to ListView for supporting various model.
Also, MainService's class variable tensorQueryServer is replaced to MutableMap.

Signed-off-by: Yelin Jeong <yelini.jeong@samsung.com>
  • Loading branch information
niley7464 committed Jun 12, 2024
1 parent 6b18d5f commit 16587d9
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package ai.nnstreamer.ml.inference.offloading

import ai.nnstreamer.ml.inference.offloading.ui.theme.NnstreamerandroidTheme
import android.annotation.SuppressLint
import android.content.ComponentName
import android.content.Context
import android.content.Intent
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.ArrayAdapter
import android.widget.Button
import android.widget.ListView
import android.widget.TextView
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
Expand All @@ -22,10 +27,42 @@ import java.io.FileOutputStream
import java.io.IOException
import java.io.OutputStream

data class ModelInfo(
val name: String,
val filter: String
)

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

inner class ModelAdapter(context: Context, resource: Int, objects: List<ModelInfo>) :
ArrayAdapter<ModelInfo>(context, resource, objects) {

@SuppressLint("ViewHolder")
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val view = LayoutInflater.from(context).inflate(R.layout.models, parent, false)
val info = getItem(position)
val start = view.findViewById<Button>(R.id.start)
val portTextView = view.findViewById<TextView>(R.id.port)
val stop = view.findViewById<Button>(R.id.stop)

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

return view
}
}

private val connection = object : ServiceConnection {

override fun onServiceConnected(className: ComponentName, service: IBinder) {
Expand Down Expand Up @@ -90,22 +127,18 @@ 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()
})
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 listView = findViewById<ListView>(R.id.model_list)
listView.adapter = ModelAdapter(this, R.layout.models, modelList)
}

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 @@ -225,21 +225,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()
}
}
14 changes: 2 additions & 12 deletions ml_inference_offloading/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,8 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
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"
<ListView
android:id="@+id/model_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</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="match_parent">

<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>

0 comments on commit 16587d9

Please sign in to comment.