Real-time mobile object detector in Xamarin.Android
Archive note: I first published this article on July 4, 2019 at geeks.ms/xamarinteam (Plain Concepts Xamarin Team). I republish it here from the Wayback Machine archive of May 18, 2024. The code is at github.com/jacano/CameraTF.
In 2019, the Plain Concepts Xamarin team contributed to TailwindTraders, Microsoft Build reference samples for Xamarin.Forms.
TailwindTraders is a fictional DIY retailer. The app sells tools for work and gardening.
Our demos focused on the main Xamarin.Forms features, especially Shell.
One part of the app was an AR experience. It read the phone’s rear camera, detected a product and showed its details with a purchase suggestion.
On Android we used the Xamarin binding of android.hardware.camera2 for the preview, and a custom version of EmguTF to detect the objects.
We selected three objects for detection, but the demo used one: a white hardhat.
This article presents CameraTF, a Xamarin.Android sample that uses the white hardhat model from TailwindTraders.
The model
The app runs an offline model. On a phone, speed matters more than accuracy, so we chose TensorFlow Lite.
EmguTF is a C# binding for TensorFlow Lite, the C++ library for mobile and IoT. It reads a serialized FlatBuffer model and runs the inference with the Interpreter class. The app references its .NET Standard wrapper, Emgu.TF.Lite, with a local build change.
For the model we used SSD MobileNet and did transfer learning over ssd_mobilenet_v1_0.75_depth_300x300_coco14_sync_2018_07_03. We trained it on Google Cloud TPUs with this pipeline config. The whole process is described in this post.
The training produced two files: hardhat_detect.tflite and hardhat_labels_list.txt.
Camera setup
The sample uses the Xamarin binding for android.hardware.camera, not the camera2 binding the TailwindTraders app used, and it gets one frame at a time.
CameraTF is an Android Activity. It shows a CameraSurfaceView with the preview. We used FastAndroidCamera to get each frame.
On the devices we tested (Nokia 6.1, Google Pixel XL, LG G4), the OnPreviewFrame callback gave about 30 fps, and the preview in CameraSurfaceView stayed smooth.
CameraController sets the preview format to NV21, and sets the fps range and the resolution with SetPreviewFpsRange and SetPreviewSize.
One step makes real time possible: converting NV21 (YUV420sp) to RGB in native code. See YuvHelper and yuv2rgb.cc.
From pixels to a detection
CameraAnalyzer runs the stages. From the RGB frame, it scales to the model input (300×300) and rotates to fix the camera orientation. SkiaSharp does the image work at native speed.
Then it fills the input tensor, calls the interpreter and reads four output tensors:
- the bounding boxes of the detected objects,
- the class index into hardhat_labels_list.txt,
- the confidence of each object,
- the number of objects.
On a Pixel XL, a screen recording put the whole pipeline inside CameraAnalyzer (scale, rotate, inference, read the outputs) at about 7 fps. I did not measure the other two devices.
The project is open for pull requests and issues.