🎥 Real-Time Face & Eye Detection with Python and OpenCV | Haar Cascade Tutorial
🧠 Ever wondered how facial recognition works behind the scenes? In this video, we’ll walk you through a real-time Face and Eye Detection system using OpenCV in Python — one of the most beginner-friendly and powerful libraries for computer vision.
📌 What You’ll Learn in This Video:
✅ How to access your webcam using OpenCV
✅ How to use pre-trained Haar Cascade classifiers to detect faces and eyes
✅ How the detectMultiScale() method works for identifying features
✅ How to draw rectangles and labels on detected faces and eyes
✅ Real-time detection that works live on your webcam!
👨💻 Code Breakdown:
python
Copy
Edit
Load your webcam
cam = cv2.VideoCapture(0)
Load Haar cascades for face and eyes
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye.xml')
Read each frame, convert to grayscale, and detect
🎯 The coordinates (x, y, w, h) represent the top-left corner and size of each detected face or eye, which we use to draw rectangles and overlay text labels like "Face" or "Eye".
👁 The script detects up to 2 eyes per face and displays it live in a window. Hit 'q' to quit the window anytime.
🔧 Requirements:
Python 🐍
OpenCV (pip install opencv-python)
👍 Like this project?
🔔 Subscribe for more beginner-friendly AI and Computer Vision tutorials!
CODE---
import cv2
Face and Eye Detection using OpenCV
cam = cv2.VideoCapture(0)
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye.xml')
while True:
ret, frame = cam.read()
if not ret:
print("Failed to load")
break
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5)
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
cv2.putText(frame, "Face", (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2)
roi_gray = gray[y:y + h, x:x + w]
roi_color = frame[y:y + h, x:x + w]
eyes = eye_cascade.detectMultiScale(roi_gray, scaleFactor=1.4, minNeighbors=15)
for (ex, ey, ew, eh) in eyes[:2]:
cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 255), 2)
cv2.putText(roi_color, "Eye", (ex, ey - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)
cv2.imshow('Face, Eye ', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cam.release()
cv2.destroyAllWindows()
Информация по комментариям в разработке