User avatar
dozencrows
Posts: 172
Joined: Sat Aug 04, 2012 6:02 pm

Re: MMAL Motion - tuning & optimisations

Sat May 10, 2014 6:10 pm

Actually, fixed it already anyway - if an image dimension isn't a multiple of 16 when using the mmal camera settings, it will be rounded up to the nearest 16.

So if you set 320 x 180, you'll actually get 320 x 192.

New source now in Github, and updated binary in Dropbox.

andies
Posts: 140
Joined: Mon Nov 11, 2013 8:12 pm
Location: Berlin

Re: MMAL Motion - tuning & optimisations

Sun May 11, 2014 11:40 am

I was aware of the 16-issue but did not realize that 360 mod 16 != 0 :?

Now, the raspberry camera has a "final" resolution of 2592x1944. If one wants to see the entire picture the width-height ration must be 2592:1944 = 4:3. But then, isn't it a good idea only to use the following resolutions:

64 x 48 secondary_buffer 40
128 x 96 secondary_buffer 20
192 x 144 secondary_buffer 13
256 x 192 secondary_buffer 10
320 x 240 secondary_buffer 8
384 x 288 secondary_buffer 6
448 x 336 secondary_buffer 5
512 x 384 secondary_buffer 5
576 x 432 secondary_buffer 4
640 x 480 secondary_buffer 4
704 x 528 secondary_buffer 3
768 x 576 secondary_buffer 3
832 x 624 secondary_buffer 3
896 x 672 secondary_buffer 2
960 x 720 secondary_buffer 2
1024 x768 secondary_buffer 2
1088 x816 secondary_buffer 2
1152 x864 secondary_buffer 2
1216 x912 secondary_buffer 2
1280 x960 secondary_buffer 2

Also, the bold ones seem to fetch the best from the camera (=resolution after secondary is close to the "final" resolution). Although other resolutions work, here we get the best results. Don't we?
raspberry B, Noir camera, Mac Book Air, iPhone, Bezzera

User avatar
dozencrows
Posts: 172
Joined: Sat Aug 04, 2012 6:02 pm

Re: MMAL Motion - tuning & optimisations

Sun May 11, 2014 12:06 pm

Your math looks correct to me - the bold entries coming closest to the full resolution of the camera.

Actual experience may vary depending on your Pi's firmware version and on the frame rate set in motion - see http://www.raspberrypi.org/new-camera-mode-released. For most realistic use cases with motion (frame rate <= 15fps), the maximum possible size should be almost the full sensor dimensions.

kyuzumaki
Posts: 22
Joined: Sat Apr 26, 2014 6:49 pm

Re: MMAL Motion - tuning & optimisations

Tue May 13, 2014 12:58 am

dozencrows wrote:@andies and @kyuzumaki - good news for you both! I think you can solve your problems without a new build...

For upside down images, the option for mmalcam_control_params should be "--vflip" or "-vf".

For night images, try this for mmalcam_control_params: "-ex night -mm average -ev 8 -ifx denoise" and set mmalcam_secondary_buffer_jpeg to 14 (to match what you had for the -q parameter).

I've tested each case and can see that these options do change the image correspondingly.
Brilliant! Hadn't known that option fed the parameters to the camera!

I initially tried this with your other build without the secondary buffer which gave poor results. I upgraded to the build from this thread and updated my firmware to the latest version. Using those same settings it works very well now in low light/night.

Below are the changes in my config should anyone else be trying to set up low light/IR night detection

Code: Select all

 
84 # Image width (pixels). Valid range: Camera dependent, default: 352
85 width 1280
87 # Image height (pixels). Valid range: Camera dependent, default: 288
88 height 960

92 framerate 2

156 mmalcam_control_params -ex night -mm average -ev 8

177 mmalcam_secondary_buffer_upscale 0
I have had to disable the secondary buffer for now as I am unable to view the stream at all when its enabled. Is this part of its normal behaviour? I used 320x240 with upscale of 4 to match the settings I'm using with the buffer disabled

Would it be possible to have two configurations for the camera that switch dependant on current time something like this.

Code: Select all

