Android SharedPreferences Tutorial and Example

Building Image Recognition into Android Apps with TensorFlow Lite

On-device image recognition has moved from research demos into everyday phone apps, and TensorFlow Lite remains the most accessible way to ship it inside an Android project. Rather than posting frames to a server and waiting for a JSON response, the model runs directly on the handset, which keeps latency in the tens of milliseconds and keeps images on the device. For Australian users that matters in practical ways: mobile data plans are still priced per gigabyte, regional coverage drops out along stretches of the Bruce Highway and the Nullarbor, and plenty of people would rather not upload photos of their workplace, their kids or their property to a third party.

This walkthrough covers the full path from a trained model to a working feature in an Android app. You will convert a checkpoint into the TensorFlow Lite format, feed camera frames into the interpreter, keep inference off the UI thread, and then package the result so the app still behaves sensibly on a five-year-old device or a phone that has just lost its signal in the outback. Along the way there are notes on model size, thread counts, and what to do when the classifier confidently labels everything the same thing.

Why on-device inference fits how Australians use their phones

Consider the common scenario: a queue at the drive-through window of a servo, or the self-service kiosk at a supermarket in Canberra. A customer holds up a loyalty card or a receipt, the app identifies it, and the screen jumps straight to the right reward. Doing that round trip to a remote API adds half a second of dead air and a dependency on a connection that may be congested at precisely the busiest moment of the day. Running the model locally removes both problems.

There is a cultural dimension too. Australians tend to be relaxed about technology in public but protective of personal images — nobody wants a snapshot of their backyard barbie circulating on a server they never chose. An app that classifies a barbecue setup, a fishing rod or a native animal entirely on the handset sidesteps that debate. It also means the feature keeps working at the bush campground near Halls Gorge, where there is no signal and no Wi-Fi hotspot.

So the architecture you are aiming for is deliberately offline-first: the .tflite file sits in the assets folder, the camera preview runs locally, and the network is never involved in the recognition step.

Converting a trained model into the TensorFlow Lite format

TensorFlow Lite cannot load a SavedModel or a full Keras checkpoint directly. You convert first, using the TensorFlow Lite Converter, and the output is a single flat file you can drop into app/src/main/assets/.

The quickest route for an existing Keras model is the converter function, which returns a byte array you can write to disk. For a model trained in PyTorch, export a TorchScript module and point the same converter at it. From there, you get a flat buffer with an input tensor that accepts three channels of pixels and an output tensor with one score per class.

Size is where most Android projects get stuck. An unquantised float model of a mid-sized vision network can land between 80 and 300 megabytes, which is not something you want a user downloading over 4G. Post-training quantisation to int8 typically shrinks the file by around four times and cuts memory use substantially, with a small accuracy cost that is usually acceptable for classification tasks. The converter accepts a representative dataset so it can measure the range of pixel values your particular images produce — use frames captured from the actual device, not synthetic images, otherwise the calibration will be misleading.

Keep the converted file and a plain text file listing your class labels in the same assets folder. Ordering matters: the label file must match the output tensor index exactly, and off-by-one errors here are the single most common reason a classifier returns something plausible but wrong.

Getting camera frames into the interpreter

The pipeline has three parts: an image source, a resize step, and the interpreter. For most apps the source is CameraX, because it handles rotation, front and back camera switching, and lifecycle management more gracefully than the older camera API.

CameraX gives you an ImageAnalysis use case that delivers frames as ImageProxy objects. Inside the analyser you convert the buffer into a Bitmap, rotate it so it matches the sensor orientation, and scale it down to the input resolution the model expects — often 224 by 224. Anything larger is wasted work, since the interpreter will only look at the pixels it declared.

Then create an Interpreter with a Tensor built from your bitmap's pixel data. Feed it, invoke the interpreter, and read the output tensor as a float array. The highest score is your prediction; the index is your label.

Two details cause most bugs. First, the input tensor layout differs between models: some expect a batch dimension of one, others do not, so check the shape your converter produced. Second, the RGB versus BGR channel order is not universal. If your predictions look like a plausible but shuffled version of reality, suspect channel order before you suspect the weights.

Keeping inference off the main thread

Calling the interpreter directly inside the camera analyser will eventually cause dropped frames, because inference is CPU-intensive and the UI thread is what draws the preview. The standard fix is to move analysis to a background executor, then post the result back to the main thread with a handler or a coroutine dispatcher.

The modern pattern is to make the analyser class ExecutorService-backed, submit each frame, and shut the executor down in onStop. Do not queue frames indefinitely — if a frame arrives while the previous one is still being processed, drop the new one. A recogniser running at five frames per second on an old handset is perfectly usable; one trying to process every frame at thirty will lag behind reality and consume battery for nothing.

If you need higher throughput on a flagship device, add the GPU delegate and check whether the delegate loads successfully on your target hardware. Not every SoC supports it, so guard it with a try and catch and fall back to the default CPU path. Quantised models also run efficiently through the XNNPACK delegate, which is enabled by default on recent library versions.

Packaging the feature for real-world use

A finished feature needs more than a working model. Keep the .tflite file compressed in assets if it is large, load it lazily rather than at application start, and expose a threshold so low-confidence predictions return "no match" instead of a confident wrong answer — a shoe shop app that always claims a trainer is a football boot will lose its users quickly.

Then think about what the app does after recognition. For the servo kiosk example, the result should populate a loyalty number field and a beep. For the bush-walk classifier that identifies kangaroos and wombats near a trail in the Grampians, the output should be a quiet caption, because tourists and locals alike are often several hundred metres from any signal.

Log the timing of each inference pass during development, along with the memory footprint of the interpreter. If a phone from a couple of years back takes longer than a couple of hundred milliseconds per frame, move to a smaller input resolution or a lighter architecture such as MobileNetV3 rather than shipping a sluggish experience.

Finally, write a graceful failure path. If the model file is missing from the install, if the camera permission is denied, or if the interpreter throws on an unexpected input shape, the app should explain the problem in plain Australian-friendly language — a quick "no worries, try again" message and a retry button — rather than crashing with a stack trace nobody can read.