Breaking

Wednesday, November 8, 2023

Face Authentication System with OpenCV and Haar Cascade | Python Tutorial

Face Authentication System


Introduction

In our rapidly evolving digital landscape, security and identity verification have taken center stage. The rise of biometric authentication methods has been nothing short of revolutionary, and one such method stands out prominently – facial recognition. Our journey begins here, in the exciting realm of creating a Face Authentication System. This blog post is your gateway to understanding and constructing a robust system, harnessing the power of Python, OpenCV, and the Haar Cascade Classifier.

Facial recognition technology has permeated every facet of our digital lives. It's not merely a buzzword; it's a transformative force. From the moment we unlock our smartphones by simply looking at them to the meticulous security protocols employed at high-security facilities, facial recognition is the invisible sentinel shaping our world.

However, the power and potential of this technology extend far beyond our smartphones and workplace access. In this blog post, we will uncover the inner workings of facial recognition and equip you with the knowledge to implement it in your projects. As we embark on this journey, you'll not only comprehend the mechanics of the technology but also the philosophy behind its real-world applications.

Understanding the Significance

The significance of facial recognition technology transcends convenience; it's a paradigm shift in how we interact with the digital and physical worlds. It represents the convergence of security and accessibility, two realms that were traditionally at odds.

Consider the sheer convenience of unlocking your phone with your face, a feature now ubiquitous across smartphones. Beyond convenience, facial recognition enhances security by providing a biometric barrier that's difficult to bypass. This technology doesn't merely verify who you are but also adapts to variations, making it increasingly resilient to fraudulent attempts.

Beyond personal devices, facial recognition is instrumental in diverse sectors, from airports using it to identify passengers to businesses securing access control. It's employed in payments, healthcare, and surveillance, making it an indispensable tool in modern society. Understanding its significance isn't just about appreciating its widespread adoption but also about recognizing the profound ways it's transforming how we live and interact with technology.

As we dive deeper into this blog post, you'll gain a holistic understanding of the significance of facial recognition, its ethical considerations, and its limitless potential for innovation in various domains. This knowledge will set the stage for your journey into constructing a Face Authentication System, underpinned by Python, OpenCV, and Haar Cascade.

Setting Up Your Development Environment

Before you can embark on the journey of creating your face authentication system, a solid foundation needs to be laid. This involves setting up your development environment, which includes installing essential libraries and tools. OpenCV and Pillow (PIL) are the primary libraries that you'll be working with in this context. A well-configured development environment ensures that you're equipped with the necessary tools for image processing and facial recognition.

The Role of Haar Cascade Classifier

At the core of effective face detection lies the Haar Cascade Classifier. This robust classifier plays a pivotal role in identifying facial features, making it a fundamental component of facial recognition systems. Understanding how to harness Haar Cascade is essential for precise facial feature detection, and it's a topic we'll delve into in this blog post.

Capturing Real-Time Video

To implement a functional face authentication system, one must first grasp the intricacies of capturing real-time video from your computer's webcam. This real-time video feed becomes the primary input for your system, serving as the source for face detection and recognition. Learning this aspect of the process is crucial for a successful facial authentication system.

Face Detection and Recognition Algorithms

While the actual code implementation won't be covered in this blog post, we will provide insights into the theoretical underpinnings of face detection and recognition algorithms. This knowledge serves as the foundation upon which you can build and customize your own face authentication system.

Authentication Process

The heart of any face authentication system is the authentication process itself. This is where the magic happens, where users are securely authenticated based on the recognized faces. While we won't delve into the actual code in this blog post, we'll provide insights into the theoretical underpinnings of the authentication process.

Authentication typically involves a series of steps, from face detection to feature extraction and matching. First, the system must detect a face within the input image or video stream. Once a face is detected, the system identifies specific facial features such as eyes, nose, and mouth, which are crucial for accurate recognition.

Feature extraction involves capturing unique characteristics of the face, often through mathematical representation. These features are then compared with a database of stored facial features. If a match is found, the system grants access. Otherwise, the user is denied.

Security is a top priority in the authentication process. Anti-spoofing measures may be employed to ensure that the system can't be tricked with photographs or videos. Multi-factor authentication can also be implemented for enhanced security.

The successful execution of the authentication process relies on the quality of the algorithms, the database of stored faces, and the real-time processing capabilities of the system. It's the culmination of all these components that makes a face authentication system both secure and efficient.

Source code

