
# Motion Detector



Python module for comparing two frames from a video for motion.
***
## Installation
### Package Installation from PyPi
```bash
$ pip install motion-detector-g4
```
### Package Installation from Source Code
The source code is available on [GitHub](https://github.com/Genzo4/motion_detector).
Download and install the package:
```bash
$ git clone https://github.com/Genzo4/motion_detector
$ cd motion_detector
$ pip install -r requirements.txt
$ pip install .
```
***
## Basic usage
Import:
```python
from motion_detector_g4 import MotionDetector
```
Create an instance of the motion detector. You can specify additional options:
- min_area - minimum tracked change size (blob size).
Default value: 4000.
- max_area - maximum tracked change size (blob size).
Default value: 150000.
- noise_size - the maximum size of the removed noise.
Default value: 10.
- debug - debug mode. If it is enabled, intermediate frames are created,
showing the process of processing.
Default value: False.
```python
md = MotionDetector(min_area=4000, max_area=150000, noise_size=10, debug=False)
```
The module uses the following algorithm:
1. The first frame is being processed (method apply_first_frame).

2. The next frame is being processed (method check_motion):

- the first frame is subtracted from this frame using the module BackgroundSubtractorMOG2
from the library OpenCV. If debug mode is enabled, then a file is created with an additional
extension "mask" with the result of the module.

- remove noise using the morphologyEx module from the OpenCV library. This process
configured using the noise_size parameter. If debug mode is enabled, then
a file is created with the additional extension "clear" with the result of the module's operation.

- looking for areas of motion (blobs) larger than min_area but smaller than max_area. If debug mode is enabled, then
files are created with the extension "blobs" and "blobs2" with the result of the module's operation
(found areas are circled in red).


See the example.py file for an example of usage.
***

# Motion Detector



Python модуль для сравнения двух кадров из видеозаписи на предмет наличия движения в них.
***
## Установка
### Установка пакета с PyPi
```bash
$ pip install motion-detector-g4
```
### Установка пакета из исходного кода
Исходный код размещается на [GitHub](https://github.com/Genzo4/motion_detector).
Скачайте его и установите пакет:
```bash
$ git clone https://github.com/Genzo4/motion_detector
$ cd motion_detector
$ pip install -r requirements.txt
$ pip install .
```
***
## Использование
Подключаем:
```python
from motion_detector_g4 import MotionDetector
```
Создаём экземпляр детектора движения. Можно указать дополнительные параметры:
- min_area - минимальный размер отслеживаемого изменения (размера blob'а).
Значение по умолчанию: 4000.
- max_area - максимальный размер отслеживаемого изменения (размера blob'а).
Значение по умолчанию: 150000.
- noise_size - максимальный размер удаляемого "шума".
Значение по умолчанию: 10.
- debug - режим отладки. Если его включить, то создаются промежуточные кадры,
показывающие процесс обработки.
Значение по умолчанию: False.
```python
md = MotionDetector(min_area=4000, max_area=150000, noise_size=10, debug=False)
```
В модуле используется следующий алгоритм:
1. Подаётся на обработку первый кадр (метод apply_first_frame).

2. Подаётся на обработку следующий кадр (метод check_motion):

- из этого кадра "вычитается" первый кадр с помощью модуля BackgroundSubtractorMOG2
из библиотеки OpenCV. Если включён режим отладки, то создаётся файл с добавочным
расширением "mask" с результатом работы модуля.

- удаляем "шум" с помощью модуля morphologyEx из библиотеки OpenCV. Данный процесс
настраивается с помощью параметра noise_size. Если включён режим отладки, то
создаётся файл с добавочным расширением "clear" с результатом работы модуля.

- ищем области движения (blob'ы) размером больше min_area, но меньше max_area. Если включён режим отладки, то
создаются файлы с добавочным расширением "blobs" и "blobs2" с результатом работы модуля
(найденные области обводятся красными кругами).


Пример использования см. в файле example.py
Raw data
{
"_id": null,
"home_page": "https://github.com/Genzo4/motion_detector",
"name": "motion-detector-g4",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": "",
"keywords": "motiondetector,motion_detector,motion-detector,opencv,opencv-contrib-python,g4",
"author": "Genzo",
"author_email": "genzo@bk.ru",
"download_url": "https://files.pythonhosted.org/packages/5d/5e/d917f755401aa561fab851a63066eade85b5f245a082eab8120605982377/motion_detector_g4-1.2.0.tar.gz",
"platform": null,
"description": "\r\n\r\n# Motion Detector\r\n\r\n\r\n\r\n\r\n\r\nPython module for comparing two frames from a video for motion.\r\n\r\n***\r\n\r\n## Installation\r\n\r\n### Package Installation from PyPi\r\n\r\n```bash\r\n$ pip install motion-detector-g4\r\n```\r\n\r\n### Package Installation from Source Code\r\n\r\nThe source code is available on [GitHub](https://github.com/Genzo4/motion_detector). \r\nDownload and install the package:\r\n\r\n```bash\r\n$ git clone https://github.com/Genzo4/motion_detector\r\n$ cd motion_detector\r\n$ pip install -r requirements.txt\r\n$ pip install .\r\n```\r\n\r\n***\r\n\r\n## Basic usage\r\n\r\nImport:\r\n```python\r\nfrom motion_detector_g4 import MotionDetector\r\n```\r\n\r\nCreate an instance of the motion detector. You can specify additional options:\r\n- min_area - minimum tracked change size (blob size).\r\n Default value: 4000.\r\n- max_area - maximum tracked change size (blob size).\r\n Default value: 150000.\r\n- noise_size - the maximum size of the removed noise.\r\n Default value: 10.\r\n- debug - debug mode. If it is enabled, intermediate frames are created, \r\n showing the process of processing.\r\n Default value: False.\r\n\r\n```python\r\nmd = MotionDetector(min_area=4000, max_area=150000, noise_size=10, debug=False)\r\n```\r\n\r\nThe module uses the following algorithm:\r\n1. The first frame is being processed (method apply_first_frame).\r\n\r\n2. The next frame is being processed (method check_motion):\r\n\r\n - the first frame is subtracted from this frame using the module BackgroundSubtractorMOG2\r\n from the library OpenCV. If debug mode is enabled, then a file is created with an additional \r\n extension \"mask\" with the result of the module. \r\n \r\n - remove noise using the morphologyEx module from the OpenCV library. This process \r\n configured using the noise_size parameter. If debug mode is enabled, then \r\n a file is created with the additional extension \"clear\" with the result of the module's operation.\r\n \r\n - looking for areas of motion (blobs) larger than min_area but smaller than max_area. If debug mode is enabled, then \r\n files are created with the extension \"blobs\" and \"blobs2\" with the result of the module's operation \r\n (found areas are circled in red).\r\n \r\n \r\n\r\nSee the example.py file for an example of usage.\r\n\r\n***\r\n\r\n\r\n\r\n# Motion Detector\r\n\r\n\r\n\r\n\r\n\r\nPython \u043c\u043e\u0434\u0443\u043b\u044c \u0434\u043b\u044f \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f \u0434\u0432\u0443\u0445 \u043a\u0430\u0434\u0440\u043e\u0432 \u0438\u0437 \u0432\u0438\u0434\u0435\u043e\u0437\u0430\u043f\u0438\u0441\u0438 \u043d\u0430 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u043d\u0430\u043b\u0438\u0447\u0438\u044f \u0434\u0432\u0438\u0436\u0435\u043d\u0438\u044f \u0432 \u043d\u0438\u0445.\r\n\r\n***\r\n\r\n## \u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430\r\n\r\n### \u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u043f\u0430\u043a\u0435\u0442\u0430 \u0441 PyPi\r\n\r\n```bash\r\n$ pip install motion-detector-g4\r\n```\r\n\r\n### \u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u043f\u0430\u043a\u0435\u0442\u0430 \u0438\u0437 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430\r\n\r\n\u0418\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434 \u0440\u0430\u0437\u043c\u0435\u0449\u0430\u0435\u0442\u0441\u044f \u043d\u0430 [GitHub](https://github.com/Genzo4/motion_detector). \r\n\u0421\u043a\u0430\u0447\u0430\u0439\u0442\u0435 \u0435\u0433\u043e \u0438 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0435 \u043f\u0430\u043a\u0435\u0442:\r\n\r\n```bash\r\n$ git clone https://github.com/Genzo4/motion_detector\r\n$ cd motion_detector\r\n$ pip install -r requirements.txt\r\n$ pip install .\r\n```\r\n\r\n***\r\n\r\n## \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\r\n\r\n\u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u043c:\r\n```python\r\nfrom motion_detector_g4 import MotionDetector\r\n```\r\n\r\n\u0421\u043e\u0437\u0434\u0430\u0451\u043c \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u0434\u0435\u0442\u0435\u043a\u0442\u043e\u0440\u0430 \u0434\u0432\u0438\u0436\u0435\u043d\u0438\u044f. \u041c\u043e\u0436\u043d\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b:\r\n- min_area - \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u0440\u0430\u0437\u043c\u0435\u0440 \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u0435\u043c\u043e\u0433\u043e \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f (\u0440\u0430\u0437\u043c\u0435\u0440\u0430 blob'\u0430).\r\n \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e: 4000.\r\n- max_area - \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u0440\u0430\u0437\u043c\u0435\u0440 \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u0435\u043c\u043e\u0433\u043e \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f (\u0440\u0430\u0437\u043c\u0435\u0440\u0430 blob'\u0430).\r\n \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e: 150000.\r\n- noise_size - \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u0440\u0430\u0437\u043c\u0435\u0440 \u0443\u0434\u0430\u043b\u044f\u0435\u043c\u043e\u0433\u043e \"\u0448\u0443\u043c\u0430\".\r\n \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e: 10.\r\n- debug - \u0440\u0435\u0436\u0438\u043c \u043e\u0442\u043b\u0430\u0434\u043a\u0438. \u0415\u0441\u043b\u0438 \u0435\u0433\u043e \u0432\u043a\u043b\u044e\u0447\u0438\u0442\u044c, \u0442\u043e \u0441\u043e\u0437\u0434\u0430\u044e\u0442\u0441\u044f \u043f\u0440\u043e\u043c\u0435\u0436\u0443\u0442\u043e\u0447\u043d\u044b\u0435 \u043a\u0430\u0434\u0440\u044b,\r\n \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0435 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438.\r\n \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e: False.\r\n\r\n```python\r\nmd = MotionDetector(min_area=4000, max_area=150000, noise_size=10, debug=False)\r\n```\r\n\r\n\u0412 \u043c\u043e\u0434\u0443\u043b\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c:\r\n1. \u041f\u043e\u0434\u0430\u0451\u0442\u0441\u044f \u043d\u0430 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u043f\u0435\u0440\u0432\u044b\u0439 \u043a\u0430\u0434\u0440 (\u043c\u0435\u0442\u043e\u0434 apply_first_frame).\r\n\r\n2. \u041f\u043e\u0434\u0430\u0451\u0442\u0441\u044f \u043d\u0430 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043a\u0430\u0434\u0440 (\u043c\u0435\u0442\u043e\u0434 check_motion):\r\n\r\n - \u0438\u0437 \u044d\u0442\u043e\u0433\u043e \u043a\u0430\u0434\u0440\u0430 \"\u0432\u044b\u0447\u0438\u0442\u0430\u0435\u0442\u0441\u044f\" \u043f\u0435\u0440\u0432\u044b\u0439 \u043a\u0430\u0434\u0440 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u043e\u0434\u0443\u043b\u044f BackgroundSubtractorMOG2\r\n \u0438\u0437 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 OpenCV. \u0415\u0441\u043b\u0438 \u0432\u043a\u043b\u044e\u0447\u0451\u043d \u0440\u0435\u0436\u0438\u043c \u043e\u0442\u043b\u0430\u0434\u043a\u0438, \u0442\u043e \u0441\u043e\u0437\u0434\u0430\u0451\u0442\u0441\u044f \u0444\u0430\u0439\u043b \u0441 \u0434\u043e\u0431\u0430\u0432\u043e\u0447\u043d\u044b\u043c\r\n \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\u043c \"mask\" \u0441 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c \u0440\u0430\u0431\u043e\u0442\u044b \u043c\u043e\u0434\u0443\u043b\u044f. \r\n \r\n - \u0443\u0434\u0430\u043b\u044f\u0435\u043c \"\u0448\u0443\u043c\" \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u043e\u0434\u0443\u043b\u044f morphologyEx \u0438\u0437 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 OpenCV. \u0414\u0430\u043d\u043d\u044b\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\r\n \u043d\u0430\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430 noise_size. \u0415\u0441\u043b\u0438 \u0432\u043a\u043b\u044e\u0447\u0451\u043d \u0440\u0435\u0436\u0438\u043c \u043e\u0442\u043b\u0430\u0434\u043a\u0438, \u0442\u043e\r\n \u0441\u043e\u0437\u0434\u0430\u0451\u0442\u0441\u044f \u0444\u0430\u0439\u043b \u0441 \u0434\u043e\u0431\u0430\u0432\u043e\u0447\u043d\u044b\u043c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\u043c \"clear\" \u0441 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c \u0440\u0430\u0431\u043e\u0442\u044b \u043c\u043e\u0434\u0443\u043b\u044f.\r\n \r\n - \u0438\u0449\u0435\u043c \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u0434\u0432\u0438\u0436\u0435\u043d\u0438\u044f (blob'\u044b) \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u043c \u0431\u043e\u043b\u044c\u0448\u0435 min_area, \u043d\u043e \u043c\u0435\u043d\u044c\u0448\u0435 max_area. \u0415\u0441\u043b\u0438 \u0432\u043a\u043b\u044e\u0447\u0451\u043d \u0440\u0435\u0436\u0438\u043c \u043e\u0442\u043b\u0430\u0434\u043a\u0438, \u0442\u043e\r\n \u0441\u043e\u0437\u0434\u0430\u044e\u0442\u0441\u044f \u0444\u0430\u0439\u043b\u044b \u0441 \u0434\u043e\u0431\u0430\u0432\u043e\u0447\u043d\u044b\u043c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\u043c \"blobs\" \u0438 \"blobs2\" \u0441 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c \u0440\u0430\u0431\u043e\u0442\u044b \u043c\u043e\u0434\u0443\u043b\u044f \r\n (\u043d\u0430\u0439\u0434\u0435\u043d\u043d\u044b\u0435 \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u043e\u0431\u0432\u043e\u0434\u044f\u0442\u0441\u044f \u043a\u0440\u0430\u0441\u043d\u044b\u043c\u0438 \u043a\u0440\u0443\u0433\u0430\u043c\u0438).\r\n \r\n \r\n\r\n\u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0441\u043c. \u0432 \u0444\u0430\u0439\u043b\u0435 example.py\r\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Motion Detector",
"version": "1.2.0",
"project_urls": {
"Bug Tracker": "https://github.com/Genzo4/motion_detector/issues",
"Homepage": "https://github.com/Genzo4/motion_detector"
},
"split_keywords": [
"motiondetector",
"motion_detector",
"motion-detector",
"opencv",
"opencv-contrib-python",
"g4"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "313ebd1af5b69f2ef23fb632138b9aa694adf013dec4c1ec927b23e6ad912517",
"md5": "38b2f39ea18d4b7aae33c5b574badeb2",
"sha256": "15a5174ea137cf3c158c6981cf185f0f9b028186ec7e48a1ca1e7c1f3f43d444"
},
"downloads": -1,
"filename": "motion_detector_g4-1.2.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "38b2f39ea18d4b7aae33c5b574badeb2",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 6845,
"upload_time": "2023-10-05T07:07:25",
"upload_time_iso_8601": "2023-10-05T07:07:25.873902Z",
"url": "https://files.pythonhosted.org/packages/31/3e/bd1af5b69f2ef23fb632138b9aa694adf013dec4c1ec927b23e6ad912517/motion_detector_g4-1.2.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "5d5ed917f755401aa561fab851a63066eade85b5f245a082eab8120605982377",
"md5": "2f48c3aa9e3958b58e56ebc70e5e1d8b",
"sha256": "c4f77acfe11fbfbda733bc60494daeb72837926e25a201047e5f4f27b5aa55c5"
},
"downloads": -1,
"filename": "motion_detector_g4-1.2.0.tar.gz",
"has_sig": false,
"md5_digest": "2f48c3aa9e3958b58e56ebc70e5e1d8b",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 8396,
"upload_time": "2023-10-05T07:07:31",
"upload_time_iso_8601": "2023-10-05T07:07:31.059926Z",
"url": "https://files.pythonhosted.org/packages/5d/5e/d917f755401aa561fab851a63066eade85b5f245a082eab8120605982377/motion_detector_g4-1.2.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-10-05 07:07:31",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "Genzo4",
"github_project": "motion_detector",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [],
"lcname": "motion-detector-g4"
}