koufs
Posts: 5
Joined: Fri Jul 26, 2013 2:17 pm

Re: Motion - MMAL camera

Mon Jul 29, 2013 4:21 pm

lancevo3 wrote:I am looking to have this record a 15 second video after motion is detected and snap a picture. What are the main config options I should be concerned with modifying to achieve this? Thanks
You should be able to do that with:

ffmpeg_output_movies on
output_pictures best

This should record any motion and save a photo of the "best" frame (most motion); you can also use first (the first frame) or center (motion is closest to center of the picture).

I don't believe video can be limited to 15 seconds, my understanding is that it records the entire duration of motion.

lancevo3
Posts: 6
Joined: Sat Jul 27, 2013 7:03 am

Re: Motion - MMAL camera

Mon Jul 29, 2013 4:29 pm

Ok thank you. That makes sense that it just records until no more motion. I got this all setup I just haven't actually had much time to play around with it yet. Hopefully tonight.

User avatar
jbeale
Posts: 3516
Joined: Tue Nov 22, 2011 11:51 pm
Contact: Website

Re: Motion - MMAL camera

Mon Jul 29, 2013 6:18 pm

koufs wrote:You should be able to do that with:

ffmpeg_output_movies on
output_pictures best

This should record any motion and save a photo of the "best" frame (most motion); you can also use first (the first frame) or center (motion is closest to center of the picture).
Just FYI, note that if you have set "ex=night" to allow better images in low light by using longer shutter times, then the "best" frame (ie, "most pixels changed") will invariably be the one with a long smear in the middle, due to the object moving during the exposure. So in this application, you cannot use longer shutter times. Unless the long smear is useful (eg. measuring speed of a car from length of a taillight streak, knowing the shutter time and the pixel scale in cm / pixel.)

User avatar
jbeale
Posts: 3516
Joined: Tue Nov 22, 2011 11:51 pm
Contact: Website

tips for auto-run of Motion

Tue Jul 30, 2013 12:33 am

