Add depth model and interface class, update dependencies and android sdk

This commit is contained in:
Piv
2021-04-24 17:18:26 +09:30
parent 8188e4a58f
commit 497c3909f8
4 changed files with 75 additions and 9 deletions

View File

@@ -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();
}
}

Binary file not shown.