vrvundyala commited on
Commit
0b21ae0
·
1 Parent(s): 77a7e4e

face recognition

Browse files
app/Hackathon_setup/decision_tree_model.sav ADDED
Binary file (1.87 kB). View file
 
app/Hackathon_setup/face_recognition.py CHANGED
@@ -104,11 +104,41 @@ def get_similarity(img1, img2):
104
  ##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
105
  def get_face_class(img1):
106
  device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
107
-
108
- det_img1 = detected_face(img1)
109
- if(det_img1 == 0):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
  det_img1 = Image.fromarray(cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY))
111
- ##YOUR CODE HERE, return face class here
112
- ##Hint: you need a classifier finetuned for your classes, it takes o/p of siamese as i/p to it
113
- ##Better Hint: Siamese experiment is covered in one of the labs
114
- return "YET TO BE CODED"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
  ##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
105
  def get_face_class(img1):
106
  device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
107
+ BASE_DIR = os.path.dirname(os.path.abspath(__file__))
108
+
109
+ # 1 Load the Decision Tree classifier
110
+ clf_path = os.path.join(BASE_DIR, "siamese_decision_tree.pkl")
111
+ clf = joblib.load(clf_path)
112
+
113
+ # 2 Load the Siamese feature extractor
114
+ myModel = Siamese().to(device)
115
+ ckpt_path = os.path.join(BASE_DIR, "siamese_model.t7")
116
+ ckpt = torch.load(ckpt_path, map_location=device)
117
+ myModel.load_state_dict(ckpt['net_dict'])
118
+ myModel.eval()
119
+
120
+ # 3 Face detection (if available)
121
+ det_img1 = detected_face(img1) # returns cropped face or 0 if not detected
122
+ if det_img1 == 0:
123
+ # fallback: use original image
124
  det_img1 = Image.fromarray(cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY))
125
+ else:
126
+ det_img1 = Image.fromarray(cv2.cvtColor(det_img1, cv2.COLOR_BGR2GRAY))
127
+
128
+ # 4 Transform the face
129
+ img_tensor = trnscm(det_img1).unsqueeze(0)
130
+
131
+ # 5 Extract embeddings
132
+ with torch.no_grad():
133
+ embedding = myModel.forward_once(img_tensor)
134
+ embedding = embedding.view(embedding.size(0), -1).cpu().numpy() # shape (1, embedding_dim)
135
+
136
+ # 6 Predict class using Decision Tree
137
+ pred_label = clf.predict(embedding)[0]
138
+
139
+ # 7 Optional: return class name (if available)
140
+ # If you have the dataset available:
141
+ # class_names = finalClassifierDset.classes
142
+ # return class_names[pred_label]
143
+ class_names = ['Vikram', 'Vikram', 'Vikram']
144
+ return pred_label