Spaces:
Sleeping
Sleeping
updated with debug and error log msgs
Browse files
app/Hackathon_setup/face_recognition.py
CHANGED
|
@@ -108,59 +108,85 @@ def get_similarity(img1, img2):
|
|
| 108 |
##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
|
| 109 |
def get_face_class(img1):
|
| 110 |
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
|
|
|
| 111 |
|
| 112 |
det_img1 = detected_face(img1)
|
| 113 |
if(det_img1 is None):
|
| 114 |
det_img1 = Image.fromarray(cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY))
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
try:
|
| 117 |
# Load the Siamese model for feature extraction
|
| 118 |
model = SiameseNetwork().to(device)
|
| 119 |
model_path = current_path + '/siamese_model.t7'
|
|
|
|
|
|
|
| 120 |
|
| 121 |
if os.path.exists(model_path):
|
| 122 |
ckpt = torch.load(model_path, map_location=device)
|
| 123 |
model.load_state_dict(ckpt['net_dict'])
|
| 124 |
model.eval()
|
|
|
|
| 125 |
|
| 126 |
# Preprocess the image
|
| 127 |
face_tensor = trnscm(det_img1).unsqueeze(0).to(device)
|
|
|
|
| 128 |
|
| 129 |
# Extract features using Siamese network
|
| 130 |
with torch.no_grad():
|
| 131 |
features = model.forward_once(face_tensor)
|
| 132 |
features_np = features.cpu().numpy()
|
|
|
|
| 133 |
|
| 134 |
# Load the classifier and scaler
|
| 135 |
classifier_path = current_path + '/face_recognition_classifier.pkl'
|
| 136 |
scaler_path = current_path + '/face_recognition_scaler.pkl'
|
| 137 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
if os.path.exists(classifier_path):
|
| 139 |
classifier = joblib.load(classifier_path)
|
|
|
|
| 140 |
|
| 141 |
# Load the scaler used during training
|
| 142 |
if os.path.exists(scaler_path):
|
| 143 |
scaler = joblib.load(scaler_path)
|
| 144 |
# Scale the features before prediction (crucial for K-NN)
|
| 145 |
features_scaled = scaler.transform(features_np)
|
| 146 |
-
print("Using saved StandardScaler for feature scaling")
|
| 147 |
else:
|
| 148 |
-
print("Warning: Scaler not found. Creating StandardScaler and fitting on current features.")
|
| 149 |
-
print("This is a temporary fix - you should train and save the scaler properly.")
|
| 150 |
# Temporary fix: create and fit scaler on current features
|
| 151 |
# Note: This is not ideal but will work for now
|
| 152 |
scaler = StandardScaler()
|
| 153 |
features_scaled = scaler.fit_transform(features_np)
|
| 154 |
|
| 155 |
predicted_class_idx = classifier.predict(features_scaled)[0]
|
| 156 |
-
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
else:
|
| 159 |
-
print(f"Classifier file not found: {classifier_path}")
|
| 160 |
return "Unknown"
|
| 161 |
else:
|
| 162 |
-
print(f"Model file not found: {model_path}")
|
| 163 |
return "Unknown"
|
| 164 |
except Exception as e:
|
| 165 |
-
print(f"
|
|
|
|
|
|
|
| 166 |
return "Unknown"
|
|
|
|
| 108 |
##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
|
| 109 |
def get_face_class(img1):
|
| 110 |
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
| 111 |
+
print(f"[DEBUG] Using device: {device}")
|
| 112 |
|
| 113 |
det_img1 = detected_face(img1)
|
| 114 |
if(det_img1 is None):
|
| 115 |
det_img1 = Image.fromarray(cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY))
|
| 116 |
+
print("[DEBUG] No face detected, using full image")
|
| 117 |
+
else:
|
| 118 |
+
print("[DEBUG] Face detected successfully")
|
| 119 |
|
| 120 |
try:
|
| 121 |
# Load the Siamese model for feature extraction
|
| 122 |
model = SiameseNetwork().to(device)
|
| 123 |
model_path = current_path + '/siamese_model.t7'
|
| 124 |
+
print(f"[DEBUG] Looking for model at: {model_path}")
|
| 125 |
+
print(f"[DEBUG] Model file exists: {os.path.exists(model_path)}")
|
| 126 |
|
| 127 |
if os.path.exists(model_path):
|
| 128 |
ckpt = torch.load(model_path, map_location=device)
|
| 129 |
model.load_state_dict(ckpt['net_dict'])
|
| 130 |
model.eval()
|
| 131 |
+
print("[DEBUG] Siamese model loaded successfully")
|
| 132 |
|
| 133 |
# Preprocess the image
|
| 134 |
face_tensor = trnscm(det_img1).unsqueeze(0).to(device)
|
| 135 |
+
print(f"[DEBUG] Face tensor shape: {face_tensor.shape}")
|
| 136 |
|
| 137 |
# Extract features using Siamese network
|
| 138 |
with torch.no_grad():
|
| 139 |
features = model.forward_once(face_tensor)
|
| 140 |
features_np = features.cpu().numpy()
|
| 141 |
+
print(f"[DEBUG] Extracted features shape: {features_np.shape}")
|
| 142 |
|
| 143 |
# Load the classifier and scaler
|
| 144 |
classifier_path = current_path + '/face_recognition_classifier.pkl'
|
| 145 |
scaler_path = current_path + '/face_recognition_scaler.pkl'
|
| 146 |
|
| 147 |
+
print(f"[DEBUG] Looking for classifier at: {classifier_path}")
|
| 148 |
+
print(f"[DEBUG] Classifier file exists: {os.path.exists(classifier_path)}")
|
| 149 |
+
print(f"[DEBUG] Looking for scaler at: {scaler_path}")
|
| 150 |
+
print(f"[DEBUG] Scaler file exists: {os.path.exists(scaler_path)}")
|
| 151 |
+
|
| 152 |
if os.path.exists(classifier_path):
|
| 153 |
classifier = joblib.load(classifier_path)
|
| 154 |
+
print("[DEBUG] Classifier loaded successfully")
|
| 155 |
|
| 156 |
# Load the scaler used during training
|
| 157 |
if os.path.exists(scaler_path):
|
| 158 |
scaler = joblib.load(scaler_path)
|
| 159 |
# Scale the features before prediction (crucial for K-NN)
|
| 160 |
features_scaled = scaler.transform(features_np)
|
| 161 |
+
print("[DEBUG] Using saved StandardScaler for feature scaling")
|
| 162 |
else:
|
| 163 |
+
print("[DEBUG] Warning: Scaler not found. Creating StandardScaler and fitting on current features.")
|
| 164 |
+
print("[DEBUG] This is a temporary fix - you should train and save the scaler properly.")
|
| 165 |
# Temporary fix: create and fit scaler on current features
|
| 166 |
# Note: This is not ideal but will work for now
|
| 167 |
scaler = StandardScaler()
|
| 168 |
features_scaled = scaler.fit_transform(features_np)
|
| 169 |
|
| 170 |
predicted_class_idx = classifier.predict(features_scaled)[0]
|
| 171 |
+
print(f"[DEBUG] Predicted class index: {predicted_class_idx}")
|
| 172 |
+
print(f"[DEBUG] Available classes: {classes}")
|
| 173 |
+
print(f"[DEBUG] Number of classes: {len(classes)}")
|
| 174 |
+
|
| 175 |
+
if predicted_class_idx < len(classes):
|
| 176 |
+
predicted_class = classes[predicted_class_idx]
|
| 177 |
+
print(f"[DEBUG] Predicted class: {predicted_class}")
|
| 178 |
+
return predicted_class
|
| 179 |
+
else:
|
| 180 |
+
print(f"[ERROR] Class index {predicted_class_idx} out of bounds! Available classes: {len(classes)}")
|
| 181 |
+
return "Unknown"
|
| 182 |
else:
|
| 183 |
+
print(f"[ERROR] Classifier file not found: {classifier_path}")
|
| 184 |
return "Unknown"
|
| 185 |
else:
|
| 186 |
+
print(f"[ERROR] Model file not found: {model_path}")
|
| 187 |
return "Unknown"
|
| 188 |
except Exception as e:
|
| 189 |
+
print(f"[ERROR] Exception in face classification: {str(e)}")
|
| 190 |
+
import traceback
|
| 191 |
+
traceback.print_exc()
|
| 192 |
return "Unknown"
|