This code captures video from the webcam, detects faces in real-time using the Haar Cascade Classifier, and draws rectangles around the detected faces. The video feed is displayed in a window, and the program can be terminated by pressing the 'Esc' key.
python
import cv2
This imports the OpenCV library, which is widely used for computer vision tasks.
python
alg = "haarcascade_frontalface_default.xml" haar_cascade = cv2.CascadeClassifier(alg)
alg
: Specifies the XML file that contains the Haar Cascade classifier data for detecting frontal faces.cv2.CascadeClassifier(alg)
: Loads the Haar Cascade classifier from the XML file into thehaar_cascade
object.
python
cam = cv2.VideoCapture(0)
cv2.VideoCapture(0)
: Initializes the webcam for video capture. The argument0
typically refers to the default webcam on the computer.
python
while True: _, img = cam.read()
cam.read()
: Captures a frame from the webcam. The underscore_
is used to ignore the first return value (a boolean indicating if the frame was read correctly), andimg
stores the captured frame.
python
grayImg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
: Converts the captured color image to grayscale. This is done because face detection using Haar Cascades works better on grayscale images.
python
face = haar_cascade.detectMultiScale(grayImg, 1.3, 4)
haar_cascade.detectMultiScale(grayImg, 1.3, 4)
: Detects faces in the grayscale image. The method returns a list of rectangles where faces are detected.grayImg
: The input grayscale image.1.3
: The scale factor for the image pyramid (how much the image size is reduced at each image scale).4
: The minimum number of neighbors each candidate rectangle should have to retain it.
python
`for (x, y, w, h) in face:
x1,y1=x+w, y+h
cv2.line(img, (x,y), (x+30, y),(0, 255, 0), 6)
cv2.line(img, (x,y), (x, y+30),(0, 255, 0), 6)
cv2.line(img, (x1,y), (x1-30, y),(0, 255, 0), 6)
cv2.line(img, (x1,y), (x1, y+30),(0, 255, 0), 6)
cv2.line(img, (x,y1), (x+30, y1),(0, 255, 0), 6)
cv2.line(img, (x,y1), (x, y1-30),(0, 255, 0), 6)
cv2.line(img, (x1,y1), (x1-30, y1),(0, 255, 0), 6)
cv2.line(img, (x1,y1), (x1, y1-30),(0, 255, 0), 6)`
- Iterates over the list of detected faces. For each face, a rectangle is drawn on the original image (
img
):(x, y)
: The top-left corner of the rectangle.(x, y1)
: The bottom-left corner of the rectangle.(x1, y)
: The bottom-right corner of the rectangle.(x1, y1)
: The bottom-leftcorner of the rectangle.(0, 0, 255)
: The color of the rectangle (red in BGR format).2
: The thickness of the rectangle's border.
python
cv2.imshow("Face Detection", img)
cv2.imshow("Face Detection", img)
: Displays the image with detected faces in a window titled "Face Detection".
python
key = cv2.waitKey(10) if key == 27: break
cv2.waitKey(10)
: Waits for 10 milliseconds for a key press.if key == 27
: Checks if the 'Esc' key (ASCII value 27) is pressed. If so, it breaks the loop and stops the program.
python
cam.release() cv2.destroyAllWindows()
cam.release()
: Releases the webcam resource.cv2.destroyAllWindows()
: Closes all OpenCV windows.
🤖 AI & ML Developer | Data Science & Analytics Expert
🌐 Web Apps & IoT Skilled | Awesome UI Creator
💻 AI is my main focus! 👾