Original Source Here
Don’t hesitate to say “Hello, Machine Learning”
Almost all developers and computer science enthusiasts wish to master the art of machine learning at some points. But most of them find it very difficult to start with. If you are one of them, you are in the right place. It’s pretty normal for beginners to route themselves into the right trajectory. Don’t worry about anything. I am here to help you out.
Trust me. You will be able to neglect the fear of machine learning at the end of this article. I am not going to waste your time with a lot of theories. Instead, I will directly jump into the action. Theoretical knowledge is important. But you don’t need to worry about them now. You’ll eventually understand everything.
“Machine learning is nothing but let the Machine learn something”
Yes, you read that correctly. It is that simple! So, we have to teach the machine first. Wait a minute! How do we teach the machine? Well, that’s where the data get into the picture. What computer knows is just ones and zeros. So let’s give ones and zeros and build our face detection application.
We will be using a few tools and libraries with python, such as OpenCV, Numpy, etc. Don’t get panic if u didn’t hear these words before. OpenCV is a library used to build powerful applications. They will give you the trained models (trained by a massive amount of data). So, If we need a face detection model, we can get the model from them and use it in our code.
It’s widely used and supports different languages. I prefer to go with python that way you can get a lot of community support as well. Numpy is another library that supports the model to run. We won’t be using it in our code, but in the background, it will help to run the program. So, no need to worry about that now. As a first step make sure you installed the python and you have pip package manager. then use the comment given below to install the OpenCV in your local machine.
As I mentioned earlier, OpenCV has a lot of trained models where we could use them for different purposes. Here, we need to get the specific trained model for face detection. one way of doing it is by having the model as an XML in the project. Click here to download the particular file. After that, you can add it to the project. Create a python file in your project folder and start writing the code.
First, we need to import the OpenCV in our program just like other libraries. Then we are creating an instance of our trained model using the CascadeClassifier method (line #5). When we have an array of the camera, it will be indexed from 0. So, we are just taking the default one in a variable which is 0 (line #9). Then we are running the loop to keep the camera open in the GUI until the user press “1”. Inside the loop, we are just iterating through the frames that are being captured on the webcam. So we can identify the face in each frame using our model by separating the coordinates (line #15). Your coordinates will be something like the following one.
Here you are getting a two-dimensional array. So, each property of the main array is a face detected in the particular frame and the values inside that property array are going to be the identified coordinates. For example,  which is 244, and  which is 364 are the bottom left corner of the detected face,  which is 55 is the height of the first face and  which is 55 is the width of the first face.
Alright, we identified the face. Now what? Well, let’s draw the rectangle around the face. You have to draw rectangles for all the faces we identified in the frame. To achieve this, we have to draw a rectangle based on each property of the main array. That’s why I have another loop in the code (line #18). To draw the rectangle in the frame, we can make use of the method in the OpenCV (line #19). So, this rectangle method takes few parameters. The frame where we are going to draw, bottom left coordinates, top-right coordinates, and the RGB mix to get the outline color. So, we are using our “frame” variable right away. The bottom right coordinates are the first two elements of the array, the left top coordinates just adding the width and height to the first two properties of the array and in the RGB for each color. It takes the value out of 255 (we are generating the random number for all three colors to make the rectangle bit colorful). After that, you need to display your entire frame while it keeps capturing. For that, I just used the imgshow method in the main loop, which takes two parameters. Those are the name and the frame.
That’s it. Congratulations..! you have successfully developed your first face detection application. Just go ahead and type python <your python file name>.py in your console. You can see the magic.
I believe this would be helpful to get rid of the fear of getting started. Now you are ready to explore more things in this particular domain.
Trending AI/ML Article Identified & Digested via Granola by Ramsey Elbasheer; a Machine-Driven RSS Bot