akuratikaustiki/hackathon4a
0
1import numpy as np2import cv23from matplotlib import pyplot as plt4import torch5# 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.6from .face_recognition_model import *7from PIL import Image8import base649import io10import os11import joblib12import pickle13 14# Add more imports if required15import torch.nn.functional as F16from torch.autograd import Variable17 18 19 20###########################################################################################################################################21# Caution: Don't change any of the filenames, function names and definitions #22# Always use the current_path + file_name for refering any files, without it we cannot access files on the server # 23###########################################################################################################################################24 25# Current_path stores absolute path of the file from where it runs. 26current_path = os.path.dirname(os.path.abspath(__file__))27 28 29# trnscm = transforms.Compose([transforms.Grayscale(num_output_channels = 1), transforms.Resize((100,100)), transforms.ToTensor()]))30 31 32 33#1) The below function is used to detect faces in the given image.34#2) It returns only one image which has maximum area out of all the detected faces in the photo.35#3) If no face is detected,then it returns zero(0).36 37def detected_face(image):38 eye_haar = current_path + '/haarcascade_eye.xml'39 face_haar = current_path + '/haarcascade_frontalface_default.xml'40 face_cascade = cv2.CascadeClassifier(face_haar)41 eye_cascade = cv2.CascadeClassifier(eye_haar)42 gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)43 faces = face_cascade.detectMultiScale(gray, 1.3, 5)44 face_areas=[]45 images = []46 required_image=047 for i, (x,y,w,h) in enumerate(faces):48 face_cropped = gray[y:y+h, x:x+w]49 face_areas.append(w*h)50 images.append(face_cropped)51 required_image = images[np.argmax(face_areas)]52 required_image = Image.fromarray(required_image)53 return required_image54 55 56#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.57#2) The image is passed to the function in base64 encoding, Code for decoding the image is provided within the function.58#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.59#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.60#5) For loading your model use the current_path+'your model file name', anyhow detailed example is given in comments to the function 61#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 function62def get_similarity(img1, img2):63 device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")64 65 det_img1 = detected_face(img1)66 det_img2 = detected_face(img2)67 if(det_img1 == 0 or det_img2 == 0):68 det_img1 = Image.fromarray(cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY))69 det_img2 = Image.fromarray(cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY))70 face1 = trnscm(det_img1).unsqueeze(0)71 face2 = trnscm(det_img2).unsqueeze(0)72 ##########################################################################################73 ##Example for loading a model using weight state dictionary: ##74 ## feature_net = light_cnn() #Example Network ##75 ## model = torch.load(current_path + '/siamese_model.t7', map_location=device) ##76 ## feature_net.load_state_dict(model['net_dict']) ##77 ## ##78 ##current_path + '/<network_definition>' is path of the saved model if present in ##79 ##the same path as this file, we recommend to put in the same directory ##80 ##########################################################################################81 ##########################################################################################82 83 # YOUR CODE HERE, load the model84 feature_net = Siamese()85 model=torch.load(current_path + '/siamese_model.t7', map_location=device)86 feature_net.load_state_dict(model['net_dict'])87 feature_net.eval()88 with torch.no_grad():89 output1,output2 = feature_net(face1,face2)90 # YOUR CODE HERE, return similarity measure using your model91 # output1,output2 = feature_net(Variable(face1).to(device),Variable(face2).to(device))92 93 euclidean_distance = F.pairwise_distance(output1, output2, keepdim = True)94 euclidean_distance = euclidean_distance.item()95 96 return euclidean_distance97 98#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"99#2) The image is passed to the function in base64 encoding, Code to decode the image provided within the function100#3) Define an object to your network here in the function and load the weight from the trained network, set it in evaluation mode101#4) Perform necessary transformations to the input(detected face using the above function).102#5) Along with the siamese, you need the classifier as well, which is to be finetuned with the faces that you are training103##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 function104def get_face_class(img1):105 device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")106 107 det_img1 = detected_face(img1)108 if(det_img1 == 0):109 det_img1 = Image.fromarray(cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY))110 ##YOUR CODE HERE, return face class here111 ##Hint: you need a classifier finetuned for your classes, it takes o/p of siamese as i/p to it112 ##Better Hint: Siamese experiment is covered in one of the labs113 image_recognition_transformation=transforms.Compose([transforms.Grayscale(num_output_channels = 1), transforms.Resize((100,100)), transforms.ToTensor()])114 face = image_recognition_transformation(det_img1).unsqueeze(0)115 feature_net = Siamese()116 model=torch.load(current_path + '/siamese_model.t7', map_location=device)117 feature_net.load_state_dict(model['net_dict'])118 119 feature_net.eval()120 with torch.no_grad():121 siamese_representation = feature_net.forward_once(face)122 siamese_representation_numpy = siamese_representation.detach().cpu().numpy()123 124 125 rf_model=joblib.load(current_path +'/random_forest_model.joblib')126 predicted_label = rf_model.predict(siamese_representation_numpy)127 print(predicted_label)128 # person_labels=["Aparna","Kaustiki","Gouthami","Venkatesh"]129 predicted_person=person_labels[predicted_label.item()]130 print(predicted_person)131 132 return predicted_person133 