Spaces:
Running
on
Zero
Running
on
Zero
Update model.py
Browse files
model.py
CHANGED
|
@@ -29,7 +29,6 @@ class ModelHandler:
|
|
| 29 |
"""
|
| 30 |
Load face analysis model.
|
| 31 |
Downloads from HF Hub to the path insightface expects.
|
| 32 |
-
Forces CPU to avoid ZeroGPU initialization errors.
|
| 33 |
"""
|
| 34 |
print("Loading face analysis model...")
|
| 35 |
|
|
@@ -50,7 +49,7 @@ class ModelHandler:
|
|
| 50 |
self.app = FaceAnalysis(
|
| 51 |
name=Config.ANTELOPEV2_NAME,
|
| 52 |
root=Config.ANTELOPEV2_ROOT,
|
| 53 |
-
providers=['CPUExecutionProvider']
|
| 54 |
)
|
| 55 |
self.app.prepare(ctx_id=0, det_size=(640, 640))
|
| 56 |
print(f" [OK] Face analysis model loaded successfully.")
|
|
@@ -113,7 +112,7 @@ class ModelHandler:
|
|
| 113 |
# --- NEW: Enable xFormers ---
|
| 114 |
try:
|
| 115 |
self.pipeline.enable_xformers_memory_efficient_attention()
|
| 116 |
-
print(" [OK]
|
| 117 |
except Exception as e:
|
| 118 |
print(f" [WARNING] Failed to enable xFormers: {e}")
|
| 119 |
# --- END NEW ---
|
|
@@ -160,7 +159,6 @@ class ModelHandler:
|
|
| 160 |
|
| 161 |
print("--- All models loaded successfully ---")
|
| 162 |
|
| 163 |
-
# --- MODIFIED: Renamed function and changed return value ---
|
| 164 |
def get_face_info(self, image):
|
| 165 |
"""Extracts the largest face, returns insightface result object."""
|
| 166 |
if not self.face_analysis_loaded:
|
|
@@ -174,7 +172,9 @@ class ModelHandler:
|
|
| 174 |
return None
|
| 175 |
|
| 176 |
# Sort by size (width * height) to find the main character
|
| 177 |
-
|
|
|
|
|
|
|
| 178 |
|
| 179 |
# Return the largest face info
|
| 180 |
return faces[0]
|
|
|
|
| 29 |
"""
|
| 30 |
Load face analysis model.
|
| 31 |
Downloads from HF Hub to the path insightface expects.
|
|
|
|
| 32 |
"""
|
| 33 |
print("Loading face analysis model...")
|
| 34 |
|
|
|
|
| 49 |
self.app = FaceAnalysis(
|
| 50 |
name=Config.ANTELOPEV2_NAME,
|
| 51 |
root=Config.ANTELOPEV2_ROOT,
|
| 52 |
+
providers=['CUDAExecutionProvider', 'CPUExecutionProvider']
|
| 53 |
)
|
| 54 |
self.app.prepare(ctx_id=0, det_size=(640, 640))
|
| 55 |
print(f" [OK] Face analysis model loaded successfully.")
|
|
|
|
| 112 |
# --- NEW: Enable xFormers ---
|
| 113 |
try:
|
| 114 |
self.pipeline.enable_xformers_memory_efficient_attention()
|
| 115 |
+
print(" [OK] xFormmrs memory efficient attention enabled.")
|
| 116 |
except Exception as e:
|
| 117 |
print(f" [WARNING] Failed to enable xFormers: {e}")
|
| 118 |
# --- END NEW ---
|
|
|
|
| 159 |
|
| 160 |
print("--- All models loaded successfully ---")
|
| 161 |
|
|
|
|
| 162 |
def get_face_info(self, image):
|
| 163 |
"""Extracts the largest face, returns insightface result object."""
|
| 164 |
if not self.face_analysis_loaded:
|
|
|
|
| 172 |
return None
|
| 173 |
|
| 174 |
# Sort by size (width * height) to find the main character
|
| 175 |
+
# --- MODIFIED: Fixed typo x['bbox[0]) -> x['bbox'][0]) ---
|
| 176 |
+
faces = sorted(faces, key=lambda x: (x['bbox'][2]-x['bbox'][0])*(x['bbox'][3]-x'bbox'[1]), reverse=True)
|
| 177 |
+
# --- END MODIFIED ---
|
| 178 |
|
| 179 |
# Return the largest face info
|
| 180 |
return faces[0]
|