Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kokilarathan siva01 addition #1502

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions face_recognition/Face_recognition_Updated.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import numpy as np
import cv2

# Load the image
img = cv2.imread('test_image.jpg')

# Convert the image to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Load the pre-trained face detection classifier
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

# Detect faces in the image
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5)

# Load the pre-trained face encoding model
encoding_model = MyFaceEncodingModel()

# Load the pre-stored face encodings and corresponding names
encodings = np.load('face_encodings.npy')
names = np.load('face_names.npy')

# Encode all faces in the input image using the face encoding model
encodings_img = []
for (x, y, w, h) in faces:
face_gray = gray[y:y+h, x:x+w]
face_resized = cv2.resize(face_gray, encoding_model.input_shape[:2])
encoding = encoding_model.encode(face_resized)
encodings_img.append(encoding)
encodings_img = np.array(encodings_img)

# Compute the Euclidean distance between the encodings of all detected faces and all stored encodings
distances = np.linalg.norm(encodings - encodings_img[:, np.newaxis], axis=-1)

# Find the closest matching encoding for each detected face
min_distance_indices = np.argmin(distances, axis=0)
min_distances = distances[min_distance_indices, np.arange(len(min_distance_indices))]

# Print the names corresponding to the closest matching encodings
for i, distance in enumerate(min_distances):
if distance < 0.6:
print(names[min_distance_indices[i]])
else:
print("Unknown")