Source Code
""" Requirements python 3.7.7 numpy==1.21.6 opencv-contrib-python==4.1.2.30 Pillow==9.5.0 """ import sys import time import os import numpy as np from PIL import Image import cv2 path = 'user_data' name ='' if not os.path.exists("user_data"): os.mkdir('user_data') # print("Directory " , dirName , " Created ") def face_generator(): global name cam = cv2.VideoCapture(0)#used to create video which is used to capture images cam.set(3,640) cam.set(4,480) dectector=cv2.CascadeClassifier('haarcascade_frontalface_default.xml') # this file is used to detect a object in an image face_id=input("enter id of user :") name=input("enter name :") sample=int(input("Enter how many sample you wish to take : ")) for f in os.listdir(path): #remove old images from user data folder if present os.remove(os.path.join(path, f)) print("taking sample image of user ...please look at camera") time.sleep(2) count=0 while True: ret,img=cam.read()# read the frames using above created objects converted_image=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)#converts image to black and white faces=dectector.detectMultiScale(converted_image,1.3,5)#detect face in image for (x,y,w,h) in faces: cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)#creates frame around face count+=1 # print(count) cv2.imwrite("user_data/face."+str(face_id)+"."+str(count)+".jpg",converted_image[y:y+h,x:x+w]) # To caputure image and save in user_data folder cv2.imshow("image",img)#displays image on window k=cv2.waitKey(1) & 0xff if k==27: break elif count>=sample: break print("Image Samples taken succefully !!!!.") cam.release() cv2.destroyAllWindows() def traning_data(): # used to recognize faces in images and videos recognizer = cv2.face.LBPHFaceRecognizer_create() #creates an instance of a face detection classifier using the Haar Cascade classifier #pre-trained model for detecting faces in images dectector = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') def Images_And_Labels(path): imagesPaths = [os.path.join(path, f) for f in os.listdir(path)] faceSamples = [] ids = [] for imagePath in imagesPaths: gray_image = Image.open(imagePath).convert('L') # convert to grayscale img_arr = np.array(gray_image, 'uint8') # creating array # extracts the label (id) from the image file name id = int(os.path.split(imagePath)[-1].split(".")[1]) # detects faces in the image using the face detection classifier faces = dectector.detectMultiScale(img_arr) for (x, y, w, h) in faces: faceSamples.append(img_arr[y:y + h, x:x + w]) ids.append(id) return faceSamples, ids print("Training Data...please wait...!!!") faces, ids = Images_And_Labels(path) # trains the face recognizer using the loaded face data (faces) and labels (ids). recognizer.train(faces, np.array(ids)) # saves the trained recognizer to a YAML file called 'trained_data.yml'. recognizer.write('trained_data.yml') print("data Trained successfully !!!!") def detection(): recognizer = cv2.face.LBPHFaceRecognizer_create() recognizer.read('trained_data.yml') # loaded trained model cascadePath = "haarcascade_frontalface_default.xml" faceCascade = cv2.CascadeClassifier(cascadePath) font = cv2.FONT_HERSHEY_SIMPLEX # denotes fonts size id = 5 # number of persons your want to recognize names = ['',name] cam = cv2.VideoCapture(0) # used to create video which is used to capture images cam.set(3, 640) cam.set(4, 480) # define min window size to be recognize as a face minW = 0.1 * cam.get(3) maxW = 0.1 * cam.get(4) no = 0 while True: if cam is None or not cam.isOpened(): print('Warning: unable to open video source: ') ret, img = cam.read() # read the frames using above created objects if ret == False: print("unable to detect img") converted_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # converts image to black and white faces = faceCascade.detectMultiScale( converted_image, scaleFactor=1.2, minNeighbors=5, minSize=(int(minW), int(minW)), ) for (x, y, w, h) in faces: cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2) id, accuracy = recognizer.predict(converted_image[y:y + h, x:x + w]) # check if accuracy is less than 100 ==> "0" is perfect match # print(accuracy) if (accuracy < 100): id = names[id] accuracy = " {0}%".format(round(100 - accuracy)) # print(no) no += 1 else: id = "unknown" accuracy = " {0}%".format(round(100 - accuracy)) # print(no) no += 1 cv2.putText(img, "press Esc to close this window", (5, 25), font, 1, (255, 0, 255), 2) cv2.putText(img, str(id), (x + 5, y - 5), font, 1, (255, 0, 255), 2) cv2.putText(img, str(accuracy), (x + 5, y + h - 5), font, 1, (255, 255, 0), 1) cv2.imshow('camera', img) k = cv2.waitKey(10) & 0xff if k == 27: break cam.release() cv2.destroyAllWindows() def permisssion(val,task): if "Y"==val or "y" ==val: if task == 1: traning_data() elif task == 2: detection() else: print("ThankYou for using this application !! ") sys.exit() print("\t\t\t ##### Welcome to face Authentication System #####") face_generator() perm=input("Do you wish to train your image data for face authentication [y|n] : ") permisssion(perm,1) authenticate=input("Do your want test authentication system [y|n] : ") permisssion(authenticate,2)

Concluding Thoughts

As we reach the culmination of our exploration into building a Face Authentication System, it's worth reflecting on the path we've traversed. Facial recognition, a technology once relegated to the realm of science fiction, has become an integral part of our daily lives. This journey has been about unraveling the mechanics behind this remarkable technology and understanding how it can be harnessed for security, convenience, and efficiency.

In our discussions, we've underscored the pivotal role played by the Haar Cascade Classifier, an essential tool for identifying facial features. We've also delved into the intricacies of capturing real-time video, a fundamental aspect of developing a functional face authentication system. We've touched upon face detection and recognition algorithms, the brainpower behind the technology, and left you with the knowledge and curiosity to explore the coding aspect on your own.

But, this journey isn't just about technology. It's about security, innovation, and the potential to enhance our digital and physical worlds. As you embark on your own path to creating a Face Authentication System, remember that you're contributing to the ever-evolving landscape of biometric security. Your insights and creations have the power to shape the future of technology and redefine how we secure our digital and physical environments.


Stay up-to-date with our latest content by subscribing to our channel! Don't miss out on our next video - make sure to subscribe today.




No comments: