OpenCV-Python is a library of Python language which includes various bindings designed to solve computer vision problems. OpenCV is an open source C++ library used for image processing and computer vision applications. However, it is not mandatory for your OpenCV applications to be open or free. Open-CV is meant to be a library of many inbuilt functions mainly aimed at real time image processing.
The abbreviated form of Open-CV is:
Open Source Computer Vision
The list of activities which can be achieved using OpenCV are mentioned follows:
1. Read and write images
2. Detection of faces with its features
3. Detection of various shapes such as Circle, rectangle and many more.
4. Modification of image quality and colors.
5. Development of augmented reality applications.
The various languages supported by Open-CV are mentioned below:
OpenCV also includes some key points which are considered as plus points which is given below:
1. Easy to learn and read (includes lots of tutorials)
2. Works with all popular languages
3. Free to use
In this article, we will focus on using OpenCV in python language and for that it is mandatory to get it installed with proper command which is mentioned below:
|pip install opencv-python|
The output of installation is as follows:
|C:\Users\DELL\Desktop\Eduonix\Image-Processing-with-OpenCV\Face-Recognition>pip install opencv-python
Using cached https://files.pythonhosted.org/packages/6f/88/a75685d7293b321f1a4b20029fd8db0f620c546a88938ffea31e8a7d2df7/opencv_python-188.8.131.52-cp34-cp34m-win_amd64.whl
Requirement already satisfied: numpy>=1.11.1 in c:\python34\lib\site-packages (from opencv-python) (1.14.2)
Installing collected packages: opencv-python
Successfully installed opencv-python-184.108.40.206
Now, we will focus on creating a demonstration of image and facial recognition using OpenCV. The major pre-requisite for this module is an XML file called “haarcascade_frontalface_default.xml”.
A Haar Cascade is peculiar classifier which is used to detect objects from the source. The haarcascade_frontalface_default.xml which will be used in project is a haar cascade designed by OpenCV to detect the frontal face. This cascade works by training the cascade on thousands of negative images with the positive image superimposed on it. The haar cascade can detect features from the source. The contents of this XML file is as follows:
<opencv_storage> <cascade type_id="opencv-cascade-classifier"><stageType>BOOST</stageType> <featureType>HAAR</featureType> <height>24</height> <width>24</width> <stageParams> <maxWeakCount>211</maxWeakCount></stageParams> <featureParams> <maxCatCount>0</maxCatCount></featureParams> <stageNum>25</stageNum> <stages> <_> <maxWeakCount>9</maxWeakCount> <stageThreshold>-5.0425500869750977e+00</stageThreshold> <weakClassifiers> <_> <internalNodes> 0 -1 0 -3.1511999666690826e-02</internalNodes> <leafValues> 2.0875380039215088e+00 -2.2172100543975830e+00</leafValues></_> <_> <internalNodes> 0 -1 1 1.2396000325679779e-02</internalNodes> <leafValues> -1.8633940219879150e+00 1.3272049427032471e+00</leafValues></_> <_> <internalNodes> 0 -1 2 2.1927999332547188e-02</internalNodes> <leafValues> -1.5105249881744385e+00 1.0625729560852051e+00</leafValues></_> <_> <internalNodes> 0 -1 3 5.7529998011887074e-03</internalNodes> <leafValues> -8.7463897466659546e-01 1.1760339736938477e+00</leafValues></_> <_> <internalNodes> 0 -1 4 1.5014000236988068e-02</internalNodes>
We will import the OpenCV module for creating a face recognition module. In this assignment, we will be creating a face recognition module which captures the images in “Datasets” folder.
Step 1: Import the necessary modules
Step 2: Start capturing video and detect object in video stream using Haarcascade Frontal Face. Detect frames of different sizes, list of faces rectangles inside the defined functions.
def assure_path_exists(path): dir = os.path.dirname(path) if not os.path.exists(dir): os.makedirs(dir) # Start capturing video vid_cam = cv2.VideoCapture(0) # Detect object in video stream using Haarcascade Frontal Face face_detector = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') # For each person, one face id face_id = 1 # Initialize sample face image count = 0 assure_path_exists("dataset/")
Step 3: Start looping the face recognition and load it in datasets folder.
# Start looping while(True): # Capture video frame _, image_frame = vid_cam.read() # Convert frame to grayscale gray = cv2.cvtColor(image_frame, cv2.COLOR_BGR2GRAY) # Detect frames of different sizes, list of faces rectangles faces = face_detector.detectMultiScale(gray, 1.3, 5) # Loops for each faces for (x,y,w,h) in faces: # Crop the image frame into rectangle cv2.rectangle(image_frame, (x,y), (x+w,y+h), (255,0,0), 2) # Increment sample face image count += 1 # Save the captured image into the datasets folder cv2.imwrite("dataset/User." + str(face_id) + '.' + str(count) + ".jpg", gray[y:y+h,x:x+w]) # Display the video frame, with bounded rectangle on the person's face cv2.imshow('frame', image_frame) # To stop taking video, press 'q' for at least 100ms if cv2.waitKey(100) & 0xFF == ord('q'): break # If image taken reach 100, stop taking video elif count>100: break
Step 4: After loading all the images in dataset, we need to stop video for recording facial recognitions and close all windows.
# Stop video vid_cam.release() # Close all started windows cv2.destroyAllWindows()
The output opens the web camera and captures all images which is given below:
The images stored in datasets folder as mentioned below:
The training of data is analysed in following code:
# Import OpenCV2 for image processing # Import os for file path import cv2, os # Import numpy for matrix calculation import numpy as np # Import Python Image Library (PIL) from PIL import Image import os def assure_path_exists(path): dir = os.path.dirname(path) if not os.path.exists(dir): os.makedirs(dir) # Create Local Binary Patterns Histograms for face recognization recognizer = cv2.face.LBPHFaceRecognizer_create() # Using prebuilt frontal face training model, for face detection detector = cv2.CascadeClassifier("haarcascade_frontalface_default.xml"); # Create method to get the images and label data def getImagesAndLabels(path): # Get all file path imagePaths = [os.path.join(path,f) for f in os.listdir(path)] # Initialize empty face sample faceSamples= # Initialize empty id ids =  # Loop all the file path for imagePath in imagePaths: # Get the image and convert it to grayscale PIL_img = Image.open(imagePath).convert('L') # PIL image to numpy array img_numpy = np.array(PIL_img,'uint8') # Get the image id id = int(os.path.split(imagePath)[-1].split(".")) # Get the face from the training images faces = detector.detectMultiScale(img_numpy) # Loop for each face, append to their respective ID for (x,y,w,h) in faces: # Add the image to face samples faceSamples.append(img_numpy[y:y+h,x:x+w]) # Add the ID to IDs ids.append(id) # Pass the face array and IDs array return faceSamples,ids # Get the faces and IDs faces,ids = getImagesAndLabels('dataset') # Train the model using the faces and IDs recognizer.train(faces, np.array(ids)) # Save the model into trainer.yml assure_path_exists('trainer/') recognizer.save('trainer/trainer.yml')
When you run this code mentioned in this article, you will see that face values are stored in the mentioned folder called datasets. The algorithm designed is context-aware. To find similar images Convolutional Neural network is being used. We can access all patterns of images with this facial recognition module which helps in maintaining security of devices. OpenCV always proves beneficial in face recognition and eye detection module.