Real Time Face Identification, Creating DataSet and Train Images
Table of Contents
Introduction
This tutorial will guide you through the process of implementing real-time human face recognition using Python and OpenCV. We will create a dataset of images, train a model using the Haar Cascade Classifier, and ultimately achieve face identification in real-time. This beginner-friendly project will enhance your understanding of computer vision and machine learning concepts.
Step 1: Set Up Your Environment
Before diving into code, ensure you have the necessary software and libraries installed.
- Install Python (version 3.6 or higher).
- Install OpenCV and other required libraries. You can do this using pip:
pip install opencv-python opencv-python-headless
- Clone the GitHub repository containing the project files:
git clone https://github.com/yashwantpathakrjit/Real-Time-Face-Identification-Creating-DataSet-and-Train-Images.git
Step 2: Create a Dataset
To train your face recognition model, you need a dataset of images.
- Import the necessary libraries:
import cv2 import os
- Create a directory to store the images:
os.makedirs('dataset', exist_ok=True)
- Use the following code snippet to capture images:
def capture_images(name): cam = cv2.VideoCapture(0) detector = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml') count = 0 while True: ret, frame = cam.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = detector.detectMultiScale(gray, 1.3, 5) for (x, y, w, h) in faces: cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) count += 1 cv2.imwrite(f"dataset/{name}.{count}.jpg", gray[y:y + h, x:x + w]) cv2.imshow('Capturing Images', frame) if cv2.waitKey(1) & 0xFF == ord('q') or count >= 100: break cam.release() cv2.destroyAllWindows()
- Call the function to start capturing images for a specific individual:
capture_images("person_name")
Step 3: Train the Face Recognition Model
Once the dataset is ready, you can train the model.
- Load the images and labels:
def load_images_and_labels(): images = [] labels = [] for root, dirs, files in os.walk('dataset'): for file in files: img = cv2.imread(os.path.join(root, file), cv2.IMREAD_GRAYSCALE) images.append(img) labels.append(int(file.split('.')[0])) # Assuming labels are integers return images, labels
- Use the
EigenFaceRecognizer
orFisherFaceRecognizer
to train:recognizer = cv2.face.LBPHFaceRecognizer_create() images, labels = load_images_and_labels() recognizer.train(images, np.array(labels)) recognizer.save('trainer.yml')
Step 4: Implement Real-Time Face Recognition
Finally, set up the code for real-time face recognition.
- Load the trained model and Haar Cascade:
recognizer.read('trainer.yml') detector = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
- Capture video from the webcam and recognize faces:
video_capture = cv2.VideoCapture(0) while True: ret, frame = video_capture.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = detector.detectMultiScale(gray, 1.3, 5) for (x, y, w, h) in faces: id_, conf = recognizer.predict(gray[y:y + h, x:x + w]) if conf >= 45: # Confidence threshold cv2.putText(frame, str(id_), (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2) cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) cv2.imshow('Real-Time Face Recognition', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break video_capture.release() cv2.destroyAllWindows()
Conclusion
In this tutorial, you learned how to create a real-time face identification system using Python and OpenCV. You captured images to create a dataset, trained a face recognition model using the Haar Cascade Classifier, and implemented real-time detection.
Next steps could include exploring other algorithms for face recognition, enhancing the dataset with more images, or integrating this system into a larger application. Happy coding!