Python tutorial creating voice assistant using python

April 06, 2022

In this article i will explain you how you can detect face in webcam

Step 1: Install necessary python library which gonna help in reading images

  • OpenCv: This is open source image recogntion library in python
pip3 install opencv-python

Step 2: For face detection you would need another xml file other than image

Download haarcascades file for face detection from this link

Step 2: Once the needed library installed copy paste the following code

import cv2
cap = cv2.VideoCapture(0)
face_Cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")

#this loop continously check for the faces and detect the faces
while True:
    success, image =
    imgGray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    faces = face_Cascade.detectMultiScale(imgGray, 1.1, 4)

    for (x, y, w, h) in faces:
      cv2.rectangle(image, (x, y), (x + w, y + h), (255, 0, 0), 2)

    cv2.imshow("Result", image)
    if cv2.waitKey(1) & 0xFF == ord('q'):