Face Recognition Code In Python Using OpenCV With Source Code
The Face Recognition Code In Python Using OpenCV was developed using Python Programming, this Face Detection In Python using Haar cascades is a machine learning based approach where a cascade function is trained with a set of input data. OpenCV already contains many pre-trained classifiers for face, eyes, smiles, etc.. Today we will be using the face classifier. You can experiment with other classifiers as well.
A Face Detection Algorithm Python can detect faces of people in an images, faces contains a list of coordinates for the rectangular regions where faces were found. We use these coordinates to draw the rectangles in our image. And also the Face Detection Using Python Project can detect also real time faces in a camera on your laptop or pc.
Similarly, the Face Detection In Python Using Opencv can detect faces in videos. As you know videos are basically made up of frames, which are still images. So we perform the face detection for each frame in a video.
The only difference here is that we use an infinite loop to loop through each frame in the video. We use cap.read() to read each frame. The first value returned is a flag that indicates if the frame was read correctly or not. We don’t need it. The second value returned is the still frame on which we will be performing the detection.
In this Code For Face Detection In Python also includes a Face Recognition Project In Python With Source Code for free, just find the downloadable source code below and click to start downloading.
By the way if you are new to python programming and you don’t know what would be the the Python IDE to use, I have here a list of Best Python IDE for Windows, Linux, Mac OS that will suit for you. I also have here How to Download and Install Latest Version of Python on Windows.
To start creating a Face Recognition Code In Python Using Opencv, make sure that you have installed Python in your computer.
Face Recognition Code In Python Using OpenCV : Prerequisites
Before starting with this Python project with source code, you should be familiar with the computer vision library of Python that is OpenCV and Numpy.
OpenCV and Numpy are the Python packages that are necessary for this project in Python. To install them, simply run this pip command in your terminal:
- pip install opencv-python numpy
Face Recognition Code In Python Using OpenCV With Source Code : Steps on How To Install Face Recognition In Python
These are the steps on how to install face recognition in python
- Step 1: Download and unzip the zip file.
First, download the source code given below and unzip the zip file.
The project folder contains 4 files:
1.) detect_face_image.py – a python file for detecting face in an image.
2.) detect_face_video.py – a python file for detecting face in a camera.
3.) haarcascade_frontalface_default_default.xml – a file that load cascade from image.
4.) sample.jpg – sample image for experimenting in detecting face. - Step 2: Creating code for detecting faces in an image.
Next, we want to create code for load cascade, read the input image and convert into grayscale, detect faces, draw rectangle around faces and display the output.
*Note: The code given below are already in a download source code. - Step 3: Creating Code for detecting faces in a video.
Next, in the same process in step 2, we want to create a code for detecting faces in a video.
*Note: The code given below are already in a download source code. - Step 4: Run detect_face_image python file.
The beginner Python project is now complete, you can run the Python file from the command prompt. Make sure to give an image path using ‘-i’ argument. If the image is in another directory, then you need to give full path of the image:
- Step 5: Run detect_face_video python file.
The beginner Python project is now complete, you can run the Python file from the command prompt.
Face Recognition Code In Python Using OpenCV : Code For Detecting Faces In An Image
import cv2 # Load the cascade face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') # Read the input image img = cv2.imread('sample.jpg') # Convert into grayscale gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Detect faces faces = face_cascade.detectMultiScale(gray, 1.1, 4) # Draw rectangle around the faces for (x, y, w, h) in faces: cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2) # Display the output cv2.imshow('img', img) cv2.waitKey()
Face Recognition Code In Python Using OpenCV: Image Output

Face Recognition Code In Python Using OpenCV : Code For Detecting Faces In A Video
import cv2 # Load the cascade face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') # To capture video from webcam. cap = cv2.VideoCapture(0) # To use a video file as input # cap = cv2.VideoCapture('filename.mp4') while True: # Read the frame _, img = cap.read() # Convert to grayscale gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Detect the faces faces = face_cascade.detectMultiScale(gray, 1.1, 4) # Draw the rectangle around each face for (x, y, w, h) in faces: cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2) # Display cv2.imshow('img', img) # Stop if escape key is pressed k = cv2.waitKey(30) & 0xff if k==27: break # Release the VideoCapture object cap.release()
Face Recognition Code In Python Using OpenCV: Camera Output

Download Source Code below
Summary
Facial detection is a powerful and common use-case of Machine Learning. It can be used to automatize manual tasks such as school attendance and law enforcement. In the other hand, it can be used for biometric authorization.
OpenCV uses machine learning algorithms to search for faces within a picture. Because faces are so complicated, there isn’t one simple test that will tell you if it found a face or not. Instead, there are thousands of small patterns and features that must be matched. The algorithms break the task of identifying the face into thousands of smaller, bite-sized tasks, each of which is easy to solve.
Other Related Articles
Related Articles
- Code For Game in Python: Python Game Projects With Source Code
- Best Python Projects With Source Code 2020 FREE DOWNLOAD
- How to Make a Point of Sale In Python With Source Code 2021
- Python Code For Food Ordering System | FREE DOWNLOAD | 2020
- Inventory Management System Project in Python With Source Code
Inquiries
If you have any questions or suggestions about Face Recognition Code In Python Using OpenCV With Source Code, please feel free to leave a comment below.