Spaces:
Sleeping
Sleeping
| """ | |
| Diagnostic script to identify common issues with face recognition setup | |
| """ | |
| import sys | |
| import os | |
| import traceback | |
| def check_python_version(): | |
| """Check Python version""" | |
| print("Python Version Check") | |
| print("=" * 20) | |
| print(f"Python version: {sys.version}") | |
| if sys.version_info < (3, 6): | |
| print("WARNING: Python 3.6+ recommended") | |
| else: | |
| print("OK: Python version is compatible") | |
| def check_packages(): | |
| """Check if required packages are installed""" | |
| print("\nPackage Installation Check") | |
| print("=" * 30) | |
| packages = [ | |
| ('numpy', 'numpy'), | |
| ('cv2', 'cv2'), | |
| ('torch', 'torch'), | |
| ('sklearn', 'sklearn'), | |
| ('PIL', 'PIL'), | |
| ('matplotlib', 'matplotlib'), | |
| ('joblib', 'joblib') | |
| ] | |
| missing_packages = [] | |
| for package_name, import_name in packages: | |
| try: | |
| __import__(import_name) | |
| print(f"OK: {package_name}") | |
| except ImportError: | |
| print(f"ERROR: {package_name} - MISSING") | |
| missing_packages.append(package_name) | |
| if missing_packages: | |
| print(f"\nWARNING: Missing packages: {', '.join(missing_packages)}") | |
| print("Install with: pip install " + " ".join(missing_packages)) | |
| return False | |
| else: | |
| print("\nOK: All required packages are installed") | |
| return True | |
| def check_files(): | |
| """Check if required files exist""" | |
| print("\nFile Structure Check") | |
| print("=" * 25) | |
| required_files = [ | |
| 'face_recognition.py', | |
| 'face_recognition_model.py', | |
| 'test_cosine_similarity.py' | |
| ] | |
| model_files = [ | |
| 'siamese_model.t7', | |
| 'decision_tree_model.sav', | |
| 'face_recognition_scaler.sav' | |
| ] | |
| missing_files = [] | |
| print("Required Python files:") | |
| for file in required_files: | |
| if os.path.exists(file): | |
| print(f"OK: {file}") | |
| else: | |
| print(f"ERROR: {file} - MISSING") | |
| missing_files.append(file) | |
| print("\nModel files (need to be trained):") | |
| for file in model_files: | |
| if os.path.exists(file): | |
| print(f"OK: {file}") | |
| else: | |
| print(f"ERROR: {file} - MISSING (needs training)") | |
| missing_files.append(file) | |
| return len(missing_files) == 0 | |
| def test_imports(): | |
| """Test importing the face recognition module""" | |
| print("\nImport Test") | |
| print("=" * 15) | |
| try: | |
| # Add current directory to path | |
| current_dir = os.path.dirname(os.path.abspath(__file__)) | |
| sys.path.insert(0, current_dir) | |
| from face_recognition import get_similarity, get_face_class | |
| print("OK: Successfully imported face_recognition functions") | |
| return True | |
| except Exception as e: | |
| print(f"ERROR: Failed to import face_recognition: {e}") | |
| print("Full error:") | |
| traceback.print_exc() | |
| return False | |
| def test_basic_functionality(): | |
| """Test basic functionality without model files""" | |
| print("\nBasic Functionality Test") | |
| print("=" * 30) | |
| try: | |
| import numpy as np | |
| from face_recognition import detected_face | |
| # Create a test image | |
| test_img = np.random.randint(0, 255, (100, 100, 3), dtype=np.uint8) | |
| # Test face detection | |
| result = detected_face(test_img) | |
| print("OK: detected_face function works") | |
| return True | |
| except Exception as e: | |
| print(f"ERROR: Basic functionality test failed: {e}") | |
| traceback.print_exc() | |
| return False | |
| def main(): | |
| """Run all diagnostic checks""" | |
| print("Face Recognition System Diagnostic") | |
| print("=" * 40) | |
| checks = [ | |
| check_python_version, | |
| check_packages, | |
| check_files, | |
| test_imports, | |
| test_basic_functionality | |
| ] | |
| results = [] | |
| for check in checks: | |
| try: | |
| result = check() | |
| results.append(result) | |
| except Exception as e: | |
| print(f"ERROR: Check failed with error: {e}") | |
| results.append(False) | |
| print("\n" + "=" * 40) | |
| print("DIAGNOSTIC SUMMARY") | |
| print("=" * 40) | |
| if all(results): | |
| print("SUCCESS: All checks passed! Your setup looks good.") | |
| print("You can now run the test script.") | |
| else: | |
| print("ERROR: Some checks failed. Please fix the issues above.") | |
| print("\nCommon solutions:") | |
| print("1. Install missing packages: pip install numpy opencv-python torch scikit-learn matplotlib joblib") | |
| print("2. Make sure all Python files are in the same directory") | |
| print("3. Train your models first before testing similarity") | |
| if __name__ == "__main__": | |
| main() | |