This script performs real-time face recognition using OpenCV and DeepFace. It captures video from your webcam, compares the detected face to a reference image, and displays appropriate messages based on whether the face is found and if it matches the reference.
- DeepFace for facial recognition
- OpenCV for video capture and image processing
-
Install Anaconda (if not already installed)
- Download and install Anaconda for Python 3.10.
-
Create a Conda Environment Using
environment.yml- Clone or download this project to your local machine.
- In the project directory, there is an
environment.ymlfile, which contains all the necessary dependencies for the script. - Create the Conda environment by running:
conda env create -f environment.yml
-
Activate the Environment
- Once the environment is created, activate it:
conda activate FaceRec
- Once the environment is created, activate it:
-
Install Additional Dependencies (if needed)
- In case
DeepFaceorOpenCVis missing, you can manually install the necessary dependencies with:pip install deepface opencv-python
- In case
-
Prepare Reference Image
- Place the reference image (the one you want to compare against) in your project directory and update the
reference_imgpath in the code:reference_img = cv2.imread("path_to_your_image.jpg")
- Place the reference image (the one you want to compare against) in your project directory and update the
-
Run the Script
-
Run the script:
python face_recognition.py
-
The webcam will start capturing, and the script will analyze each frame to see if the face matches the reference.
-
-
Video Capture:
- The script captures frames from your webcam using OpenCV (
cv2.VideoCapture(0)).
- The script captures frames from your webcam using OpenCV (
-
Face Detection and Matching:
- Every 30 frames, the
check_facefunction is called in a separate thread. This function:- Uses DeepFace's
verifyfunction to compare the captured face with the reference image. - Updates the flags (
face_match,face_found) depending on the result.
- Uses DeepFace's
- Every 30 frames, the
-
Display Messages:
- The script overlays messages on the video feed based on the face matching result:
"Hi, {Name}"if a matching face is detected."Face found no match!"if a face is found but doesn't match."Face not found!"if no face is detected.
- The script overlays messages on the video feed based on the face matching result:
-
Exit:
- Press
qto exit the video feed.
- Press
- Ensure that your webcam is properly set up and recognized by your system.
- The script uses threading to handle the face comparison without blocking the main video loop.
- You may need to adjust the reference image path based on your system and file structure.