vlordier commited on
Commit
48953e9
·
verified ·
1 Parent(s): fc35e87

Upload hf_job_gaze.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. hf_job_gaze.py +81 -28
hf_job_gaze.py CHANGED
@@ -56,20 +56,88 @@ def init_gaze_estimator(device='cuda'):
56
  return pipeline
57
 
58
 
59
- def estimate_gaze_batch(pipeline, image_bgr, bboxes):
60
- """Run L2CS once and match results to bboxes"""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  try:
 
 
 
 
 
 
 
 
 
62
  detections = pipeline.step(image_bgr)
63
  if not detections:
64
  return [None] * len(bboxes)
65
 
66
  results = []
67
- for bbox in bboxes:
68
- if bbox is None:
69
  results.append(None)
70
  continue
71
 
72
- x1, y1, x2, y2 = bbox
73
  bbox_center = np.array([(x1 + x2) / 2, (y1 + y2) / 2])
74
 
75
  best_result = None
@@ -110,6 +178,7 @@ def process_batch(batch, sam3d_dataset):
110
 
111
  for idx, image_pil in enumerate(images):
112
  image_id = Path(image_paths[idx]).stem if image_paths[idx] else f'img_{idx:06d}'
 
113
 
114
  # Find corresponding SAM3D data
115
  sam3d_row = sam3d_dataset.filter(lambda x: x['image_id'] == image_id).take(1)
@@ -128,37 +197,21 @@ def process_batch(batch, sam3d_dataset):
128
  image_rgb = np.array(image_pil.convert('RGB'))
129
  image_bgr = cv2.cvtColor(image_rgb, cv2.COLOR_RGB2BGR)
130
 
131
- # Collect bboxes for valid faces
132
  bboxes = []
133
  for human in humans_data:
134
  bbox = human.get('bbox')
135
  kpts2d = human.get('keypoints_2d')
136
  kpts3d = human.get('keypoints_3d')
137
 
138
- if bbox is None or kpts2d is None or kpts3d is None:
139
- bboxes.append(None)
140
- continue
141
-
142
- kpts3d_arr = np.array(kpts3d)
143
- if len(kpts3d_arr) < 3:
144
- bboxes.append(None)
145
- continue
146
-
147
- # Check face keypoints valid
148
- nose_3d = kpts3d_arr[0]
149
- left_eye_3d = kpts3d_arr[1]
150
- right_eye_3d = kpts3d_arr[2]
151
-
152
- if (np.linalg.norm(nose_3d) < 1e-6 or
153
- np.linalg.norm(left_eye_3d) < 1e-6 or
154
- np.linalg.norm(right_eye_3d) < 1e-6):
155
  bboxes.append(None)
156
- continue
157
-
158
- bboxes.append(bbox)
159
 
160
- # Estimate gaze for all faces in one pass
161
- gaze_results = estimate_gaze_batch(gaze_pipeline, image_bgr, bboxes)
162
 
