This project demonstrates a real-time face detection system using Python, OpenCV, and Matplotlib. The program captures video from your webcam, detects faces in real-time using Haar cascades, and displays the video feed with detected faces highlighted in a matplotlib plot.
- Real-Time Face Detection: Detects faces in live video captured from your webcam.
- Haar Cascade Classifier: Uses OpenCV's pre-trained Haar cascade classifier for face detection.
- Interactive Plot: Displays the video feed with detected faces in a matplotlib plot. You can exit the program by pressing any key in the plot window.
- Python 3.x
- OpenCV (
cv2) - Matplotlib
You can install the required dependencies using:
pip install opencv-python matplotlib- Clone this repository or download the script file.
- Open a terminal or command prompt and navigate to the directory containing the script.
- Run the script using Python:
python face_detection.py- The program will activate your webcam and display a live video feed with detected faces highlighted by green rectangles.
- Press any key in the matplotlib plot window to terminate the program.
- Face Detection:
- The program uses OpenCV's
CascadeClassifierwith thehaarcascade_frontalface_default.xmlmodel to detect faces in each frame of the video.
- Webcam Video Capture:
- The
cv2.VideoCapture(0)function captures live video from your default webcam.
- Matplotlib Integration:
- The live video feed is displayed using Matplotlib's
imshowfunction, allowing for an interactive display.
- Exit Mechanism:
- The program terminates when you press any key inside the matplotlib plot window.
- Webcam Not Detected:
- Ensure that your webcam is connected and accessible.
- Check if another application is using your webcam.
- No Faces Detected:
- Ensure proper lighting conditions for better face detection.
- Adjust the parameters of
detectMultiScale(e.g.,scaleFactororminNeighbors) if necessary.
- Matplotlib Window Freezes:
- Ensure that you're running the script in an environment that supports interactive plotting (e.g., local Python installation, not Jupyter Notebook).
This project is licensed under the MIT License. Feel free to use, modify, and distribute it as needed.
- OpenCV for providing powerful computer vision tools.
- Matplotlib for enabling interactive visualization.