Face recognition using Artificial Intelligence (original) (raw)

Last Updated : 4 Jun, 2026

Face Recognition is a technology that identifies or verifies a person from an image or video by analyzing unique facial features. It uses machine learning and deep learning models to extract facial patterns and compare them against stored embeddings to confirm identity.

Working

Face Recognition follows a sequence of AI-driven steps that detect, align, encode and match facial features to identify or verify a person.

face_detector

Face Recognition

1. Face Detection

The first step identifies the region where a face is present in an image or video frame. Popular algorithms include:

2. Face Alignment

Once a face is detected, the system aligns it by adjusting key facial landmarks such as the eyes, nose, and lips. Alignment helps handle variations caused by rotation, tilt, lighting or facial expressions, ensuring that the model works on a normalized and correctly oriented face.

Deep learning models convert each face into a numerical vector called an embedding. This embedding uniquely represents facial features. These embeddings allow comparison between two faces using similarity scores. Some widely used AI models for face embeddings include:

4. Face Matching

After extracting embeddings, the system compares them to identify or verify the person. Common similarity techniques:

Lower distance means higher similarity and a greater chance that the two faces belong to the same person.

AI/ML Pipeline for Training Facial Recognition

Building a facial recognition system involves a systematic pipeline that covers data preparation, model training, evaluation, and deployment. Each step ensures that the system becomes accurate, robust and ready for real world use.

1. Data Collection

2. Data Labeling

3. Data Pre-processing

4. Training the Model

CNNs or Transformer-based networks learn facial features and generate embeddings using losses like Triplet Loss or ArcFace.

5. Testing and Validation

The model is evaluated on unseen data using metrics like accuracy, FAR and similarity thresholds to ensure reliability.

6. Deployment

After achieving the desired accuracy, the trained face recognition system is optimized and integrated into real applications. Deployment steps include:

Implementation

Here we capture a known and a test face using the webcam, encodes them compares the faces and labels the test image based on whether it matches the known person.

Step 1: Install Required Libraries

Installs the required libraries for face recognition, image processing and visualization.

Python `

!pip install face_recognition opencv-python matplotlib

`

Step 2: Import Required Modules

from IPython.display import display, Javascript from google.colab.output import eval_js import numpy as np import cv2 import base64 import face_recognition

`

Step 3: Define Webcam Image Capture Function

def take_photo(filename='photo.jpg', quality=0.8): js = Javascript(''' async function takePhoto(quality) { const div = document.createElement('div'); const capture = document.createElement('button'); capture.textContent = 'Capture'; div.appendChild(capture);

      const video = document.createElement('video');
      video.style.display = 'block';

      const stream = await navigator.mediaDevices.getUserMedia({video: true});
      document.body.appendChild(div);
      div.appendChild(video);
      video.srcObject = stream;
      await video.play();

      await new Promise((resolve) => capture.onclick = resolve);

      const canvas = document.createElement('canvas');
      canvas.width = video.videoWidth;
      canvas.height = video.videoHeight;
      canvas.getContext('2d').drawImage(video, 0, 0);

      stream.getVideoTracks()[0].stop();
      div.remove();

      return canvas.toDataURL('image/jpeg', quality);
    }
''')

display(js)
data = eval_js(f'takePhoto({quality})')

image_bytes = base64.b64decode(data.split(',')[1])
np_arr = np.frombuffer(image_bytes, np.uint8)
img = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)

cv2.imwrite(filename, img)
return img

`

Step 4: Capture Known Person Face

print("Capture KNOWN face") known_frame = take_photo('known.jpg')

`

Step 5: Generate Encoding for Known Face

known_image = face_recognition.load_image_file('known.jpg') known_encoding = face_recognition.face_encodings( known_image, num_jitters=50, model='large' )[0]

`

Step 6: Capture Test Face

print("Capture TEST face") test_frame = take_photo('test.jpg')

`

Step 7: Detect Faces and Generate Encodings

face_locations = face_recognition.face_locations(test_frame) face_encodings = face_recognition.face_encodings( test_frame, face_locations, num_jitters=23, model='large' )

`

Step 8: Compare Faces and Draw Bounding Boxes

for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings): match = face_recognition.compare_faces([known_encoding], face_encoding)[0]

label = "Recognized" if match else "Unrecognized"
color = (0, 255, 0) if match else (0, 0, 255)

cv2.rectangle(test_frame, (left, top), (right, bottom), color, 2)
cv2.putText(test_frame, label, (left, top - 10),
            cv2.FONT_HERSHEY_SIMPLEX, 0.9, color, 2)

print("Recognized" if match else "Unrecognized")

`

Step 10: Display Final Output Image

import matplotlib.pyplot as plt

plt.imshow(cv2.cvtColor(test_frame, cv2.COLOR_BGR2RGB)) plt.axis('off') plt.show()

`

**Output:

Face_reco1

Output

We can see our model is working fine.

You can download full code from here

Face Recognition vs Face Detection

Aspect Face Detection Face Recognition
**Definition Detects and locates faces in an image or video Identifies or verifies a person using facial features
**Main Goal Find faces Recognize or verify identity
**Output Face location or bounding box Name, ID, or similarity score
**Input Image or video frame Detected face image
**Data Requirement Does not require labeled identities Requires labeled face data
**Common Algorithms Haar Cascades, HOG, CNN detectors FaceNet, ArcFace, DeepFace
**Applications Face filters, autofocus, crowd analysis Phone unlock, attendance, surveillance

Applications

Advantages

Challenges