Breakthrough / PySceneDetect

:movie_camera: Python and OpenCV-based scene cut/transition detection program & library.
https://www.scenedetect.com/
BSD 3-Clause "New" or "Revised" License
3.16k stars 387 forks source link

Add MotionDetector / detect-motion Command #43

Open xufuou opened 7 years ago

xufuou commented 7 years ago

Hey @Breakthrough, I noticed that you have started the implementation of a Motion Detector algorithm but it hasn't been finished. I was wondering if you could provide me some insights or any reference in how the algorithm works. Thanks for your work!

Breakthrough commented 7 years ago

EDIT AFTER ~2 YEARS: After much thought, what I said below was not the best approach for going forwards, and I have changed the project's development goals to reflect this. DVR-Scan will be absorbed into PySceneDetect in order to simplify development, maintenance, and software distribution (see below). I am leaving the original comment here for transparency, however.

Edit (Sept 8/2019): I may keep DVR-Scan for commercial users, but rewrite it to use the new detect-motion command (i.e. have it depend on PySceneDetect, and serve as an example of what you can build with the library) to allow for some transition time to re-evaluate DVR-Scan.


Hello @xufuou;

Thanks for the comment. As an update in this regard, I realized this is different than the goal of PySceneDetect - namely, this is to detect sparse scenes, where PySceneDetect is for linear scene boundaries. Thus, the full version of this algorithm has been implemented in a different project which you can also find here on Github: DVR-Scan.

Ultimately, support for the MotionDetector class has been depreciated since the release of DVR-Scan, although with API improvements in the next release of PySceneDetect (v0.5), adding support for sparse timecodes will be evaluated at this time, which will determine if this will be included as a feature for future releases. Until that time, for people requiring motion detection support, I highly recommend you check out the DVR-Scan project.


That being said, the following is an overview of the implementation as it sits for PySceneDetect.

The general idea is to perform background subtraction (which uses a given number of frames to determine what is "background" and what is "foreground", or moving). At this point, morphological transformations are used on the shapes/blobs in the foreground as a kind of noise removal technique, ensuring real movements cause the frame score to go up, not noise.

Thus, for any given frame, there is a score determining the amount of "motion" in the scene - the threshold. This, which combined with the other, constant, input (the kernel size for the morphological transform - typically 3x3 for SD video, 5x5 to 7x7 for HD video), are the two parameters which control the motion detection algorithm.

Since each frame generates a score, it can be used like any other scene detection algorithm in PySceneDetect. When the motion score for a frame exceeds a certain value, a frame boundary is set, and when it falls back below, another is set, marking the exact frames in the video where motion occurs - or, where it doesn't.


I hope that's the kind of reference you were looking for... If you have any further questions/comments, feel free to share them, I would be happy to address them - and lastly, thank you for your interest in PySceneDetect.

Breakthrough commented 5 years ago

Hi @xufuou;

I've decided to revisit this issue, in retrospect there is no reason PySceneDetect can't take on the capabilities of both programs and "absorb" that particular detector from DVR-Scan. I thought that keeping the projects separate was the correct way to go initially, but after giving it much thought, that was not the correct approach. You can see the SceneStats project for a summary of how things will proceed.

There will need to be some modifications made to PySceneDetect to handle the differences between linear/dense and sparse detectors, but this is definitely possible and something I think is worth following up with. I've reopened this issue to track progress on the matter.

In addition, documentation will also need to be created to help users transitioning from DVR-Scan by providing the equivalent PySceneDetect command line options to achieve the same output as from DVR-Scan, although now the output will be of much higher quality.

Thanks.

Hellowlol commented 5 years ago

Any updates on this one?

Breakthrough commented 5 years ago

Hi @Hellowlol;

Unfortunately no updates yet - if you need motion detection, you can use DVR-Scan in the meantime (which does a role similar to what PySceneDetect does, but using background subtraction for motion detection instead of content-aware scene changes).

I still need to figure out how to get sparse/dense scene detectors to "play" nicely with each-other (esp. with respect to the statsfile).

Thanks!

Edit: Just took a first go at adding the required changes to the API, it wasn't too bad actually. I managed to do it in a way that doesn't break anything that exists and only expands upon it by using inheritance, so that was nice as well.

Breakthrough commented 3 years ago

Quick update:

In Python, there is a working MotionDetector detection algorithm that you can use. It is not exposed to the CLI currently, however. There are some upcoming changes to the Python API for v0.6.x that I am proposing right now, which should make this much easier to integrate with the rest of PySceneDetect intuitively once that has been completed.

In the meantime, anyone requiring motion scanning via command line can use DVR-Scan (https://github.com/Breakthrough/DVR-Scan).