Firebase Series | Blog 5 |A Easy Hack to Detect Faces in Pictures and Videos with Firebase MLKit.
A few years back detecting faces in photos and videos seems to be a big tech rocket science. Wonder what made this easy? Its Googles Firebase ML Kit. It’s an is an amazing tool kit which has made face detection easy as pie. It has many capabilities, but below mentioned are the key capabilities of ML Kit: Face detector.
Recognise and locate facial features
Get the contours of facial features
Recognise facial expressions
Track faces across video frames
Process video frames in real time
ML-Kit face detector allows developers to identify many facial features from an image. The input for the face detector is a bitmap, but with some constraints. The image you provide as input should be minimum of 100x100 pixel. If the image a scaled-down well, the processing time of the detection will be very fast, but it should meet the minimum constraint which I mentioned before. Once all the faces are identified, the detector object will return an Array-list of faces. This is that simple, a few lines of code and you making your app AI-powered. Thrilled?
In this blog, I will be discussing how ML Kit can be integrated into an Android app which will run an image through the Face detector model of ML Kit and retrieves all the faces in it.
- Connect your Project with Firebase Face detection
- Create a bitmap from the camera or from the gallery. (In this project I am taking a picture using phone’s camera)
- Create a builder using FirebaseVisionFaceDetectorOptions object.
- Create a FirebaseVision object and pass the above builder into it. This object will work as a detector.
- Pass your bitmap to this detector which will be returning the ArrayList of faces.
Connect your Project with Firebase Face detection
Create a bitmap from the camera or from the gallery. (In this project I am taking a picture using phone’s camera)
The above code is very straight forward. takepicture method takes the user straight to the camera. Once the user clicks the picture and clicks on the “OK” button, the app will return the bitmap to the method activityforresult.
Create a builder using FirebaseVisionFaceDetectorOptions object.
Before passing the bitmap to the detector, a detector should be build using FirebaseVisionFaceDetectorOptions. In a FirebaseVisionFaceDetectorOptions object, we will set the features we intend to detect in the uploaded bitmap. Features such as detect landmarks, detect contours, classify faces(emotions), minimum face size, face tracking etc.
Create a FirebaseVision object and pass the above builder into it. This object will work as a detector.
This one line of code creates an instance of FibrebaseVision that takes in a builder. The builder that we have already created should be passed to initiate FibrebaseVision. Now the detector is ready and can be used to detect all the faces in the bitmap.
Pass your bitmap to this detector which will be returning the ArrayList of faces.
In the above code, we are feeding a bitmap to the detector and the detector returns an array of faces. I have used a third party class to draw the overlay on the detected faces. FaceContourGraphic is the class which I have used. There are many features which we can fetch from the face object. Some of them I have already commented in the code, but apart from that, you can refer Android developer documentation which I will be adding at the end of this blog.
By doing these five steps, we have successfully detected all the faces in a picture. This API is very helpful in many use cases, photo editing, adding face overlays, generating avatars etc. Android has given beautiful documentation for ML Kit face detection, you can also dive deep into it for more information on this topic.
You can find the whole project here in Github. This is a public repo which has many demo projects. You can use GitZip to download a specific project from this repo. Paste this link in GitZip and you have it.
Follow me on Twitter and medium. Feel free to contribute to Repo — AndroidAppSamples. Let’s grow together 💚