Skip to content

Commit

Permalink
Update Flutter plugin 0.0.1-beta01
Browse files Browse the repository at this point in the history
  • Loading branch information
kuloud committed Mar 16, 2020
1 parent 94f9871 commit 34aa717
Show file tree
Hide file tree
Showing 132 changed files with 2,686 additions and 1,943 deletions.
6 changes: 3 additions & 3 deletions Android/examples/demo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 29
buildToolsVersion = '29.0.2'
buildToolsVersion = '29.0.3'

defaultConfig {
applicationId "com.didi.aoe.examples.demo"
Expand Down Expand Up @@ -57,8 +57,8 @@ dependencies {
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.2'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.2'

implementation 'com.noctis:camera-view:1.0.0'
implementation 'com.noctis:camera-view-common:1.0.0'
implementation 'com.noctis:camera-view:1.0.1'
implementation 'com.noctis:camera-view-common:1.0.1'

implementation deps.aoe.library.service

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# flutter_aoe_plugin
# aoeflutter

A new Flutter plugin.
Flutter plugin for AoE.

## Getting Started

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.flutter.plugins;

import io.flutter.plugin.common.PluginRegistry;
import com.didi.aoe.aoeflutter.AoeFlutterPlugin;

/**
* Generated file. Do not edit.
*/
public final class GeneratedPluginRegistrant {
public static void registerWith(PluginRegistry registry) {
if (alreadyRegisteredWith(registry)) {
return;
}
AoeFlutterPlugin.registerWith(registry.registrarFor("com.didi.aoe.aoeflutter.AoeFlutterPlugin"));
}

private static boolean alreadyRegisteredWith(PluginRegistry registry) {
final String key = GeneratedPluginRegistrant.class.getCanonicalName();
if (registry.hasPlugin(key)) {
return true;
}
registry.registrarFor(key);
return false;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group 'com.didi.aoe.flutter_aoe_plugin'
version '1.0-SNAPSHOT'
group 'com.didi.aoe.aoeflutter'
version '1.0.0-SNAPSHOT'

buildscript {
ext.kotlin_version = '1.3.61'
Expand All @@ -9,7 +9,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:3.5.2'
classpath 'com.android.tools.build:gradle:3.6.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
Expand Down Expand Up @@ -41,4 +41,9 @@ android {

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

api 'com.didi.aoe:library-core:1.1.4'
api 'com.didi.aoe:library-common:1.1.4'
api 'com.didi.aoe:runtime-tensorflow-lite:1.1.4'
api 'com.didi.aoe:runtime-pytorch:1.1.4'
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#Mon Mar 16 00:06:01 CST 2020
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip
1 change: 1 addition & 0 deletions flutter/aoe_flutter_plugin/android/settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rootProject.name = 'aoeflutter'
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.didi.aoe.flutter_aoe_plugin">
package="com.didi.aoe.aoeflutter">
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package com.didi.aoe.aoeflutter

import android.content.Context
import com.didi.aoe.aoeflutter.model.model.InterceptorError
import com.didi.aoe.aoeflutter.model.model.ModelOutput
import com.didi.aoe.aoeflutter.model.model.PerformanceInfo
import com.didi.aoe.library.core.AoeClient
import com.didi.aoe.library.logging.LoggerFactory
import com.example.aoe_flutter.interpreter.MnistTensorFlowLiteInterpreter
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import io.flutter.plugin.common.PluginRegistry.Registrar

/** AoeFlutterPlugin */
class AoeFlutterPlugin(private val mContext: Context) : FlutterPlugin, MethodCallHandler {
private val mLogger = LoggerFactory.getLogger("AoeFlutterPlugin")
private var mAoeClient: AoeClient? = null

private val mPerf = PerformanceInfo()

override fun onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
val channel = MethodChannel(flutterPluginBinding.binaryMessenger, "aoeflutter")
channel.setMethodCallHandler(AoeFlutterPlugin(flutterPluginBinding.applicationContext));
}

companion object {
@JvmStatic
fun registerWith(registrar: Registrar) {
val channel = MethodChannel(registrar.messenger(), "aoeflutter")
channel.setMethodCallHandler(AoeFlutterPlugin(registrar.context()))
}
}

override fun onMethodCall(call: MethodCall, result: Result) {
if ("start" == call.method) {
start(call, result)
} else if ("process" == call.method) {
process(call, result)
} else if ("stop" == call.method) {
stop(call, result)
} else {
result.notImplemented()
}
}

override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {

}

private fun start(call: MethodCall, result: MethodChannel.Result) {
val inputs = call.arguments as Map<*, *>
val runtime = inputs["runtime"]
val modelDir = inputs["modelDir"] as String
mLogger.debug("runtime: $runtime, modelDir: $modelDir")
if (inputs.isEmpty()) {
result.error("", InterceptorError.parameterError.errMsg, InterceptorError.parameterError.toMap())
return
}
// Fixme 临时处理
val aoeClient = AoeClient(mContext,
AoeClient.Options()
.setInterpreter(MnistTensorFlowLiteInterpreter::class.java),
modelDir)
mAoeClient = aoeClient
mAoeClient?.init(object : AoeClient.OnInitListener() {
override fun onSuccess() {
super.onSuccess()
mLogger.debug("init Success")
result.success(ModelOutput(data = true).toMap())
}

override fun onFailed(code: Int, msg: String?) {
super.onFailed(code, msg)
mLogger.debug("init Failed $code $msg")
result.error(code.toString(), msg,
ModelOutput(error = InterceptorError.modelError).toMap())
}
})

}

private fun process(call: MethodCall, result: MethodChannel.Result) {

val input = call.argument<Any>("data")
val output = mAoeClient?.process(input)
val statInfo = mAoeClient?.acquireLatestStatInfo()

if (statInfo != null) {
mPerf.time.tracks(statInfo.timeCostInMills.toFloat())
mPerf.cpu.tracks(statInfo.cpuRate)
mPerf.mem.tracks(statInfo.memoryInfo)
}
val out = ModelOutput(data = output, performance = mPerf)
mLogger.debug("process $out")
result.success(out.toMap())
}

private fun stop(call: MethodCall, result: MethodChannel.Result) {
mAoeClient?.release()
result.success(ModelOutput(data = true).toMap())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.example.aoe_flutter.interpreter

import com.didi.aoe.runtime.tensorflow.lite.TensorFlowInterpreter
import java.nio.ByteBuffer
import android.util.Log
import kotlin.math.roundToInt

/**
*
*
* @author noctis
* @since 1.1.0
*/
class MnistTensorFlowLiteInterpreter : TensorFlowInterpreter<ByteArray, List<Int>?, FloatArray, Array<FloatArray>>() {
private val TAG = "MnistInterpreter"
override fun postProcess(modelOutput: Array<FloatArray>?): List<Int>? {

if (modelOutput != null && modelOutput.size == 1) {
val result = modelOutput[0].map {it.roundToInt()}
Log.d(TAG, "postProcess ${result.toTypedArray().contentToString()}")
return result
//return modelOutput[0].indexOf(modelOutput[0].max()!!)
//
//
//for (i in modelOutput[0].indices) {
// if (modelOutput[0][i].compareTo(1f) == 0) {
// return i
// }
//}
}

return null
}

override fun preProcess(input: ByteArray?): FloatArray? {
//Log.d(TAG, "preProcess ${input?.contentToString()} ${input?.size}")
if (input == null) {
return null
}
val r = FloatArray(input.size / 4)
for (i in input.indices step 4) {
r[i / 4] = Float.fromBits(input[i].toInt() or
input[i + 1].toInt() shl 8 or
input[i + 2].toInt() shl 16 or
input[i + 3].toInt() shl 24)
}
return r
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.didi.aoe.aoeflutter.model.model

/**
*
*
* @author noctis
* @since 1.1.0
*/
data class DeviceInfo @JvmOverloads constructor(
var uuid: String = "",
var name: String = "",
var model: String = "",
var system: String = "",
var version: String = "",
var disk: String = "",
var memory: String = "",
var ip: String = "",
var macAddress: String = ""
) {
fun toMap(): Map<String, Any?> {
return mapOf(
Pair("uuid", uuid),
Pair("name", name),
Pair("model", model),
Pair("system", system),
Pair("version", version),
Pair("disk", disk),
Pair("memory", memory),
Pair("ip", ip),
Pair("macAddress", macAddress)
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.didi.aoe.aoeflutter.model.model

/**
*
*
* @author noctis
* @since 1.1.0
*/
enum class InterceptorErrorType {
InValidParameter, //参数异常
InValidInterceptorError, //推理框架无效
InValidModelError, //模型无效
InValidSampleError, //测试数据集无效
ProcessError, //推理异常
}

data class InterceptorError @JvmOverloads constructor(
var errType: InterceptorErrorType = InterceptorErrorType.InValidParameter,
var errMsg: String = ""

) {
fun toMap(): Map<String, Any?> {
return mapOf(
Pair("errType", errType),
Pair("errMsg", errMsg)
)
}

companion object {
@JvmStatic
val parameterError =
InterceptorError(InterceptorErrorType.InValidParameter, "the parameters is invalid")

@JvmStatic
val interceptorError = InterceptorError(InterceptorErrorType.InValidInterceptorError,
"can not suppport the infer inframework or can not load the model")

@JvmStatic
val modelError = InterceptorError(InterceptorErrorType.InValidModelError,
"model does not exist")

@JvmStatic
val sampleError = InterceptorError(InterceptorErrorType.InValidSampleError,
"sample data invalid")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.didi.aoe.aoeflutter.model.model

/**
*
*
* @author noctis
* @since 1.1.0
*/
data class ModelOutput @JvmOverloads constructor(
var data: Any? = null,
var error: InterceptorError? = null,
var performance: PerformanceInfo? = null) {

fun toMap(): Map<String, Any?> {
return mapOf(
Pair("data", if (data != null) data else Any()),
Pair("error", if (error != null) error?.toMap() else emptyMap()),
Pair("performance", if (performance != null) performance?.toMap() else emptyMap())
)
}
}


Loading

0 comments on commit 34aa717

Please sign in to comment.