hydra3333
Posts: 108
Joined: Thu Jan 10, 2013 11:48 pm

Re: MMAL Motion - tuning & optimisations

Mon Dec 15, 2014 7:48 am

Attached is the Windows batch file to imprint the jpeg dates into the images and then make a (10fps) .mp4 video out of it.
Maybe some kind soul could consider "translating" it into a .sh script file so it'd run on the Pi (being a newbie, it's beyond me at present) ...

Code: Select all

@echo on
REM renames all of the .jpg files in the current folder to a sequence of image filenames which ffmpeg likes
setlocal EnableDelayedExpansion
set newpath=.\working\
set prefix=Image
MD  "%newpath%"
DEL  "%newpath%*.png"
set i=0
for %%a in (*.jpg) do (
    set /a i+=1
    set nnn=000000000!i!
    set ooo=!nnn:~-6!
    set bbb=%%~na
    set fpre=!bbb:~0,3!
    set fyyyy=!bbb:~3,4!
    set fmo=!bbb:~7,2!
    set fdd=!bbb:~9,2!
    set fhh=!bbb:~11,2!
    set fmm=!bbb:~13,2!
    set fss=!bbb:~15,2!
    set fpost=!bbb:~17,3!
    set fstamp=!fpre!!fyyyy!.!fmo!.!fdd!-!fhh!.!fmm!.!fss!!fpost!
    set fname=!newpath!!prefix!_!ooo!.png
    "C:\SOFTWARE\ffmpeg\ffmpeg.exe" -i "%%~a" -vf "format=yuv420p,drawtext=fontfile='C\:\\Windows\\Fonts\\arial.ttf': fontsize=20: text='Created %%{metadata\:creation_time} !fstamp!': x=(w-(tw*1.05)): y=main_h-(text_h*1.2): [email protected]: box=1: [email protected]" -y "!fname!"
)
REM note the double percentage sign which DOS converts into one 
"C:\SOFTWARE\ffmpeg\0-latest-x64\bin\ffmpeg.exe" -framerate 10 -start_number 1 -i "!newpath!!prefix!_%%06d.png" -c:v h264 -preset slow -profile:v high -level 4.1 -crf 16 -coder 0 -movflags +faststart -vf "fps=10,format=yuv420p" -coder 0 -y ".\000-out.mp4"

pause
exit

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

Re: MMAL Motion - tuning & optimisations

Mon Dec 15, 2014 9:27 am

abishur wrote:Just read through this thread and I was wondering if there was ever a fix released to be able to rotate the video 180 degrees, I saw the suggestion of using -vflip,
@abishur - you could combine a vflip and an hflip to achieve 180 degree rotation. Note that the shorter forms of the command line options should also work, e.g.

Code: Select all

-vf -hf

hydra3333
Posts: 108
Joined: Thu Jan 10, 2013 11:48 pm

Re: MMAL Motion - tuning & optimisations

Fri Dec 26, 2014 12:08 am

Hello. I just saw this:
http://www.raspberrypi.org/forums/viewt ... 43#p656243
in which the latest python camera library use mmal, and that the example in that thread used the "latest techniques" per other sample code from examples around this http://picamera.readthedocs.org/en/rele ... processing, such as such as assessing motion from "motion vectors".
The picamera library relies on libmmal which is specific to the Pi’s camera module.

Code: Select all

# This script implements a motion capture surveillace cam for raspery pi using picam.
# It uses the motion vecors magnitude of the h264 hw-encoder to detect motion activity.
# At the time of motion detection a jpg snapshot is saved together with a h264 video stream
# some seconds before, during and after motion activity to the 'filepath' directory.
The python code in http://www.raspberrypi.org/forums/viewt ... 43#p656243 is quite small. Has anyone has experience with that as compared to this "motion" program in this thread ?
eg speed. The program in this thread seems more versatile and has a lot more options, however is the other "newer(?)"

thuranga
Posts: 9
Joined: Fri Jan 02, 2015 12:13 pm
Location: DE

Re: MMAL Motion - tuning & optimisations

Fri Jan 02, 2015 12:20 pm

Hi,
I have a limitation of only 2 frames/sec when using "mmalcam_use_still on". It does not matter what resolution I set. I want a wider field of view, so I use the still images. Is there a possibility to tune the config for a higher framerate?

Greetings
Thuranga

bdalton
Posts: 4
Joined: Fri Jan 02, 2015 9:18 pm

