Now to make an Android app based on image recognition, leaving aside the UI part, the first thing to do is to run TensorFlow on Android.
There are two ways to use TensorFlow on Android:
- TensorFlow for Mobile, more mature, contains many functional methods.
- TensorFlow Lite, is a 1 upgrade version, currently in the developer Preview stage, the advantage is small volume performance has been optimized. is the future trend.
In view of the project reasons, the first type of use.
The first step is to add the TensorFlow Library reference to Android studio.
There are three different ways
Since the network is fine, I use the first approach directly (Include the Jcenter AAR which contains it):
build.gradle
add dependencies in. compile ‘org.tensorflow:tensorflow-android:+‘
The second step is to invoke the TensorFlow interface for use.
The code of the official website:
// Load the model from disk.TensorFlowInferenceInterface inferenceInterface =newTensorFlowInferenceInterface(assetManager, modelFilename);// Copy the input data into TensorFlow.inferenceInterface.feed13);// Run the inference call.inferenceInterface.run(outputNames, logStats);// Copy the output Tensor back into the output array.inferenceInterface.fetch(outputName, outputs);
Do not understand how to set these parameters, but can use the official example, so directly copy the image recognition code
Copy these two files and you can: Classifier.java and Tensorflowimageclassifier.java.
The third step is to identify.
//Create a classifier with model. FinalClassifier Classifier = Tensorflowimageclassifier.Create(getassets(), Model_file, Label_file, Input_size, Image_mean, IMAGE_STD, Input_name, OUTPU T_name);//Load PictureFinalBitmap Bitmap = bitmapfactory.Decoderesource(getresources(), R.drawable.Noodle);//Image recognitionBtn.Setonclicklistener(NewView.Onclicklistener() {@Override Public void OnClick(View view) {List<classifier.Recognition> results = classifier.Recognizeimage(bitmap); for(Classifier.RecognitionResult:results) {TV.SetText(TV.GetText().toString() +"\ r \ n"+ result.GetTitle()); } }});
At this point, the success of the Android phone ran the TensorFlow library, really is very simple to use.
PS: Part of the picture encountered Arrayoutofindex problem is this reason.
How to use TensorFlow for mobile, development environment for Android Studio