Last Updated : 11 Jul, 2025
Face detection is a important application of computer vision that involves identifying human faces in images or videos. In this Article, we will see how to build a simple real-time face detection application using Python and OpenCV where webcam will be used as the input source.
Step 1: Installing OpenCV LibraryWe have to install OpenCV library in our system, Use the following command to install it:
Step 2: Importing OpenCV and Haar Cascade Classifierpip install opencv-python
We will use Haar Cascade Classifier which is a machine learning-based method for detecting objects in images. OpenCV provides this classifier as a pre-trained model for detecting faces. This classifier will help us to detect faces in the webcam feed. Download the Haar Cascade file from this link.
import cv2
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
Step 3: Open Webcam and Check for Webcam Access
We use the cv2.VideoCapture() function to open the webcam. Here passing 0 refers to the default webcam. If the webcam is not accessible, we display an error message and stop the program.
Python
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("Error: Could not open webcam.")
exit()
Step 4: Capture Frames, Convert to Grayscale and Detect Faces
Now it will continuously captures frames from the webcam. Each frame is converted to grayscale as face detection algorithms perform better on grayscale images. Then the Haar Cascade Classifier is applied to detect faces.
while True:
ret, frame = cap.read()
if not ret:
print("Error: Could not read frame.")
break
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30)) # Detect faces
Step 5: Draw Rectangles Around Detected Faces and Display the Frame
For each face detected a green rectangle is drawn around it and frame with rectangles is displayed in a window titled "Face Detection"
Python
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.imshow('Face Detection', frame)
Step 6: Exit the Program
if cv2.waitKey(1) & 0xFF == ord('q'):
break
Step 7: Release the Webcam and Close All Windows
When 'q' is pressed then webcam is released and any OpenCV windows are closed.
Python
cap.release()
cv2.destroyAllWindows()
Full Code: Python
import cv2
def main():
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("Error: Could not open webcam.")
return
while True:
ret, frame = cap.read()
if not ret:
print("Error: Could not read frame.")
break
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.imshow('Face Detection', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
main()
Output:
OutputUsing OpenCV's Haar Cascade Classifier this method provides a solid starting point for exploring real-time face detection and other computer vision projects.
Note : Above code will not run on online IDE. It will work on local system only.
You can download source code from here.
Face Detection using Python and OpenCV with webcam
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4