keronarticle.blogg.se

Perfect face filter snapchat
Perfect face filter snapchat






perfect face filter snapchat

If an eye is located outside the face, then either something is wrong, such as a false positive, or something is very wrong. The face is detected before the eye, because any eye detected should be located within the face. The frame is converted into grayscale and then passed into the Haar Cascades to detect any face. # Convert to grayscale gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Detect faces faces = face_tectMultiScale(gray_frame, 1.3, 5) The variable “ret” is simply a boolean that denotes if video was actually captured from the camera and “frame” is the current frame from the camera. Next, a infinite loop is initialized and the data from the camera is read. If multiple are used, then passing any integer will use the nth camera.Īdditionally, if a pre-recorded video needs to be used instead, then passing a string to the video’s location will also work. Note that passing 0 will use the 0th camera on the computer. # Capture video from the local camera cap = cv2.VideoCapture(0) while True: # Read the frame ret, frame = cap.read() # Check to make sure camera loaded the image correctly if not ret: break For somebody sitting calmly or even swaying slowly, the effect isn’t very noticeable, but a quickly moving face will see the impact. By continually referring to the location of the eyes in previous frames, the position of the glasses can lag. While this makes the program run smoother, it comes at a cost. Like before, using the cache, this can be corrected by referring to a previous location of the eyes. Sometimes the Haar Cascades will falsely identify more than two eyes, which will mess up glasses placement.This will remove the flickering and make the program run more smoothly. By using a cache, however, the program can refer to the location of the eyes from the last successful identification and place the glasses accordingly. The Haar Cascades classifier will not be able to identify the eyes in every single frame, which will create a flickering effect.# Global variable to cache the location of eyes eye_cache = NoneĪ global variable is declared to create a cache. Please note, however, that these pre-trained models are licensed to Intel and may have restrictions for use.

perfect face filter snapchat

These pre-trained models come built-in with OpenCV, but can be downloaded separately here.

perfect face filter snapchat

The XML files passed as their arguments are actually pre-trained models that specialize in detecting a frontal view of a face and eyes specifically. Nothing is classified, yet, but this serves to initialize the models that will be used. Next, OpenCV’s built-in cascade classifier method is called. Detecting the Eyes import numpy as np import cv2 # Import the pre-trained models for face and eye detection face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml") eye_cascade = cv2.CascadeClassifier("haarcascade_eye.xml")īefore doing much, NumPy is imported for efficient numerical calculation and OpenCV is imported for image processing.

Perfect face filter snapchat code#

Obviously, both Haar Cascades and Homorgraphy are deep concepts whose details go beyond the scope of this article, but a basic understanding of what they are and what they do will help understand the code implementation. While the concept itself sound scary, we’ll use it to project the sunglasses onto the video frame so that it appears natural. Homography, which concerns itself with isomorphism in projected spaces, offers a solution. The second challenge is to transform a projected image to ensure that its proportional to the face. Instead of training from scratch, however, OpenCV provides a number of pre-built models, including face and eye detection. The topic of facial detection and facial landmark detection are both huge topics within computer vision with many unique approaches, but this method will use Haar Cascades.įirst introduced in a paper in 2001 by Paul Viola and Michael Jones, Haar Cascades are a generalized supervised machine learning technique designed specifically for quick facial detection. The first challenge to overcome is to detect a pair of eyes. While both of these challenges sound daunting, Python’s implementation to OpenCV make it fairly easy. In this case, the image is a pair of sunglasses which must fit on a pair of eyes when projected. Second, it must transform the projected image so that it’s proportional to the rest of the video frame. In this example, whatever algorithm I use must correctly identify the location of my eyes. First, it must determine where in the video frame to project an image. This filter, as with any AR, relies on two fundamental steps. I drew these pair of sunglasses for this project.








Perfect face filter snapchat