Re: MMAL Motion - tuning & optimisations

Fri Jan 02, 2015 9:47 pm

thuranga wrote:Hi,
I have a limitation of only 2 frames/sec when using "mmalcam_use_still on". It does not matter what resolution I set. I want a wider field of view, so I use the still images. Is there a possibility to tune the config for a higher framerate?
From my research, it seems to be a CPU limitation with the Pi. I've also tried the picam.py script which is supposedly light-weight (and doesn't use motion), and it also has the same 2 frames/sec problem when it writes JPEGs. I'm pretty sure the motion detection analysis is slowing the system down in both cases. If we were to tweak the picam.py script to record video instead with raspivid instead of raspistill, I imagine the results would have a better frame rate. The problem is that I don't think there's a way to do the motion detection while the video is recording.

hydra3333
Posts: 108
Joined: Thu Jan 10, 2013 11:48 pm

Re: MMAL Motion - tuning & optimisations

Fri Jan 02, 2015 11:08 pm

A latest version here (picam python using motion vectors) http://www.raspberrypi.org/forums/viewt ... 43#p656243 I've had 15fps h264 no probs.

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

Re: MMAL Motion - tuning & optimisations

Fri Jan 02, 2015 11:33 pm

thuranga wrote:Hi,
I have a limitation of only 2 frames/sec when using "mmalcam_use_still on".
In still mode, the Pi camera has to spend fair while setting itself up (e.g. calculating exposure) to get a good shot; that along with the CPU cost of the motion detection will limit the frame rate that can be achieved in still mode.

The best frame rate is with video mode (turn mmalcam_use_still to off) using the secondary buffer mechanism to capture a high-res image for recording while performing motion detection on the lower resolution primary buffer (see earlier posts in this thread for info about these settings).

But as mentioned, you don't get the full frame.

thuranga
Posts: 9
Joined: Fri Jan 02, 2015 12:13 pm
Location: DE

Re: MMAL Motion - tuning & optimisations

Sat Jan 03, 2015 11:22 pm

Thanks for your replies!
dozencrows wrote: The best frame rate is with video mode (turn mmalcam_use_still to off) using the secondary buffer mechanism to capture a high-res image for recording while performing motion detection on the lower resolution primary buffer
I tried this mechanism with 15 frames successfully, but I got the mentioned lower flield of view. Is there a possibilty to perform the motion detection on the lower resolution primary buffer and take a high res image with a wider field of view using the secondary buffer?

Greetings
Thuranga

valcam
Posts: 1
Joined: Tue Feb 17, 2015 4:19 pm

Re: MMAL Motion - tuning & optimisations

Tue Feb 17, 2015 4:57 pm

Hello everyone, I wanted to know how to record a video without interruption (even when no motion is detected). I tried changing the "event_gap" (0 and -1) but without success.

:roll:

ps * with my version unfortunately MMAL-opt not work. Using the configuration file of the version MMAL instead okay

Code: Select all

Linux raspberrypi 3.12.20+ # 687 PREEMPT Fri May 30 16:39:11 BST 2014 armv6l GNU / Linux
thanks for help

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

Re: MMAL Motion - tuning & optimisations

Wed Feb 18, 2015 9:15 am

thuranga wrote:... Is there a possibilty to perform the motion detection on the lower resolution primary buffer and take a high res image with a wider field of view using the secondary buffer?
Unfortunately not - the low resolution primary buffer is a downscaled copy of the high resolution secondary buffer, so they both have the same FOV. The secondary buffer is in fact the image captured by the camera.

Using the camera's stills mode to achieve full image sensor FOV will never be able to achieve as good a frame rate as the tigher FOV video mode, because of the time the camera takes to 'settle' (e.g. get correct exposure). You'll find various posts about that timing all over this part of the forum.

rpiforfun
Posts: 28
Joined: Wed Jan 15, 2014 11:25 pm

Re: MMAL Motion - tuning & optimisations

Sat Feb 21, 2015 10:46 pm

I have tried to update to the mmal-test code from dropbox. I essentially was unable to implement ANY of the options I think. It failed to attach the camera until I disabled almost all of them.


That isn't as important as this:

I am trying to use this as a simple motion detection pi recording a video and best images when motion is seen.

I can't say when it started but I think it is fairly recent. When I start motion now, it automatically records a video shows the brightness changing. I have done firmware updates lately. How do I stop motion-mmal from recording this video of the brightness changing upon camera launch?

