Tensorflow
Android
Front Camera
Object Detection
Mobile AI

Tensorflow Android demo Detection using Front Camera

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Running an object detection demo on the Android front camera is mostly a camera pipeline problem, not a model problem. The detector can stay the same, but you need to select the front-facing lens, correct the image orientation, and handle the mirror effect so the preview and detection overlay stay consistent.

Select the Front Camera Explicitly

If the app uses Camera2, choose the camera whose LENS_FACING value is FRONT.

kotlin
1import android.content.Context
2import android.hardware.camera2.CameraCharacteristics
3import android.hardware.camera2.CameraManager
4
5fun findFrontCameraId(context: Context): String? {
6    val manager = context.getSystemService(Context.CAMERA_SERVICE) as CameraManager
7
8    for (cameraId in manager.cameraIdList) {
9        val characteristics = manager.getCameraCharacteristics(cameraId)
10        val facing = characteristics.get(CameraCharacteristics.LENS_FACING)
11        if (facing == CameraCharacteristics.LENS_FACING_FRONT) {
12            return cameraId
13        }
14    }
15
16    return null
17}

In many demo apps, the back camera is hard-coded or chosen as the default. Switching to the front camera starts by changing that selection logic.

Keep Preview Coordinates and Model Input Aligned

Once the front camera is selected, the next issue is frame orientation. Front camera previews are often mirrored for user comfort, but the tensor fed into the model may not be mirrored, rotated, or cropped the same way.

A minimal preprocessing step might look like this:

kotlin
1import android.graphics.Bitmap
2import android.graphics.Matrix
3
4fun prepareFrontCameraBitmap(source: Bitmap): Bitmap {
5    val matrix = Matrix()
6    matrix.postScale(-1f, 1f) // mirror horizontally
7    matrix.postRotate(270f)   // example rotation, device dependent
8
9    return Bitmap.createBitmap(
10        source,
11        0,
12        0,
13        source.width,
14        source.height,
15        matrix,
16        true
17    )
18}

The exact rotation depends on device orientation and how the demo obtains frames. The important point is consistency: the image shown to the user, the image sent to the model, and the coordinates used by the overlay must describe the same geometry.

Draw Detection Boxes Correctly

If the front preview is mirrored but the detector output is not, bounding boxes appear on the wrong side of the screen. You can compensate during overlay drawing by flipping the X coordinate.

kotlin
fun mirrorX(x: Float, previewWidth: Float): Float {
    return previewWidth - x
}

In practice, you usually transform the whole rectangle rather than a single point, but the idea is the same. Front camera support often fails because developers switch the lens and forget to update the overlay transform.

The model itself usually does not care whether the source is front or back camera. The surrounding camera and rendering code does.

Performance Still Matters

Front camera demos run on the same mobile hardware limits as back camera demos. If the frame rate drops badly, reduce input resolution, skip some frames, or use a lighter TensorFlow Lite model. A slow mirrored preview plus delayed boxes feels worse than a fast, stable experience with a smaller model.

Also remember camera permission handling, lifecycle cleanup, and background threading. Those are easy to overlook when the only visible goal is "use the front camera."

Common Pitfalls

  • Switching to the front camera ID but forgetting to update preview rotation or mirroring.
  • Feeding one orientation to the model and drawing boxes in another orientation on screen.
  • Assuming the detector must be retrained just because the source changed to the front lens.
  • Debugging accuracy when the real problem is coordinate transformation in the overlay.

Summary

  • Front camera object detection is mainly about camera selection and coordinate handling.
  • Choose the LENS_FACING_FRONT camera explicitly in the Android camera stack.
  • Keep preview, model input, and overlay transforms consistent.
  • Mirror or rotate frames only when the rest of the pipeline matches that choice.
  • Optimize for responsive inference so the front camera demo remains usable in real time.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.