This is an example of how to use TensorFlow library on Android to classify images. You can find the code explanation in details on my blog post. If you're interested how the model was created, you can read about it on my other blog post.
The example was created for the presentation purpose which you can find here.
Flow of the app is pretty simple:
- Take a photo.
- Classify if it's hot or not.
- Show the results.
The app consists of two main components:
MainActivity
which is responsible for taking a photo.ImageClassifier
which classifies the photo.
ImageClassifier
properties:
inputName
- the name of the classifier's input (the photo pixels goes in there),outputName
- the name of the classifier's output (the results can be found there),imageSize
- the size of the photo,labels
- the list of the labels (in our case "hot" and "not"),imageBitmapPixels
- the array with bitmap pixels (int values before normalization),imageNormalizedPixels
- the array with normalized pixels,results
- the list with the results,tensorFlowInference
- the TensorFlow API object (which is used for inference).
For classifying photos the app is using retrained MobileNet model. The model can be found inside the assets
folder together with the labels file.
Before classification the photo needs to be prepared to fit the input of the classifier which is 224x224 pixels. Because of that the photo is resized and cropped which is happening inside the ImageUtils
.
Prepared photo is passed to the ImageClassifier
. The class responsibilities are as follows:
- Nomalizing pixels of the photo -
preprocessImageToNormalizedFloats()
method. - Classifying -
classifyImageToOutputs()
method. - Getting the results -
getResults()
method.
For the classification process the instance of the TensorFlowInferenceInterface
is used. The classification looks as follows:
- Put the data to the classifier:
tensorFlowInference.feed(inputName, imageNormalizedPixels, 1L, imageSize, imageSize, COLOR_CHANNELS.toLong())
- Run the classifier:
tensorFlowInference.run(arrayOf(outputName), ENABLE_LOG_STATS)
- Get the results from the output:
tensorFlowInference.fetch(outputName, results)
The results are then passed to the MainActivity
and shown on the screen.