Spaces:
Sleeping
Sleeping
Commit
·
25e986d
1
Parent(s):
1bf6e79
changes
Browse files
app/Hackathon_setup/face_recognition.py
CHANGED
|
@@ -95,7 +95,7 @@ def get_similarity(img1, img2):
|
|
| 95 |
siamese_net.load_state_dict(torch.load(SIAMESE_MODEL_PATH, map_location=device))
|
| 96 |
siamese_net.eval()
|
| 97 |
except Exception as e:
|
| 98 |
-
print(f"Error loading Siamese Model: {e}")
|
| 99 |
return -1 # Return error code
|
| 100 |
|
| 101 |
# 2. Get Features (Embeddings)
|
|
@@ -141,7 +141,7 @@ def get_face_class(img1):
|
|
| 141 |
siamese_net.load_state_dict(torch.load(SIAMESE_MODEL_PATH, map_location=device))
|
| 142 |
siamese_net.eval()
|
| 143 |
except Exception as e:
|
| 144 |
-
return f"Error loading Siamese Model: {e}"
|
| 145 |
|
| 146 |
# 2. Extract Embedding
|
| 147 |
with torch.no_grad():
|
|
|
|
| 95 |
siamese_net.load_state_dict(torch.load(SIAMESE_MODEL_PATH, map_location=device))
|
| 96 |
siamese_net.eval()
|
| 97 |
except Exception as e:
|
| 98 |
+
print(f"Error loading Siamese Model get_similarity: {e}")
|
| 99 |
return -1 # Return error code
|
| 100 |
|
| 101 |
# 2. Get Features (Embeddings)
|
|
|
|
| 141 |
siamese_net.load_state_dict(torch.load(SIAMESE_MODEL_PATH, map_location=device))
|
| 142 |
siamese_net.eval()
|
| 143 |
except Exception as e:
|
| 144 |
+
return f"Error loading Siamese Model get_face_class: {e}"
|
| 145 |
|
| 146 |
# 2. Extract Embedding
|
| 147 |
with torch.no_grad():
|
app/Hackathon_setup/face_recognition_model.py
CHANGED
|
@@ -81,4 +81,49 @@ def get_similarity(img1, img2):
|
|
| 81 |
# Option B: Cosine Similarity (Higher is better) -> Recommended
|
| 82 |
similarity = cosine_similarity(embed1, embed2)[0][0]
|
| 83 |
|
| 84 |
-
return float(similarity)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
# Option B: Cosine Similarity (Higher is better) -> Recommended
|
| 82 |
similarity = cosine_similarity(embed1, embed2)[0][0]
|
| 83 |
|
| 84 |
+
return float(similarity)
|
| 85 |
+
|
| 86 |
+
def get_face_class(img1):
|
| 87 |
+
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
| 88 |
+
|
| 89 |
+
det_img1 = detected_face(img1)
|
| 90 |
+
if(det_img1 == 0):
|
| 91 |
+
det_img1 = Image.fromarray(cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY))
|
| 92 |
+
##YOUR CODE HERE, return face class here
|
| 93 |
+
##Hint: you need a classifier finetuned for your classes, it takes o/p of siamese as i/p to it
|
| 94 |
+
##Better Hint: Siamese experiment is covered in one of the labs
|
| 95 |
+
face1_tensor = trnscm(det_img1).unsqueeze(0).to(device)
|
| 96 |
+
|
| 97 |
+
# 1. Load Siamese Network (Feature Extractor)
|
| 98 |
+
try:
|
| 99 |
+
siamese_net = SiameseNetwork().to(device)
|
| 100 |
+
siamese_net.load_state_dict(torch.load(SIAMESE_MODEL_PATH, map_location=device))
|
| 101 |
+
siamese_net.eval()
|
| 102 |
+
except Exception as e:
|
| 103 |
+
return f"Error loading Siamese Model get_face_class: {e}"
|
| 104 |
+
|
| 105 |
+
# 2. Extract Embedding
|
| 106 |
+
with torch.no_grad():
|
| 107 |
+
embedding_np = siamese_net.forward_once(face1_tensor).cpu().numpy()
|
| 108 |
+
|
| 109 |
+
# 3. Load Sklearn Scaler and Classifier (Joblib)
|
| 110 |
+
try:
|
| 111 |
+
knn_classifier = joblib.load(KNN_CLASSIFIER_PATH)
|
| 112 |
+
scaler = joblib.load(SCALER_PATH)
|
| 113 |
+
except Exception as e:
|
| 114 |
+
return f"Error loading Sklearn models: {e}"
|
| 115 |
+
|
| 116 |
+
# 4. Preprocess Embedding and Predict
|
| 117 |
+
# The embedding must be reshaped to (1, N_features) for the scaler
|
| 118 |
+
embedding_scaled = scaler.transform(embedding_np.reshape(1, -1))
|
| 119 |
+
|
| 120 |
+
# Perform prediction (returns a NumPy array with the predicted label index)
|
| 121 |
+
predicted_label_index = knn_classifier.predict(embedding_scaled)[0]
|
| 122 |
+
|
| 123 |
+
# 5. Map index to Class Name
|
| 124 |
+
if predicted_label_index < len(CLASS_NAMES):
|
| 125 |
+
predicted_class_name = CLASS_NAMES[predicted_label_index]
|
| 126 |
+
else:
|
| 127 |
+
predicted_class_name = "UNKNOWN_CLASS"
|
| 128 |
+
|
| 129 |
+
return predicted_class_name
|