Hackthon4 / app /Hackathon_setup /face_recognition.py
PavaniYerra's picture
changes
25e986d
import numpy as np
import cv2
from matplotlib import pyplot as plt
import torch
# In the below line,remove '.' while working on your local system. However Make sure that '.' is present before face_recognition_model while uploading to the server, Do not remove it.
from .face_recognition_model import *
from PIL import Image
import base64
import io
import os
import joblib
import pickle
# Add more imports if required
###########################################################################################################################################
# Caution: Don't change any of the filenames, function names and definitions #
# Always use the current_path + file_name for refering any files, without it we cannot access files on the server #
###########################################################################################################################################
# Current_path stores absolute path of the file from where it runs.
current_path = os.path.dirname(os.path.abspath(__file__))
# --- GLOBAL SETUP: Must match your training transforms ---
# Define the transformation pipeline for inference
trnscm = transforms.Compose([
transforms.Grayscale(num_output_channels=1),
transforms.Resize((100, 100)),
transforms.ToTensor()
])
CLASS_NAMES = ['Person0', 'Person1', 'Person2', 'Person3', 'Person4'] # ADJUST THIS!
# --- Model Filenames ---
SIAMESE_MODEL_PATH = current_path + '/siamese_model.t7'
KNN_CLASSIFIER_PATH = current_path + '/decision_tree_model.sav'
SCALER_PATH = current_path + '/face_recognition_scaler.sav'
#1) The below function is used to detect faces in the given image.
#2) It returns only one image which has maximum area out of all the detected faces in the photo.
#3) If no face is detected,then it returns zero(0).
def detected_face(image):
eye_haar = current_path + '/haarcascade_eye.xml'
face_haar = current_path + '/haarcascade_frontalface_default.xml'
face_cascade = cv2.CascadeClassifier(face_haar)
eye_cascade = cv2.CascadeClassifier(eye_haar)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
face_areas=[]
images = []
required_image=0
for i, (x,y,w,h) in enumerate(faces):
face_cropped = gray[y:y+h, x:x+w]
face_areas.append(w*h)
images.append(face_cropped)
required_image = images[np.argmax(face_areas)]
required_image = Image.fromarray(required_image)
return required_image
#1) Images captured from mobile is passed as parameter to the below function in the API call. It returns the similarity measure between given images.
#2) The image is passed to the function in base64 encoding, Code for decoding the image is provided within the function.
#3) Define an object to your siamese network here in the function and load the weight from the trained network, set it in evaluation mode.
#4) Get the features for both the faces from the network and return the similarity measure, Euclidean,cosine etc can be it. But choose the Relevant measure.
#5) For loading your model use the current_path+'your model file name', anyhow detailed example is given in comments to the function
#Caution: Don't change the definition or function name; for loading the model use the current_path for path example is given in comments to the function
def get_similarity(img1, img2):
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
det_img1 = detected_face(img1)
det_img2 = detected_face(img2)
if(det_img1 == 0 or det_img2 == 0):
det_img1 = Image.fromarray(cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY))
det_img2 = Image.fromarray(cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY))
face1 = trnscm(det_img1).unsqueeze(0)
face2 = trnscm(det_img2).unsqueeze(0)
##########################################################################################
##Example for loading a model using weight state dictionary: ##
## feature_net = light_cnn() #Example Network ##
## model = torch.load(current_path + '/siamese_model.t7', map_location=device) ##
## feature_net.load_state_dict(model['net_dict']) ##
## ##
##current_path + '/<network_definition>' is path of the saved model if present in ##
##the same path as this file, we recommend to put in the same directory ##
##########################################################################################
##########################################################################################
# YOUR CODE HERE, load the model
# YOUR CODE HERE, return similarity measure using your model
# 1. Initialize and Load Siamese Network
try:
# Assuming your Siamese Network class is named 'SiameseNetwork'
siamese_net = SiameseNetwork().to(device)
siamese_net.load_state_dict(torch.load(SIAMESE_MODEL_PATH, map_location=device))
siamese_net.eval()
except Exception as e:
print(f"Error loading Siamese Model get_similarity: {e}")
return -1 # Return error code
# 2. Get Features (Embeddings)
with torch.no_grad():
# Get the feature vector from one tower/forward_once method
# Ensure your SiameseNetwork class has a forward_once or get_embedding method
embed1 = siamese_net.forward_once(face1).cpu().numpy()
embed2 = siamese_net.forward_once(face2).cpu().numpy()
# 3. Calculate Similarity Measure
# The Euclidean distance is the fundamental metric used by the Triplet/Contrastive loss.
# We return the NEGATIVE Euclidean distance or COSINE similarity, as *higher* value usually means *more* similar.
# Option A: Euclidean Distance (Lower is better) -> return NEGATIVE distance for API expectation
# distance = euclidean_distances(embed1, embed2)[0][0]
# similarity = -distance
# Option B: Cosine Similarity (Higher is better) -> Recommended
similarity = cosine_similarity(embed1, embed2)[0][0]
return float(similarity)
#1) Image captured from mobile is passed as parameter to this function in the API call, It returns the face class in the string form ex: "Person1"
#2) The image is passed to the function in base64 encoding, Code to decode the image provided within the function
#3) Define an object to your network here in the function and load the weight from the trained network, set it in evaluation mode
#4) Perform necessary transformations to the input(detected face using the above function).
#5) Along with the siamese, you need the classifier as well, which is to be finetuned with the faces that you are training
##Caution: Don't change the definition or function name; for loading the model use the current_path for path example is given in comments to the function
def get_face_class(img1):
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
det_img1 = detected_face(img1)
if(det_img1 == 0):
det_img1 = Image.fromarray(cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY))
##YOUR CODE HERE, return face class here
##Hint: you need a classifier finetuned for your classes, it takes o/p of siamese as i/p to it
##Better Hint: Siamese experiment is covered in one of the labs
face1_tensor = trnscm(det_img1).unsqueeze(0).to(device)
# 1. Load Siamese Network (Feature Extractor)
try:
siamese_net = SiameseNetwork().to(device)
siamese_net.load_state_dict(torch.load(SIAMESE_MODEL_PATH, map_location=device))
siamese_net.eval()
except Exception as e:
return f"Error loading Siamese Model get_face_class: {e}"
# 2. Extract Embedding
with torch.no_grad():
embedding_np = siamese_net.forward_once(face1_tensor).cpu().numpy()
# 3. Load Sklearn Scaler and Classifier (Joblib)
try:
knn_classifier = joblib.load(KNN_CLASSIFIER_PATH)
scaler = joblib.load(SCALER_PATH)
except Exception as e:
return f"Error loading Sklearn models: {e}"
# 4. Preprocess Embedding and Predict
# The embedding must be reshaped to (1, N_features) for the scaler
embedding_scaled = scaler.transform(embedding_np.reshape(1, -1))
# Perform prediction (returns a NumPy array with the predicted label index)
predicted_label_index = knn_classifier.predict(embedding_scaled)[0]
# 5. Map index to Class Name
if predicted_label_index < len(CLASS_NAMES):
predicted_class_name = CLASS_NAMES[predicted_label_index]
else:
predicted_class_name = "UNKNOWN_CLASS"
return predicted_class_name