Face Detection using Open-CV

Face Detection using Open-CV

This is a simple article on “Face detection “using OpenCV. OpenCv is the most popular computer vision library. And today we will be using it to create our simple module for face detection.

This is a simple article on “Face detection “using OpenCV. OpenCv is the most popular computer vision library. And today we will be using it to create our simple module for face detection.

Once you successfully implement this module you can advance to my next articles for face recognition.

Before starting with the tutorial this is the list of Prerequisites you would need.

Prerequisites:-

· *Python *(I am using version 3.6.4)

· OpenCV

Once you have Python, You can simply Do It by typing following command in your command prompt.

pip install opencv-python

(This will install the latest version of openCV in your system.)

· The** Haar cascade** file

(Now the Haar Cascade is basically a classifier which is used to detect the object for which it has been trained for.)

We need .xml file of frontal face Haar-Cascade.

This file along with Couple of more default cascades re provided with the OpenCv package itself. So to find it follow up:-

  1. Open Command prompt in windows /terminal or bash in other Os.

Now assuming that you have already installed python.

Start python in your prompt.

  1. Once the python is started type following to get the location of opencv.

Import cv2

And then type following command to get the location .

Print(cv2.file)

Copy the location as shown and paste it into the explorer.

Inside the data folder you will find the Haar cascade file for frontal face that we will be using

Copy this file create a new folder on your desktop And paste this file in that folder .

Make sure that your python code and this file are in the same folder. In this way it will be way simpler to give the path.

· Lets start the code Now:-

  1. open python IDLE or any other IDE that you will be using , create a new file in the same folder where you Copied your har cascade.

·** Import The libraries:-**

import cv2
import numpy as np

.** Detection part:-**

· Import the Haar Cascade

detector=cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

.Adding a video capture object

cap=cv2.VideoCapture(0)

The argument (0) in the above line indicates the default web cam you your are using USB-camera you can use the argument (1) it goes on increasing for the no. of usb cameras connected to system.

· Lets read the Image from the video Capture object.

Which will return a *TRUE/FALSE *and the capture frame will be shown using imshow().

· This will be a while loop in which we will capture the image convert it to gray scale for face detection.

And then we will apply face detector on the Grayscale image.

Then we will extract the X,Y,W,H parameters from the image. Where X, and Y are screen coordinates and W=width of the box for face H=height.

while(True):
ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = detector.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)

once done with this we will show the image with face detector .and I have added q as a key to break operation. And waitkey is used for delay.

THE COMPLETE CODE:-

import numpy as np
import cv2
detect=cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
c = cv2.VideoCapture(0)
while(True):
ret, img = c.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = detect.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
cv2.imshow('frame',img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
c.release()
cv2.destroyAllWindows()

Here are the results. 😊

Output 1.

Output 2.

I hope this was helpful. In case you get any problem feel free to post your query I will try my best to solve it .

Keep Learning . All the best.

python opencv

What's new in Bootstrap 5 and when Bootstrap 5 release date?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Random Password Generator Online

HTML Color Picker online | HEX Color Picker | RGB Color Picker

Create a Virtual Pen and Eraser with Python OpenCV - Genial Code

Learn Free how to create a virtual pen and eraser with python and OpenCV with source code and complete guide. This entire application is built fundamentally on contour detection. It can be thought of as something like closed color curves on compromises that have the same color or intensity, it's like a blob. In this project we use color masking to get the binary mask of our target color pen, then we use the counter detection to find the location of this pen and the contour to find it.

Basic Data Types in Python | Python Web Development For Beginners

In the programming world, Data types play an important role. Each Variable is stored in different data types and responsible for various functions. Python had two different objects, and They are mutable and immutable objects.

OpenCV Python Tutorial: Computer Vision With OpenCV In Python

OpenCV Python Tutorial: Computer Vision With OpenCV In Python: Learn Vision Includes all OpenCV Image Processing Features with Simple Examples. Face Detection, Face Recognition. Computer Vision is an AI based, that is, Artificial Intelligence based technology that allows computers to understand and label images. Use OpenCV to work with image files. Create Face Detection Software. Detect Objects, including corner, edge, and grid detection techniques with OpenCV and Python. Use Python and Deep Learning to build image classifiers. Use Python and OpenCV to draw shapes on images and videos. Create Color Histograms with OpenCV

OpenCV Python Tutorial - Computer Vision With OpenCV In Python

In this OpenCV Python Tutorial article, we will be covering various aspects of Computer Vision using OpenCV in Python. OpenCV has been a vital part in the development of software for a long time. Learning OpenCV is a good asset to the developer to improve aspects of coding and also helps in building a software development career.

Python OpenCV Tutorial - Learn Computer Vision with OpenCV and Python

Python OpenCV Tutorial - Learn Computer Vision with OpenCV and Python: Introduction to OpenCV and Image Processing in Python, Image processing basics, Object detection and tracking, Deep Learning, Facial landmarks and many special applications.