edit: Note that what you see in this post is an old version, go further in this thread for recent versions:
http://chdk.setepontos.com/index.php/topic,471.msg17797.html#msg17797Documentation for MDFB-{VERSION}-Digic{II,III}.bas
Fast motion detector script with burst/review support and optional masking
Version 080411
by fudgey
-----
Note:
There are two versions of the script, one for Digic II and one
for Digic III. This is because the script uses the get_prop 206
or get_prop 205 commands depending on the platform to prevent
md shoot looping.
-----
This is my fast motion detection script. It's ment to be as fast as
currently possible (at least for my A570IS), but it also has a "slow
shoot" mode of operation. Both have their pros and cons. By "fast"
I mean the time from motion to shutter opening, a cruicial time in
things such as lightning photography.
As a direct consequence of the speed, this script will not autofocus
right before it shoots (unless it's in the slow shoot mode). In other
words this script does not focus to a moving object when one appears
before the lens. This is because focusing takes a lot of time.
If not in slow shoot mode, this script will autofocus and autoexposure
(if the camera is in automatic focus mode) when it starts and AFTER
every time it has finished shooting AND after a timeout. This means
that if an object appears in front of the camera and stays there,
moving around for a while, the camera will probably detect motion
again and you will get in-focus images too.
The speed that can be achieved is different for each camera model.
There will be significant variation from shot to shot too. Also, changing
parameters may affect speed of detection; at least if you set pixel step
too low. With my A570IS I get motion-to-shutter-opening delays between
80 ms and 150 ms, averaging at about 110 ms, but don't be surprised if
you occasionally get a significantly slower response.
This script will not change camera shooting parameters. You need to
set the camera into the shooting modes you want the script to operate
with. The camera will take images just as it would if it was you
who is keeping the shutter depressed:
- If in normal single shooting drive mode, a single photo will be
taken after each time motion is detected. If you set burst/review time
to >0 seconds, the shot will be reviewed on the LCD for the set time
before motion detection will be armed again.
- If in continous shooting drive mode, a burst of images will be taken.
The shooting time is set by burst/review time in seconds, it must
be greater than 0 for more than 1 shot to be taken. No extended
review of the last shot available. The number of shots is determined
by your camera's shooting speed and depends on things such as
camera model, shutter speed, ISO value, RAW mode, memory card speed,
noise reduction setting etc. This is why even a 3 second burst may
sometime give you no more than one photo.
- If in custom shooting mode, images will be taken after the delay
you have set in the camera and the AF light will blink, then 1 to 10
shots will be taken depending on how many you've selected (your
camera model may have a different set of parameters), and the AF
light will blink between the shots. Note that the repetition rate is
slower than in continous drive mode.
The script lets the camera autoexposure and autofocus unless you prevent
it. This means you can use shooting modes other than M with this script
(but not video mode). Be aware that exposure and focus are set first when
the script starts and after that only after the script shoots or timeouts.
Timeout is by default set to 5 minutes (300 seconds) so even fairly rapid
changes in sunlight will be adjusted to properly.
If you shoot fast objects, make sure focus isn't a problem (for
example by using manual focus to the distance your objects are moving
at by average and/or by shooting from a distance) and that shutter speed
is short enough.
If you shoot objects that move slowly, you can use the slow shoot option
to solve the focus problem.
This script is ment to shoot quickly after something moves on the LCD.
If you also want the script to be detecting motion again as soon as possible
after taking a shot, you should:
- use manual exposure (camera in M mode), must be set before running the script
- use manual focus, must be set before running the script
- set trigger delay to the minimum allowable in your camera in your
shooting conditions (possibly 0)
- set burst/review time = 0 unless you want several images in
continous shooting drive mode
- disable noise reduction in CHDK RAW menu (if using long exposures)
- disable RAW saving
- use a fast, defragmented memory card
Also, it may not be a good idea to enable histogram and zebra in a way
that makes them be displayed during motion detect.
Parameters:
===========
Channel:
Selects the color space component to monitor for motion detection.
U, Y and V are the YUV space color components. Default is to use luminance
Y (brightness). Alternatively red, green or blue channels of RGB color
space can be used.
Columns, Rows:
Motion detect works by averaging the color space parameter
selected in the "Channel" parameter on the screen. The screen is divided
in to N=columns*rows cells, and average is calculated for each
cell. Smaller cells may make detection more sensitive to smaller object
and smaller movements. The cells are drawn on the LCD when MD
is running. Also study the Pixel Step and threshold parameters.
Pixel Step:
An average is calculated from the contents of each cell.
This pixel step parameter makes this a bit more complicated, because
if it is >1, it makes the motion detector skip pixels and only
use every Nth pixel vertically and horizontally (N is the value you give,
default is every 6th pixel).
Setting this parameter 1 is not recommended, because calculating all
pixels will take a lot of time from the camera and you might end up using
more time than the 10 ms CHDK interval. Odd things might happen. If you
must use a low pixel step value, be instructed that you may need to increase
trigger delay (explained later). Setting this parameter too big will make
detection of small objects a matter of luck as they may or may not move
around the pixels that are being monitored.
Threshold:
The lower the threshold, the smaller a change in a cell will trigger
motion detection. In low light, higher threshold is required
or noise will cause spurious triggering. Along with decrease in the
number of cells, increasing threshold will make motion detect less
trigger-happy if you have problems with e.g. trees moving in the wind
causing unwanted pictures to be taken. Changing compare interval
will also affect triggering.
Compare interval (milliseconds):
Motion detect takes one sample from the display, then waits for
compare interval until taking another one and then compares these two.
The default is sub-minimal to optimize speed of detection.
If interval is low enough, objects that move slowly or gradual
changes in the amount of sunlight will not trigger motion detection
no matter how big the object or change in light is.
Note that while you can specify an interval as low as 1 ms, only
multiplies of 10 ms will actually have any effect as this is the
finest time step available for motion detection (it comes from the
camera's keyboard polling interval). And even further, 30 ms may be
the shortest you can go if/when the LCD update period is 30 ms.
So with intervals shorter 10 ms, the motion detector is probably
comparing identical pictures two times out of three but it is also
working at maximum available speed of detection because an LCD update
will be noticed during the first CHDK period after the update.
Trigger delay:
How long to wait for camera to stabilize before starting to detect
motion. A low value means the camera will be detecting motion again
promptly after taking a picture. A large value means that after
your shooting (and picture review) the motion detector will be
started and the motion detector grid will be drawn, but it will
not react to any movement until trigger delay has passed. This may
be useful for making sure you don't get too many images.
In past a long trigger delay was needed to prevent motion detector
scripts from entering eternal shooting loops, where the camera's
post-shooting actions (such as adjusting LCD contrast) caused
motion detect to trigger instantly. Increasing trigger delay
helped, but the problem was that a lot of things affected the
length of the required delay (slow shutter speeds, ISO values,
noise reduction setting, memory card speed, RAW saving, low light
to name a few).
This has now mostly been fixed with a few get_prop 206
(or 205) commands and the default trigger delay is 0.5 s. If
you experience loop anyway, try increasing trigger delay.
Burst/Review time:
If 0, take single shots. If >0, how many seconds to keep shutter
depressed. If continuous shooting mode is enabled ("burst mode"),
the camera will shoot using this mode, if it is not enabled, the
camera will show the shot review for the specified time on LCD
(unless your camera doesn't work this way or is configured in a way
that disables this review feature; review by holding the shutter
is a standard feature of your camera when it's in single shot
drive mode).
The script does not switch to continuous shooting mode, the user
must do this before running the script from the camera's own
menus.
Timeout:
This is the timeout value for the motion detector. Most of the time
this script sits in the motion detection function waiting for
motion. It exits this function only if motion is detected or
enough time passes (timeout). This timeout is specified in
tens of seconds (default is 30 == 300 seconds == 5 minutes).
The longer the timeout, the more of the script's time will be
spent in detecting motion. However, if you are not running in
the "Slow Shoot" mode, and you have autoexposure or autofocus
enabled, you may want to let the camera do those things every
now and then. The default of 5 minutes should be short enough
to adapt to changes in sunlight but long enough for almost
perfect coverage in time spent detecting motion (if autoexposure
and focus takes 3 seconds, this is 99 % coverage).
Slow Shoot:
When Slow Shoot is disabled, this script acts as a fast motion
detector as described above. This means it first half presses
the shutter (causing autoexposure and autofocus if those are
currently enabled in your camera) and fully presses the shutter
when it detects motion. This way the picture is taken very fast
after motion is detected, but the problem is that many objects
will be out of focus.
If you enable slow shoot, the script will go into motion
detection without pressing the shutter. When motion is detected,
the shutter is pressed and thus exposure and focus are set
and only when that's done with, a the picture is taken by
the camera.
Masking:
Using the four Mask/Use Columns/Rows Left/Right/Top/Bottom
parameters you can select any rectangular area on your LCD using
your MD cell grid as the resolution of this rectangle. You can
then set the motion detector to ignore everything inside that
rectangular mask (Masking=1, Mask) or to use only the cells in your
mask for motion detection (Masking=2, Use). To disable masking
entirely, set Masking=0 (No).
The green motion detection grid is always up to date with the
masking settings, so it's easy to see if you are getting the mask
you wanted (btw, this masking part was shamelessly copied directly
from Barney Fife's 'MD LightningB3' script).
---
Note: To conserve batteries, you may plug an A/V cable (any plug
or adapter that fits the jack will do, no need to have any
cables or equipment connected) to your A/V jack. What will
happen is that your LCD will turn off saving significant amounts
of battery power. Motion detection still works, because
the camera thinks you have an external TV monitor connected
and will update it's viewfinder image (which is what MD uses
for detection). Unplug to get your LCD image back (may not
be instant and you may need to interrupt the script, but there
should be no need to reboot the camera).
Note: This script is endless and will never end. This is not
good script coding practice, but checking for button presses
would take precious time from sitting in the motion detection
routine.
You can interrupt the script (like any script) by full-pressing
shutter. No camera settings are changed (I think), especially
not if you interrupt while MD is waiting for motion.
Technical note: md_detect_mode "PARAMETERS" argument is always 9.
This means "make immediate shoot" and the new feature, "no shutter
release on shoot" are enabled. In other words, md_detect_motion
presses the shutter and the script releases it.
Note: It appears that you can turn the mode dial while running
this script... so you can start in M modes with finely tuned
parameters and then try an automatic mode for a while and go
back to M mode without ever interrupting the script.
---
Thanks go to MX3 for md_detect_motion, Barney Fife for masking
code among other things, Anonymous for solving trigger delay loop
problems and all the other lovely CHDK developers.
Obviously, a standard disclaimer applies i.e. while I believe this script
to be as safe as any part of CHDK, there is no warranty and if this
breaks your camera or your marriage it is of your own responsibility
entirely. In that case you may feel better if you blame the cat.
---
TODO:
- Testing. Things I haven't tested much or at all:
* any camera other than a570is (this is a Digic III model)
* slow mode hasn't been tested much, especially not for trigger delay
loops. The current code is almost just a quick guess in that context.
- add option for number of continuous drive mode shoots to take using
the new commands for this (are they already in allbest or was it just propcase?).
Somehow avoid getting stuck if we're not in continous drive mode (propcase
may be used?)
- possibly make an option for not filling the card using the new disk space functions?
Or maybe an "exit after N*10 photos"? Would be a good safety option when
leaving the camera alone and fearing it may start triggering too often.
Maybe even shut down camera at exit.
- think about how to modify the script for video mode. MD doesn't seem to run
but modes dial changes have been implemented for a570is already (not in trunk
yet?) burst/review time could be burst/review/video time and slow on/off could
be fast/slow/video.
The current dialmode patch can only change mode, not poll (or is that already
possible using a propcase?). That's why video switching will change dialmode
and can't know what to return to... I guess it should (for now) use P mode
(dm_p) for motion detection and click (or press wait release) dm_video...