Just in case it helps anyone (maybe myself in a few weeks when I've forgotten what I did...) here's what I did to get 'motion' to auto-start and work correctly on powerup. As recommended elsewhere, I have two simple scripts to start and stop the process:

Code: Select all

[email protected]:~/mmal$ cat startmotion
#!/bin/sh
nohup /home/pi/mmal/motion-mmal -n -c /home/pi/mmal/cmmal.conf 1>/dev/null 2>&1 </dev/null &

[email protected]:~/mmal$ cat stopmotion
#!/bin/sh
ps -ef | grep motion-mmal | awk '{print $2}' | xargs kill
I am using the utility "sitecopy" (sudo apt-get install sitecopy) to upload stills and videos from the directory on my Pi to a remote webserver. It gets triggered to upload any changed files in my still/video directory each time a new video is saved. Now, my sitecopy config file is in the home directory for user 'pi', but the motion-mmal process is running as root from rc.local

Code: Select all

added to file: /etc/rc.local 
# start the motion service (runs as root!)
/home/pi/mmal/startmotion
so I need to change my process user-ID to make this work. I use 'sudo -u pi' inside my config file: /home/pi/mmal/cmmal.conf (note 'sitecopy -u bcpics' means "update any changed files from local to remote site, the one named bcpics in the ~/.sitecopyrc config file")

Code: Select all

# Command to be executed when a movie file (.mpg|.avi) is closed. (default: none)
# To give the filename as an argument to a command append it with %f
on_movie_end sudo -u pi sitecopy -u bcpics
I use a PHP script on the remote server to generate a separate index of JPEG and AVI files. Sorting by name is OK because the name is also the file creation date.

Code: Select all

<?php
echo "<b>Current Files:</b><br><hr><br>";
$dir = ".";

$files = scandir($dir, 0);      // in ascending order

$i = 1;
foreach($files as $file) {
  $ext = pathinfo($file, PATHINFO_EXTENSION);
  if (is_file($file) && ($ext == "jpg")) {
    echo $i;
    echo ": <a href='$file'>$file</a>";
    if (($i%3) == 0) {  // display in three columns, 'cause that fits nicely
      echo "<br>", PHP_EOL;
    } else {
      echo " &nbsp; ", PHP_EOL;
    }
    $i = ($i + 1);
  }
}
echo "<br><hr><br>";

$i = 1;
foreach($files as $file) {
  $ext = pathinfo($file, PATHINFO_EXTENSION);
  if (is_file($file) && ($ext == "avi")) {
    echo $i;
    echo ": ";
    $fsk = intval((filesize($file)) / 1024);
    echo $fsk;
    echo ": <a href='$file'>$file</a>";
    if (($i%3) == 0) {
      echo "<br>", PHP_EOL;
    } else {
      echo " &nbsp; ", PHP_EOL;
    }
    $i = ($i + 1);
  }
}

?>
Example output (filenames are clickable links on the actual webpage). For the video files, I include the size in kBytes so I can see at a glance which are longer or shorter.

Code: Select all

Current Files:

1: 01-2013_0729_1701_14.jpg   2: 02-2013_0729_1707_05.jpg   3: 03-2013_0729_1708_28.jpg
4: 04-2013_0729_1709_12.jpg   5: 05-2013_0729_1726_51.jpg   6: 06-2013_0729_1726_56.jpg
7: 07-2013_0729_1727_06.jpg   8: 08-2013_0729_1727_09.jpg  

1: 533: 01-2013_0729_1701_13.avi   2: 166: 02-2013_0729_1707_05.avi   3: 112: 03-2013_0729_1708_27.avi
4: 114: 04-2013_0729_1709_12.avi   5: 75: 05-2013_0729_1726_51.avi   6: 207: 06-2013_0729_1726_54.avi
7: 139: 07-2013_0729_1727_05.avi   8: 59: 08-2013_0729_1727_09.avi   

lancevo3
Posts: 6
Joined: Sat Jul 27, 2013 7:03 am

Re: Motion - MMAL camera

Tue Jul 30, 2013 12:45 am

Looking for some linux advice (I know enough about linux to be dangerous). I am using ssh to access the pi from my macbook, what I am wanting to do is grab a file from the pi and move it to my mac. Below is what I would trying but I am getting a 10.0.1.27 port22: connection refused error:

scp [email protected]:/home/pi/01-20130728000620.avi [email protected]:/Desktop/

Any help would be appreciated! Thanks

User avatar
jbeale
Posts: 3516
Joined: Tue Nov 22, 2011 11:51 pm
Contact: Website

Re: Motion - MMAL camera

Tue Jul 30, 2013 12:59 am

I'm confused by the syntax you show- if you are running scp from your Mac, don't you just specify the local filename, not also the local host IP? In other words, something like

Code: Select all

scp [email protected]:/home/pi/01-20130728000620.avi /Desktop/
However I use a graphical SCP client on Windows (WinSCP), so I forgot the command line syntax.
Consulting this page, http://www2.imperial.ac.uk/~andy/sysnews/scp/ it looks like your original syntax is close to right if you are running the scp command on a third computer on your LAN, that is neither the Pi nor the Mac, in which case your Mac is apparently not set up to accept incoming scp connection.

lancevo3
Posts: 6
Joined: Sat Jul 27, 2013 7:03 am

Re: Motion - MMAL camera

Tue Jul 30, 2013 1:34 am

Updated to the below and it worked, thanks for the pointer.

Code: Select all

scp [email protected]:/home/pi/02-20130729202718.avi ~/Desktop/

blugerchug
Posts: 21
Joined: Tue Jul 03, 2012 6:46 pm

Re: Motion - MMAL camera

Wed Jul 31, 2013 12:20 pm

jbeale wrote:note that if you have set "ex=night" to allow better images in low light by using longer shutter times, then...
<stupid question> where does the ex=night command go? In the motion.conf file? </stupid question>


ps - thanks for start on boot post - just what I was after...

User avatar
jbeale
Posts: 3516
Joined: Tue Nov 22, 2011 11:51 pm
Contact: Website

Re: Motion - MMAL camera

Wed Jul 31, 2013 2:17 pm

blugerchug wrote:where does the ex=night command go? In the motion.conf file?
Yes, in the motion.conf file (and you can name that file something else, like I did, you just need to specify the name with the -c option when you invoke motion). For example:

Code: Select all

(...various stuff...)

############################################################
# OpenMax/MMAL camera support for Raspberry Pi
############################################################
mmalcam_name vc.ril.camera
mmalcam_control_params -rot 180 -mm matrix -ex night

(...various other stuff...)
Just be aware, in dim light = 1/4 second exposure, moving objects become blurry!

blugerchug
Posts: 21
Joined: Tue Jul 03, 2012 6:46 pm

Re: Motion - MMAL camera

Wed Jul 31, 2013 11:13 pm

Thank you! I'm looking forward to trying that out

grem.lin
Posts: 3
Joined: Fri Aug 02, 2013 11:32 am

Re: Motion - MMAL camera

Fri Aug 02, 2013 12:04 pm

Dear all,
I'm trying to get motion-mmal running.
When I use the extract of motion-mmal.tar.gz and try to start ./motion the error message I get is this one:

Code: Select all

[email protected] /root/motiondir/mmal-cam $ ./motion
-bash: ./motion: No such file or directory
[email protected] /root/motiondir/mmal-cam $ sudo ./motion
sudo: unable to execute ./motion: No such file or directory
Ok, don't care and tryed to compile the source. No luck as well. I'm using a FFMpeg version which is able to stream h.264 directly. I intend to keep that version, but the motion-mmal make- command stops with that error message:

Code: Select all

[email protected] /root/motiondir $ sudo make
Welcome to the setup procedure for Motion, the motion detection daemon! If you get
error messages during this procedure, please report them to the mailing list. The
Motion Guide contains all information you should need to get Motion up and running.
Run "make updateguide" to download the latest version of the Motion Guide.

Version: Git-8619d7c17ce112e7196975905c6e840f345141ba
Platform: Linux (if this is incorrect, please read README.FreeBSD)

Compiling Motion object files...
--------------------------------------------------------------------------------
gcc -g -O2 -D_REENTRANT -DHAVE_FFMPEG -I/usr/local/include -DFFMPEG_NEW_INCLUDES -DHAVE_FFMPEG_NEW -DMOTION_V4L2 -DMOTION_V4L2_OLD -DTYPE_32BIT="int" -DHAVE_BSWAP    -Wall -DVERSION=\"Git-8619d7c17ce112e7196975905c6e840f345141ba\" -Dsysconfdir=\"/usr/local/etc\"    -c -o ffmpeg.o ffmpeg.c
ffmpeg.c:27:10: warning: #warning ************************************************** [-Wcpp]
ffmpeg.c:28:10: warning: #warning Your version of FFmpeg is newer than version 0.4.8 [-Wcpp]
ffmpeg.c:29:10: warning: #warning Newer versions of ffmpeg do not support MPEG1 with [-Wcpp]
ffmpeg.c:30:10: warning: #warning non-standard framerate. MPEG1 will be disabled for [-Wcpp]
ffmpeg.c:31:10: warning: #warning normal video output. You can still use mpeg4 and [-Wcpp]
ffmpeg.c:32:10: warning: #warning and mpeg4ms which are both better in terms of size [-Wcpp]
ffmpeg.c:33:10: warning: #warning and quality. MPEG1 is always used for timelapse. [-Wcpp]
ffmpeg.c:34:10: warning: #warning Please read the Motion Guide for more information. [-Wcpp]
ffmpeg.c:35:10: warning: #warning Note that this is not an error message! [-Wcpp]
ffmpeg.c:36:10: warning: #warning ************************************************** [-Wcpp]
ffmpeg.c: In function ‘ffmpeg_deinterlace’:
ffmpeg.c:1001:5: warning: ‘avpicture_deinterlace’ is deprecated (declared at /usr/local/include/libavcodec/avcodec.h:4387) [-Wdeprecated-declarations]
/tmp/ccdquTRU.s: Assembler messages:
/tmp/ccdquTRU.s:1656: Error: bad instruction `emms'
make: *** [ffmpeg.o] Error 1
[email protected] /root/motiondir $ 
Any idea? Thank you all in advance.
Gremlin

User avatar
jbeale
Posts: 3516
Joined: Tue Nov 22, 2011 11:51 pm
Contact: Website

Re: Motion - MMAL camera

Fri Aug 02, 2013 2:29 pm

THe first problem, is that apparently the filename 'motion' is not present. Was the executable file named 'motion-mmal' ? What was in that directory?

The second compile problem: The assembler mentions 'emms' which is an x86 opcode. So you are trying to compile Intel x86 assembly code for the ARMv6 which is in the R-Pi, and that doesn't work :-) You need a version of the FFMPEG code which has been ported to the Raspberry Pi.

grem.lin
Posts: 3
Joined: Fri Aug 02, 2013 11:32 am

Re: Motion - MMAL camera

Fri Aug 02, 2013 3:31 pm

Hello jbeale,
thank you very much for that quick response. The second par of my question understood, my fault. The first part is really strange, look....
Gremlin

Code: Select all

[email protected] /root/motiondir/mmal-cam $ ls -al
total 344
drwxr-xr-x 2 root root    4096 Aug  2 11:25 .
drwxr-xr-x 4 root root    4096 Aug  2 14:02 ..
-rwxrwxr-x 1 pi   input 311788 Aug  2 13:47 motion
-rw-rw-r-- 1 1001 input  28421 Aug  2 13:47 motion-mmalcam.conf
[email protected] /root/motiondir/mmal-cam $ ./motion
-bash: ./motion: No such file or directory
[email protected] /root/motiondir/mmal-cam $ sudo ./motion
sudo: unable to execute ./motion: No such file or directory
[email protected] /root/motiondir/mmal-cam $ 

User avatar
jbeale
Posts: 3516
Joined: Tue Nov 22, 2011 11:51 pm
Contact: Website

Re: Motion - MMAL camera

Fri Aug 02, 2013 4:22 pm

Interesting, so the error message is not very helpful. I *suspect* what has happened is the executable depends on some library which for some reason is not present on your system. Obviously, the error message should really specify which file is missing, but I have heard (in general) of this kind of error. All I can say is that it "just worked" for me. Are you running the current version of the system (sudo rpi-update) ?

Two other comments:
1) Your directory is under /root but in my case, I put the file under my home directory: /home/pi/mmal/
2) I did not run it with sudo, just simply ./motion

I'm not sure why what you did would be in any way wrong, but you might try those two changes and see if that works.

PeterN
Posts: 4
Joined: Thu Jan 10, 2013 12:36 pm

Re: Motion - MMAL camera

Fri Aug 02, 2013 4:27 pm

Hi jbeale,
Great work, I got Motion -MMAL Camera working in a few minutes thanks to your work. However as a novice I'm not sure what to do next. I would like to be able to look at latest .avi file - these are located in home/pi directory at present. I have installed lighttpd assuming it will be needed. :?:
Thanks
Peter

Sorted thanks. Start from scratch using steps @ http://rbnrpi.wordpress.com/project-lis ... etect-cam/
Last edited by PeterN on Fri Aug 02, 2013 9:47 pm, edited 1 time in total.

peewee2
Posts: 61
Joined: Sun Apr 14, 2013 7:04 pm

Re: Motion - MMAL camera

Fri Aug 02, 2013 9:41 pm

Hi,

Is there a simple way to get motion to work under florescent lights?

I find that my set up works perfectly in daylight but flakey indoors.

I notice that it detects motion almost every second, and the saved image brightness varies. I figure this is because the light is flickering at 50hz and consequently the brightness varies.

Pete

User avatar
jbeale
Posts: 3516
Joined: Tue Nov 22, 2011 11:51 pm
Contact: Website

Re: Motion - MMAL camera

Fri Aug 02, 2013 9:48 pm

@PeterN: I'm not much of an expert really, just using what 'dozencrows' has made for us. By the way, the JPEG images are fine and viewable by anything, but the AVI files generated by this motion-mmal + ffmpeg combination seem to be nonstandard in some way. VLC does not always show them properly; you may see just the first frame, or nothing at all. But Windows Media Player on my old WinXP box does play them, as does VirtualDub 1.9.11 (also WinXP).

I want to view the images and video from outside my home LAN, but I don't want the security issues of running a public-facing webserver from home. So what I do is use another program 'sitecopy' (sudo apt-get install sitecopy) on the Pi to upload new files in my mmal images directory to an account on a commercial webhost. I view and/or download them from a browser pointing to that site.

If you want to use the Pi as a webserver too, of course you could do that, I have not tried it. I think there are several how-to documents for setting up various webservers.

@peewee2: The camera defaults to video mode at 30 fps. If you have 50 Hz power this may give you flickering under fluorescent lighting. Try setting -fps 25 in the mmalcam_control_params line in the config file, this may help. See also: http://www.raspberrypi.org/phpBB3/viewt ... 90#p350434

peewee2
Posts: 61
Joined: Sun Apr 14, 2013 7:04 pm

Re: Motion - MMAL camera

Fri Aug 02, 2013 10:51 pm

Hi JBeale

yes that seems to solve the problem

thanks,

Pete

grem.lin
Posts: 3
Joined: Fri Aug 02, 2013 11:32 am

Re: Motion - MMAL camera

Sat Aug 03, 2013 3:46 pm

Hello JBeale,
I followed your suggestion and installed a new OS SD Card from scratch and .... it works! Thank you very much for your help.
Just for info: The C-Code of FFMeg was universal. When you type ./configure, all necessary switches to produce a ARM executable will be done automatically. To compile FFMpeg as stand alone, it worked. If motion referenced the ffmpeg.c, all CC switches are not set for ARM and compiling ends with 'emm' error.

kind regards
Gremlin

pd283289
Posts: 3
Joined: Mon Aug 05, 2013 1:00 pm

Re: Motion - MMAL camera

Mon Aug 05, 2013 1:32 pm

Hello,

First of all thanks to dozencrows for the excellent work.

I was thinking I could build a rasppi 720p motion detection ip camera like the Axis M1054.
However 640x480 already tops the cpu of the pi.
How does axis achieve this then with a comparable size device? Stronger CPU? Better algorithms? Hardware designed exclusively for motion detection?

In the near future would it be technically possible that this would be achieved with the pi or are we already looking at the pi's limits and I should give up?

greets
pd

User avatar
jbeale
Posts: 3516
Joined: Tue Nov 22, 2011 11:51 pm
Contact: Website

Re: Motion - MMAL camera

Mon Aug 05, 2013 2:21 pm

Hardware designed exclusively for motion detection?
would be my guess. Remember, on the R-Pi SoC, the ARMv6 processor we are using for motion detection is something like 6% of the chip's total silicon area (I vaguely remember Eben saying in one interview?) The rest is the VideoCore4 GPU that enables 1080p encode and decode among other things. If we had access to the GPU for motion detection, I think it could be done.

It has occurred to me that to encode H.264 video, you actually do motion detection as part of the encode step (this block of pixels moves over here, because the associated object has moved that way in the frame, etc). So I speculate that something that read the raw .h264 to look at these data vectors could, in theory, extract useful information about motion, without doing any actual heavy algorithms itself. Maybe it could be as simple as just reading the instantaneous compression or quality values; just like the size of a JPEG of a given resolution, can tell you the complexity of a scene (or for a given scene, how close to being "in focus" it is). Is something like that available as raw codes witin a .h264 stream, or does it have to be derived indirectly through calculations?

On the other hand, maybe the ARM is not even strong enough to scan the .h264 stream at 720p30 looking for some opcode values in real time- I am not sure.

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

Re: Motion - MMAL camera

Mon Aug 05, 2013 4:24 pm

Very interesting thought on the inherent motion analysis in H.264... that's worth some research, I think. The CPU can certainly get easy access to the encoded H.264 data, but as you say the question is how easily and quickly can it process the motion related data.

I'm gradually looking at options to make more Pi-specific optimisations for motion. There are a couple of steps in the image processing chain that may be amenable to execution on the GPU - I've started trying these out but it's early days. It is also possible that this might not help as the cost of transferring data to and back from the GPU could outweigh any gains from doing the work on it.

I think there are also options to provide a less feature-rich "cut down" configuration that would have some performance benefits - e.g. special code paths when certain features aren't used, such as smart masking, or capping framerate (definitely above currently achievable level) to reduce the memory load of some time-dependent information.

I'll keep you all posted.

User avatar
jbeale
Posts: 3516
Joined: Tue Nov 22, 2011 11:51 pm
Contact: Website

Re: Motion - MMAL camera

Mon Aug 05, 2013 6:40 pm

It seems I am not the only person to have this thought, there are some papers published on it at least from 2006. Here is one:
Fast compressed domain motion detection in H.264 video streams for video surveillance applications from IEEE conference paper at 2009 Advanced Video and Signal Based Surveillance http://orbit.dtu.dk/fedora/objects/orbi ... 91/content

The paper is a good read and the results are encouraging. They tested two IP cameras with H.264 output (Sony @ 320×240 and Axis @ 704×576) in realistic situations. The algorithm is not entirely trivial (like simple JPEG filesize). Here they are partially decoding the stream to find motion vectors for each 4x4 pixel block on every frame (and interpolating motion across I frames). They are looking at vectors in one location which are similar over a series of frames, to find real object motion. The better matched a group of vectors are in space and time, the more likely it marks a real event. Real H.264 streams also contain many "motion" vectors caused by chance matching of random pixels, but these tend to shift randomly from one frame to the next.

Their algorithm worked pretty well, creating pretty good motion masks based on the example they show. They find most of the frames with motion, and all of the motion events (because one "event" covers many frames, you can miss a few frames and still reliably detect the event).

The paper comments "The parsing process requires entropy decoding and reconstruction of motion vectors, but this process is a minor effort compared to complete decoding and reconstruction." I suppose the only way to determine how it would work is to try an implementation on the R-Pi and see what framerate/resolution is possible. Seems like you would start with a full H.264 software decoder, and then strip out everything but the entropy decoding/ MV reconstruction. I don't know much about H.264, but I see http://en.wikipedia.org/wiki/Context-ad ... tic_coding says that CABAC entropy coding used in H.264/AVC is relatively processor intensive to decode (compared with "similar algorithms").

dktak
Posts: 3
Joined: Thu Jun 13, 2013 6:04 pm

Re: Motion - MMAL camera

Thu Aug 08, 2013 7:59 pm

Thanx to peewee2's tip, I was able to configure the Rasberry to run Motion as a service, and to mail a video (when motion were detected) with PostFix and a status picture every 6 hours. It was running for 14 days when we were on holiday, no problems at all.

User avatar
jbeale
Posts: 3516
Joined: Tue Nov 22, 2011 11:51 pm
Contact: Website

thumbnail of motion area only

Fri Aug 09, 2013 8:20 pm

My motion-mmal setup is working well. I have turned on the dynamic mask ('smart_mask_speed ') which has cut down detection of trees blowing in the wind. I have also turned on 'locate_motion_mode' to draw a box around the moving object, this works well to show the area of interest.

Next question: is there any way to generate a thumbnail image which is just the area inside the 'motion box' that appears on the full image? This would be good for viewing a large set of events at once. Looking at at a web page with many scaled down 200 pixel wide thumbnails showing the full frame, they all look the same when the "motion event" of interest occupies only a small portion of the frame. I suppose I could try to hack the code myself to support this, but I presume I'm not the first person to want such a thing...

...wait, I see under External Commands, eg. 'on_picture_save' can include in the command string

Code: Select all

# %i and %J = width and height of motion area,
# %K and %L = X and Y coordinates of motion center
so I could call my own script that uses ImageMagick 'convert' for example, to extract the region of interest and create a thumbnail showing that only. Hmm...

EDIT: got this working. Posted as a new thread: http://www.raspberrypi.org/phpBB3/viewt ... 43&t=52855
Last edited by jbeale on Thu Aug 15, 2013 5:55 am, edited 2 times in total.

Return to “Camera board”