163
  results_list.append({
164
  'image_id': image_id,
 
56
  return pipeline
57
 
58
 
59
+ def make_square_bbox_with_padding(bbox, img_width, img_height, padding=0.15):
60
+ """Convert bbox to square with padding for gaze estimation"""
61
+ x1, y1, x2, y2 = bbox
62
+ w = x2 - x1
63
+ h = y2 - y1
64
+
65
+ # Make square
66
+ size = max(w, h)
67
+ cx = (x1 + x2) / 2
68
+ cy = (y1 + y2) / 2
69
+
70
+ # Add padding
71
+ size = size * (1 + padding)
72
+
73
+ # Get square bbox
74
+ x1_sq = max(0, int(cx - size / 2))
75
+ y1_sq = max(0, int(cy - size / 2))
76
+ x2_sq = min(img_width, int(cx + size / 2))
77
+ y2_sq = min(img_height, int(cy + size / 2))
78
+
79
+ return [x1_sq, y1_sq, x2_sq, y2_sq]
80
+
81
+
82
+ def has_valid_face(keypoints_2d, keypoints_3d, img_width, img_height):
83
+ """Check if human has a valid, visible face"""
84
+ if keypoints_2d is None or keypoints_3d is None:
85
+ return False
86
+
87
+ kpts2d_arr = np.array(keypoints_2d)
88
+ kpts3d_arr = np.array(keypoints_3d)
89
+
90
+ if len(kpts2d_arr) < 3 or len(kpts3d_arr) < 3:
91
+ return False
92
+
93
+ # Check face keypoints (nose, left eye, right eye)
94
+ nose_2d = kpts2d_arr[0]
95
+ left_eye_2d = kpts2d_arr[1]
96
+ right_eye_2d = kpts2d_arr[2]
97
+ nose_3d = kpts3d_arr[0]
98
+ left_eye_3d = kpts3d_arr[1]
99
+ right_eye_3d = kpts3d_arr[2]
100
+
101
+ # Check 3D keypoints are valid (not at origin)
102
+ keypoints_valid_3d = (np.linalg.norm(nose_3d) > 1e-6 and
103
+ np.linalg.norm(left_eye_3d) > 1e-6 and
104
+ np.linalg.norm(right_eye_3d) > 1e-6)
105
+
106
+ if not keypoints_valid_3d:
107
+ return False
108
+
109
+ # Check 2D keypoints are within image bounds
110
+ for kp in [nose_2d, left_eye_2d, right_eye_2d]:
111
+ if (kp[0] < 0 or kp[0] >= img_width or
112
+ kp[1] < 0 or kp[1] >= img_height):
113
+ return False
114
+
115
+ return True
116
+
117
+
118
+ def estimate_gaze_batch(pipeline, image_bgr, bboxes, img_width, img_height):
119
+ """Run L2CS once on full image and match results to face bboxes"""
120
  try:
121
+ # Convert bboxes to square with padding
122
+ square_bboxes = []
123
+ for bbox in bboxes:
124
+ if bbox is not None:
125
+ square_bbox = make_square_bbox_with_padding(bbox, img_width, img_height, padding=0.15)
126
+ square_bboxes.append(square_bbox)
127
+ else:
128
+ square_bboxes.append(None)
129
+
130
  detections = pipeline.step(image_bgr)
131
  if not detections:
132
  return [None] * len(bboxes)
133
 
134
  results = []
135
+ for square_bbox in square_bboxes:
136
+ if square_bbox is None:
137
  results.append(None)
138
  continue
139
 
140
+ x1, y1, x2, y2 = square_bbox
141
  bbox_center = np.array([(x1 + x2) / 2, (y1 + y2) / 2])
142
 
143
  best_result = None
 
178
 
179
  for idx, image_pil in enumerate(images):
180
  image_id = Path(image_paths[idx]).stem if image_paths[idx] else f'img_{idx:06d}'
181
+ img_width, img_height = image_pil.size
182
 
183
  # Find corresponding SAM3D data
184
  sam3d_row = sam3d_dataset.filter(lambda x: x['image_id'] == image_id).take(1)
 
197
  image_rgb = np.array(image_pil.convert('RGB'))
198
  image_bgr = cv2.cvtColor(image_rgb, cv2.COLOR_RGB2BGR)
199
 
200
+ # Collect bboxes for valid faces only
201
  bboxes = []
202
  for human in humans_data:
203
  bbox = human.get('bbox')
204
  kpts2d = human.get('keypoints_2d')
205
  kpts3d = human.get('keypoints_3d')
206
 
207
+ # Check if this human has a valid, visible face
208
+ if has_valid_face(kpts2d, kpts3d, img_width, img_height) and bbox is not None:
209
+ bboxes.append(bbox)
210
+ else:
 
 
 
 
 
 
 
 
 
 
 
 
 
211
  bboxes.append(None)
 
 
 
212
 
213
+ # Estimate gaze for all valid faces in one pass
214
+ gaze_results = estimate_gaze_batch(gaze_pipeline, image_bgr, bboxes, img_width, img_height)
215
 
216
  results_list.append({
217
  'image_id': image_id,