Docker + OpenCV: Real-Time Facial Recognition in 1 minute | by Chamal Gomes | Towards Data Science

Integrating Docker, OpenCV.js and Nginx for quick deployment of real-time facial recognition machine learning models. This is a convenient solution for the Mac Docker community who is struggling to get webcam access due to the drawbacks of the Docker hyper kit support with the help of Nginx. This solution could easily be extended to include any readymade machine learning model from OpenCV, Keras, Tensorflow, etc..
Motivation:
The process of building OpenCV.js from source, though recommended, requires LLVM-to-JavaScript compiler and CMAKE which is quite involved.Accessing the Mac webcam from Docker container was not possible due to the drawbacks of the Docker hyper kit support. Thus, deploying real-time facial recognition models was not possible on a Mac.
The process of building OpenCV.js from source, though recommended, requires LLVM-to-JavaScript compiler and CMAKE which is quite involved.
Accessing the Mac webcam from Docker container was not possible due to the drawbacks of the Docker hyper kit support. Thus, deploying real-time facial recognition models was not possible on a Mac.
Hence, I decided to make it easy as possible for anyone to deploy an OpenCV model with the help of Docker within a matter of seconds. I overcame point 1 by using a nightly build and point 2 with the use of Nginx. This is strictly designed to acquaint the reader on the deployment aspect of machine learning with Docker and is NOT concerned with building a machine learning model for facial recognition.
Project Structure: Clone the project from the Github repo here.
. ├── Version1            │ ├── Dockerfile                    #Building the image │ ├── docker-compose.yml            #Defining the files for mounting    │ ├── CV                            #OpenCV.js soure code|    ├──js_face_recognition.html|    ├──opencv.js|    ├──utils.js
Dockerfile
Dockerfile
2. docker-compose.yml
Please update the path to your project folder in this file. Note Nginx server is set to run on port 80.
The CV folder resides the source code for the project inclusive of the nightly build of OpenCV.js. I have used the facial recognition code from the tutorials of OpenCV.js official site, it is available from the official GitHub repo of OpenCV as well. The tutorials they provide is really helpful and is where I learned most of the OpenCV.js applications.
#Clone the repo git clone git@github.com:gomesc166/MediumRepo.git#Go to ObjectDetection directory cd Version1
#Build the imagedocker build -t object-detection-image .
#Start the Containerdocker-compose up
NOTE: Edit the path to the project on your host.
Visit localhost:80 on your browser
Click the download model button. Once the model is downloaded you can add as many people as you like for the recognition. If you want to add your custom Keras/Tensorflow models, you will have to tweak the JS scripts.
If you have any concerns regarding the code please leave a response and I will get back to you. I have used the JS script from the tutorials at the official OpenCV.js. Keep in mind the key objective of this post is to acquaint the user on how to deploy ready-made models easily with Docker and overcome the Docker hyper kit limitations when using on Mac. Cheers..