Optimize PoseVisualizer.draw (~10x faster, simpler)#229
Merged
Conversation
The draw path spent ~75% of its time in numpy MaskedArray element indexing (per-point `person[i+idx]` and `point[:2]` lookups). Resolve all coordinates to plain ints once per frame, vectorize per-component color computation, drop the per-component lru_cache closure, and remove a leftover debug print in the limb loop. Draw path on a 799-frame 256x256 pose: 18.7 -> 190 fps. End-to-end visualize_pose: ~40s -> ~5s. Output frames are identical up to 1 color-value of anti-aliasing rounding. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Precompute static connectivity (limb index pairs, color palettes) once in __init__ instead of rebuilding them every frame, and replace the per-point/per-limb Python color and depth averaging with vectorized numpy over each component. Only the cv2 draw calls remain per-op. Draw path: 190 -> 240 fps. Output unchanged (frames identical to the prior commit). Remaining time is dominated by OpenCV's anti-aliased line rasterization of the face-mesh tessellation. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
visualize_posewas running at ~20 fps. Profiling the draw path showed ~75% of time in numpyMaskedArrayelement indexing — the per-pointperson[i+idx]lookups andpoint[:2]slicing inpoint_to_xy, each of which walks__array_finalize__/_update_from/view.Changes to
_draw_frame@lru_cacheclosure rebuilt every frame).print()inside the limb loop.Results
Measured on a 799-frame 256×256 pose:
End-to-end
visualize_pose: ~40s → ~5s.Correctness
visualization_test.pytests pass;ruffclean.draw_on_video(plain-array path) andFastAndUglyPoseVisualizer(overrides_draw_frame) unaffected.🤖 Generated with Claude Code