Welcome to your first Computer Vision project!
In this project, you'll teach a computer to identify human faces in images and live video streams.
This project introduces one of the most important AI domains:
π Computer Vision
Computer Vision enables machines to understand and analyze visual information from images and videos.
π― Project Goal
Build a Face Detection System that can:
β Detect faces in images
β Detect faces in videos
β Detect faces through webcam feed
β Draw bounding boxes around detected faces
π§ Skills You'll Learn
Python
Functions
Loops
File Handling
Computer Vision
OpenCV
Image Processing
AI Concepts
Face Detection
Object Detection
Real-Time Video Processing
Deployment
Streamlit
π Difference Between Face Detection & Face Recognition
Face Detection
Answers:
Is there a face in the image?
Example: Image 3 Faces Found
Face Recognition
Answers:
Whose face is it?
Example: Face Found John
This project focuses on:
β Face Detection
π Step 1: Install Required Libraries
pip install opencv-pythonVerify Installation:
import cv2
print(cv2.__version__)
πΌοΈ Step 2: Read an Image
import cv2
image = cv2.imread("person.jpg")
cv2.imshow("Image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
π Step 3: Convert Image to Grayscale
Face detection works better on grayscale images.
gray = cv2.cvtColor(
image,
cv2.COLOR_BGR2GRAY
)
Why?
β Faster processing
β Less memory usage
β Better detection performance
π€ Step 4: Load Pre-Trained Face Detector
OpenCV provides a pre-trained Haar Cascade model.
face_cascade = cv2.CascadeClassifier(
cv2.data.haarcascades +
"haarcascade_frontalface_default.xml"
)
π― Step 5: Detect Faces
faces = face_cascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5
)
What Happens Here?
The model scans the image and returns coordinates:
x = 120
y = 60
width = 180
height = 180
Each coordinate represents a detected face.
π₯ Step 6: Draw Bounding Boxes
for (x,y,w,h) in faces:
cv2.rectangle(
image,
(x,y),
(x+w,y+h),
(255,0,0),
2
)
Output:
π Face Detected
ββββββββββββ
β Face β
ββββββββββββ
π₯οΈ Step 7: Display Result
cv2.imshow(
"Face Detection",
image
)
cv2.waitKey(0)
cv2.destroyAllWindows()
Now detected faces appear inside rectangles.
π₯ Step 8: Real-Time Webcam Detection
This is where the project becomes exciting.
Access Webcam:
cap = cv2.VideoCapture(0)
π Step 9: Process Video Frames
while True:
success, frame = cap.read()
gray = cv2.cvtColor(
frame,
cv2.COLOR_BGR2GRAY
)
faces = face_cascade.detectMultiScale(
gray,
1.1,
5
)
for (x,y,w,h) in faces:
cv2.rectangle(
frame,
(x,y),
(x+w,y+h),
(255,0,0),
2
)
cv2.imshow(
"Face Detector",
frame
)
if cv2.waitKey(1) == 27:
break
Press: ESC to stop.
π Step 10: Release Camera
cap.release()
cv2.destroyAllWindows()