Skip to main content

🎞️ Adaptive Motion Preprocessing

Turn video into motion images your neural network can read.

While something moves, each window of frames becomes one grayscale picture: older frames dim, newer ones bright, so a single image shows where the subject went and how fast. Every image has the same shape, so your model never gets a surprise.

📦 Install

pip install adaptive-motion-preprocessing

Then import amprep.

🚀 Quick start

You bring the frames (any iterable of uint8 BGR arrays) and the package does the rest:

import cv2

from amprep import AdaptiveMotionPreprocessor


def frames_from(path):
    capture = cv2.VideoCapture(path)
    try:
        if not capture.isOpened():
            raise OSError(f"cannot open video: {path}")
        while True:
            ok, frame = capture.read()
            if not ok:
                return
            yield frame
    finally:
        capture.release()


for image in AdaptiveMotionPreprocessor().process(frames_from("clip.mp4")):
    print(image.data.shape)

🎥 Try it on your webcam

See your camera and the motion images side by side, live:

git clone https://github.com/sdrfsh/adaptive-motion-preprocessing
cd adaptive-motion-preprocessing
pip install -e .
python examples/live_camera.py

Stay out of shot for a second while it learns the background, then move. Press q or Esc to quit. Add --camera 1 for an external webcam, or --threshold 0.03 if it triggers when nothing is moving.

⚙️ Settings

All optional keyword arguments of AdaptiveMotionPreprocessor(...):

Setting Default What it does
motion_threshold 0.01 Share of the frame that must be moving before frames are collected
window_frames 10 Frames per window, and one image per full window
sample_frames 4 Frames painted into each image (at most window_frames)
width, height None Output size; leave unset to keep the frame size, or set both
noise_reducer median filter Your own NoiseReducer subclass
background_subtractor KNN Your own BackgroundSubtractor subclass

💡 Good to know

  • ⏱️ Frames, not seconds. 10 frames is about 0.33 s at 30 fps and 1 s at 10 fps. The package never reads the frame rate, so that math is yours.
  • 🔁 A steady stream. While motion lasts you get one image every window_frames frames. A half-full window is dropped when motion stops.
  • 🌱 Warm-up. The default subtractor spends its first 4 frames learning the background, so they never produce images. Change it with KNNBackgroundSubtractor(warmup_frames=...).
  • 🎬 New scene? Call reset(). It forgets the background, any half-built window and the frame size. Otherwise state carries over between process() calls.
  • 📐 One frame size per scene. Frames that change size mid-stream raise a ValueError. Call reset() first if the change is on purpose.

📚 Examples

Release files for adaptive-motion-preprocessing 1.0.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 adaptive-motion-preprocessing 1.0.0
File Size Uploaded
adaptive_motion_preprocessing-1.0.0.tar.gz 45.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for adaptive-motion-preprocessing 1.0.0
File Interpreter ABI Platform
adaptive_motion_preprocessing-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 67.8 kB

Release files / adaptive_motion_preprocessing-1.0.0.tar.gz

Download URL adaptive_motion_preprocessing-1.0.0.tar.gz
Size 45.8 kB
Tags Source
SHA-256 checksum
How to use checksums
53343d22a01c2c30ab85aea099e914ee629c5acc7083a083fdea68819cb2fe3d
BLAKE2b-256 checksum
How to use checksums
c546a166b01eaad3b6619cddebc81bef58bad441aad9284ded3e77786d786c14
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 14, 2026.

Transparency log

Release files / adaptive_motion_preprocessing-1.0.0-py3-none-any.whl

Download URL adaptive_motion_preprocessing-1.0.0-py3-none-any.whl
Size 22.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3ad6ef826503ec1c77a407c5d351986011375c404d3633715f80184f1ac52740
BLAKE2b-256 checksum
How to use checksums
270f43a4d026492dbbbdc05f4917fe282600b3a46229a0c3c383a6d95252eb86
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 14, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

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