OpenCV: Face Recognition on Images

OpenCV Facial Recognition, Face Recognition

Exploring Facial Recognition Technology with OpenCV

In a world driven by technological advancements, the field of computer vision has emerged as a powerful force. Among its myriad applications, face recognition stands out as a captivating and transformative technology. In this Python journey into the world of OpenCV, we explore the intricacies of facial recognition algorithms, the magic behind the code, and the limitless possibilities this technology unlocks. Join us as we unravel the complexities of Face Recognition with OpenCV and discover the innovations that are reshaping the way we interact with the digital realm.

What’s Covered

  • What is Face Recognition?
  • The Facial Recognition Process.
  • Applications for Face Recognition.
  • Training Faces.
  • The Face Recognition Program.
  • Resulting Images.

What is Face Recognition?

Face recognition, also known as facial recognition, is a technology that involves identifying and verifying individuals by analyzing their unique facial features. It is a biometric technology that uses various algorithms and machine learning techniques to analyze facial patterns and match them against a database of known faces.

The Facial Recognition Process:

The process of face recognition typically involves the following steps:

Face Detection:

In this step, a face detection algorithm locates and detects faces within an image or video frame. It identifies the regions of the image that contain faces.

Face Alignment:

Once the faces are detected, the algorithm aligns them to a standardized position and size. This step normalizes the face images for consistent analysis.

Feature Extraction:

The algorithm extracts distinctive features from the aligned faces, such as the position, size, and shape of facial landmarks (e.g., eyes, nose, mouth). These features are then converted into numerical representations called feature vectors.

Face Matching:

The extracted feature vectors are compared with the feature vectors stored in a database or watchlist. This comparison is typically performed using machine learning techniques like deep learning and pattern recognition algorithms. The system calculates a similarity score or a distance metric between the feature vectors to determine the likelihood of a match.

Identification or Verification:

Depending on the specific application, face recognition can be used for identification or verification purposes. In identification mode, the algorithm attempts to determine the identity of an individual by comparing their face against a large database of known faces. In verification mode, the algorithm verifies whether a presented face matches the face associated with a specific identity.

Applications for Face Recognition

Face recognition technology has various applications, including:

Access Control:

It can be used for secure authentication and access control systems, replacing traditional methods like passwords or keycards.

Surveillance and Security:

It enables the automatic identification of individuals in surveillance footage, assisting in law enforcement and security applications.

User Experience:

Face recognition can enhance user experience by enabling features like face unlocking on smartphones, personalized marketing, and customized services.

Human-Computer Interaction:

It can be used to develop interactive systems that respond to users’ facial expressions, gestures, or emotions.

Biometric Identification:

Face recognition is one of the biometric modalities used for identity verification along with fingerprints, iris recognition, and voice recognition.

A Potential for Misuse:

While face recognition technology offers significant benefits, there are also concerns related to privacy, security, and potential misuse. As a result, its deployment and regulation are subject to ethical considerations and legal frameworks in many jurisdictions.

Training Faces

Gathering Images

For every person of interest, you should gather 3-4 full frontal face images. Make sure to label only one of the images with their full name. Now, inside the Python-AI directory create a new directory labelled images, inside the image’s directory create two new directories one labelled known and another labelled unknown. The named image needs to be added to the known directory. The rest of the images need to be added to the unknown directory. Do this for every person you wish to run the Facial recognition scanner on.

Training Known Faces

To be able to recognize an individual we need to create a database that contains their facial profile. The person of interest has their facial measurements taken and stored in the database file that we can later use when scanning frames to identify the persons we are looking for.

Sample Images

The images below are the images I will be using to create the training database file, I will not be using these later when recognizing faces. These images are stored in the known directory.

You can add an image of whoever you want into the known directory, just make sure to use a full-frontal image of their face and label the image with the person’s name.

Python Code for Creating Facial Profiles

The code below will scan the images stored in the known directory and create a database of each person’s facial profile. The images should be titled with the full name of each person.

Important: You need to alter the code for the /image/known/ location to the location on your own system.

import face_recognition
import cv2
import os
import pickle
Encodings=[] Names=[] image_dir='/home/meganano/Desktop/Python-AI/images/known/' for root, dirs, files in os.walk(image_dir): print(files) for file in files: path=os.path.join(root,file) print(path) name=os.path.splitext(file)[0] print(name) person=face_recognition.load_image_file(path) encoding=face_recognition.face_encodings(person)[0] Encodings.append(encoding) Names.append(name) print(Names) with open('train.pkl','wb') as f: pickle.dump(Names, f) pickle.dump(Encodings, f)

