This is a personal project, a Face Detection algorithm in Python using OpenCV
- OpenCV is a library of programming functions mainly for real-time computer vision. Originally developed by Intel, it was later supported by Willow Garage, then Itseez. The library is cross-platform and licensed as free and open-source software under Apache License. [Wikipedia]
- Face recognition is a method of verifying the identity of a person using their face.
- Deep learning is super popular for face recognition applications.
Training a complex network required here will take a significant amount of data and computation power.
A pre-trained network trained by Davis King on a dataset of ~3 million images is used to speed up the process.
The network outputs a vector of 128 numbers which represent the most important features of a face.
- Step 1: Face Detection
- The exact location/coordinatesof face is extracted from media.
- Step 2: Feature Extraction
- Face embedding is used with each face to convert it into a vectorand this technique is called Deep Metric Learning.
- Step 3: Training a neural network
- A neural network may output faces which look very similar to each other.
- Step 4: Feature map across the face
- After training the network, it understands to group similar looking facestogether into one category.
- Step 5: Embeddings for images are obtained after training
This an article by Adam Geitgey about how Modern Face Recognition works, in both theoritical and practical ways. It's a 13-15 minutes read. You can read this article before starting this project yourself, it's very informative and helpful!
This project demonstrates real-time face recognition using the OpenCV library along with the simple_facerec
library. The program captures video frames from your webcam and detects known faces in the frames by comparing them with previously encoded faces.
Follow the steps below to set up and run the project on your local machine.
- Python 3.x
- OpenCV (install with
pip install opencv-python
) simple_facerec
(keep the file in the base directory)
-
Clone the repository:
git clone https://github.com/arindal1/Face-Detection-using-OpenCV.git cd face-recognition-project
-
Install the required Python packages:
pip install -r requirements.txt
- Place face images for encoding in the
InData/
directory. - Run the script:
python main.py
- The script will open a window showing the webcam feed with recognized faces highlighted.
Press the "Esc" key to exit the application.
- The program initializes the
simple_facerec
library and loads face encodings from theInData/
directory. - It captures video frames from the webcam using OpenCV.
- Detected faces in each frame are compared with the loaded encodings to recognize known faces.
- Recognized faces are highlighted with bounding boxes and labels on the webcam feed.
The base.py
is the base file. A Python script using the OpenCV and face_recognition libraries to perform face recognition on two images. The script first loads and encodes faces from the provided images, and then compares the facial encodings to determine if the faces in the images match. Finally, it displays the images using OpenCV's imshow
function.
Here's a breakdown of the script:
-
Import the necessary libraries:
import cv2 import face_recognition
-
Load the first image (
elon1.jpg
) and convert it to RGB color format:img = cv2.imread("InData/elon1.jpg") rgb_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
-
Encode the face(s) present in the first image:
img_encoding = face_recognition.face_encodings(rgb_img)[0]
-
Load the second image (
musk1.jpg
) and convert it to RGB color format:img2 = cv2.imread("TestData/musk1.jpg") rgb_img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB)
-
Encode the face(s) present in the second image:
img_encoding2 = face_recognition.face_encodings(rgb_img2)[0]
-
Compare the two face encodings to determine if they match:
result = face_recognition.compare_faces([img_encoding], img_encoding2)
-
Print the result of the face comparison:
print("Result: ", result)
-
Display the two images using OpenCV's
imshow
function:cv2.imshow("Img", img) cv2.imshow("Img 2", img2) cv2.waitKey(0)
If the result is [True]
, it means that the faces in the two images match; if it's [False]
, the faces don't match.
Make sure that you have the required image files (elon1.jpg
and musk1.jpg
) in the specified paths ("InData/" and "TestData/") before running the script. Also, ensure you have the OpenCV and face_recognition libraries installed in your environment.
The facedetector.py
is the file for real time face detection, a Python script that uses the simple_facerec
library along with OpenCV to perform real-time face recognition using your webcam feed. The script first loads face encodings from a folder, then utilizes the webcam to detect and recognize faces in the live video stream.
Here's a breakdown of the script:
-
Import the necessary libraries:
import cv2 from simple_facerec import SimpleFacerec
-
Initialize the
SimpleFacerec
object and load encoded images from the specified folder ("InData/"):sfr = SimpleFacerec() sfr.load_encoding_images("InData/")
-
Load the webcam capture device:
cap = cv2.VideoCapture(2)
-
Start an infinite loop to continuously process frames from the webcam:
while True: ret, frame = cap.read()
-
Detect known faces in the current frame using the loaded encodings:
face_locations, face_names = sfr.detect_known_faces(frame)
-
Iterate over the detected face locations and corresponding names:
for face_loc, name in zip(face_locations, face_names): y1, x2, y2, x1 = face_loc[0], face_loc[1], face_loc[2], face_loc[3] # Draw the name and bounding box around the detected face cv2.putText(frame, name, (x1, y1 - 10), cv2.FONT_HERSHEY_DUPLEX, 1, (0, 0, 200), 2) cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 0, 200), 4)
-
Display the processed frame in a window named "Frame":
cv2.imshow("Frame", frame)
-
Check for a key press and break the loop if the "Esc" key (key code 27) is pressed:
key = cv2.waitKey(1) if key == 27: break
-
Release the webcam capture device and close all OpenCV windows when the loop is exited:
cap.release() cv2.destroyAllWindows()
This script essentially captures video frames from your webcam, processes them to detect and recognize faces using the loaded encodings, and displays the frames with recognized names and bounding boxes. Pressing the "Esc" key will stop the script and close the OpenCV windows. Make sure you have the simple_facerec
library installed in your environment, and ensure your webcam is accessible and properly configured.
Calculating the accuracy of a face recognition model like the one you've implemented can be a bit more involved than a traditional machine learning classification model because it doesn't follow the typical training/testing split. However, you can still evaluate the performance of your face recognition system by considering some aspects:
-
Known Face Identification Accuracy: Since your model is detecting known faces from a set of loaded encodings, you could evaluate how well it correctly identifies the pre-registered faces in real-time. This could be done by comparing the recognized names with the actual names of the individuals in the frame and calculating the percentage of correct identifications.
-
False Positives and False Negatives: You can also keep track of false positives (recognizing a face that shouldn't be recognized) and false negatives (not recognizing a face that should be recognized). This will provide insight into the model's performance with respect to both over-recognition and under-recognition.
Remember that these calculations are rough estimates and may not provide a complete picture of the model's performance, especially considering factors like varying lighting conditions, angles, and other real-world challenges. Also, the effectiveness of the accuracy calculation largely depends on the quality of the encodings, the diversity of the training data, and the robustness of the recognition process.
Contributions to this project are welcome! If you have any suggestions, improvements, or bug fixes, feel free to open an issue or submit a pull request.
- This project uses the simple_facerec library for face recognition.
- Special thanks to the authors and contributors of the OpenCV and
simple_facerec
libraries for their valuable work.
If you have any questions or want to connect, feel free to reach out:
This script detects faces in an input image and draws rectangles around the detected faces. Here's a breakdown of the code:
-
Importing OpenCV: Import the OpenCV library using
import cv2
. -
Loading the Pre-trained Classifier: The
CascadeClassifier
class from OpenCV is used to load the pre-trained classifier for face detection. The classifier XML file should be in the same directory as your script. The lineface_cascade = cv2.CascadeClassifier('haarcascade_frontalface.xml')
loads the classifier. -
Reading the Input Image: The input image, named
test3.jpg
, is read using thecv2.imread
function. -
Face Detection: The
detectMultiScale
function is used to detect faces in the input image. It takes the input image, a scale factor, and a minimum number of neighbors as parameters. Thefaces
variable will store the coordinates of the detected faces. -
Drawing Rectangles: A loop iterates through the detected faces (stored as
(x, y, w, h)
), where(x, y)
is the top-left corner of the face and(w, h)
is its width and height. For each detected face, a rectangle is drawn around it using thecv2.rectangle
function. The rectangle color is set to blue(255, 0, 0)
and the thickness is set to 2. -
Exporting the Result: The modified image with rectangles drawn around the detected faces is saved as
face_detected.png
usingcv2.imwrite
. -
Print Confirmation: A message is printed to indicate that the photo with detected faces has been successfully exported.
Remember to replace the input image name (test3.jpg
) with the actual image you want to perform face detection on. Also, make sure you have the haarcascade_frontalface.xml
classifier file available in the same directory. This classifier is based on Haar cascades and is a pre-trained model for face detection provided by OpenCV.
Here test3.jpg is the input data.
The Haarcascade processed output:
haarcascade_frontalface
is a pre-trained Haar Cascade classifier provided by OpenCV, a popular computer vision library. Haar Cascade classifiers are machine learning-based object detection methods that are used to detect objects (in this case, faces) within images or video frames. These classifiers work by analyzing patterns of intensity gradients in the image.
The haarcascade_frontalface.xml
file is a configuration file that contains the information needed to perform frontal face detection. It has been trained on a large number of positive and negative images to recognize specific patterns associated with human faces, particularly when they are facing the camera directly (frontal faces).
When you use the CascadeClassifier
in OpenCV and load the haarcascade_frontalface.xml
classifier, you're essentially utilizing a model that has already learned how to detect frontal faces. The classifier works by sliding a window of different sizes across the input image and looking for patterns that match the characteristics of faces. If the classifier detects a pattern that resembles a face, it marks that region as a potential face detection.
Keep in mind that while Haar Cascade classifiers are fast and lightweight, they might not perform well under various conditions, such as when faces are tilted, occluded, or captured under different lighting conditions. For more advanced scenarios, deep learning-based object detection methods like Single Shot MultiBox Detector (SSD) or You Only Look Once (YOLO) are often used, as they can handle more complex cases effectively.