You may have noticed that the flow of the code looks similar to the face detection code that we discussed in the Overlaying a face mask in a live video section. We load a face detection cascade classifier as well as the eye detection cascade classifier. Now, why do we need to load the face cascade classifier when we are detecting eyes? Well, we don't really need to use the face detector, but it helps us in limiting our search for the eyes' location. We know that the eyes are always located on somebody's face, so we can limit eye detection to the face region. The first step would be to detect the face and then run our eye detector code on this region. Since we would be operating on a smaller region, it would be faster and way more efficient.
For each frame, we start by detecting the face. We then go ahead and detect the location of the eyes by operating on this region. After this step, we need to overlay the sunglasses. To do that, we need to resize the sunglasses image to make sure it fits our face. To get the proper scale, we can consider the distance between the two eyes that are being detected. We overlay the sunglasses only when we detect both eyes. That's why we run the eye detector first, collect all the centers, and then overlay the sunglasses. Once we have this, we just need to overlay the sunglasses mask. The principle used for masking is very similar to the principle we used to overlay the face mask. You may have to customize the sizing and position of the sunglasses, depending on what you want. You can play around with different types of sunglasses to see what they look like.