A motion detector
Project description
python-detection
A motion detector. Currently two detectors, SimpleDetector
and BlobDetector
are implemented.
The former performs simple frame differencing, and may not be useful for most purposes.
The latter does a bit more and utilizes the OpenCV
MOG2
background subtractor to get more realistic results. The detectors try to use OpenCL if it's available.
Using the BlobDetector
:
import numpy as np
from detection.detectors.blob.detector import BlobDetector
detector = BlobDetector({"blur": 7, "learningrate": 0.001, "accelerate": True})
(_boundingboxes, _metadata) = detector.feed(
np.reshape(np.frombuffer(frame, dtype=np.uint8), newshape=(height, width, 3))
)
The blur
value, if specified, will be performed before feeding the frame to the detector.
If no blur
is given, the frame is fed as-is.
The learningrate
parameter is described
here.
If no learningrate
is specified, it will default to 0.001
.
The accelerate
parameter defaults to True
, and the detector attempts to exploit any GPU resources
available on the host.
The metadata contains details (in order) about the operations performed on the input image, and for how long each of those took:
{
'start_timestamp': datetime.datetime(2020, 6, 16, 17, 7, 6, 443946),
'took': [
('cv.UMat', 0.0007669925689697266),
('cv.GaussianBlur', 0.0014679431915283203),
('cv.BackgroundSubtractorMOG2.apply', 0.0003933906555175781),
('cv.findContours', 0.001050710678100586),
('cv.approxPolyDP', 1.7404556274414062e-05),
('cv.boundingRect', 2.384185791015625e-06),
('total', 0.005391)
],
'end_timestamp': datetime.datetime(2020, 6, 16, 17, 7, 6, 449337)}
}
The bounding boxes contain any detected movement:
{
(
UUID('091c2620-ebb6-4cbe-b59d-42da156c3ba9'),
datetime.datetime(2020, 6, 16, 16, 11, 35, 931755)
): (279, 324, 710, 755),
(
UUID('bcbf2e18-ed2b-4e68-b94c-abe021071e61'),
datetime.datetime(2020, 6, 16, 16, 11, 35, 931755)
): (489, 324, 920, 755)
}
The UUIDs are here to provide forward compatibility with a hypothetical new detector that also tracks movement.
Development
TLDR:
Create and activate a Python 3.7 virtualenv:
$ python3.7 -m venv .venv && . .venv/bin/activate
Change to a branch:
git checkout -b my_branch
Install Poetry: https://python-poetry.org/docs/#installation
Install project deps and pre-commit hooks:
poetry install
pre-commit install
pre-commit run --all-files
Ready to go.
Remember to activate your virtualenv whenever working on the repo, this is needed because pylint and mypy pre-commit hooks use the "system" python for now (because reasons).
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
File details
Details for the file detection-0.1.2-py3-none-any.whl
.
File metadata
- Download URL: detection-0.1.2-py3-none-any.whl
- Upload date:
- Size: 7.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.1.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d797810777ed19bbd3b6b96d44e534c6f989d0b2f64fcc934d904d3c2ddfb567 |
|
MD5 | 08f77696a718594235f0637025cf4c9f |
|
BLAKE2b-256 | 62704be7c70f4f228c3cb18fffeed1909a47cf57a66b4df703d1a3fa8c2295fb |