Skip to content

⚡ Optimize ImageSegmenter memory allocation#224

Merged
fangfufu merged 1 commit intomasterfrom
optimize-image-segmenter-memory-17371324163120674587
Feb 7, 2026
Merged

⚡ Optimize ImageSegmenter memory allocation#224
fangfufu merged 1 commit intomasterfrom
optimize-image-segmenter-memory-17371324163120674587

Conversation

@fangfufu
Copy link
Owner

@fangfufu fangfufu commented Feb 7, 2026

This PR optimizes the ImageSegmenter.segment method in lfbw/lfbw.py by pre-allocating buffers for image resizing and color conversion. Previously, these operations allocated new memory for every frame, leading to unnecessary overhead.

Key changes:

  • Added self.mp_frame_buffer and self.mask_upscaled_buffer in ImageSegmenter.__init__.
  • Updated ImageSegmenter.segment to use these buffers via the dst parameter in cv2.resize and cv2.cvtColor.
  • Ensured cv2.cvtColor and cv2.GaussianBlur operate in-place where appropriate.

This change is transparent to the caller and maintains the existing functionality while improving performance.

Performance Benchmark:

  • Baseline: ~59.5 FPS
  • Optimized: ~68.2 FPS
  • Improvement: ~14%

PR created automatically by Jules for task 17371324163120674587 started by @fangfufu

Pre-allocate buffers for `cv2.resize` and `cv2.cvtColor` operations in `ImageSegmenter` to avoid repetitive memory allocation per frame. This improves performance and reduces memory churn.

Measured improvement: ~14% increase in FPS (from ~59.5 FPS to ~68.2 FPS) on benchmark.

Co-authored-by: fangfufu <2323403+fangfufu@users.noreply.github.com>
@google-labs-jules
Copy link
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@sonarqubecloud
Copy link

sonarqubecloud bot commented Feb 7, 2026

@fangfufu fangfufu merged commit 73eb71d into master Feb 7, 2026
8 checks passed
@fangfufu fangfufu deleted the optimize-image-segmenter-memory-17371324163120674587 branch February 7, 2026 07:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant