FACE DETECTION USING PYTHON
OBJECTIVE:
OpenCV is a computer vision library that supports programming languages like Python,
C++, and Java.
The package was initially created by Intel in 1999 and was later made open-source and
released to the public.
ABSTRACT:
OpenCV allows developers and non-mathematicians to build computer vision applications
easily without having to code them from scratch. The library has over 2,500 algorithms that
allow users to perform tasks like face recognition and object detection.
INTRODUCTION:
Face detection involves identifying a person’s face in an image or video. This is done by
analyzing the visual input to determine whether a person’s facial features are present.
Since human faces are so diverse, face detection models typically need to be trained on
large amounts of input data for them to be accurate. The training dataset must contain a
sufficient representation of people who come from different backgrounds, genders, and
cultures.
HARDWARE/SOFTWARE REQUIREMENTS:
OpenCV and Python
PROGRAM:
import cv2
# Load the pre-trained face detection model
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades +
'haarcascade_frontalface_default.xml')
# Open a connection to the webcam (usually, 0 or 1 for the default camera)
cap = cv2.VideoCapture(0)
1
while True:
# Read a frame from the webcam
ret, frame = cap.read()
if not ret:
break
# Convert the frame to grayscale for better face detection
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Perform face detection
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5,
minSize=(3, 3))
# Draw rectangles around detected faces
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 3)
# Display the frame with detected faces
cv2.imshow('Face Detection', frame)
# Exit the loop by pressing the 'q' key
if cv2.waitKey(1) & 0xFF == ord('q'):
break
2
# Release the webcam and close all OpenCV windows
cap.release()
cv2.destroyAllWindows()
CONCLUSION:
You can also test the efficacy of this model by holding up multiple pictures or by getting
different people to stand at various angles behind the camera. The model should be able to
identify all human faces in different backgrounds or lighting settings.
REFERENCES:
https://www.datacamp.com/tutorial/face-detection-python-opencv