LOADING

Type to search

Face Detection with Python using OpenCV – For Beginners

Computers

Face Detection with Python using OpenCV – For Beginners

Code for face detection

# -*- coding: utf-8 -*-
"""
Tasty tech harbour

@author: cmsrujan
"""
import cv2

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

webcam = cv2.VideoCapture(0)


while(True):
    ret, frame = webcam.read()
    
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    
    cv2.imshow('gray video', gray)
    
    faces = face_cascade.detectMultiScale(gray, 1.1 , 5) 
    for (x,y,w,h) in faces:
        cv2.rectangle(frame, (x,y), (x+w,y+h),(0,255,240), 2)
        
    cv2.imshow('Web camera output', frame)
    
    if cv2.waitKey(1) & 0xFF == ord('d'):
        break
    
webcam.release()
cv2.destroyAllWindows()

Download the haarcascade_frontalface_default below

Download other xml from the OpenCV Github repository from this link

Tags:

Leave a Comment

Your email address will not be published. Required fields are marked *