Skip to content

How to Tune Motion Tracking Settings

Claude Pageau edited this page Jul 1, 2021 · 8 revisions

If you are using a plugin you can copy any required config.py settings to the plugin. The plugin settings will over ride the current config.py settings. You can also create your own plugin. plugins allow customizing settings for certain situations. Eg some users have multiple cameras and/or camera locations

x_diff (Distance in pixels from last track point)

Here are my config.py motion settings image resolution 320x240 No plugin

# Motion Event Settings``
# ---------------------``
SPEED_MPH = False      # Set Speed Units   kph=False  mph=True``
track_counter = 5      # one cpu core=5 four cores=10 Number of Consecutive Motion Events to trigger speed photo``
                             ``# Testing with RPI2 B one core using opencv ver 3.4.2``
MIN_AREA = 100         # Default= 100 Exclude all contours less than or equal to this sq-px Area``
show_out_range = True  # Default= True Show Out of Range Events per x_diff settings below False= Off``
x_diff_max = 20        # Default= 20 Exclude if max px away >= last motion event x position``
x_diff_min = 1         # Default= 1 Exclude if min px away <= last event x position``
x_buf_adjust = 10      # Default= 10 Divides motion Rect x for L&R Buffer Space to Ensure contours are in``
track_timeout = 0.0    # Default= 0.0 Optional seconds to wait after track End (Avoids dual tracking)``
event_timeout = 0.3    # Default= 0.3 seconds to wait for next motion event before starting new track``
max_speed_over = 0     # Exclude track if Speed less than or equal to value specified 0=All``
# Can be useful to exclude pedestrians and/or bikes, Etc or track only fast objects``

Take a look at the data logs when you run speed-cam.py to view logs (Not as background task). Look at data of the problem tracks A good track should look like

----------------------------------------------------------------------`
2021-03-09 06:47:58 INFO     speed_camera New  - 0/5 xy(61,73) Start New Track`
2021-03-09 06:48:52 INFO     speed_camera Reset- event_timer 53.84>0.30 sec Exceeded`
2021-03-09 06:48:52 INFO     speed_camera New  - 0/5 xy(163,69) Start New Track`
2021-03-09 06:48:52 INFO     speed_camera  Add - 1/5 xy(152,70) 83.33 kph D=11/20 C=4 49x35=661 sqpx R2L`
2021-03-09 06:48:52 INFO     speed_camera  Add - 2/5 xy(142,70) 28.14 kph D=10/20 C=5 49x35=638 sqpx R2L`
2021-03-09 06:48:52 INFO     speed_camera  Add - 3/5 xy(132,71) 52.88 kph D=10/20 C=5 48x35=562 sqpx R2L`
2021-03-09 06:48:52 INFO     speed_camera  Add - 4/5 xy(122,70) 49.85 kph D=10/20 C=4 49x36=566 sqpx R2L`
2021-03-09 06:48:52 INFO     speed_camera  Add - 5/5 xy(112,70) 26.94 kph D=10/20 C=4 51x37=652 sqpx R2L`
2021-03-09 06:48:52 INFO     speed_camera  Saved media/images/speed-20210308-1215/speed-20210309-0648529.jpg`
2021-03-09 06:48:53 INFO     isSQLite3  Success: File is sqlite3 Format /home/pi/speed-camera/data/speed_cam.db`
2021-03-09 06:48:53 INFO     db_check   Success: sqlite3 Connected to DB /home/pi/speed-camera/data/speed_cam.db`
2021-03-09 06:48:54 INFO     speed_camera  SQL - Inserted sqlite3 Data Row into /home/pi/speed-camera/data/speed_cam.db`
2021-03-09 06:48:54 INFO     saveRecent    symlink /home/pi/speed-camera/media/recent/speed-20210309-0648529.jpg`
2021-03-09 06:48:54 INFO     speed_camera End  - R2L Ave Speed 48.2 kph Tracked 51 px in 0.248 sec Calib 85px 4700mm`
----------------------------------------------------------------------`