I think this is all you'd need to see:

Code: Select all

launch command:
sudo /media/data/storage/motion-mmal/motion -n -c /media/data/storage/motion-mmal/motion-mmalcam.conf



Config:

v4l2_palette 17
input -1
norm 0
frequency 0
rotate 180
width 1024
height 768
framerate 2
minimum_frame_time 0
netcam_keepalive off
netcam_tolerant_check off
auto_brightness off
brightness 0
contrast 0
saturation 0
hue 0

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

Re: MMAL Motion - tuning & optimisations

Sun Feb 22, 2015 10:22 am

rpiforfun wrote:I have tried to update to the mmal-test code from dropbox. I essentially was unable to implement ANY of the options I think. It failed to attach the camera until I disabled almost all of them.
There could be changes in recent firmware that MMAL motion isn't happy with (which has occurred in the past and been fixed). I've not had time to work on this for a while, so haven't tested with the latest firmware.

It doesn't exist yet, but a 'settle delay' time that can be set which pauses motion detection for a period of time after the camera is first switched on might achieve what you want. I'll add that to the 'to do' list. No guarantees on when I could get round to doing this, I'm afraid at the moment.

rpiforfun
Posts: 28
Joined: Wed Jan 15, 2014 11:25 pm

Re: MMAL Motion - tuning & optimisations

Sun Feb 22, 2015 1:04 pm

Thanks for the quick response dozencrows. Your work is appreciated here. I agree that a delay feature seems like it would be handy.

And in further testing, it is NOT every time. I do wonder if it is a temp issue. This is an indoor camera (no sunlight) and the temp only swings ~10 degrees from 62F to 72F.

unconquered
Posts: 2
Joined: Wed Mar 18, 2015 3:52 pm

Re: MMAL Motion - tuning & optimisations

Wed Mar 18, 2015 9:35 pm

Thought I would start here before starting a new thread. First off thanks for your work on this, much appreciated.

I'm having issues with latency. I followed the setup, made the needed adjustments and so on. When motion-mmal isn't running I see virtually no issues. But when running, even when I don't see image files outputbeing created in target dir I experience latency over ssh. I am using a wifi USB dongle, and will test wired. I did see a note on that. Is that likely the issue? It does appear to get worse when i open an SFTP session and copy a few of the files. Again, with motion-mmal isn't running, changing directories is really snappy. I've looked at top, iotop etc, and nothing crazy is going on. CPU never goes above 15%. Its usually closer to 5%.

Not that this is a 1:1 comparison, I had an old ubuntu box running a cheap usb webcam and it ran great, even when "motion" was being detected. This was on a 1.6ghz AMD, with maybe 2gb of ram.

What other things should I be checking?

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

Re: MMAL Motion - tuning & optimisations

Fri Mar 20, 2015 1:00 pm

How long roughly is the latency that you experience?

One thought I had is that when motion is running, for some reason your Pi is dropping packets sent over the WiFi. Not quite sure how you'd test that, but it would be worth trying a wired connection as you suggest.

I'd also make sure that you've definitely turned off all the output methods in motion.conf - i.e. output to file, output to movie, stream to web.

Both the camera and USB WiFi tend to be power hungry; if your power supply isn't quite up to it, the voltage when both are running could be dropping low enough to start causing some instability. If you're careful, you can test the power supply voltage while the Pi is running as described here - http://elinux.org/R-Pi_Troubleshooting# ... r_problems.

You could also try using SSH over WiFi and wired while running say raspivid continuously for several minutes to isolate whether any use of the camera affects it.

unconquered
Posts: 2
Joined: Wed Mar 18, 2015 3:52 pm

Re: MMAL Motion - tuning & optimisations

Thu Mar 26, 2015 3:57 pm

Thanks for the reply.

I ended up doing a fresh image on the SDCard and install of motion mmal. I had it running yesterday over ethernet. Normal CPU was between 5-15%. When I got home and hopped around in front of the cam like an idiot, SSHjuice open on my phone in one hand looking at top I want to say it maxed around 45% CPU usage. I'll run some more tests when I get a chance with the WiFi adapter and see how it goes. I need to get into my old linux box and get the config files (send emails on motion detected, crontab, web server, etc) and do some more playing on the Pi. Ideally I'm running this as a security function (email me on motion, offload files somewhere), but I'd like to use it to take some time lapse outdoors-y stuff. Which I would just need power really, get capture going and disable WiFi and let it run.

