Python code for Face Detection in Image

2019-08-19 Uttkarsh Kabde Download Code

Hello programmers, today we are going to detect face or (faces) in image using simple technique which is old(not that old) for beginners who want to detect face in images.  Here the actual process is we are going to pass path of image in code.  The code will process on it and box the faces in an image. So here we begin

                Firstly we need some files or libraries to execute today’s program and here is the list:

  1. Python (Obviously)
  2. Opencv 2.x
  3. numpy
  4. One haar cascade file(.xml)


So before writing the code make sure you have installed Opencv and numpy and its working properly. For checking this just open terminal and start python and then insert command as shown in below picture.



After that download .xml files which are required to execute our Face Detection program from here(

Note: Here find file with name {'haarcascade_frontalface_default.xml'} and yes we need just this file.

Or you can download all files by clicking on download button

After this lets start writing code by createing new file with name

or just copy and paste following code


import numpy as np
import cv2



for(x,y,w,h) in faces:



After writing code just save it and run with following command


After executing the code you will get result as given below



Where the original image was




Thats it we did it.  We just detected faces in image using just opencv and haar cascade file.  you can change input file name(hp.jpg) with your own file


For now we will stop here.  After running above code if you get any error let us know by commenting below we will make sure to solve your problem ASAP


Happy Coding :) :)

About author

Card image cap
Uttkarsh Kabde

Currently I am Persuing Mtech in IT at Shri Guru Gobind Singh Institute of Engineering and Technology, Nanded!

-Web App Developer


Scroll to Top