בינה מאלכותית RB14-10 : זיהוי פנים של תמונה אדם

בינה מאלכותית RB14-10 : זיהוי פנים של תמונה אדם

 

 

 

 


 

 

 


 

 

 


 

 

 

Face Comparison with InsightFace

 :embedding vector

mbedding Vector?

  • An embedding is just a list of numbers.

  • These numbers describe the important features of a face (shape of nose, distance between eyes, jawline, etc.).

  • The list is usually long (for InsightFace, often 512 numbers).

  • Together, those numbers create a unique “fingerprint” of the face.


Why is it useful?

  • The raw image (like 100×100 pixels = 10,000 numbers) is too big to compare directly.

  • The embedding reduces this to 512 numbers that keep only the most important face features.

  • Two embeddings from the same person will look very similar.

  • Two embeddings from different people will look very different.


Simple Example

Think of it like describing a person in short words:

  • Instead of writing a full biography, you say:

    • Height: 1.8

    • Hair: 0.3

    • Skin tone: 0.7

    • Nose: 0.5

    • Smile: 0.9

This short list is the embedding vector.
It’s not the full photo, but enough to recognize who it is.

This example shows how to use InsightFace to compare two face images and decide if they belong to the same person. The program uses cosine similarity between face embeddings.

Explanation

  • 512 numbers total → this is the embedding vector.

  • Each number captures some hidden feature of the face.

  • The exact meaning of each number is not human-readable (like “eye width” or “nose length”), but together they form the face fingerprint.

When comparing two faces, InsightFace checks how close these 512-number fingerprints are, usually using cosine similarity.


Step 1 – Imports and setup

import sys
print(sys.executable)

import cv2
import numpy as np
from insightface.app import FaceAnalysis

  • sys.executable prints the Python path in use.

  • cv2 is OpenCV for handling images.

  • numpy is used for math.

  • FaceAnalysis is the InsightFace tool for detection and embeddings.


Step 2 – Load the InsightFace model

app = FaceAnalysis(name="buffalo_l")
app.prepare(ctx_id=-1, det_size=(640, 640))
  • buffalo_l is a strong pre-trained face recognition model.

  • ctx_id=-1 forces CPU usage.

  • det_size=(640,640) sets the detection image size.


Step 3 – Load two images

img1 = cv2.imread(r"d:\temp\trump1.jpeg")
img2 = cv2.imread(r"d:\temp\trump1.jpeg")

if img1 is None or img2 is None:
raise FileNotFoundError("Could not open one of the images.")

  • Loads the two face images from disk.

  • Raises an error if any image cannot be read.


Step 4 – Detect and encode faces

faces1 = app.get(img1)
faces2 = app.get(img2)

if len(faces1) == 0 or len(faces2) == 0:
raise ValueError("No face detected in one of the images.")

embedding1 = faces1[0].embedding
embedding2 = faces2[0].embedding

print("Embedding shape:", embedding1.shape)

  • Detects faces in both images.

  • Each face object contains bounding box, landmarks, and a 512-dimensional embedding.

  • Here we take the first detected face.


Step 5 – Cosine similarity function

def cosine_similarity(a, b):
return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))

similarity = cosine_similarity(embedding1, embedding2)
print(f"Cosine similarity: {similarity:.4f}")

  • Cosine similarity measures the angle between two vectors.

  • Value close to 1.0 means the faces are very similar.

  • Value near 0.0 means no relation.


Step 6 – Decision

threshold = 0.35
if similarity > threshold:
print("Result: SAME person")
else:
print("Result: DIFFERENT persons")
  • If similarity is above the threshold, the images are considered the same person.

  • Threshold can be tuned (typical range: 0.3–0.5).


How it works

  1. InsightFace detects and aligns the face.

  2. It encodes the face into a vector of 512 numbers (the embedding).

  3. Cosine similarity compares how close two vectors are.

  4. If the vectors are close enough, the faces are the same person.

 


 

compares Trump1, Trump2, and Obama embeddings, and rewrite it with step-by-step remarks so even someone new can follow

 

 

 

 

כתיבת תגובה