Commit ·
b5f9145
1
Parent(s): 959730d
face recognition
Browse files- app/Hackathon_setup/face_recognition.py +120 -120
app/Hackathon_setup/face_recognition.py
CHANGED
|
@@ -102,147 +102,147 @@ def get_similarity(img1, img2):
|
|
| 102 |
#4) Perform necessary transformations to the input(detected face using the above function).
|
| 103 |
#5) Along with the siamese, you need the classifier as well, which is to be finetuned with the faces that you are training
|
| 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
|
| 110 |
-
# # clf_path = os.path.join(BASE_DIR, "decision_tree_model.sav")
|
| 111 |
# clf_path = os.path.join(BASE_DIR, "logistic_regression_5.sav")
|
| 112 |
-
# clf = joblib.load(clf_path)
|
| 113 |
-
#
|
| 114 |
# scaler_path = os.path.join(BASE_DIR, "standar_scaler.sav")
|
|
|
|
| 115 |
# scaler = joblib.load(scaler_path)
|
| 116 |
#
|
| 117 |
-
# # 2 Load
|
| 118 |
# myModel = Siamese().to(device)
|
| 119 |
# ckpt_path = os.path.join(BASE_DIR, "siamese_model.t7")
|
| 120 |
# ckpt = torch.load(ckpt_path, map_location=device)
|
| 121 |
# myModel.load_state_dict(ckpt['net_dict'])
|
| 122 |
# myModel.eval()
|
| 123 |
#
|
| 124 |
-
# # 3 Face detection
|
| 125 |
-
# det_img1 = detected_face(img1) #
|
| 126 |
# if det_img1 == 0:
|
| 127 |
-
# # fallback:
|
| 128 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
#
|
| 130 |
-
# # 4 Transform the face
|
| 131 |
-
# img_tensor = trnscm(det_img1).unsqueeze(0)
|
|
|
|
| 132 |
#
|
| 133 |
-
# # 5 Extract embeddings
|
| 134 |
# with torch.no_grad():
|
| 135 |
-
#
|
| 136 |
-
#
|
|
|
|
| 137 |
#
|
| 138 |
-
# #
|
| 139 |
-
#
|
|
|
|
|
|
|
| 140 |
#
|
|
|
|
|
|
|
|
|
|
| 141 |
#
|
| 142 |
-
# #
|
| 143 |
-
#
|
| 144 |
-
#
|
| 145 |
-
#
|
| 146 |
-
#
|
| 147 |
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
#
|
| 150 |
-
# #
|
| 151 |
-
# #
|
| 152 |
-
#
|
| 153 |
-
#
|
| 154 |
-
#
|
| 155 |
-
#
|
| 156 |
-
#
|
| 157 |
-
#
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
def get_face_class(img1):
|
| 161 |
-
"""
|
| 162 |
-
img1: BGR image as numpy array (from cv2) OR path string accepted by detected_face.
|
| 163 |
-
Returns: "Name label_index" or debug info.
|
| 164 |
-
"""
|
| 165 |
-
|
| 166 |
-
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
| 167 |
-
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 168 |
-
|
| 169 |
-
# 1) Load classifier + scaler
|
| 170 |
-
clf_path = os.path.join(BASE_DIR, "logistic_regression_5.sav")
|
| 171 |
-
scaler_path = os.path.join(BASE_DIR, "standar_scaler.sav")
|
| 172 |
-
clf = joblib.load(clf_path)
|
| 173 |
-
scaler = joblib.load(scaler_path)
|
| 174 |
-
|
| 175 |
-
# 2) Load Siamese feature extractor
|
| 176 |
-
myModel = Siamese().to(device)
|
| 177 |
-
ckpt_path = os.path.join(BASE_DIR, "siamese_model.t7")
|
| 178 |
-
ckpt = torch.load(ckpt_path, map_location=device)
|
| 179 |
-
myModel.load_state_dict(ckpt['net_dict'])
|
| 180 |
-
myModel.eval()
|
| 181 |
-
|
| 182 |
-
# 3) Face detection & crop
|
| 183 |
-
det_img1 = detected_face(img1) # your function: should return cropped face (preferably PIL.Image or np.uint8)
|
| 184 |
-
if det_img1 == 0:
|
| 185 |
-
# fallback: convert original to grayscale PIL
|
| 186 |
-
if isinstance(img1, str):
|
| 187 |
-
pil_img = Image.open(img1).convert("L")
|
| 188 |
-
else:
|
| 189 |
-
# img1 assumed BGR numpy (cv2)
|
| 190 |
-
gray = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
|
| 191 |
-
pil_img = Image.fromarray(gray)
|
| 192 |
-
det_img1 = pil_img
|
| 193 |
-
|
| 194 |
-
# Ensure det_img1 is a PIL Image in mode 'L' (single channel). Convert if needed.
|
| 195 |
-
if isinstance(det_img1, np.ndarray):
|
| 196 |
-
# if it's color BGR -> convert to gray
|
| 197 |
-
if det_img1.ndim == 3 and det_img1.shape[2] == 3:
|
| 198 |
-
det_img1 = cv2.cvtColor(det_img1, cv2.COLOR_BGR2GRAY)
|
| 199 |
-
det_img1 = Image.fromarray(det_img1)
|
| 200 |
-
det_img1 = det_img1.convert("L") # enforce single-channel
|
| 201 |
-
|
| 202 |
-
# 4) Transform the face: trnscm must be the exact same transform used when creating embeddings
|
| 203 |
-
img_tensor = trnscm(det_img1).unsqueeze(0) # shape: (1, C, H, W)
|
| 204 |
-
img_tensor = img_tensor.to(device) # <--- IMPORTANT: move to device!
|
| 205 |
-
|
| 206 |
-
# 5) Extract embeddings
|
| 207 |
-
with torch.no_grad():
|
| 208 |
-
embedding_t = myModel.forward_once(img_tensor) # tensor on device
|
| 209 |
-
embedding_t = embedding_t.view(embedding_t.size(0), -1)
|
| 210 |
-
embedding = embedding_t.cpu().numpy() # shape (1, embedding_dim)
|
| 211 |
-
|
| 212 |
-
# Debug prints (uncomment if needed)
|
| 213 |
-
# print("embedding shape:", embedding.shape)
|
| 214 |
-
# print("embedding min/max:", embedding.min(), embedding.max())
|
| 215 |
-
# print("embedding mean/std:", embedding.mean(), embedding.std())
|
| 216 |
-
|
| 217 |
-
# 6) Check for NaNs / inf
|
| 218 |
-
if np.isnan(embedding).any() or np.isinf(embedding).any():
|
| 219 |
-
return "ERROR: embedding contains NaN or inf"
|
| 220 |
-
|
| 221 |
-
# 7) Scale + predict
|
| 222 |
-
try:
|
| 223 |
-
scaled = scaler.transform(embedding) # ensure scaler expects shape (1, D)
|
| 224 |
-
except Exception as e:
|
| 225 |
-
return f"Scaler transform error: {e}"
|
| 226 |
-
|
| 227 |
-
try:
|
| 228 |
-
pred_label = clf.predict(scaled)[0]
|
| 229 |
-
except Exception as e:
|
| 230 |
-
return f"Classifier predict error: {e}"
|
| 231 |
-
|
| 232 |
-
# 8) Optional: probabilities (if classifier supports it)
|
| 233 |
-
confidence = None
|
| 234 |
-
if hasattr(clf, "predict_proba"):
|
| 235 |
-
try:
|
| 236 |
-
probs = clf.predict_proba(scaled)
|
| 237 |
-
confidence = float(probs.max())
|
| 238 |
-
except Exception:
|
| 239 |
-
confidence = None
|
| 240 |
-
|
| 241 |
-
# 9) Map to class names
|
| 242 |
-
class_names = ['Aayush', 'Aditya', 'Vikram'] # replace with your saved names or load from file
|
| 243 |
-
name = class_names[pred_label] if pred_label < len(class_names) else str(pred_label)
|
| 244 |
-
|
| 245 |
-
if confidence is not None:
|
| 246 |
-
return f"{name} {pred_label} (conf={confidence:.3f})"
|
| 247 |
-
else:
|
| 248 |
-
return f"{name} {pred_label}"
|
|
|
|
| 102 |
#4) Perform necessary transformations to the input(detected face using the above function).
|
| 103 |
#5) Along with the siamese, you need the classifier as well, which is to be finetuned with the faces that you are training
|
| 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, "decision_tree_model.sav")
|
| 111 |
+
clf_path = os.path.join(BASE_DIR, "logistic_regression_5.sav")
|
| 112 |
+
clf = joblib.load(clf_path)
|
| 113 |
+
|
| 114 |
+
scaler_path = os.path.join(BASE_DIR, "standar_scaler.sav")
|
| 115 |
+
scaler = joblib.load(scaler_path)
|
| 116 |
+
|
| 117 |
+
# 2 Load the Siamese feature extractor
|
| 118 |
+
myModel = Siamese().to(device)
|
| 119 |
+
ckpt_path = os.path.join(BASE_DIR, "siamese_model.t7")
|
| 120 |
+
ckpt = torch.load(ckpt_path, map_location=device)
|
| 121 |
+
myModel.load_state_dict(ckpt['net_dict'])
|
| 122 |
+
myModel.eval()
|
| 123 |
+
|
| 124 |
+
# 3 Face detection (if available)
|
| 125 |
+
# det_img1 = detected_face(img1) # returns cropped face or 0 if not detected
|
| 126 |
+
# if det_img1 == 0:
|
| 127 |
+
# fallback: use original image
|
| 128 |
+
# det_img1 = Image.fromarray(cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY))
|
| 129 |
+
|
| 130 |
+
# 4 Transform the face
|
| 131 |
+
img_tensor = trnscm(img1).unsqueeze(0)
|
| 132 |
+
|
| 133 |
+
# 5 Extract embeddings
|
| 134 |
+
with torch.no_grad():
|
| 135 |
+
embedding = myModel.forward_once(img_tensor)
|
| 136 |
+
embedding = embedding.view(embedding.size(0), -1).cpu().numpy() # shape (1, embedding_dim)
|
| 137 |
+
|
| 138 |
+
# 6 Predict class using Decision Tree
|
| 139 |
+
pred_label = clf.predict(scaler.transform(embedding))[0]
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
# --- Predict ---
|
| 143 |
+
# scaled_emb = scaler.transform(embedding)
|
| 144 |
+
# probs = clf.predict_proba(scaled_emb)
|
| 145 |
+
# pred_label = np.argmax(probs)
|
| 146 |
+
# confidence = probs[0, pred_label]
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
# 7 Optional: return class name (if available)
|
| 151 |
+
# If you have the dataset available:
|
| 152 |
+
# class_names = finalClassifierDset.classes
|
| 153 |
+
# return class_names[pred_label]
|
| 154 |
+
# class_names = ['Aayush', 'Aditya', 'Vikram']
|
| 155 |
+
# return class_names[pred_label] + " " + str(pred_label)
|
| 156 |
+
class_names = ['Aayush', 'Aditya', 'Vikram']
|
| 157 |
+
return f"{class_names[pred_label]} {pred_label}"
|
| 158 |
+
|
| 159 |
+
|
| 160 |
# def get_face_class(img1):
|
| 161 |
+
# """
|
| 162 |
+
# img1: BGR image as numpy array (from cv2) OR path string accepted by detected_face.
|
| 163 |
+
# Returns: "Name label_index" or debug info.
|
| 164 |
+
# """
|
| 165 |
+
#
|
| 166 |
# device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
| 167 |
# BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 168 |
#
|
| 169 |
+
# # 1) Load classifier + scaler
|
|
|
|
| 170 |
# clf_path = os.path.join(BASE_DIR, "logistic_regression_5.sav")
|
|
|
|
|
|
|
| 171 |
# scaler_path = os.path.join(BASE_DIR, "standar_scaler.sav")
|
| 172 |
+
# clf = joblib.load(clf_path)
|
| 173 |
# scaler = joblib.load(scaler_path)
|
| 174 |
#
|
| 175 |
+
# # 2) Load Siamese feature extractor
|
| 176 |
# myModel = Siamese().to(device)
|
| 177 |
# ckpt_path = os.path.join(BASE_DIR, "siamese_model.t7")
|
| 178 |
# ckpt = torch.load(ckpt_path, map_location=device)
|
| 179 |
# myModel.load_state_dict(ckpt['net_dict'])
|
| 180 |
# myModel.eval()
|
| 181 |
#
|
| 182 |
+
# # 3) Face detection & crop
|
| 183 |
+
# det_img1 = detected_face(img1) # your function: should return cropped face (preferably PIL.Image or np.uint8)
|
| 184 |
# if det_img1 == 0:
|
| 185 |
+
# # fallback: convert original to grayscale PIL
|
| 186 |
+
# if isinstance(img1, str):
|
| 187 |
+
# pil_img = Image.open(img1).convert("L")
|
| 188 |
+
# else:
|
| 189 |
+
# # img1 assumed BGR numpy (cv2)
|
| 190 |
+
# gray = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
|
| 191 |
+
# pil_img = Image.fromarray(gray)
|
| 192 |
+
# det_img1 = pil_img
|
| 193 |
+
#
|
| 194 |
+
# # Ensure det_img1 is a PIL Image in mode 'L' (single channel). Convert if needed.
|
| 195 |
+
# if isinstance(det_img1, np.ndarray):
|
| 196 |
+
# # if it's color BGR -> convert to gray
|
| 197 |
+
# if det_img1.ndim == 3 and det_img1.shape[2] == 3:
|
| 198 |
+
# det_img1 = cv2.cvtColor(det_img1, cv2.COLOR_BGR2GRAY)
|
| 199 |
+
# det_img1 = Image.fromarray(det_img1)
|
| 200 |
+
# det_img1 = det_img1.convert("L") # enforce single-channel
|
| 201 |
#
|
| 202 |
+
# # 4) Transform the face: trnscm must be the exact same transform used when creating embeddings
|
| 203 |
+
# img_tensor = trnscm(det_img1).unsqueeze(0) # shape: (1, C, H, W)
|
| 204 |
+
# img_tensor = img_tensor.to(device) # <--- IMPORTANT: move to device!
|
| 205 |
#
|
| 206 |
+
# # 5) Extract embeddings
|
| 207 |
# with torch.no_grad():
|
| 208 |
+
# embedding_t = myModel.forward_once(img_tensor) # tensor on device
|
| 209 |
+
# embedding_t = embedding_t.view(embedding_t.size(0), -1)
|
| 210 |
+
# embedding = embedding_t.cpu().numpy() # shape (1, embedding_dim)
|
| 211 |
#
|
| 212 |
+
# # Debug prints (uncomment if needed)
|
| 213 |
+
# # print("embedding shape:", embedding.shape)
|
| 214 |
+
# # print("embedding min/max:", embedding.min(), embedding.max())
|
| 215 |
+
# # print("embedding mean/std:", embedding.mean(), embedding.std())
|
| 216 |
#
|
| 217 |
+
# # 6) Check for NaNs / inf
|
| 218 |
+
# if np.isnan(embedding).any() or np.isinf(embedding).any():
|
| 219 |
+
# return "ERROR: embedding contains NaN or inf"
|
| 220 |
#
|
| 221 |
+
# # 7) Scale + predict
|
| 222 |
+
# try:
|
| 223 |
+
# scaled = scaler.transform(embedding) # ensure scaler expects shape (1, D)
|
| 224 |
+
# except Exception as e:
|
| 225 |
+
# return f"Scaler transform error: {e}"
|
| 226 |
#
|
| 227 |
+
# try:
|
| 228 |
+
# pred_label = clf.predict(scaled)[0]
|
| 229 |
+
# except Exception as e:
|
| 230 |
+
# return f"Classifier predict error: {e}"
|
| 231 |
#
|
| 232 |
+
# # 8) Optional: probabilities (if classifier supports it)
|
| 233 |
+
# confidence = None
|
| 234 |
+
# if hasattr(clf, "predict_proba"):
|
| 235 |
+
# try:
|
| 236 |
+
# probs = clf.predict_proba(scaled)
|
| 237 |
+
# confidence = float(probs.max())
|
| 238 |
+
# except Exception:
|
| 239 |
+
# confidence = None
|
| 240 |
#
|
| 241 |
+
# # 9) Map to class names
|
| 242 |
+
# class_names = ['Aayush', 'Aditya', 'Vikram'] # replace with your saved names or load from file
|
| 243 |
+
# name = class_names[pred_label] if pred_label < len(class_names) else str(pred_label)
|
| 244 |
+
#
|
| 245 |
+
# if confidence is not None:
|
| 246 |
+
# return f"{name} {pred_label} (conf={confidence:.3f})"
|
| 247 |
+
# else:
|
| 248 |
+
# return f"{name} {pred_label}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|