Object Detection App Using the ML Kit

Learn to deploy a TF Lite object detection model to an Android app using the ML Kit.

We'll cover the following

Android App

The MainActivity class of the app integrates ML Kit’s object detection API to perform object detection on selected images. It provides methods for image loading, object detection processing, and visualizing the detection results on the UI. The MainActivity class has the fields and methods given below.

Fields

  • loadImageButton: This is a Button instance to load images from storage.

  • imageView: This is nan ImageView instance to display images.

  • selectedImage: This is a Bitmap instance representing the currently selected image.

  • resultTextView: This is a TextView instance to display the classification result.

  • pickImage: This is to facilitate the picking of an image from the device’s storage.

Methods

  • onCreate(): This method initializes the UI components, sets up click listeners for buttons, and registers an activity result for image picking.

  • runMLKITObjectDetection(bitmap : Bitmap): This method performs object detection using the ML Kit. It creates an InputImage from the provided Bitmap, initializes the object detector using a local model, processes the image for object detection, and handles the success and failure callbacks.

  • addDimensionToBitmap(bitmap : Bitmap): This adds an extra dimension to the input Bitmap by creating a new bitmap with adjusted dimensions.

  • drawBoxTextDetections(bitmap : Bitmap, detectionResults : List<dataClassBoxText>): This draws bounding boxes and text around detected objects on the input Bitmap. It returns a modified Bitmap with the visualized results.

  • print_results(detectedObjects : List<DetectedObject>): This prints the details of the detected objects, such as tracking IDs, bounding box coordinates, categories, and confidence scores.

  • dataClassBoxText : This is a data class that represents detection results for visualization. It holds the bounding box coordinates (as a Rect) and the associated text.

Get hands-on with 1200+ tech skills courses.