Skip to main content

vlm-sieve

PyPI version Python 3.9+ License: MIT Zero-GPU CPU Engine

Event-Driven Video Frame Gatekeeper, Active ROI Cropper & Temporal Storyboard Packer for Vision-Language Models.


Overview

Sending raw 30fps or 60fps video directly to frontier Vision-Language Models (such as Google Gemini, OpenAI GPT-4o, or Anthropic Claude 3.5) quickly becomes cost-prohibitive, introduces severe latency, and floods context windows with static background tokens.

vlm-sieve (vlmsieve) is an ultra-fast, CPU-only intelligent pre-filter that monitors live webcams, RTSP security feeds, or recorded video files. It discards redundant or idle frames, tracks active motion regions, and packs salient temporal moments into structured, timestamped, and spatially grounded storyboard image grids—reducing VLM token consumption by >95% while boosting temporal and spatial comprehension.


Visual Showcase

vlm-sieve Structured Storyboard Demo

In the example above, vlm-sieve monitored a 150-frame ($5.0\text{s}$) simulated surveillance feed. Instead of transmitting 150 separate images ($168,000$ naive tokens), vlm-sieve emitted a single $2 \times 3$ storyboard ($1,120$ tokens)—achieving 99.33% token savings (150x compression) while providing:

  • Chronological Indexing: Each tile contains a high-contrast badge (#1 | +0.00s to #6 | +2.53s) specifying exact relative offsets.
  • $4 \times 4$ Spatial Coordinate Grounding: Tiles feature cell coordinate labels (A1–D4) allowing VLMs to reason about exact spatial trajectories (e.g., "vehicle entered at B1, traversed C2-C3, and parked at D4").
  • Active ROI Zooming: Automatically crops out uninformative backgrounds without losing context.

The 4-Stage Architecture Funnel

Raw Video Stream (30/60 FPS)
 │
 ├──► [Stage 1: Downscaled CPU Motion Gatekeeper]
 │     • Sub-2ms frame evaluation on CPU via 320p grayscale differencing
 │     • Rejects camera sensor noise, exposure flickers, and static intervals
 │     • Automated webcam sensor warmup & 1-frame settling tail capture
 │
 ├──► [Stage 2: Temporal-Peak Keyframe Sampler]
 │     • Pins event onset (first motion) and termination (settled action)
 │     • Stratified bucket sampling combining motion energy (60%) + visual diversity (40%)
 │
 ├──► [Stage 3: Active ROI Auto-Cropper]
 │     • Computes union bounding box over entire motion trajectory
 │     • Applies contextual padding with edge-aware minimum ROI size clamping
 │
 └──► [Stage 4: Structured Storyboard Packer]
       • Assembles RxC composite image grid with uniform gutters
       • Burns chronological relative timestamps and optional A1-D4 spatial overlays
       • Outputs ready-to-prompt PIL.Image, raw bytes, or base64 data URIs

Installation

pip install vlm-sieve

Core Dependency Guardrails

vlm-sieve is engineered for ultra-lean edge and cloud deployment. Core dependencies are strictly limited to numpy, opencv-python-headless, Pillow, and pydantic. Zero heavy deep-learning frameworks (PyTorch, TensorFlow, Transformers) are installed or required.


Quickstart Code Examples

1. Basic 5-Line Python SDK

from vlmsieve import VideoSieve

# Works with local video files, RTSP/HTTP streams, or webcam index (0)
sieve = VideoSieve("warehouse_cam.mp4", grid_layout=(2, 3), spatial_grid_overlay=True)

for event in sieve.stream_events():
    print(f"Captured Event #{event.event_id} ({event.raw_frame_count} frames)")
    # Save storyboard directly to disk or send to VLM
    event.save(f"event_{event.event_id}.jpg")

report = sieve.get_savings_report()
print(f"Tokens saved: {report.tokens_saved_pct:.1f}% ({report.compression_ratio:.1f}x compression)")

2. Google Gemini (google-genai) Integration

Pass storyboard PIL images directly to Google Gemini 2.0 / 1.5 Flash:

from google import genai
from vlmsieve import VideoSieve

client = genai.Client()
sieve = VideoSieve("security_feed.mp4", spatial_grid_overlay=True)

for event in sieve.stream_events():
    prompt = (
        "Analyze this chronological 2x3 video storyboard. Each tile has a relative timestamp badge "
        "and a 4x4 spatial grid (rows A-D, columns 1-4). "
        "Describe what occurred, noting exact grid coordinates and timestamps."
    )
    
    response = client.models.generate_content(
        model="gemini-2.0-flash",
        contents=[event.storyboard_pil, prompt]
    )
    print(f"Gemini Analysis for Event #{event.event_id}:\n{response.text}\n")

3. OpenAI (gpt-4o / gpt-4o-mini) Integration

Encode the storyboard to a base64 Data URI for OpenAI's Chat Completions API:

from openai import OpenAI
from vlmsieve import VideoSieve

client = OpenAI()
sieve = VideoSieve("traffic_cam.mp4", spatial_grid_overlay=True)

for event in sieve.stream_events():
    response = client.chat.completions.create(
        model="gpt-4o",
        messages=[
            {
                "role": "user",
                "content": [
                    {
                        "type": "text",
                        "text": "Identify any safety incidents in this 6-panel storyboard.",
                    },
                    {
                        "type": "image_url",
                        "image_url": {"url": event.to_data_uri(format="JPEG", quality=88)},
                    },
                ],
            }
        ],
    )
    print(response.choices[0].message.content)

4. Non-Blocking Async Streaming (FastAPI / Agent Loops)

import asyncio
from vlmsieve import VideoSieve

async def live_monitoring_agent():
    sieve = VideoSieve("rtsp://admin:pass@192.168.1.50/stream")
    
    # astream_events runs CPU frame gating on worker threads without blocking asyncio
    async for event in sieve.astream_events():
        print(f"New event #{event.event_id} captured at {event.start_time_sec:.1f}s")
        # Trigger background webhook, alerts, or asynchronous LLM calls
        await dispatch_vlm_task(event.to_bytes())

asyncio.run(live_monitoring_agent())

Command-Line Interface (CLI)

vlm-sieve ships with a command-line tool vlmsieve:

# Process video into 2x3 storyboards with spatial coordinate grid
vlmsieve warehouse.mp4 --output-dir ./events --grid 2x3 --spatial-grid

# Inspect live webcam feed (index 0) with higher sensitivity threshold
vlmsieve 0 --threshold 0.01 --max-frames 300 -o ./webcam_events

# Process RTSP feed with full frames (disable active ROI cropping)
vlmsieve "rtsp://camera.local/live" --no-crop -o ./rtsp_events

Sample CLI Output

============================================================
                vlm-sieve Processing Summary
============================================================
 Raw Frames Inspected  : 1,800
 Storyboards Emitted   : 4
 Frames Dropped        : 1,776
 Naive Token Estimate  : 2,016,000
 Sieve Token Estimate  : 4,480
 Compression Ratio     : 450.0x
 Token Savings         : 99.78%
============================================================
 Output Directory      : C:\Users\user\Documents\events
 Total Events Saved    : 4
============================================================

SieveConfig Reference Guide

Every hyperparameter can be fine-tuned via SieveConfig:

Parameter Type Default Description
downscale_width int 320 Downscaled frame width for motion evaluation. Guarantees sub-2ms CPU gating latency.
motion_threshold float 0.015 Minimum fraction of moving pixels to trigger an active motion state.
pixel_diff_threshold int 25 Pixel intensity delta threshold in $[0, 255]$ for frame differencing.
min_contour_area int 16 Minimum contour area in downscaled pixels required to eliminate sensor noise.
cooldown_frames int 12 Static frames required after motion ceases before closing an event.
min_event_frames int 3 Minimum motion frames required to emit an event (rejects single-frame flicker).
max_event_frames int 150 Maximum frames in a single event buffer before flushing to prevent unbounded latency.
grid_layout tuple[int, int] (2, 3) Storyboard grid layout specified as (rows, cols) (e.g., (2, 3) or (3, 3)).
auto_crop_roi bool True Automatically detect and crop the active motion region across the action trajectory.
roi_padding_ratio float 0.15 Symmetrical padding ratio added around the motion bounding box.
min_roi_size int 224 Minimum cropped ROI width and height, preserving framing for small objects.
burn_timestamps bool True Burn relative elapsed timestamp badges (#1 | +0.00s) onto each tile.
spatial_grid_overlay bool False Render a $4 \times 4$ spatial grid (A1–D4) across each tile for coordinate grounding.
tile_max_width int 640 Maximum pixel width per tile before proportional Lanczos downscaling.
tokens_per_image int 1120 Standard VLM image tile token cost used to compute savings reports.
camera_warmup_frames int 8 Number of initial frames discarded when opening live webcams for exposure stabilization.

Performance & Benchmarks

  • CPU Frame Gating Latency: < 2.5ms per 1080p frame and < 1.2ms per 720p frame on single-core standard x86_64 CPUs.
  • Zero GPU Memory: All downscaling, differencing, and morphological operations execute within CPU L2/L3 cache without requiring CUDA or GPU buffers.
  • Token Compression: Typically compresses continuous video by 95% to 99%, translating directly to a 20x to 100x reduction in API costs.

License

This project is licensed under the terms of the MIT License.

Metadata

Release files for vlm-sieve 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for vlm-sieve 0.1.0
File Size Uploaded Provenance Actions
vlm_sieve-0.1.0.tar.gz 190.7 kB Without provenance Details for vlm_sieve-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for vlm-sieve 0.1.0
File Interpreter ABI Platform Actions
vlm_sieve-0.1.0-py3-none-any.whl Python 3 none any Details for vlm_sieve-0.1.0-py3-none-any.whl

Total release size: 213.0 kB

Release files / vlm_sieve-0.1.0.tar.gz

Download URL vlm_sieve-0.1.0.tar.gz
Size 190.7 kB
Tags Source
SHA-256 checksum
How to use checksums
325404986e625ab83b922147a82876afb5517a075c063b2aaaf8f594bab98f6f
BLAKE2b-256 checksum
How to use checksums
3141321ae6b92fb1abf0b259e3e346f49ef5240966b1c613776f4870ab582168
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.1

Release files / vlm_sieve-0.1.0-py3-none-any.whl

Download URL vlm_sieve-0.1.0-py3-none-any.whl
Size 22.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7a028d170f94d5c3ecf9b75bce2044a52fa05f6891caca42cbed515413ebdb54
BLAKE2b-256 checksum
How to use checksums
213c304ecbc575bf48631cb95aae63fcd760737adc77ca8e7efa812f6ca3851b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.1

Security for vlm-sieve 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page