mmalcam_control_params1 -ex night -mm average -ev 8
mmalcam_control_params2 -ex auto -mm average -ev 0
#Where start is the time in hours at which the parameters take effect
mmalcam_params1_start 22
mmalcam_params1_start 6
EDIT: For the meantime theres a workaround

I've made two shell scripts for crontab to edit the config and restart motion at dawn and dusk if anyones reading this and needs something similar.

To make this work you need to make two config files one motion.conf.night one motion.conf.day you can save them anywhere you like but I suggest alongside /etc/motion.conf for ease

Create a new day.sh and night.sh store these wherever you like below is the day version of the script

Code: Select all

  1 #!/bin/bash
  2 sudo cp /etc/motion.conf.day /etc/motion.conf
  3 sudo service motion restart
open crontab

Code: Select all

crontab -e
add the two lines below. The number means hours so 6 is 6am 21 is 9pm edit them to match your sunrise and sunset time

Code: Select all

 
0 6 * * * /home/pi/day.sh > /home/pi/day.log 2>&1
0 21 * * * /home/pi/night.sh > /home/pi/night.log 2>&1

kyuzumaki
Posts: 22
Joined: Sat Apr 26, 2014 6:49 pm

Re: MMAL Motion - tuning & optimisations

Thu May 15, 2014 7:45 am

Just read a post about inbuilt h.264 hardware motion detection on the gpu. Replacing motions cpu intensive algorithm with this inbuilt hardware system would surely improve performance.
It seems the vector system has been exposed for use in the last update http://www.raspberrypi.org/forums/viewt ... 5&start=25

andies
Posts: 140
Joined: Mon Nov 11, 2013 8:12 pm
Location: Berlin

Re: MMAL Motion - tuning & optimisations

Thu May 15, 2014 11:41 am

May I ask something about
This "pre-encoded" secondary buffer is then used as the output for writing pictures, MJPEG stream output and external pipe.
What exactly does that mean: Is the small resolution pre-encoded? Is the higher resolution pre-encoded? And why does that save CPU? I was not able to get this working (my motion simply crashed) and/or I did not see any difference. So when should this be enabled? And what value is desirable?
raspberry B, Noir camera, Mac Book Air, iPhone, Bezzera

User avatar
dozencrows
Posts: 172
Joined: Sat Aug 04, 2012 6:02 pm

Re: MMAL Motion - tuning & optimisations

Thu May 15, 2014 12:22 pm

@andies: The high resolution image is encoded. It reduces the CPU load as the encoding happens on the GPU instead, enabling better performance. This option is intended to be used when you want higher frame rate capture and don't need to have any text or location marker overlays on the captured images. The values for the options indicate the quality of the compressed image from 1 (highly compressed, smaller size, lesser quality) to 100 (less compressed, larger size, higher quality).

It is strange that this is crashing for you. What memory size setting does your Pi have? I would recommend allowing as much as you can for the GPU, as this option requires more memory on that side of things.

@kyuzumaki: yes, the H.264 motion vector approach looks very interesting. When I can make some time free, I want to look into it. It may easier to start a new codebase for that rather than modify motion, as the motion codebase structure is quite closely coupled to the software based image processing algorithms it uses. However motion does have a very flexible feature set for outputting images, which is very useful to have.

andies
Posts: 140
Joined: Mon Nov 11, 2013 8:12 pm
Location: Berlin

Re: MMAL Motion - tuning & optimisations

Sun May 18, 2014 7:33 pm

It is strange that this is crashing for you. What memory size setting does your Pi have? I would recommend allowing as much as you can for the GPU, as this option requires more memory on that side of things.
I have a 16GB card and free -h says

Code: Select all

[email protected] ~ $ free -h
             total       used       free     shared    buffers     cached
Mem:          373M       106M       267M         0B       9,1M        38M
-/+ buffers/cache:        59M       314M
Swap:          99M         0B        99M
And gpu_mem=128 in /boot/config.txt

I also had a camera issues (cable was broken) but this was not the reason. I am getting the same problems with the new cable. Well, I do not have high framerates, so that is not necessary.
raspberry B, Noir camera, Mac Book Air, iPhone, Bezzera

tgwaste
Posts: 41
Joined: Thu Jan 31, 2013 7:15 pm

Re: MMAL Motion - tuning & optimisations

Sun May 18, 2014 11:22 pm