D= is x_diff (x distance away from previous track point. This track is telling you it had a value eg 11 out of 20 where 20 is the config.py setting. There will be some variation of speed and tracking due to variations in object angle on camera (different distance and movement of opencv contours. C= is the number of contours. Speed camera tracks the largest contour. x-diff is designed to eliminate other maybe larger contours that are farther away like another object or part of the same object..

----------------------------------------------------------------------`
2021-03-09 06:53:00 INFO     speed_camera Reset- event_timer 219.27>0.30 sec Exceeded`
2021-03-09 06:53:00 INFO     speed_camera New  - 0/5 xy(34,87) Start New Track`
2021-03-09 06:53:00 INFO     speed_camera  Add - 1/5 xy(46,86) 35.56 kph D=12/20 C=2 27x29=486 sqpx L2R`
2021-03-09 06:53:00 INFO     speed_camera  Add - 2/5 xy(57,85) 107.92 kph D=11/20 C=2 27x30=558 sqpx L2R`
2021-03-09 06:53:00 INFO     speed_camera  Add - 3/5 xy(63,84) 17.98 kph D=6/20 C=2 32x30=504 sqpx L2R`
2021-03-09 06:53:00 INFO     speed_camera  Out - 3/5 xy(107,83) Max D=44>=20px C=3 22x31=506 sqpx L2R`
2021-03-09 06:53:00 INFO     speed_camera  Add - 4/5 xy(118,82) 12.33 kph D=11/20 C=5 22x32=482 sqpx L2R`
2021-03-09 06:53:00 INFO     speed_camera  Add - 5/5 xy(130,83) 63.77 kph D=12/20 C=7 21x30=404 sqpx L2R`
2021-03-09 06:53:00 INFO     speed_camera  Saved media/images/speed-20210308-1215/speed-20210309-0653004.jpg`
2021-03-09 06:53:00 INFO     isSQLite3  Success: File is sqlite3 Format /home/pi/speed-camera/data/speed_cam.db`
2021-03-09 06:53:00 INFO     db_check   Success: sqlite3 Connected to DB /home/pi/speed-camera/data/speed_cam.db`
2021-03-09 06:53:01 INFO     speed_camera  SQL - Inserted sqlite3 Data Row into /home/pi/speed-camera/data/speed_cam.db`
2021-03-09 06:53:01 INFO     saveRecent    symlink /home/pi/speed-camera/media/recent/speed-20210309-0653004.jpg`
2021-03-09 06:53:01 INFO     speed_camera End  - L2R Ave Speed 47.5 kph Tracked 96 px in 0.392 sec Calib 80px 4700mm`
----------------------------------------------------------------------`

Example track above show one OUT log entry in bold. You can see D= exceeded the max so was rejected but track was still successful since the timeout was not exceeded. If settings are not right or vehicle speed is too great for your situation you will get a lot of OUT entries and track may timeout. You will then need to adjust the config.py motion settings. Larger objects (trucks, busses, limos, Etc) and event some larger objects can be detected as multiple contours like wheel wells, trunk, graphics, shadows, reflected lighting, Etc. Check the C= value to see how many contours were detected.. Too many can be a problem and you may need to change camera position, resolution, Etc. Note you may also see OUT due to x-diff being less than 1 (contour detected behind previous point)

If objects are moving faster and you get a lot of track timeouts and incomplete tracks eg track_count not achieve and could not calculate a final speed

If objects are too close or too large eg camera is too close or objects too large in camera view then speed camera will have problems.
Take a look at your log tracking info and try to diagnose what your problem could be base on info above.

Tune Out smaller moving objects

Recommend you Upgrade speed camera to the latest version using menubox.sh UPGRADE menu pick

You can use the one or both config.py setting below to tune out smaller moving object like pedestrians, bikes, Etc.

MIN_AREA = 200  

and or

max_speed_over = 0  

Start ./speed-cam.py with vebose enabled. Monitor log activity to see what smaller moving object average square pixel value and speed are. In config.py set MIN_SREA = value and or max_speed_over = variables close to observed log values.

You can also view historical data by enabling csv logging by setting the config.py variable below

log_data_to_CSV = True

Run speed camera for a while to collect data and update the speed-cam.csv

Then run the ./makehtml.py script. This can also be run from menuubox.sh by running the HTML menu pick and viewing results in the web browser interface under html directory.

Note this report defaults to last 100 records but can be increased by editing the HTML_MAX_FILES variable in the makehtml.py script. I am looking at putting makehtml.py settings in config.py rather than in the makehtml.py

IMPORTANT Try not to make the MIN_AREA and/or max_speed_over variable too large since you may eliminate some objects that are tracked with smaller contours due to lighting or style. Example sometimes a wheel, trunk or other smaller areas can be tracked on larger moving objects. This can depend on the camera distance away from the moving objects. Ideally greater than approx 15 meters (50 feet)

Also I have found I get best results by using a smaller stream image size that reduces the amount of opencv processing. Default is 320x240 for webcam and pi-camera