Also, where can I donate (thought I read somewhere to donate for mmal)? Thanks!

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

Re: MMAL Motion - tuning & optimisations

Thu Mar 26, 2015 10:01 pm

Good luck! Do post back on how you get on with WiFi.

And many thanks for considering donating... but no need!

If anyone is asking for donations to support the MMAL version of motion, then I'm pretty sure they're scamming - as far as I know, I'm the only one who has publicly worked on the code and have not asked for any return on it. Heck, it's experimental and needs further work which I'm not free to do for the time being, so I wouldn't dream of asking for cash!

I do drop the occasional hint to see if anyone else wants to contribute some coding time however...

Mikeych
Posts: 1
Joined: Sat Sep 26, 2015 10:13 am

Re: MMAL Motion - tuning & optimisations

Sat Sep 26, 2015 10:29 am

Lots of really great work here.

I must admit to being confused about the best option for my needs (motion v Python).

I have motion running at the moment ("standard" one without secondary buffer option) and suffering from high cpu / low frame rate / low quality. Currently:
- I take snapshots every minute and send to central server
- have full day timelapse from images taken every 10s
- motion detection saved to movie.

Ideally I'd to keep all these features but up resolution to full capability of the camera at c.5 fps (and output to movie when motion detected).

I'm leaning towards the secondary buffer option in motion (so thanks again for the great solution) and have recently built latest version.
However, I don't seem to be able to output motion video from the secondary buffer (ie high res); I know there's a setting for this (default off) but this does not give me the desired results.

My choices at the moment as I see it are (all with motion secondary buffer version):
1. Get high quality (secondary buffer) video output working
2. Post process the full res captured images (on pi with low priority process) with times stamping added with ffmpeg

Before I embark on these, is there any advice people could provide to help solidify my thoughts / reduce the permutations I will end up trying?

Thanks
Mike

User avatar
Niehztog
Posts: 14
Joined: Wed May 23, 2012 4:35 pm

Re: MMAL Motion - tuning & optimisations

Fri Oct 02, 2015 6:33 pm

Hello dozencrows,

now the new raspbian based on debian jessie has been released, a few of motions dependencies are only available in newer versions. This leads to the fact that your per-built motion binary will no longer start on up-to-date raspbian systems.

The following error messages are shown:

Code: Select all

/usr/bin/motion: error while loading shared libraries: libavformat.so.53: cannot open shared object file: No such file or directory
up-to-date file would be: libavformat.so.56

Code: Select all

/usr/bin/motion: error while loading shared libraries: libavcodec.so.53: cannot open shared object file: No such file or directory
up-to-date file would be: libavcodec.so.56

Code: Select all

/usr/bin/motion: error while loading shared libraries: libavutil.so.51: cannot open shared object file: No such file or directory
up-to-date file would be: libavutil.so.54

I tried setting up symlinks for the files pointing to the new versions. This won't help either:

Code: Select all

