Spaces:
Sleeping
Sleeping
Update src/models.py
Browse files- src/models.py +38 -53
src/models.py
CHANGED
|
@@ -52,67 +52,52 @@ class AIModelManager:
|
|
| 52 |
|
| 53 |
return object_vec.flatten().numpy()
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
Master function: Extracts EVERY face and EVERY non-human object from an image.
|
| 58 |
-
|
| 59 |
-
Key design decisions:
|
| 60 |
-
- Face lane runs first and tags every face with its bounding box area.
|
| 61 |
-
- Only faces above MIN_FACE_AREA are indexed (filters background/tiny faces).
|
| 62 |
-
- For queries, ALL detected faces are used (not just the first one).
|
| 63 |
-
- Object lane SKIPS any YOLO detection whose class is 'person', so humans
|
| 64 |
-
never pollute the object index when faces were already found.
|
| 65 |
-
- If NO faces are found at all, humans caught by YOLO DO go into the object
|
| 66 |
-
lane (as a fallback for silhouettes, backs-of-head, full body shots etc.)
|
| 67 |
-
"""
|
| 68 |
extracted_vectors = []
|
| 69 |
original_img_pil = Image.open(image_path).convert('RGB')
|
| 70 |
img_np = np.array(original_img_pil)
|
| 71 |
img_h, img_w = img_np.shape[:2]
|
| 72 |
|
| 73 |
-
faces_were_found = False
|
| 74 |
|
| 75 |
# ==========================================
|
| 76 |
-
# LANE 1: THE FACE LANE
|
| 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 |
-
# (is_query flag is kept as parameter for future use / logging only)
|
| 113 |
-
|
| 114 |
-
except ValueError:
|
| 115 |
-
print("🟠 NO FACES DETECTED -> Falling back to Object Lane for any humans.")
|
| 116 |
|
| 117 |
# ==========================================
|
| 118 |
# LANE 2: THE OBJECT LANE
|
|
|
|
| 52 |
|
| 53 |
return object_vec.flatten().numpy()
|
| 54 |
|
| 55 |
+
# Change the function signature to accept detect_faces
|
| 56 |
+
def process_image(self, image_path: str, is_query=False, detect_faces=True):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
extracted_vectors = []
|
| 58 |
original_img_pil = Image.open(image_path).convert('RGB')
|
| 59 |
img_np = np.array(original_img_pil)
|
| 60 |
img_h, img_w = img_np.shape[:2]
|
| 61 |
|
| 62 |
+
faces_were_found = False
|
| 63 |
|
| 64 |
# ==========================================
|
| 65 |
+
# LANE 1: THE FACE LANE (NOW TOGGLEABLE)
|
| 66 |
# ==========================================
|
| 67 |
+
if detect_faces:
|
| 68 |
+
try:
|
| 69 |
+
print("Running heavy face detection...")
|
| 70 |
+
face_objs = DeepFace.represent(
|
| 71 |
+
img_path=img_np,
|
| 72 |
+
model_name="GhostFaceNet",
|
| 73 |
+
detector_backend="retinaface",
|
| 74 |
+
enforce_detection=True,
|
| 75 |
+
align=True
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
for index, face in enumerate(face_objs):
|
| 79 |
+
facial_area = face.get("facial_area", {})
|
| 80 |
+
fw = facial_area.get("w", img_w)
|
| 81 |
+
fh = facial_area.get("h", img_h)
|
| 82 |
+
face_area_px = fw * fh
|
| 83 |
+
|
| 84 |
+
if face_area_px < MIN_FACE_AREA:
|
| 85 |
+
continue
|
| 86 |
+
|
| 87 |
+
face_vec = torch.tensor([face["embedding"]])
|
| 88 |
+
face_vec = F.normalize(face_vec, p=2, dim=1)
|
| 89 |
+
|
| 90 |
+
extracted_vectors.append({
|
| 91 |
+
"type": "face",
|
| 92 |
+
"vector": face_vec.flatten().numpy()
|
| 93 |
+
})
|
| 94 |
+
faces_were_found = True
|
| 95 |
+
|
| 96 |
+
except ValueError:
|
| 97 |
+
print("🟠 NO FACES DETECTED -> Falling back to Object Lane.")
|
| 98 |
+
else:
|
| 99 |
+
print("⏩ FAST MODE: Skipping Face Detection Lane entirely.")
|
| 100 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
# ==========================================
|
| 103 |
# LANE 2: THE OBJECT LANE
|