AdarshDRC commited on
Commit
8c6ce56
·
verified ·
1 Parent(s): 7977b3d

Update src/models.py

Browse files
Files changed (1) hide show
  1. src/models.py +38 -53
src/models.py CHANGED
@@ -52,67 +52,52 @@ class AIModelManager:
52
 
53
  return object_vec.flatten().numpy()
54
 
55
- def process_image(self, image_path: str, is_query=False):
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 # Track whether Lane 1 found anything usable
74
 
75
  # ==========================================
76
- # LANE 1: THE FACE LANE
77
  # ==========================================
78
- try:
79
- face_objs = DeepFace.represent(
80
- img_path=img_np,
81
- model_name="GhostFaceNet",
82
- detector_backend="retinaface",
83
- enforce_detection=True,
84
- align=True
85
- )
86
-
87
- for index, face in enumerate(face_objs):
88
- # --- BUG FIX 5: Filter out tiny/background faces ---
89
- facial_area = face.get("facial_area", {})
90
- fw = facial_area.get("w", img_w)
91
- fh = facial_area.get("h", img_h)
92
- face_area_px = fw * fh
93
-
94
- if face_area_px < MIN_FACE_AREA:
95
- print(f"🟡 FACE {index+1} SKIPPED: Too small ({fw}x{fh}px = {face_area_px}px²) likely background noise.")
96
- continue
97
-
98
- face_vec = torch.tensor([face["embedding"]])
99
- face_vec = F.normalize(face_vec, p=2, dim=1)
100
-
101
- extracted_vectors.append({
102
- "type": "face",
103
- "vector": face_vec.flatten().numpy()
104
- })
105
- faces_were_found = True
106
- print(f"🟢 FACE {index+1} EXTRACTED: {fw}x{fh}px — Added to Face Index.")
107
-
108
- # --- BUG FIX 2: For queries, do NOT break — search with ALL faces ---
109
- # The calling code in main.py already loops over all returned vectors,
110
- # so returning multiple face vectors means we search for every person
111
- # in a group photo query simultaneously.
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