The Facial Recognition Program

Sample Images of Unknown Faces

The images below are just some of the images I used for face recognition. These images do not have any identifiable titles they are just labelled u1, u2, u3 etc… These images are stored in the unknown directory. The images in the unknown directory should not be the same as the images you added to the known directory, this way you will know it’s a fair test.

Python Code for Facial Recognition

The code below will run through every image stored in the unknown directory and will attempt to identify the person or persons in each image. When you start the program, an image is scanned and displayed to the screen, if a face is detected a bounding box will be added around the face, if the person or persons are recognized their name will be displayed above the box, otherwise Unknown will be displayed instead. The next image won’t be scanned or displayed until you press any key on your keyboard except the letter “q”, the letter “q” key is reserved for quitting the program at any point.

You will need to change the /image/unknown/ location to the location on your own system.

import face_recognition
import cv2
import os
import pickle
width=600 height=600 dimensions=(width,height) font=cv2.FONT_HERSHEY_SIMPLEX Encodings=[] Names=[] with open ('train.pkl','rb') as f: Names=pickle.load(f) Encodings=pickle.load(f) image_dir='/home/meganano/Desktop/Python-AI/images/unknown' for root,dirs, files in os.walk(image_dir): for file in files: print(root) print(file) testImagePath=os.path.join(root,file) testImage=face_recognition.load_image_file(testImagePath) facePositions=face_recognition.face_locations(testImage) allEncodings=face_recognition.face_encodings(testImage,facePositions) testImage=cv2.cvtColor(testImage,cv2.COLOR_RGB2BGR) for (top,right,bottom,left),face_encoding in zip(facePositions,allEncodings): name='Unknown' matches=face_recognition.compare_faces(Encodings,face_encoding) if True in matches: first_match_index=matches.index(True) name=Names[first_match_index] print(name+' Recognised') cv2.rectangle(testImage,(left,top),(right,bottom),(0,0,255),2) cv2.putText(testImage,name,(left,top-6),font,.75,(255,0,0),2) resImage=cv2.resize(testImage,dimensions,interpolation = cv2.INTER_AREA) cv2.imshow('Picture:',resImage) cv2.moveWindow('Picture',0,0) if cv2.waitKey(0)==ord('q'): break if cv2.waitKey(0): cv2.destroyAllWindows() cv2.destroyAllWindows() print('Program Terminated')

Results

The images below are screenshots I took as the face recognition program identified each person or persons in an image it was scanning, as you can see it was very successful. If you come across any false readings, run the training process again with a different image of anyone the recognition program struggled to find.

Conclusion

As we draw the curtains on our exploration of Face Recognition with OpenCV, it’s evident that we’ve only scratched the surface of this transformative technology. From security applications to user authentication, and beyond, the potential for OpenCV-driven face recognition is vast. As we step into the future, the lines between science fiction and reality continue to blur, and OpenCV remains at the forefront of this technological revolution. Stay curious, stay innovative, and continue to unlock the possibilities that lie within the fascinating world of computer vision.

In the next installment of our OpenCV for Beginners guide we will be moving onto Motion Detection

That’s All Folks!

You can find all of our OpenCV guides here: OpenCV for Beginners

Luke Barber

Hello, fellow tech enthusiasts! I'm Luke, a passionate learner and explorer in the vast realms of technology. Welcome to my digital space where I share the insights and adventures gained from my journey into the fascinating worlds of Arduino, Python, Linux, Ethical Hacking, and beyond. Armed with qualifications including CompTIA A+, Sec+, Cisco CCNA, Unix/Linux and Bash Shell Scripting, JavaScript Application Programming, Python Programming and Ethical Hacking, I thrive in the ever-evolving landscape of coding, computers, and networks. As a tech enthusiast, I'm on a mission to simplify the complexities of technology through my blogs, offering a glimpse into the marvels of Arduino, Python, Linux, and Ethical Hacking techniques. Whether you're a fellow coder or a curious mind, I invite you to join me on this journey of continuous learning and discovery.

Leave a Reply

Your email address will not be published. Required fields are marked *

Verified by MonsterInsights