Codementor Events

How to do Facial Recognition And Overlay with Image Asset (Python2 & OpenCV: png with Alpha Channel Transparency)

Published Oct 03, 2019Last updated Nov 04, 2019
How to do Facial Recognition And Overlay with Image Asset (Python2 & OpenCV: png with Alpha Channel Transparency)

Facial recognition has never been easier!

By using open source XML models, we can track faces, eyes, and even superimpose image assets over the region of interest! (ROI)

This code does use the PNG alpha channel in order to obtain transperency on the PNG files!

glasses.png

Code (GitHub)

I have published all the code for this project in my GitHub!

Checkout, Fork, & Star the Repo!!!

Follow me on GitHub for more cool projects πŸ˜ƒ

#Blade Nelson
#Written in August 2019
import cv2
import numpy as np

eyeData = "xml/eyes.xml"
faceData = "xml/face.xml"
DOWNSCALE = 3

#Bools for control
add_face_rect = False
add_objects = False
add_eye_rect = False

#OpenCV boiler plate
webcam = cv2.VideoCapture(0)
cv2.namedWindow("Webcam Facial Tracking")
classifier = cv2.CascadeClassifier(eyeData)
faceClass = cv2.CascadeClassifier(faceData)

#Loading glasses asset
glasses = cv2.imread('assets/glasses.png', cv2.IMREAD_UNCHANGED)

ratio = glasses.shape[1] / glasses.shape[0]

if webcam.isOpened(): # try to get the first frame
    rval, frame = webcam.read()
else:
    rval = False

#Main loop
while rval:
    # detect eyes and draw glasses
    minisize = (frame.shape[1]/DOWNSCALE,frame.shape[0]/DOWNSCALE)
    miniframe = cv2.resize(frame, minisize)
    faces = faceClass.detectMultiScale(miniframe)
    eyes = classifier.detectMultiScale(miniframe)
    
    if add_eye_rect:
        for eye in eyes:
            x, y, w, h = [v * DOWNSCALE for v in eye]

            pts1 = (x, y+h)
            pts2 = (x + w, y)
            # pts1 and pts2 are the upper left and bottom right coordinates of the rectangle
            cv2.rectangle(frame, pts1, pts2, color=(0, 255, 0), thickness=3)

            if add_objects:
                h = w / ratio
                y += h / 2
                # resize glasses to a new var called small glasses
                smallglasses = cv2.resize(glasses, (w, h))
                # the area you want to change
                bg = frame[y:y+h, x:x+w]
                np.multiply(bg, np.atleast_3d(255 - smallglasses[:, :, 3])/255.0, out=bg, casting="unsafe")
                np.add(bg, smallglasses[:, :, 0:3] * np.atleast_3d(smallglasses[:, :, 3]), out=bg)
                # put the changed image back into the scene
                frame[y:y+h, x:x+w] = bg

    if add_face_rect:
        for face in faces:
            x, y, w, h = [v * DOWNSCALE for v in face]

            pts1 = (x, y+h)
            pts2 = (x + w, y)
            # pts1 and pts2 are the upper left and bottom right coordinates of the rectangle
            cv2.rectangle(frame, pts1, pts2, color=(255, 0, 0), thickness=3)

    cv2.imshow("Webcam Glasses Tracking", frame)

    # get next frame
    rval, frame = webcam.read()

    key = cv2.waitKey(20)
    if key in [27, ord('Q'), ord('q')]: # exit on ESC
        cv2.destroyWindow("Webcam Face Tracking")
        break

    #Keyboard input
    if key == ord('1'):
        if add_face_rect:
            add_face_rect = False
        else:
            add_face_rect = True

    if key == ord('2'):
        if add_eye_rect:
            add_eye_rect = False
        else:
            add_eye_rect = True

    if key == ord('3'):
        if add_objects:
            add_objects = False
        else:
            add_objects = True

Feel free to message me if you have any questions!
This is a Python2 file. It has not worked in the past when I tried it with Python3. The models and file assets are all on Git as well as the internet!

Tags:
Python2, OpenCV, Python3, Facial Recognition, overlay, superimpose, png, alpha, alpha channel, channel,.png, computer vision, vision, cv, gpu, xml, model, ml, machine learning, ai, snapchat, filter, webcam, realtime, Python OpenCV, Python2 OpenCV2

Discover and read more posts from Blade Nelson
get started