Face Detection using Open-cv

Giri Prasath.D
2 min readJun 5, 2021

Face Detection using Open CV and HaarCascades

This requires Open CV, Python, HaarCascades training data and suitable OS platform (Mac, Linux or Windows). For specific version please check vendor websites for the latest.

I used Python 3.9.4 on a test machine running Windows 10

To determine which version of Python you have installed,

Type the following on command prompt:

python — version

OpenCV 3.4.3 was used with this project, suitable for Python 2.7.14.

To determine if you have properly installed OpenCV, type the following from the python command line:

pip install opencv-python

see this website for further classification

https://pypi.org/project/opencv-python/

import cv2

Images used in this project were set to a resolution of 500 x 331 pixels (JPEG)

For documentation, please read:

code for the face detectionimport cv2 # Gets the name of the image file (filename) 
from sys.argv
imagePath = #enter the image path here
cascPath = “haarcascade_frontalface_default.xml”
`# This creates the cascade classifcation from file
faceCascade = cv2.CascadeClassifier(cascPath)
# The image is read and converted to grayscale
image = cv2.imread(imagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# The face or faces in an image are detected
# This section requires the most adjustments to get accuracy on face being detected.
faces = faceCascade.detectMultiScale( gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(1,1),
flags = cv2.CASCADE_SCALE_IMAGE )
print(“Detected {0} faces!”.format(len(faces)))
# This draws a green rectangle around the faces detected
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x+w, y+h), (255,0,0), 2) cv2.imshow(“Faces Detected”, image)
cv2.waitKey(0
)

--

--

Giri Prasath.D

Biomedical engineer by Degree 👨‍🎓| Data Analyst by Profession |🧑‍💻||Python||Sql||AWS||Neo4j||Knime||Alteryx