Created
March 4, 2019 22:20
-
-
Save navarasu/f9f272f6b46ebd845aa2050643a33d77 to your computer and use it in GitHub Desktop.
MainActivity java for TF Lite object detection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package francium.tech.objectdetector; | |
import android.content.res.AssetFileDescriptor; | |
import android.os.Bundle; | |
import android.renderscript.RenderScript; | |
import java.io.FileInputStream; | |
import java.io.IOException; | |
import java.nio.ByteBuffer; | |
import java.nio.channels.FileChannel; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import io.flutter.app.FlutterActivity; | |
import io.flutter.plugins.GeneratedPluginRegistrant; | |
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; | |
public class MainActivity extends FlutterActivity { | |
private static final String CHANNEL = "francium.tech/tensorflow"; | |
private static YoloDetector detector; | |
private static boolean modalLoaded = false; | |
private RenderScript rs; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
GeneratedPluginRegistrant.registerWith(this); | |
rs = RenderScript.create(this); | |
new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler( | |
new MethodCallHandler() { | |
@Override | |
public void onMethodCall(MethodCall call, Result result) { | |
if (call.method.equals("loadModel")) { | |
String modalPath = call.argument("modal_path"); | |
Map metaData = call.argument("meta_data"); | |
loadModel(modalPath,metaData,result); | |
} else if (call.method.equals("detectObject")) { | |
HashMap image = call.arguments(); | |
detectObject(image,result); | |
} | |
} | |
}); | |
} | |
protected void loadModel(final String modalPath, final Map metaData, final Result result) { | |
new Thread(new Runnable() { | |
public void run() { | |
try { | |
String modalPathKey = getFlutterView().getLookupKeyForAsset(modalPath); | |
ByteBuffer modalData = loadModalFile(getApplicationContext().getAssets().openFd(modalPathKey)); | |
detector = new YoloDetector(rs,modalData, metaData); | |
modalLoaded=true; | |
result.success("Modal Loaded Sucessfully"); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
result.error("Modal failed to loaded", e.getMessage(), null); | |
} | |
} | |
}).start(); | |
} | |
public ByteBuffer loadModalFile(AssetFileDescriptor fileDescriptor) throws IOException { | |
FileInputStream inputStream = new FileInputStream(fileDescriptor.getFileDescriptor()); | |
FileChannel fileChannel = inputStream.getChannel(); | |
long startOffset = fileDescriptor.getStartOffset(); | |
long declaredLength = fileDescriptor.getDeclaredLength(); | |
return fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength); | |
} | |
public void detectObject(final HashMap image, final Result result) { | |
new Thread(new Runnable() { | |
public void run() { | |
if (!modalLoaded) | |
result.error("Model is not loaded", null, null); | |
try { | |
List<Map<String, Object>> prediction = detector.detect(image); | |
result.success(prediction); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
result.error("Running model failed", e.getMessage(), null); | |
} | |
} | |
}).start(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment