How to use TensorFlow for mobile, development environment for Android Studio

Source: Internet
Author: User

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:

    1. TensorFlow for Mobile, more mature, contains many functional methods.
    2. 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.gradleadd 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.