Challenge - Create Snapchat filter doubt

I have detected the position of eyes and nose using haarcascade classifier but I am not getting how to add glasses and mustache to an input image using the positions of eyes and nose, can you please tell me how to do that?

hey @Mohit2000 ,
after getting a rectangular box on eyes and nose,
you will get the coordinates from where to start on.

Now , you need to iterate over all the pixels in that rectangle and change each pixel in original image with the respective pixel in the glasses or moustache image provided.
remember that , moustache and glasses are in RGBA format , so while you are replacing those pixel values , you need to take only the first 3 channels and not the 4 one.

I hope this helps.

But the bounding box which I am getting for the eyes and nose is large. One more thing, how mustache can be there on the nose bounding box?

large means in what way?

Take the bottom coordinates and just add offset to them manually.
in this way you will reach a somewhat point where mustache is

eyes box is correct ,
and for the nose use the bottom left and bottom right .
They will do your task.

When I write glasses.shape it is showing (221, 483, 3) so where is the last channel? (A)

oh sorry ,
maybe i would have misunderstand it.


The glasses image is displayed as shown. What is the reason for this and how to resolve this?

try converting them to RGBA once.

We have to convert glasses from BGR to RGBA or BGRA?

convert them to RGBA

I didn’t get why to convert them to RGBA, we are reading glasses image from opencv right? and they reads the image in BGR format. So when we will display RGBA image, will the color not be inversed?

yeah they will be .
But we do it for presentation of those images ,
if you try to imshow those images , once in BGR and once in RGB you will get the difference

I have tried converting the glasses image from BGR to RGBA, it is displaying in the same way as before.


When I am reading and showing glasses and mustache images, it is been displayed in this way. The white background is becoming black

hey @Mohit2000 ,
can you please provide me with that code snippet that you are using to read these images.

import cv2

input_image = cv2.imread(‘Train/Jamie_Before.jpg’)

moustache = cv2.imread(‘Train/mustache.png’)

glasses = cv2.imread(‘Train/glasses.png’)

cv2.imshow(‘image’,input_image)

cv2.imshow(‘mustache’,moustache)

cv2.imshow(‘glasses’,glasses)

cv2.waitKey(0)

cv2.destroyAllWindows()

hey @Mohit2000,
try reading it like

moustache = cv2.imread(‘Train/mustache.png’,-1)

glasses = cv2.imread(‘Train/glasses.png’,-1)

No change, It’s same as before