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 aButton
instance to load images from storage.imageView
: This is nanImageView
instance to display images.selectedImage
: This is aBitmap
instance representing the currently selected image.resultTextView
: This is aTextView
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 anInputImage
from the providedBitmap
, 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 inputBitmap
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 inputBitmap
. It returns a modifiedBitmap
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 aRect
) and the associated text.
Get hands-on with 1400+ tech skills courses.