Face Recognition Using Python and OpenCV

We will create a  Face Recognition Project using Python with OpenCV. Before we start, let’s learn the basic definitions below related to the project.

Please note that this project for high school students. The students should have some basic knowledge of python programming and OpenCV.

Learn – Practice – Share

What is Face Detection: Face detection is a computer technology being used in a variety of applications that identifies human faces in digital images. Face detection also refers to the psychological process by which humans locate and attend to faces in a visual scene.

What is OpenCV: OpenCV is a huge open-source library for computer vision, machine learning, and image processing. OpenCV supports a wide variety of programming languages like Python, C++, Java, etc. It can process images and videos to identify objects, faces, or even the handwriting of a human.

We will use the haar cascade classifier in this project.

Haar cascade classifier is an Object Detection Algorithm used to identify faces in an image or a real-time video. The algorithm uses edge or line detection features proposed by Viola and Jones in their research paper “Rapid Object Detection using a Boosted Cascade of Simple Features” published in 2001. The algorithm is given a lot of positive images consisting of faces, and a lot of negative images not consisting of any face to train on them. The model created from this training is available at the OpenCV GitHubrepository  https://github.com/opencv/opencv/tree/master/data/haarcascades.

Face Recognition Project with an Image

Requirements for Face Recognition Project

To start creating a Face Recognition Project, make sure you install the following programs on your computer.

  • Install python – Download the latest version for your computer. www.python.org/downloads
  • Install Pycharm  – Download the latest version for your computer www.jetbrains.com/pycharm/download
  • Install OpenCV  and Numpy Packages by using pip install library. Run this pip command in your terminal:  –pip install OpenCV-python numpy


Configure Your Python interpreter

Click File / Settings / Project / Python Interpreter for Windows .

Then click the search box to find opencv , then click install.

Install all the packages shown on the photo below.

Open the PyCharm, and then enter the following codes below.

#import libraries
import numpy as np
import cv2

# Read the input image
img = cv2.imread("faces.jpeg",1)

# Convert into grayscale
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
path = "haarcascade_frontalface_default.xml"

# Create Cascade Classifiers
face_cascade = cv2.CascadeClassifier(path)

# Detect faces using the classifiers
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.10, minNeighbors=5, minSize=(40,40))
print(len(faces))


# Draw a rectangle around the faces
for (x, y, w, h) in faces:
   cv2.rectangle(img, (x,y), (x+w,y+h), (0,255,0), 2)

# Display the result
cv2.imshow("Image",img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Result:


Face Recognition Project using a Webcam

You will need a webcam for this project.

Open the PyCharm, and then enter the following codes below.

#import libraries
import cv2
import os
#Credit realpython.com

#start webcam
cap=cv2.VideoCapture(0)

# Capture frame-by-frame
# Convert into grayscale
# Create Cascade Classifiers

while True:
    ret,frame=cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faceCascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
    faces = faceCascade.detectMultiScale(
    gray,
    scaleFactor=1.1,
    minNeighbors=5,
    minSize=(30, 30)
    )

# Draw a rectangle around the faces
    for (x, y, w, h) in faces:
        cv2.putText(frame,"face", (x, y - 10), 
cv2.FONT_HERSHEY_DUPLEX,0.5, (194, 231, 26), 1)
        cv2.rectangle(frame, (x, y), (x+w, y+h), (194, 231, 26), 2)

# Display the resulting frame
    cv2.imshow("frame",frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break;
cap.release()
cv2.destroyAllWindows()

Result:

 

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Top