FangSen9000 commited on
Commit
4ea120d
·
1 Parent(s): 5b5507b

When dealing with interpolation, the issue of confidence level was not taken into consideration.

Browse files
text2pose.py CHANGED
@@ -230,9 +230,10 @@ class SignLanguageQA:
230
  frame_data = self.apply_y_offset(frame_data, y_offset)
231
 
232
  if draw_style == 'openpose':
 
233
  # Use OpenPose style (simpler, no processing needed)
234
  vis_img = draw_pose_openpose(
235
- pose=frame_data,
236
  height=height,
237
  width=width,
238
  include_face=True,
@@ -435,6 +436,66 @@ class SignLanguageQA:
435
 
436
  return offset_data
437
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
438
  def normalize_pose_data(self, npz_data, reference_point='neck', scale_by='shoulders',
439
  target_shoulder_width=0.35, target_shoulder_y=0.45):
440
  """
 
230
  frame_data = self.apply_y_offset(frame_data, y_offset)
231
 
232
  if draw_style == 'openpose':
233
+ filtered_frame = self.filter_pose_for_openpose(frame_data)
234
  # Use OpenPose style (simpler, no processing needed)
235
  vis_img = draw_pose_openpose(
236
+ pose=filtered_frame,
237
  height=height,
238
  width=width,
239
  include_face=True,
 
436
 
437
  return offset_data
438
 
439
+ def filter_pose_for_openpose(self, frame_data, conf_threshold=0.2):
440
+ """Filter low-confidence joints for OpenPose rendering to avoid ghost limbs"""
441
+ filtered = copy.deepcopy(frame_data)
442
+
443
+ # Bodies
444
+ bodies = filtered.get('bodies', None)
445
+ body_scores = filtered.get('body_scores', None)
446
+ if bodies is not None and body_scores is not None:
447
+ scores = np.array(body_scores).reshape(-1)
448
+ mask = (scores < conf_threshold) | (scores <= 0)
449
+ if mask.shape[0] < bodies.shape[0]:
450
+ mask = np.pad(mask, (0, bodies.shape[0] - mask.shape[0]), constant_values=False)
451
+ elif mask.shape[0] > bodies.shape[0]:
452
+ mask = mask[:bodies.shape[0]]
453
+ bodies = bodies.copy()
454
+ bodies[mask, :] = 0
455
+ filtered['bodies'] = bodies
456
+
457
+ # Hands
458
+ hands = filtered.get('hands', None)
459
+ hand_scores = filtered.get('hands_scores', None)
460
+ if hands is not None and hand_scores is not None:
461
+ scores = np.array(hand_scores)
462
+ hands = hands.copy()
463
+ if hands.ndim == 3 and scores.ndim == 3:
464
+ for h in range(hands.shape[0]):
465
+ mask = (scores[h] < conf_threshold) | (scores[h] <= 0)
466
+ hands[h][mask, :] = 0
467
+ elif hands.ndim == 3 and scores.ndim == 2:
468
+ for h in range(hands.shape[0]):
469
+ mask = (scores[h] < conf_threshold) | (scores[h] <= 0)
470
+ hands[h][mask, :] = 0
471
+ elif hands.ndim == 2 and scores.ndim == 2:
472
+ mask = (scores < conf_threshold) | (scores <= 0)
473
+ hands[mask, :] = 0
474
+ elif hands.ndim == 2 and scores.ndim == 1:
475
+ mask = (scores < conf_threshold) | (scores <= 0)
476
+ hands[mask, :] = 0
477
+ filtered['hands'] = hands
478
+
479
+ # Faces
480
+ faces = filtered.get('faces', None)
481
+ face_scores = filtered.get('faces_scores', None)
482
+ if faces is not None and face_scores is not None:
483
+ scores = np.array(face_scores)
484
+ faces = faces.copy()
485
+ if faces.ndim == 3 and scores.ndim == 3:
486
+ for f in range(faces.shape[0]):
487
+ mask = (scores[f] < conf_threshold) | (scores[f] <= 0)
488
+ faces[f][mask, :] = 0
489
+ elif faces.ndim == 2 and scores.ndim == 2:
490
+ mask = (scores < conf_threshold) | (scores <= 0)
491
+ faces[mask, :] = 0
492
+ elif faces.ndim == 2 and scores.ndim == 1:
493
+ mask = (scores < conf_threshold) | (scores <= 0)
494
+ faces[mask, :] = 0
495
+ filtered['faces'] = faces
496
+
497
+ return filtered
498
+
499
  def normalize_pose_data(self, npz_data, reference_point='neck', scale_by='shoulders',
500
  target_shoulder_width=0.35, target_shoulder_y=0.45):
501
  """
utils/__pycache__/npz_interpolation.cpython-312.pyc CHANGED
Binary files a/utils/__pycache__/npz_interpolation.cpython-312.pyc and b/utils/__pycache__/npz_interpolation.cpython-312.pyc differ
 
utils/npz_interpolation.py CHANGED
@@ -138,6 +138,56 @@ def interpolate_pose_npz(npz1_data, npz2_data, num_frames=10, method='catmull-ro
138
  # Extract all components
139
  components = ['bodies', 'body_scores', 'hands', 'hands_scores', 'faces', 'faces_scores']
140
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
141
  for i in range(num_frames):
142
  t = i / (num_frames - 1) if num_frames > 1 else 0
143
  interpolated_data = {}
@@ -205,6 +255,16 @@ def interpolate_pose_npz(npz1_data, npz2_data, num_frames=10, method='catmull-ro
205
  t_eased = ease_in_out_sine(t)
206
  interpolated = data1 * (1 - t_eased) + data2 * t_eased
207
 
 
 
 
 
 
 
 
 
 
 
208
  # Create frame key for interpolated frame
209
  frame_key = f"frame_{i+1:08d}_{component}"
210
  interpolated_data[frame_key] = interpolated
@@ -251,4 +311,4 @@ def smooth_trajectory(keypoints_sequence, window_size=5):
251
  avg_kp = np.mean(window_kps, axis=0)
252
  smoothed.append(avg_kp)
253
 
254
- return smoothed
 
138
  # Extract all components
139
  components = ['bodies', 'body_scores', 'hands', 'hands_scores', 'faces', 'faces_scores']
140
 
141
+ conf_threshold = 0.3
142
+ score_component_map = {
143
+ 'bodies': 'body_scores',
144
+ 'hands': 'hands_scores',
145
+ 'faces': 'faces_scores'
146
+ }
147
+ validity_masks = {}
148
+
149
+ def compute_valid_mask(component_name):
150
+ score_component = score_component_map.get(component_name)
151
+ if not score_component:
152
+ return None
153
+ score_key1 = f"frame_{frame_num1}_{score_component}"
154
+ score_key2 = f"frame_{frame_num2}_{score_component}"
155
+ if score_key1 not in npz1_data or score_key2 not in npz2_data:
156
+ return None
157
+ score1 = np.array(npz1_data[score_key1])
158
+ score2 = np.array(npz2_data[score_key2])
159
+ if score1.shape != score2.shape:
160
+ return None
161
+ return (score1 >= conf_threshold) & (score2 >= conf_threshold)
162
+
163
+ def apply_mask_to_data(data, mask, invalid_value=0.0, treat_scores=False):
164
+ if mask is None:
165
+ return data
166
+
167
+ data = np.array(data, copy=True)
168
+ valid_mask = np.array(mask, dtype=bool)
169
+
170
+ if treat_scores:
171
+ try:
172
+ valid_mask = np.broadcast_to(valid_mask, data.shape)
173
+ except ValueError:
174
+ valid_mask = np.squeeze(valid_mask)
175
+ valid_mask = np.broadcast_to(valid_mask, data.shape)
176
+ data = np.where(valid_mask, data, invalid_value)
177
+ return data
178
+
179
+ # Coordinates: ensure mask matches all dims except the last coordinate axis
180
+ target_mask_shape = data.shape[:-1]
181
+ try:
182
+ valid_mask = np.broadcast_to(valid_mask, target_mask_shape)
183
+ except ValueError:
184
+ valid_mask = np.squeeze(valid_mask)
185
+ valid_mask = np.broadcast_to(valid_mask, target_mask_shape)
186
+
187
+ valid_mask = np.expand_dims(valid_mask, axis=-1)
188
+ data = np.where(valid_mask, data, invalid_value)
189
+ return data
190
+
191
  for i in range(num_frames):
192
  t = i / (num_frames - 1) if num_frames > 1 else 0
193
  interpolated_data = {}
 
255
  t_eased = ease_in_out_sine(t)
256
  interpolated = data1 * (1 - t_eased) + data2 * t_eased
257
 
258
+ # Apply confidence-aware masking so we don't invent joints with low confidence
259
+ if component in score_component_map:
260
+ if component not in validity_masks:
261
+ validity_masks[component] = compute_valid_mask(component)
262
+ interpolated = apply_mask_to_data(interpolated, validity_masks.get(component), invalid_value=0.0)
263
+ elif component.endswith('_scores'):
264
+ base_component = component.replace('_scores', '')
265
+ mask = validity_masks.get(base_component)
266
+ interpolated = apply_mask_to_data(interpolated, mask, invalid_value=-1.0, treat_scores=True)
267
+
268
  # Create frame key for interpolated frame
269
  frame_key = f"frame_{i+1:08d}_{component}"
270
  interpolated_data[frame_key] = interpolated
 
311
  avg_kp = np.mean(window_kps, axis=0)
312
  smoothed.append(avg_kp)
313
 
314
+ return smoothed