/usr/bin/motion: /usr/lib/arm-linux-gnueabihf/libavutil.so.51: version `LIBAVUTIL_51' not found (required by /usr/bin/motion)
/usr/bin/motion: /usr/lib/arm-linux-gnueabihf/libavcodec.so.53: version `LIBAVCODEC_53' not found (required by /usr/bin/motion)
/usr/bin/motion: /usr/lib/arm-linux-gnueabihf/libavformat.so.53: version `LIBAVFORMAT_53' not found (required by /usr/bin/motion)
Could you provide an updated pre-built binary?

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

Re: MMAL Motion - tuning & optimisations

Sat Oct 03, 2015 9:50 am

Niehztog wrote:Hello dozencrows,

now the new raspbian based on debian jessie has been released, a few of motions dependencies are only available in newer versions. This leads to the fact that your per-built motion binary will no longer start on up-to-date raspbian systems.

Could you provide an updated pre-built binary?
Many thanks for trying this! Unfortunately I don't have a working camera set-up any more, so won't be able to do this. Full source is available on Github in the mmal-test branch (https://github.com/dozencrows/motion/tree/mmal-test), so you could try building from that to resolve the dependency issue.

User avatar
Niehztog
Posts: 14
Joined: Wed May 23, 2012 4:35 pm

Re: MMAL Motion - tuning & optimisations

Sat Oct 03, 2015 11:42 am

Hello dozencrows,

thank you for the suggestion to compile it myself. I even managed to compile it on my raspberry pi, but ran into some error later. Here is what I did:

Code: Select all

sudo apt-get install git cmake

sudo apt-get install libjpeg62-turbo libjpeg62-turbo-dev libavformat56 libavformat-dev libavcodec56 libavcodec-dev libavutil54 libavutil-dev libc6-dev zlib1g-dev libmysqlclient18 libmysqlclient-dev libpq5 libpq-dev

git clone [email protected]:raspberrypi/userland.git

git clone -b mmal-test [email protected]:dozencrows/motion.git

cd motion

USERLANDPATH=/home/pi/userland cmake .
make
make first gave me some error in ffmpeg.c, but after reading this page, I just replaced CODEC_ID_ with AV_CODEC_ID_ in ffmpeg.c and it compiled (throwing a few deprecation and unused variable warnings though).

When executing the compiled binary it shows the following error

Code: Select all

[email protected] ~/mmal-test $ sudo ./motion -n -c /etc/motion.conf
[0] [NTC] [ALL] conf_load: Processing thread 0 - config file /etc/motion.conf
[0] [NTC] [ALL] motion_startup: Motion mmaltest Started
[0] [NTC] [ALL] motion_startup: Logging to file (/tmp/motion.log)
mmal: mmal_vc_component_enable: failed to enable component: ENOSPC
I am not sure how to fix that. It doesn't take any pictures. My camera board is working fine (tested it with "raspistill -o cam.jpg")

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

Re: MMAL Motion - tuning & optimisations

Sun Oct 04, 2015 10:31 am

Try putting the text of the error message into Google; I'm pretty sure you'll find some other folk have had that sort of issue... ;)

lowflyer
Posts: 78
Joined: Sat Jun 01, 2013 2:27 pm

Re: MMAL Motion - tuning & optimisations

Tue Dec 22, 2015 2:32 pm

See my post of Sat Nov 14, 2015 over on viewtopic.php?p=843625#p843625

I updated dozencrows' very wonderful motion-mmal to run on jessie.

Please let me know if you use it and/or like it.

PiJay
Posts: 6
Joined: Mon Mar 07, 2016 7:08 pm

Re: MMAL Motion - tuning & optimisations

Tue Mar 08, 2016 4:49 pm

Two questions:
1. Is it possible to display the output of the camera, real-time, on the display in addition to recording to file?

2. Does anyone use BlueIris? Is it possible to attach to the web page on port 8081? I have tried lots of settings and can't seem to get it to show up.

lowflyer
Posts: 78
Joined: Sat Jun 01, 2013 2:27 pm

Re: MMAL Motion - tuning & optimisations

Tue Mar 08, 2016 5:19 pm

@PiJay

I am not quite sure I understand your question 1, given that you mention port 8081 in your second question. For me, I view the camera output in realtime by pointing my browser (Firefox) at http://IP_address_of_my_pi:8081, where IP_address_of_my_pi is the local IP address of the Pi running motion. The computer with the display (running Firefox) needs to be on the same local network and the config file for motion on the Pi needs to have the webserver enabled and not restricted to the local machine - the Live Stream Server section of my config file is this:

Code: Select all

############################################################
# Live Stream Server
############################################################

# The mini-http server listens to this port for requests (default: 0 = disabled)
stream_port 8081

# Quality of the jpeg (in percent) images produced (default: 50)
stream_quality 50

# Output frames at 1 fps when no motion is detected and increase to the
# rate given by stream_maxrate when motion is detected (default: off)
stream_motion off

# Use secondary buffer as stream image source (default: off)
stream_secondary off

# Maximum framerate for stream streams (default: 1)
stream_maxrate 4

# Restrict stream connections to localhost only (default: on)
stream_localhost off

# Limits the number of images per connection (default: 0 = unlimited)
# Number can be defined by multiplying actual stream rate by desired number of seconds
# Actual stream rate is the smallest of the numbers framerate and stream_maxrate
stream_limit 0

# Set the authentication method (default: 0)
# 0 = disabled
# 1 = Basic authentication
# 2 = MD5 digest (the safer authentication)
stream_auth_method 0

# Authentication for the stream. Syntax username:password
# Default: not defined (Disabled)
; stream_authentication username:password

To view this from outside your local network, you have to use port forwarding on your router and have some way of knowing your public IP address.

Not sure it this answers your question.

I have no idea about BlueIris - it looks expensive.

Good luck!

Return to “Camera board”