Hi,
I want to zoom out to see as much as my camera lens will allow. If I use -mm matrix with raspistill this works but if I do:

mmalcam_control_params "-mm matrix"

Nothing seems to change.

Any ideas?

tgwaste
Posts: 41
Joined: Thu Jan 31, 2013 7:15 pm

Re: MMAL Motion - tuning & optimisations

Sun May 18, 2014 11:41 pm

Also,
I don't really understand what to download.

I tried the dropbox as well as a git clone and compile.

the dropbox:

[-1242639520] [NTC] [ALL] conf_load: Processing thread 0 - config file ./motion-mmalcam.conf
[-1242639520] [ALR] [ALL] conf_cmdparse: Unknown config option "mmalcam_secondary_buffer_upscale"
[-1242639520] [ALR] [ALL] conf_cmdparse: Unknown config option "stream_secondary"
[-1242639520] [NTC] [ALL] motion_startup: Motion mmaltest Started
[-1242639520] [NTC] [ALL] motion_startup: Logging to file (/usr/local/etc/tmp/motion.log)

the git clone tons more errors.

The instructions in this thread say to use things like: mmalcam_secondary_buffer_upscale & stream_secondary but those options don't seem to be valid.

tgwaste
Posts: 41
Joined: Thu Jan 31, 2013 7:15 pm

Re: MMAL Motion - tuning & optimisations

Mon May 19, 2014 12:05 am

tgwaste wrote:Hi,
I want to zoom out to see as much as my camera lens will allow. If I use -mm matrix with raspistill this works but if I do:

mmalcam_control_params "-mm matrix"

Nothing seems to change.

Any ideas?
figured this out. I have to use mmalcam_use_still. Would be cool if I didn't :)

tgwaste
Posts: 41
Joined: Thu Jan 31, 2013 7:15 pm

Re: MMAL Motion - tuning & optimisations

Mon May 19, 2014 2:52 am

now motion has stopped working completely.

log:

[0] [NTC] [ALL] [May 18 19:53:39] main: Thread 1 is device: /dev/video0 input -1
[0] [NTC] [ALL] [May 18 19:53:39] main: Stream port 8081
[0] [NTC] [ALL] [May 18 19:53:39] main: Waiting for threads to finish, pid: 12077
[1] [NTC] [ALL] [May 18 19:53:39] motion_init: Thread 1 started , motion detection Enabled
[1] [ALR] [VID] [May 18 19:53:39] mmalcam_start: MMAL Camera thread starting... for camera (vc.ril.camera) of 640 x 480 at 15 fps
[1] [ALR] [VID] [May 18 19:53:39] mmalcam_start: MMAL Camera using video capture
[0] [NTC] [STR] [May 18 19:53:39] httpd_run: motion-httpd testing : IPV4 addr: 127.0.0.1 port: 8080
[0] [NTC] [STR] [May 18 19:53:39] httpd_run: motion-httpd Bound : IPV4 addr: 127.0.0.1 port: 8080
[0] [NTC] [STR] [May 18 19:53:39] httpd_run: motion-httpd/mmaltest running, accepting connections
[0] [NTC] [STR] [May 18 19:53:39] httpd_run: motion-httpd: waiting for data on 127.0.0.1 port TCP 8080
[0] [ERR] [ALL] [May 18 19:54:09] main: Thread 1 - Watchdog timeout, trying to do a graceful restart
[0] [NTC] [STR] [May 18 19:54:09] httpd_run: motion-httpd - Finishing
[0] [NTC] [STR] [May 18 19:54:09] httpd_run: motion-httpd Closing
[0] [NTC] [STR] [May 18 19:54:09] motion_web_control: motion-httpd thread exit

tried to restart and still nothing and I have to kill -9 it now. control-c doesn't work.

User avatar
dozencrows
Posts: 172
Joined: Sat Aug 04, 2012 6:02 pm

Re: MMAL Motion - tuning & optimisations

Mon May 19, 2014 8:28 am

@tgwaste - with regard to the "unknown config option" errors - that's probably because you're not running the right motion executable for the config file. Do you also have the original motion package installed from the Raspbian repositories?

