diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index a9737bd..6c47b6b 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -11,7 +11,7 @@ cache: - .gradle/ protoc_gen: - image: vato.ddns.net:8083/gradle + image: vato.ddns.net:8083/gradle:6.7.1 stage: protoc rules: - changes: diff --git a/app/build.gradle b/app/build.gradle index 0818652..bff755d 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -4,12 +4,12 @@ plugins{ } android { - compileSdkVersion 29 + compileSdkVersion 30 buildToolsVersion "29.0.2" defaultConfig { applicationId "org.vato.carcontroller" minSdkVersion 26 - targetSdkVersion 29 + targetSdkVersion 30 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" @@ -24,18 +24,24 @@ android { sourceCompatibility = 1.8 targetCompatibility = 1.8 } + buildFeatures { + mlModelBinding true + } } dependencies { implementation project(':protobuf') implementation fileTree(dir: 'libs', include: ['*.jar']) - implementation 'androidx.appcompat:appcompat:1.1.0' - implementation 'androidx.constraintlayout:constraintlayout:1.1.3' - implementation 'com.google.android.material:material:1.1.0' + implementation 'androidx.appcompat:appcompat:1.2.0' + implementation 'androidx.constraintlayout:constraintlayout:2.0.4' + implementation 'com.google.android.material:material:1.3.0' implementation 'androidx.preference:preference:1.1.1' - testImplementation 'junit:junit:4.12' - androidTestImplementation 'androidx.test:runner:1.2.0' - androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' + implementation 'org.tensorflow:tensorflow-lite-support:0.1.0' + implementation 'org.tensorflow:tensorflow-lite-metadata:0.1.0-rc1' + implementation 'org.tensorflow:tensorflow-lite-gpu:2.3.0' + testImplementation 'junit:junit:4.13.2' + androidTestImplementation 'androidx.test:runner:1.3.0' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' implementation 'io.grpc:grpc-okhttp:1.29.0' // CURRENT_GRPC_VERSION implementation 'io.grpc:grpc-protobuf-lite:1.29.0' // CURRENT_GRPC_VERSION implementation 'io.grpc:grpc-stub:1.29.0' // CURRENT_GRPC_VERSION diff --git a/app/src/main/java/org/vato/carcontroller/depth/DepthPredictionModel.java b/app/src/main/java/org/vato/carcontroller/depth/DepthPredictionModel.java new file mode 100644 index 0000000..c6019eb --- /dev/null +++ b/app/src/main/java/org/vato/carcontroller/depth/DepthPredictionModel.java @@ -0,0 +1,60 @@ +package org.vato.carcontroller.depth; + +import android.content.Context; +import android.graphics.Bitmap; + +import org.tensorflow.lite.gpu.CompatibilityList; +import org.tensorflow.lite.support.image.ColorSpaceType; +import org.tensorflow.lite.support.image.TensorImage; +import org.tensorflow.lite.support.model.Model; +import org.vato.carcontroller.ml.MobilenetNnconv5; + +import java.io.IOException; + +/** + * Predicts the depth + */ +public class DepthPredictionModel implements AutoCloseable { + + private MobilenetNnconv5 model = null; + + private DepthPredictionModel() { + } + + public static DepthPredictionModel newModel(Context context) { + try { + DepthPredictionModel created = new DepthPredictionModel(); + Model.Options.Builder options = new Model.Options.Builder(); + CompatibilityList compatList = new CompatibilityList(); + if (compatList.isDelegateSupportedOnThisDevice()) { + // TODO: Choice of gpu and nnapi? + options.setDevice(Model.Device.GPU); + } else { + // TODO: User configurable? + options.setNumThreads(4); + } + created.model = MobilenetNnconv5.newInstance(context, options.build()); + return created; + } catch (IOException e) { + return null; + } + } + + public Bitmap predict(Bitmap bitmap) { + // TODO: Resizing/cropping, handle it all in the framework + TensorImage image = TensorImage.fromBitmap(bitmap); + // Runs model inference and gets result. + MobilenetNnconv5.Outputs outputs = model.process(image.getTensorBuffer()); + TensorImage depth = new TensorImage(); + depth.load(outputs.getOutputFeature0AsTensorBuffer(), ColorSpaceType.GRAYSCALE); + // TODO: Resize back to something similar to original (ignore crop obviously)? + return depth.getBitmap(); + + } + + + @Override + public void close() { + model.close(); + } +} diff --git a/app/src/main/ml/mobilenet_nnconv5.tflite b/app/src/main/ml/mobilenet_nnconv5.tflite new file mode 100644 index 0000000..ccf902b Binary files /dev/null and b/app/src/main/ml/mobilenet_nnconv5.tflite differ diff --git a/build.gradle b/build.gradle index 975620b..1929f25 100644 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:4.0.1' + classpath 'com.android.tools.build:gradle:4.1.3' classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.10' // NOTE: Do not place your application dependencies here; they belong