Implementing Face Detection using Python and OpenCV

What is Face detection?

How Face detection works?

OpenCV

source: https://medium.com/@gsari/digit-recognition-with-opencv-and-python-cbf962f7e2d0

How to find faces using OpenCV?

import cv2
import numpy as np
from matplotlib import pyplot as plt
import glob
txtfiles = []
for file in glob.glob("*.jpg"):
txtfiles.append(file)

for ix in txtfiles:
img = cv2.imread(ix,cv2.IMREAD_COLOR)
imgtest1 = img.copy()
imgtest = cv2.cvtColor(imgtest1, cv2.COLOR_BGR2GRAY)
facecascade = cv2.CascadeClassifier('D:\\KJ\\Nagesh\\Downloads\\face_recognition\\haarcascade_frontalface_default.xml') eye_cascade = cv2.CascadeClassifier('D:\\KJ\\Nagesh\\Downloads\\face_recognition\\haarcascade_eye.xml')

faces = facecascade.detectMultiScale(imgtest, scaleFactor=1.2, minNeighbors=5)

print(’Total number of Faces found’,len(faces))

for (x, y, w, h) in faces:
face_detect = cv2.rectangle(imgtest, (x, y), (x+w, y+h), (255, 0, 255), 2)
roi_gray = imgtest[y:y+h, x:x+w]
roi_color = imgtest[y:y+h, x:x+w] plt.imshow(face_detect)
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex,ey,ew,eh) in eyes:
eye_detect = cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(255,0,255),2)
plt.imshow(eye_detect)
import numpy as np
from matplotlib import pyplot as plt
import cv2
import glob
txtfiles = [] 
for file in glob.glob("*.jpg"):
txtfiles.append(file)
img = cv2.imread(ix,cv2.IMREAD_COLOR)
imgtest1 = img.copy()
imgtest = cv2.cvtColor(imgtest1, cv2.COLOR_BGR2GRAY)
facecascade = cv2.CascadeClassifier('D:\\KJ\\Nagesh\\Downloads\\face_recognition\\haarcascade_frontalface_default.xml')eye_cascade = cv2.CascadeClassifier('D:\\KJ\\Nagesh\\Downloads\\face_recognition\\haarcascade_eye.xml')
detectMultiScale(image, scaleFactor, minNeighbors)
faces = facecascade.detectMultiScale(imgtest, scaleFactor=1.2, minNeighbors=5)
print(’Total number of Faces found ',len(faces))
for (x, y, w, h) in faces:face_detect = cv2.rectangle(imgtest, (x, y), (x+w, y+h), (255, 0, 255), 2)
roi_gray = imgtest[y:y+h, x:x+w]
roi_color = imgtest[y:y+h, x:x+w]
plt.imshow(face_detect)
eyes = eye_cascade.detectMultiScale(roi_gray)for (ex,ey,ew,eh) in eyes:eye_detect = cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(255,0,255),2)plt.imshow(eye_detect)
Output file : friends.PNG
Output file : Leonardo.PNG
Output file : Nagesh.PNG
Output file : Rahul.PNG
Output file : Amar.PNG
Output file : Sachin.PNG
Output file : Vishak.PNG
Output file : Shahrukh.PNG
Output file : Dog.PNG
Output file : download1.PNG

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Nagesh Singh Chauhan

Written by

Data Science | Big Data | Machine Learning | Python | https://www.linkedin.com/in/nagesh-singh-chauhan-6936bb13b/

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com