-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_deepface.py
More file actions
235 lines (187 loc) Β· 7.7 KB
/
test_deepface.py
File metadata and controls
235 lines (187 loc) Β· 7.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
"""
DeepFace Setup and Model Download Test
Tests DeepFace installation and ensures models download to correct location
"""
import yaml
import sys
import os
import cv2
import numpy as np
def load_config():
"""Load configuration file"""
try:
with open("config/config.yaml", 'r') as f:
config = yaml.safe_load(f)
print("β Configuration file loaded")
return config
except Exception as e:
print(f"β Failed to load configuration: {str(e)}")
return None
def test_deepface_installation():
"""Test DeepFace installation"""
print("\n--- Test 1: DeepFace Installation ---")
try:
from deepface import DeepFace
print("β DeepFace imported successfully")
return True
except ImportError as e:
print(f"β DeepFace not installed: {str(e)}")
print(" Run: pip install deepface tf-keras")
return False
def test_custom_model_directory(config):
"""Test custom model directory setup"""
print("\n--- Test 2: Custom Model Directory ---")
try:
models_path = "d:\\CASC Project\\deepface_models"
# Set environment variable
os.environ['DEEPFACE_HOME'] = models_path
# Create directory
os.makedirs(models_path, exist_ok=True)
print(f"β Models directory created: {models_path}")
# Verify environment variable
deepface_home = os.environ.get('DEEPFACE_HOME')
print(f"β DEEPFACE_HOME set to: {deepface_home}")
return True
except Exception as e:
print(f"β Model directory setup failed: {str(e)}")
return False
def test_model_download_and_analysis():
"""Test model download and basic analysis"""
print("\n--- Test 3: Model Download and Analysis ---")
try:
from deepface import DeepFace
# Create test image
test_img = np.zeros((224, 224, 3), dtype=np.uint8)
# Add a simple face-like pattern
cv2.circle(test_img, (112, 112), 50, (255, 255, 255), -1) # Face
cv2.circle(test_img, (95, 95), 8, (0, 0, 0), -1) # Left eye
cv2.circle(test_img, (129, 95), 8, (0, 0, 0), -1) # Right eye
cv2.ellipse(test_img, (112, 130), (15, 8), 0, 0, 180, (0, 0, 0), 2) # Mouth
print(" Created test image")
# Test analysis (this will download models)
print(" Running analysis (models will download if needed)...")
result = DeepFace.analyze(
img_path=test_img,
actions=['emotion', 'age', 'gender'],
detector_backend='opencv',
enforce_detection=False,
silent=True
)
print("β Analysis completed successfully")
if isinstance(result, list):
result = result[0] if result else {}
print(f" Detected emotion: {result.get('dominant_emotion', 'unknown')}")
print(f" Estimated age: {result.get('age', 0)}")
print(f" Detected gender: {result.get('dominant_gender', 'unknown')}")
return True
except Exception as e:
print(f"β Analysis failed: {str(e)}")
return False
def test_face_database_setup(config):
"""Test face database directory setup"""
print("\n--- Test 4: Face Database Setup ---")
try:
db_path = config['deepface']['db_path']
# Create database directory
os.makedirs(db_path, exist_ok=True)
print(f"β Database directory created: {db_path}")
return True
except Exception as e:
print(f"β Database setup failed: {str(e)}")
return False
def test_casc_deepface_integration():
"""Test CASC DeepFace client integration"""
print("\n--- Test 5: CASC Integration ---")
try:
from src.deepface_client import DeepFaceClient
config = load_config()
if not config:
return False
# Initialize CASC DeepFace client
deepface_client = DeepFaceClient(config)
print("β CASC DeepFace client initialized")
# Test list known persons
persons = deepface_client.list_known_persons()
print(f"β Listed {len(persons)} known person(s)")
return True
except Exception as e:
print(f"β CASC integration failed: {str(e)}")
return False
def check_model_files():
"""Check if models were downloaded to correct location"""
print("\n--- Test 6: Model File Verification ---")
try:
models_path = "d:\\CASC Project\\deepface_models"
if os.path.exists(models_path):
files = os.listdir(models_path)
model_files = [f for f in files if f.endswith(('.h5', '.pb', '.weights'))]
print(f"β Models directory exists: {models_path}")
print(f"β Found {len(model_files)} model file(s)")
if model_files:
print(" Model files:")
for file in model_files[:5]: # Show first 5 files
print(f" - {file}")
if len(model_files) > 5:
print(f" ... and {len(model_files) - 5} more")
# Check directory size
total_size = 0
for root, dirs, files in os.walk(models_path):
for file in files:
file_path = os.path.join(root, file)
total_size += os.path.getsize(file_path)
size_mb = total_size / (1024 * 1024)
print(f"β Models directory size: {size_mb:.1f} MB")
else:
print(f"β Models directory not found: {models_path}")
print(" Models may download on first use")
return True
except Exception as e:
print(f"β Model verification failed: {str(e)}")
return False
def main():
print("="*70)
print("DeepFace Setup and Model Download Test")
print("="*70)
print("This test will verify DeepFace setup and download models to D drive")
print("Models will be stored in: d:\\CASC Project\\deepface_models")
print("="*70)
# Load config
config = load_config()
if not config:
sys.exit(1)
# Track results
results = {}
# Run tests
results['installation'] = test_deepface_installation()
if not results['installation']:
print("\nβ Cannot proceed without DeepFace installation")
sys.exit(1)
results['model_directory'] = test_custom_model_directory(config)
results['database_setup'] = test_face_database_setup(config)
results['model_download'] = test_model_download_and_analysis()
results['casc_integration'] = test_casc_deepface_integration()
results['model_verification'] = check_model_files()
# Summary
print("\n" + "="*70)
print("TEST SUMMARY")
print("="*70)
for test_name, passed in results.items():
status = "β PASSED" if passed else "β FAILED"
print(f"{test_name.replace('_', ' ').title()}: {status}")
all_passed = all(results.values())
print("\n" + "="*70)
if all_passed:
print("β ALL DEEPFACE TESTS PASSED")
print(" DeepFace is ready to use!")
print(f" Models stored in: d:\\CASC Project\\deepface_models")
print("\nNext steps:")
print(" 1. Run: python src/server.py")
print(" 2. Select option 4: Face Recognition with DeepFace")
print(" 3. Add known persons to the database")
else:
print("β SOME TESTS FAILED")
print(" Check DeepFace installation and configuration")
print("="*70)
return 0 if all_passed else 1
if __name__ == "__main__":
sys.exit(main())