Face and Eye Detection step by step Step
#1 Installing Requirements
Open cmd and type pip install OpenCV-python and pip install NumPy press Enter
#2 Creating a folder and paste files
First, make a folder and give a name
Then you need two haarcascade (haarcascade_frontalface_default ; haarcascade_eye) file to the folder which you have created earlier
#3 Open the folder on an editor and start coding
for face and eye detection
Open the folder on an editor.
I am using Visual Studio Code and create a file Face_Eye_Detection.py
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 #
import numpy as np
import cv2
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
cap = cv2.VideoCapture(0)
while 1:
ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
cv2.imshow('img',img)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
Contact me on Telegram if any problem:- @odeyash
#4 READY TO RUN
RUN THE Face_Eye_Detection.py file








0 Comments