Face detection using openCV python.

Face detection is really fun, attractive and it has its own use.

Without going in much definition let’s jump to our implementation.

What is openCV?

openCV stands for Open source computer vision. it is a library of programming fun…


This content originally appeared on DEV Community and was authored by Rishabh Dwivedi

Face detection is really fun, attractive and it has its own use.

Without going in much definition let's jump to our implementation.

What is openCV?

openCV stands for Open source computer vision. it is a library of programming functions mainly aimed at real-time computer vision.

For making task easier they have pretrained model name haarcascade
which provides good accuracy.

Approach.

  • install opencv at first.
pip install opencv-python
  • Import libraries
import cv2
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# Read the input image
img = cv2.imread('your-image')
# convert it into gray code from BGR
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# apply a scale of 1.1 and 4
faces = face_cascade.detectMultiScale(gray, 1.1, 4)

# Make rectangle around face of blue colour and thickness of 3.
for (x, y , w ,h) in faces:
  cv2.rectangle(img, (x,y), (x+w, y+h), (255, 0 , 0), 3)

# Display the output
cv2.imshow('img', img)
cv2.waitKey()
  • And here you are done.... Output image Alt Text

This code will detect multiple faces on any image with great accuracy.

It is also possible to detect multiple faces in realtime in videos.
That will be a part of my next blog.
(You can ask doubt in comment section.)

Thanks for reading.
Rishabh Dwivedi.


This content originally appeared on DEV Community and was authored by Rishabh Dwivedi


Print Share Comment Cite Upload Translate Updates
APA

Rishabh Dwivedi | Sciencx (2021-05-17T07:17:57+00:00) Face detection using openCV python.. Retrieved from https://www.scien.cx/2021/05/17/face-detection-using-opencv-python/

MLA
" » Face detection using openCV python.." Rishabh Dwivedi | Sciencx - Monday May 17, 2021, https://www.scien.cx/2021/05/17/face-detection-using-opencv-python/
HARVARD
Rishabh Dwivedi | Sciencx Monday May 17, 2021 » Face detection using openCV python.., viewed ,<https://www.scien.cx/2021/05/17/face-detection-using-opencv-python/>
VANCOUVER
Rishabh Dwivedi | Sciencx - » Face detection using openCV python.. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/17/face-detection-using-opencv-python/
CHICAGO
" » Face detection using openCV python.." Rishabh Dwivedi | Sciencx - Accessed . https://www.scien.cx/2021/05/17/face-detection-using-opencv-python/
IEEE
" » Face detection using openCV python.." Rishabh Dwivedi | Sciencx [Online]. Available: https://www.scien.cx/2021/05/17/face-detection-using-opencv-python/. [Accessed: ]
rf:citation
» Face detection using openCV python. | Rishabh Dwivedi | Sciencx | https://www.scien.cx/2021/05/17/face-detection-using-opencv-python/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.