Regarding it not working completely - it looks like you've uncommented the device option (/dev/video0). That's not the right option to enable the MMAL camera support; it should be commented out (put a # or a ; at the start of the line).

If none of that helps, please post the command line that you use to launch motion, and also make your config file available in some way (e.g. in a public Dropbox folder) so I can try it for myself.

@andies - please post the config file that causes your Pi to crash to your public Dropbox folder for me? I'll try running it and see what I get.

andies
Posts: 140
Joined: Mon Nov 11, 2013 8:12 pm
Location: Berlin

Re: MMAL Motion - tuning & optimisations

Mon May 19, 2014 5:02 pm

@andies - please post the config file that causes your Pi to crash to your public Dropbox folder for me? I'll try running it and see what I get.
Here it is: https://dl.dropboxusercontent.com/u/495 ... alcam.conf
raspberry B, Noir camera, Mac Book Air, iPhone, Bezzera

User avatar
dozencrows
Posts: 172
Joined: Sat Aug 04, 2012 6:02 pm

Re: MMAL Motion - tuning & optimisations

Tue May 20, 2014 8:24 am

Many thanks! I see you have a mask image specified in the settings; please would you supply a copy of that also?

andies
Posts: 140
Joined: Mon Nov 11, 2013 8:12 pm
Location: Berlin

Re: MMAL Motion - tuning & optimisations

Tue May 20, 2014 9:17 am

raspberry B, Noir camera, Mac Book Air, iPhone, Bezzera

User avatar
dozencrows
Posts: 172
Joined: Sat Aug 04, 2012 6:02 pm

Re: MMAL Motion - tuning & optimisations

Sat May 24, 2014 3:58 pm

Many thanks!

I've just tried your config file and mask file directly on my camera Pi, with the same GPU memory (128Mb) - and it is working fine for me. All I have done is to recreate your directory structure for the mask file and where motion images are saved.

I started motion directly from the command line, using the latest binary build from my public dropbox folder. This started up in daemon mode, and I could:
  • See the live feed (512x384) in Firefox
  • See images captured when motion detected at 2560 x 1920 resolution
Please would you try the following for me...

Run each of these commands and post your output:

Code: Select all

uname -a
/opt/vc/bin/vcgencmd version
Increase the log_level setting in the motion config file to 8 and try running it - then post your log file to dropbox for me to examine.

And finally, confirm for me how you're starting up motion when it crashes - e.g. are you just running it from a shell, or are you using some way of starting it automatically after booting up?

andies
Posts: 140
Joined: Mon Nov 11, 2013 8:12 pm
Location: Berlin

Re: MMAL Motion - tuning & optimisations

Sat May 24, 2014 7:49 pm

Code: Select all

uname -a
Linux Garage 3.12.19+ #684 PREEMPT Fri May 16 19:27:32 BST 2014 armv6l GNU/Linux

/opt/vc/bin/vcgencmd version
May 16 2014 18:45:07 
Copyright (c) 2012 Broadcom
version 3a67cfadce4d248b764d06dea2291917bc5db802 (clean) (release)
Logfile is here: https://dl.dropboxusercontent.com/u/4953669/motion.log
how you're starting up motion when it crashes?
I am starting motion with /etc/rc.local and

Code: Select all

/home/pi/motion/motion -c /home/pi/motion/motion-mmalcam.conf
raspberry B, Noir camera, Mac Book Air, iPhone, Bezzera

tgwaste
Posts: 41
Joined: Thu Jan 31, 2013 7:15 pm

Re: MMAL Motion - tuning & optimisations

Sat May 24, 2014 7:58 pm

etc/rc.local never worked for me for auto startup.

i wrote this script. edit paths:

then just put

@hourly root /path/to/motion.sh start >/dev/null

in crontab

i restart motion every hour because it gets locked up so I do:

@hourly root /path/to/motion.sh restart >/dev/null



#!/bin/bash

pid=`ps ax | grep motion | grep conf | grep -v grep | awk '{print $1}'`
cmd="/usr/src/motion/motion -c /usr/src/motion/motion.conf"

if [ "$1" = "start" ]; then
if [ "$pid" != "" ] && [ "$pid" -gt "0" ]; then
# already running
echo "motion: already running"
exit
fi
# start
nohup $cmd 2>/dev/null
echo "motion: started"
exit
fi

if [ "$1" = "restart" ]; then
# kill if running
if [ "$pid" != "" ] && [ "$pid" -gt "0" ]; then
kill -9 $pid
echo "motion: stopped"
fi
# start
nohup $cmd 2>/dev/null
echo "motion: started"
exit
fi

if [ "$1" = "stop" ]; then
# kill if running
if [ "$pid" != "" ] && [ "$pid" -gt "0" ]; then
kill -9 $pid
echo "motion: stopped"
else
echo "motion: not running"
fi
exit
fi

echo "usage: motion.sh start|stop|restart"

User avatar
dozencrows
Posts: 172
Joined: Sat Aug 04, 2012 6:02 pm

Re: MMAL Motion - tuning & optimisations

Sun May 25, 2014 10:19 am

Thank you for the log file and version info, @andies. On the whole, the logfile seems to show motion running correctly - however there is some weird corruption at 21:40:49, then the log shows a new instance of motion starting up but at an earlier time! 21:17:26. Not sure what to make of that - did it crash at around 21:40, reboot and restart motion and somehow have the time incorrect? Or have you seen any corruption on your SD card?

I'm really not sure what's behind the instabilities... I don't see them at all in my setup. I have motion running successfully for 14 hours per day, using the secondary buffer optimisation, pre-encoded in JPEG. I don't use the still camera mode, though, as I want higher frame rates. And I'm running on an older version of Raspbian (3.6.11+ #557 from Oct 2 2013) and firmware (Oct 18 2013, version dbda126981820ea5681263042435388dddf65131).

I tested your config script on that setup, but also on a fresh install of Raspbian and firmware as up to date as possible (3.12.20+ #685 from May 23 2014, firmware version 21d0de90961749983d49ec4e819a98a8ba4287fc) and that was working ok at least for a run of several minutes.

andies
Posts: 140
Joined: Mon Nov 11, 2013 8:12 pm
Location: Berlin

Re: MMAL Motion - tuning & optimisations

Sun May 25, 2014 11:47 am

Well, I think it is the SD-Card. I had a problem with an earlier card and replaced them by SanDisk (it showed weird behavior). Right now I have a new 64GB card but since yesterday I cannot reboot using terminal (I have to interrupt the power supply). It seems it is not motion-related but has to do with the card.

PS: After reading some threads it might also be the power supply. I am using POE, power by a (expensive) Cisco router. I am trying to get some toys to measure the power supply, hopefully this will solve it.
raspberry B, Noir camera, Mac Book Air, iPhone, Bezzera

Miguel22
Posts: 3
Joined: Wed May 28, 2014 8:15 pm

Re: MMAL Motion - tuning & optimisations

Fri Jun 06, 2014 7:16 am

Hello!

I am testing the latest build of motion-mmal with PiNoir camera module. Secondary buffer works flawlessly but only if mmalcam_secondary_buffer_jpeg option is disabled. Any other value than 0 causes motion to hang and I have to kill it. Did anybody experience the same issue as me?

Michal

andies
Posts: 140
Joined: Mon Nov 11, 2013 8:12 pm
Location: Berlin

Re: MMAL Motion - tuning & optimisations

Fri Jun 06, 2014 9:22 pm

Same with me. Dozencrows looked at my conf-file and found nothing that could cause the problem. With my conf his RPi was running perfect. Either it is a SD-Card issue or some other package is interfering. What do you suspect?
raspberry B, Noir camera, Mac Book Air, iPhone, Bezzera

vvanhee
Posts: 3
Joined: Sat Jun 07, 2014 11:39 am

Re: MMAL Motion - tuning & optimisations

Sat Jun 07, 2014 11:53 am

I have the same issue with mmalcam_secondary_buffer_jpeg causing hangs if not set to 0 -- for now I'm just using the presumably slower 'quality' option for jpeg compression and it's working.

andies
Posts: 140
Joined: Mon Nov 11, 2013 8:12 pm
Location: Berlin

Re: MMAL Motion - tuning & optimisations

Sun Jun 08, 2014 9:54 am

For now I'm just using the presumably slower 'quality' option for jpeg compression and it's working.
What exactly is your option in the conf-file?
raspberry B, Noir camera, Mac Book Air, iPhone, Bezzera

Return to “Camera board”