From 2c60146419f98c332fff3e4ea35574da7b7bdd81 Mon Sep 17 00:00:00 2001 From: Clement Rey Date: Tue, 30 Jan 2024 09:38:21 +0100 Subject: [PATCH] Don't clear scalars in `face tracking` example (#4950) This is what caused the deadlocks/crashes we were after (which I'm glad it did -- those have been fixed in #4949). Even though they are not a problem anymore, I'm not sure why they're here to begin with? Clearing scalars does nothing unless you specifically intend to use them with latest-at queries (i.e. not plots, which isn't the case here), otherwise it's just inefficient with no upside (which I'd rather not teach users). Am I missing something? :thinking: --- I've updated the code to only clear faces when they disappear. I might come up with a follow up PR soon so that the timeseries view interprets those clears as plot breakpoints. --- examples/python/face_tracking/main.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/examples/python/face_tracking/main.py b/examples/python/face_tracking/main.py index fa7c3eecfe03..3bb90c6a4212 100755 --- a/examples/python/face_tracking/main.py +++ b/examples/python/face_tracking/main.py @@ -237,13 +237,28 @@ def detect_and_log(self, image: npt.NDArray[np.uint8], frame_time_nano: int) -> if self._video_mode else self._detector.detect(image) ) - rr.log("video/landmarker/faces", rr.Clear(recursive=True)) - rr.log("reconstruction/faces", rr.Clear(recursive=True)) - rr.log("blendshapes", rr.Clear(recursive=True)) + + def is_empty(i): # type: ignore[no-untyped-def] + try: + next(i) + return False + except StopIteration: + return True + + if is_empty(zip(detection_result.face_landmarks, detection_result.face_blendshapes)): + rr.log("video/landmarker/faces", rr.Clear(recursive=True)) + rr.log("reconstruction/faces", rr.Clear(recursive=True)) + rr.log("blendshapes", rr.Clear(recursive=True)) for i, (landmark, blendshapes) in enumerate( zip(detection_result.face_landmarks, detection_result.face_blendshapes) ): + if len(landmark) == 0 or len(blendshapes) == 0: + rr.log(f"video/landmarker/faces/{i}/landmarks", rr.Clear(recursive=True)) + rr.log(f"reconstruction/faces/{i}", rr.Clear(recursive=True)) + rr.log(f"blendshapes/{i}", rr.Clear(recursive=True)) + continue + # MediaPipe's keypoints are normalized to [0, 1], so we need to scale them to get pixel coordinates. pts = [(math.floor(lm.x * width), math.floor(lm.y * height)) for lm in landmark] keypoint_ids = list(range(len(landmark)))