Face Detection using Python Step By Step

Face Detection Using Python Step By Step



#1 Installing OpenCV

Open cmd and type pip install opencv python and press Enter
And wait for several minutes to install OpenCV 

#2 Creating a folder and files

First, make a folder and give a name
Then you need two haarcascade files(haarcascade_frontalface_default)
Make a folder and give a name



Paste the downloaded file

#3 Open the folder on an editor and start coding

for face detection

Open the folder on an editor.
I am using Visual Studio Code and create a file Face_Detection.py
Creating a file on the editor



Then Start Writing the Code
#Note in cap = cv2.VideoCapture (0)
 I am using 0 because
 I am using internal camera
 If you are using external camera 
Then you put 1 #


#first import cv2 
import cv2
# Load the cascade
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

# To capture video from webcam. 
cap = cv2.VideoCapture(0)
# To use a video file as input 
# cap = cv2.VideoCapture('filename.mp4')

while True:
    # Read the frame
    _, img = cap.read()

    # Convert to grayscale
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # Detect the faces
    faces = face_cascade.detectMultiScale(gray, 1.1, 4)

    # Draw the rectangle around each face
    for (x, y, w, h) in faces:
        cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)

    # Display
    cv2.imshow('img', img)

    # Stop if escape key is pressed
    k = cv2.waitKey(30) & 0xff
    if k==27:
        break
        
# Release the VideoCapture object
cap.release()
Write Code 
Contect me on Telegram if any problem :-  @odeyash

#4 Ready to Run

Run the Code and See the Result



0 Comments