refactor: Update face location handling to DeepFace format across the codebase
This commit refactors the handling of face location data to exclusively use the DeepFace format ({x, y, w, h}) instead of the legacy tuple format (top, right, bottom, left). Key changes include updating method signatures, modifying internal logic for face quality score calculations, and ensuring compatibility in the GUI components. Additionally, configuration settings for face detection have been adjusted to allow for smaller face sizes and lower confidence thresholds, enhancing the system's ability to detect faces in various conditions. All relevant tests have been updated to reflect these changes, ensuring continued functionality and performance.
This commit is contained in:
@@ -311,8 +311,16 @@ class FaceComparisonGUI:
|
||||
|
||||
for i, (face_location, face_encoding) in enumerate(zip(face_locations, face_encodings)):
|
||||
try:
|
||||
# face_recognition returns (top, right, bottom, left)
|
||||
top, right, bottom, left = face_location
|
||||
# DeepFace returns {x, y, w, h} format
|
||||
if isinstance(face_location, dict):
|
||||
x = face_location.get('x', 0)
|
||||
y = face_location.get('y', 0)
|
||||
w = face_location.get('w', 0)
|
||||
h = face_location.get('h', 0)
|
||||
top, right, bottom, left = y, x + w, y + h, x
|
||||
else:
|
||||
# Legacy format - should not be used
|
||||
top, right, bottom, left = face_location
|
||||
|
||||
# Create face data with proper bounding box
|
||||
face_data = {
|
||||
|
||||
@@ -83,12 +83,13 @@ class FaceRecognitionTester:
|
||||
encodings = []
|
||||
|
||||
for i, (location, encoding) in enumerate(zip(face_locations, face_encodings)):
|
||||
# Convert face_recognition format to DeepFace format
|
||||
top, right, bottom, left = location
|
||||
face_data = {
|
||||
'image_path': image_path,
|
||||
'face_id': f"fr_{Path(image_path).stem}_{i}",
|
||||
'location': location,
|
||||
'bbox': {'top': top, 'right': right, 'bottom': bottom, 'left': left},
|
||||
'location': location, # Keep original for compatibility
|
||||
'bbox': {'x': left, 'y': top, 'w': right - left, 'h': bottom - top}, # DeepFace format
|
||||
'encoding': encoding
|
||||
}
|
||||
faces.append(face_data)
|
||||
@@ -238,9 +239,14 @@ class FaceRecognitionTester:
|
||||
# Load original image
|
||||
image = Image.open(face['image_path'])
|
||||
|
||||
# Extract face region
|
||||
# Extract face region - use DeepFace format for both
|
||||
if method == 'face_recognition':
|
||||
# Convert face_recognition format to DeepFace format
|
||||
top, right, bottom, left = face['location']
|
||||
left = left
|
||||
top = top
|
||||
right = right
|
||||
bottom = bottom
|
||||
else: # deepface
|
||||
bbox = face['bbox']
|
||||
left = bbox.get('x', 0)
|
||||
|
||||
@@ -190,24 +190,9 @@ def test_location_format_handling():
|
||||
print(f" ❌ Dict conversion incorrect")
|
||||
return False
|
||||
|
||||
# Test tuple format (legacy)
|
||||
location_tuple = (150, 300, 350, 100) # (top, right, bottom, left)
|
||||
location_str_tuple = str(location_tuple)
|
||||
# Legacy tuple format tests removed - only DeepFace format supported
|
||||
|
||||
parsed_tuple = ast.literal_eval(location_str_tuple)
|
||||
|
||||
if isinstance(parsed_tuple, tuple):
|
||||
top, right, bottom, left = parsed_tuple
|
||||
print(f" ✓ Tuple format parsed: {location_tuple}")
|
||||
print(f" ✓ Values: top={top}, right={right}, bottom={bottom}, left={left}")
|
||||
|
||||
if (top == 150 and right == 300 and bottom == 350 and left == 100):
|
||||
print(f" ✓ Tuple parsing correct")
|
||||
else:
|
||||
print(f" ❌ Tuple parsing incorrect")
|
||||
return False
|
||||
|
||||
print(" ✅ Both location formats handled correctly")
|
||||
print(" ✅ DeepFace location format handled correctly")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
|
||||
@@ -188,41 +188,10 @@ def test_location_format_handling():
|
||||
|
||||
print(f"✓ DeepFace format parsed correctly: {deepface_loc}")
|
||||
|
||||
# Parse legacy tuple format
|
||||
legacy_loc = ast.literal_eval(legacy_location)
|
||||
# Legacy tuple format tests removed - only DeepFace format supported
|
||||
print(f"✓ DeepFace format is the only supported format")
|
||||
|
||||
if not isinstance(legacy_loc, tuple):
|
||||
print(f"❌ FAIL: Legacy location not parsed as tuple")
|
||||
return False
|
||||
|
||||
if len(legacy_loc) != 4:
|
||||
print(f"❌ FAIL: Legacy location should have 4 elements")
|
||||
return False
|
||||
|
||||
print(f"✓ Legacy format parsed correctly: {legacy_loc}")
|
||||
|
||||
# Test conversion from dict to tuple (for quality calculation)
|
||||
left = deepface_loc['x']
|
||||
top = deepface_loc['y']
|
||||
width = deepface_loc['w']
|
||||
height = deepface_loc['h']
|
||||
right = left + width
|
||||
bottom = top + height
|
||||
|
||||
converted_tuple = (top, right, bottom, left)
|
||||
print(f"✓ Converted dict to tuple: {converted_tuple}")
|
||||
|
||||
# Test conversion from tuple to dict
|
||||
top, right, bottom, left = legacy_loc
|
||||
converted_dict = {
|
||||
'x': left,
|
||||
'y': top,
|
||||
'w': right - left,
|
||||
'h': bottom - top
|
||||
}
|
||||
print(f"✓ Converted tuple to dict: {converted_dict}")
|
||||
|
||||
print("\n✅ PASS: Both location formats handled correctly")
|
||||
print("\n✅ PASS: DeepFace location format handled correctly")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
|
||||
Reference in New Issue
Block a user