Project Short Details
Title: Dual-Camera Human Detection and Face Recognition System
Description: This project aims to develop a real-time human detection and face recognition system using dual cameras—a laptop's built-in camera and an external USB camera. The system captures video from both cameras simultaneously, detects faces in the frames, and saves images of detected faces along with video recordings. The saved files are organized into separate folders based on the camera used, allowing for easy access and management. The system also employs face recognition to ensure that images of the same person are captured only if a specific time interval has elapsed, thus optimizing storage and processing.
Project Targets
Simultaneous Video Capture:
- Implement a system to capture video streams from both the laptop's built-in camera and the USB camera concurrently.
Real-Time Face Detection:
- Utilize Haar cascade classifiers for detecting faces in the video frames from both cameras.
Face Recognition:
- Employ the
face_recognition
library to recognize faces, comparing them against previously detected faces to identify new individuals.
- Employ the
Efficient Image Saving:
- Save images of detected faces only if a minimum time interval (2 minutes) has passed since the last image was captured for the same person, thereby optimizing storage.
Organized File Management:
- Create a structured file storage system, with separate directories for videos and photos from each camera, named with timestamps for easy identification.
User Interface:
- Provide a simple display for the video feeds from both cameras, allowing users to monitor the detection process in real time.
Clean Resource Management:
- Ensure proper release of camera resources and cleanup of all processes after the application is terminated.
Target Outcomes
- Successfully build a dual-camera face detection and recognition system that operates in real time.
- Demonstrate the ability to manage and process multiple video streams efficiently.
- Showcase the capability to save and organize multimedia files effectively while minimizing redundancy in captured images.
Here’s a detailed explanation of the final code, which involves capturing video from both the laptop's primary camera and a USB camera simultaneously, performing face detection and recognition, and saving both video and face-detected images in separate folders for each camera.
1. Library Imports
- cv2 (OpenCV): Used for capturing video, detecting faces, and saving video files and images.
- datetime: Helps generate timestamps to create unique filenames for the videos and images.
- os: Used to create directories and manage file paths dynamically.
- time: Provides functions to work with time intervals (for controlling the frequency of image capture).
- face_recognition: Allows for face encoding and comparing faces for recognition.
2. Loading Face Detection Model
- The Haar cascade model is pre-trained to detect faces. It is used here to quickly identify faces in frames from both cameras.
3. Initialize Video Capture
cv2.VideoCapture(0)
: Initializes the primary camera (typically, the laptop's built-in camera).cv2.VideoCapture(1)
: Initializes the USB camera (external webcam).
Both cameras will capture video frames at the same time.
4. Creating Directories for Videos and Photos
The code creates separate directories for:
- Videos and photos from the primary camera.
- Videos and photos from the USB camera.
Each directory is timestamped using the current date (
YYYYMMDD
), ensuring that all recordings and images are saved in a structured manner.
5. Setting Up Video Recording
VideoWriter Objects are created for each camera:
- out_primary: Writes the video captured from the primary camera to a
.avi
file. - out_usb: Writes the video captured from the USB camera to a different
.avi
file.
- out_primary: Writes the video captured from the primary camera to a
Each video file is named with a timestamp (
primary_video_YYYYMMDD_HHMMSS.avi
andusb_video_YYYYMMDD_HHMMSS.avi
) and saved in their respective directories.
6. Face Detection and Recognition
- detected_faces_primary and detected_faces_usb: Dictionaries that store the faces detected by each camera.
- capture_interval: Ensures that images of the same face are saved only if 2 minutes have passed since the last capture.
7. Processing Frames for Face Detection and Saving Images
- This function processes each frame from either the primary or USB camera:
- It converts the frame to grayscale for face detection.
- If a face is detected, the face encoding is computed using the
face_recognition
library. - The face encoding is compared with previously detected faces:
- If the face has been seen before, it checks if 2 minutes have passed since the last capture. If so, a new image is saved.
- If it’s a new face, the image is saved immediately, and the encoding is stored in the
detected_faces
dictionary for future comparisons.
- Face images are saved in the corresponding photo folder (
photos_primary_camera
orphotos_usb_camera
).
8. Main Loop to Capture and Process Video
- In the main loop, frames are captured simultaneously from both the primary and USB cameras.
- Each frame is written to its respective video file (
out_primary
for the primary camera andout_usb
for the USB camera). - Face detection and processing are performed on each frame using the
process_frame
function. - The
imshow
function displays the video streams in separate windows for each camera. - The loop runs continuously until the user presses the 'q' key to quit.
9. Cleanup
- Once the loop ends (on pressing 'q'), the cameras and video writers are released, and all windows are closed to free up resources.
Key Features of the Code:
- Dual Camera Support: Records from both the laptop's built-in camera and a USB camera at the same time.
- Face Detection and Recognition: Detects faces and saves images only if a new face is detected or if a sufficient time interval has passed since the last capture.
- Organized Storage: Videos and images are saved in separate directories for each camera, with timestamped filenames.
Requirements to Install on Windows 10 for Face Detection and Recognition
To run the provided code for dual-camera human detection and face recognition on Windows 10, you'll need the following:
Python Installation:
- Install Python (preferably 3.6 or higher). You can download it from the official Python website.
Install Required Libraries:
- Open a command prompt and install the required Python libraries using pip. Run the following commands:
- Make sure to have
pip
installed. If it's not available, you can use the Python installer to add it.
- Open a command prompt and install the required Python libraries using pip. Run the following commands:
Additional Dependencies:
- Haar Cascade Classifier: This is included with OpenCV, so you don't need to install it separately. However, ensure that OpenCV is correctly set up so that it can access the haarcascade files.
- CMake and dlib: The
face_recognition
library requires dlib. You may need to install CMake and Visual Studio Build Tools to compile it, which can be installed from the CMake website and Visual Studio.
Camera Setup:
- Ensure that both the primary laptop camera and the USB camera are connected and properly recognized by Windows. You can check this in the Device Manager.
IDE or Code Editor:
- Install an Integrated Development Environment (IDE) like Visual Studio Code or PyCharm to write and run your Python code easily.
System Permissions:
- Make sure that your system permissions allow access to the camera for the Python application.
Summary of Steps to Run the Code
Install Python and Required Libraries:
- Download and install Python.
- Use
pip
to installopencv-python
,opencv-python-headless
, andface_recognition
.
Set Up the Cameras:
- Connect the USB camera and ensure that the laptop's built-in camera is operational.
Write and Save the Code:
- Copy the provided code into a new Python file (e.g.,
dual_camera_face_recognition.py
).
- Copy the provided code into a new Python file (e.g.,
Run the Code:
- Open a command prompt in the directory where your Python file is saved and run the command:
- Open a command prompt in the directory where your Python file is saved and run the command:
Monitor the Output:
- The video feed from both cameras will display, and images of detected faces will be saved in the corresponding folders based on the camera used.
Stop the Program:
- Press 'q' to terminate the program and release the camera resources.
Comments
Post a Comment