This release is a pre-release and may not be stable for production use.
🎞️ 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_framesframes. 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 betweenprocess()calls. - 📐 One frame size per scene. Frames that change size mid-stream raise a
ValueError. Callreset()first if the change is on purpose.
📚 Examples
- examples/live_camera.py: watch it live on your webcam, camera and motion image side by side
- examples/from_video_file.py: run it on a video file
- examples/custom_background_subtractor.py: plug in your own stage
Release files for adaptive-motion-preprocessing 1.0.0rc1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| adaptive_motion_preprocessing-1.0.0rc1.tar.gz | 44.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| adaptive_motion_preprocessing-1.0.0rc1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 66.7 kB
Release files / adaptive_motion_preprocessing-1.0.0rc1.tar.gz
| Download URL | adaptive_motion_preprocessing-1.0.0rc1.tar.gz |
|---|---|
| Size | 44.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6088b22fb618e7630d97df6c89170ab4004341b4d3d0fa35cade25160402004f
|
|
BLAKE2b-256 checksum How to use checksums |
49e6cda3b7b8ac3c326c29eb3eebdf98109778f6b900710d9117cb383c73ecfd
|
| 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 13, 2026.
Transparency logRelease files / adaptive_motion_preprocessing-1.0.0rc1-py3-none-any.whl
| Download URL | adaptive_motion_preprocessing-1.0.0rc1-py3-none-any.whl |
|---|---|
| Size | 22.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
31cff2c532a9155817a8b7cf48ada9e88dfad8c0fcaf0b776846c4c9e01c3a65
|
|
BLAKE2b-256 checksum How to use checksums |
87465a6e24d25fc7819cfff3ba8e3d38154f72c419475f62b9024cb871df2801
|
| 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 13, 2026.
Transparency log