Skip to content

Commit

Permalink
App: Let the activity handles model
Browse files Browse the repository at this point in the history
This patch makes the activity to handle model.
When a model is added, it can be accessed through an activity.

Signed-off-by: Yelin Jeong <[email protected]>
  • Loading branch information
niley7464 authored and wooksong committed Jun 13, 2024
1 parent 9e97dea commit 5ee7526
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.content.Intent
import android.content.ServiceConnection
import android.os.Bundle
import android.os.IBinder
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.TextView
Expand All @@ -16,8 +17,13 @@ import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.ui.Modifier
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.io.OutputStream

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

private val connection = object : ServiceConnection {
Expand All @@ -31,6 +37,42 @@ class MainActivity : ComponentActivity() {
mService = null
}
}
private fun copyFilesToExternalDir() {
val am = resources.assets
var files: Array<String>? = null

try {
files = am.list("models/")
} catch (e: java.lang.Exception) {
Log.e(TAG, "#### Failed to get asset file list")
e.printStackTrace()
return
}

// Copy files into app-specific directory.
for (filename in files!!) {
try {
val inFile = am.open("models/$filename")
val outDir = getExternalFilesDir(null)!!.absolutePath
val outFile = File(outDir, filename)
val out: OutputStream = FileOutputStream(outFile)

val buffer = ByteArray(1024)
var read: Int
while ((inFile.read(buffer).also { read = it }) != -1) {
out.write(buffer, 0, read)
}

inFile.close()
out.flush()
out.close()
} catch (e: IOException) {
Log.e(TAG, "Failed to copy file: $filename")
e.printStackTrace()
return
}
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -45,11 +87,17 @@ class MainActivity : ComponentActivity() {
}
setContentView(R.layout.activity_main)

copyFilesToExternalDir()
startForegroundService(Intent(this, MainService::class.java))

val start = findViewById<Button>(R.id.start)
start.setOnClickListener(View.OnClickListener {
val serverPort = mService?.startServer()
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();
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,13 @@ import android.os.Looper
import android.os.Message
import android.os.Process
import android.util.Log
import android.widget.Button
import android.widget.Toast
import androidx.core.app.NotificationCompat
import androidx.core.app.ServiceCompat
import androidx.core.content.ContextCompat
import org.nnsuite.nnstreamer.NNStreamer
import org.nnsuite.nnstreamer.Pipeline
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.io.OutputStream
import java.net.Inet4Address
import java.net.InetAddress
import java.net.ServerSocket
import kotlin.concurrent.thread

Expand Down Expand Up @@ -81,7 +75,6 @@ class MainService : Service() {
private var initialized = false
private var port = -1
private lateinit var tensorQueryServer: Pipeline
private lateinit var model: File

private fun startForeground() {
// Get NotificationManager
Expand Down Expand Up @@ -124,10 +117,7 @@ class MainService : Service() {
Log.e(TAG, "Failed to initialize nnstreamer")
stopSelf()
}
copyFilesToExternalDir()

val path = getExternalFilesDir(null)!!.absolutePath
model = File("$path/mobilenet_v1_1.0_224_quant.tflite")
handlerThread = HandlerThread("ServiceStartArguments", Process.THREAD_PRIORITY_BACKGROUND).apply {
start()
}
Expand Down Expand Up @@ -158,43 +148,6 @@ class MainService : Service() {
Toast.makeText(this, "The MainService has been gone", Toast.LENGTH_SHORT).show()
}

private fun copyFilesToExternalDir() {
val am = resources.assets
var files: Array<String>? = null

try {
files = am.list("models/")
} catch (e: java.lang.Exception) {
Log.e(TAG, "#### Failed to get asset file list")
e.printStackTrace()
return
}

// Copy files into app-specific directory.
for (filename in files!!) {
try {
val inFile = am.open("models/$filename")
val outDir = getExternalFilesDir(null)!!.absolutePath
val outFile = File(outDir, filename)
val out: OutputStream = FileOutputStream(outFile)

val buffer = ByteArray(1024)
var read: Int
while ((inFile.read(buffer).also { read = it }) != -1) {
out.write(buffer, 0, read)
}

inFile.close()
out.flush()
out.close()
} catch (e: IOException) {
Log.e(TAG, "Failed to copy file: $filename")
e.printStackTrace()
return
}
}
}

private fun initNNStreamer() {
if (this.initialized) {
return
Expand Down Expand Up @@ -272,16 +225,14 @@ class MainService : Service() {
return port
}

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

val desc = "tensor_query_serversrc host=" + hostAddress + " port=" + port.toString() + " ! " +
"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 ! tensor_query_serversink async=false"
filter + " ! tensor_query_serversink async=false"
tensorQueryServer = Pipeline(desc, null)
tensorQueryServer.start()

Expand Down

0 comments on commit 5ee7526

Please sign in to comment.