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

Re: Motion - MMAL camera

Wed Aug 14, 2013 10:08 am

Hello raspies,
I came up with 3 scenario's for motion detection:

1)Do not use motion, but use a raspivid stream over the network and let a powerful server do the motion detection and video encoding (e.g iSpy).
->Problem: constant network bandwith hogging.

2)Use motion and let ffmpeg do the video encoding.
->Problem: CPU can't handle the higher resolutions

3)Use motion, record raw video and let a powerful server do the encoding.
-> CPU usage on RPi is low and network bandwith is limited to when there is actual motion.
For this scenario I am writing first to the SD-card for max throughput and then crontab a copy to the server.
I am using extpipe and I disabled image, ffmpeg and http-streaming outputs.

Code: Select all

# Maximum number of frames to be captured per second.
# Valid range: 2-100. Default: 100 (almost no limit).
framerate 10

extpipe mencoder -demuxer rawvideo -rawvideo w=640:h=480 -ovc raw -of avi -o %f.avi - -fps 25
Output:

Code: Select all

[-1242688672] [NTC] [ALL] motion_startup: Logging to file (/home/pi/motion.log)
MEncoder svn r34540 (Debian), built with gcc-4.6 (C) 2000-2012 MPlayer Team
Reading from stdin...
success: format: 0  data: 0x0 - 0x0
rawvideo file format detected.
[V] filefmt:26  fourcc:0x30323449  size:640x480  fps:25.000  ftime:=0.0400
Input fps will be interpreted as 25.000 instead.
Opening video filter: [expand osd=1]
Expand: -1 x -1, -1 ; -1, osd: 1, aspect: 0.000000, round: 1
==========================================================================
Opening video decoder: [raw] RAW Uncompressed Video
Movie-Aspect is undefined - no prescaling applied.
Selected video codec: [rawi420] vfm: raw (RAW I420)
==========================================================================
Writing header...
ODML: vprp aspect is 4:3.
Writing header...
ODML: vprp aspect is 4:3.
Pos:   0.8s     21f ( 0%)  2.42fps Trem:   0min   0mb  A-V:0.000 [0:0]

Flushing video frames.
Writing index...
Writing header...
ODML: vprp aspect is 4:3.

Video stream: 92160.000 kbit/s  (11520000 B/s)  size: 9216000 bytes  0.800 secs  21 frames
As you can see I cannot go higher than 2-3 fps. While the raspivid commands can easily do 25fps.
Alternatives:
a)I also tried to use the built in ffmpeg instead of extpipe, but it doesn't take the rawvideo codec.
b)Can I launch raspivid when motion is detected?

Any idea's?

Greets PD

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

Re: Motion - MMAL camera

Wed Aug 14, 2013 12:40 pm

Unfortunately the motion detection algorithm used in motion is also quite a load on the Pi, so just disabling ffmpeg output isn't enough on its own to achieve much better framerates. It also depends on whether you turn on some of the other image processing options such as masking or smart masking. I'm currently looking at optimising this algorithm for the Pi when I have time available.

I don't think you'll be able to launch raspivid while motion is running, as motion-mmal will be using the Pi's camera. You'd have to kill motion and start raspivid, then restart motion when you've decided to stop recording video. The likelihood there is that you'll miss some initial portion of the detected motion in the recording, and if it is quick motion you could miss all of it!

It ought to be possible to extend the Pi camera support in motion to also encode video using the H.264 encoder in a similar way to raspivid - that's on my list to look at when I get time. However I think the motion detection algorithm will need to use less CPU in order to make the encoding smooth.

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

Re: Motion - MMAL camera

Wed Aug 14, 2013 1:17 pm

Thx for the info dozencrows.

However in my current config, CPU doesn't seem to be the issue.
I can do motion detection and let mencoder encode to raw video while top still tells me I have 50% idle CPU.
Also when motion is running and I do the same mencoder command with an avi-file as input: I get 60fps instead of 5fps.

So it seems extpipe is being capped to a framerate of max 5fps by motion itself?
Which is strange as mencoder detects the stream as 25fps

Code: Select all

rawvideo file format detected.
[V] filefmt:26  fourcc:0x30323449  size:640x480  fps:25.000  ftime:=0.0400
Input fps will be interpreted as 25.000 instead.
Is this correct? And if yes would it be possible to feed motion a 25 fps input, while he only checks every 5th frame (thus 5 fps), and yet still send the 25fps to extpipe?

greets
PD

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

Re: Motion - MMAL camera

Wed Aug 14, 2013 1:54 pm

I'm afraid I've no experience with rawvideo and mencoder to make further suggestions at the moment.

motion-mmal does have some issues with reliability of frame rate in encoded video; however I don't yet know if this is due to performance or poor synchronisation timing.

If I have some time this weekend, I'll try it out to see if I can shed further light on it.

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

Re: Motion - MMAL camera

Thu Aug 15, 2013 7:51 am

Hi, Great thread. I'm using Motion -MMAL which is working great thanks solely to work done here. I also added sitecopy proposed by jbeale to ftp avi file to web site and it works great. Question I hope someone can help with. I would like to be able to access and play avi files from phone or other computers over web. I use wordpress web site. Any suggestions welcome. Cheers Peter
P.S. FFMPEG not supported on my web server package, Viper's Video Quicktags plugin in wordpress plays avi file but these files have to be manually placed in post

clint9090
Posts: 7
Joined: Thu Jan 17, 2013 3:40 pm

Re: Motion - MMAL camera

Thu Aug 15, 2013 3:31 pm

First: Awesome thread!!!

Second: I've got everything up and running smoothly thanks guys! However, I'd like to be able to capture images with the same quality/field of view that I get out of raspistill, but I end up with poorly exposed, mostly underexposed/dark images. I've tried setting:

mmalcam_use_still 'on'
framerate 2
minimum_frame_time 0.25

I've turned off capturing movies and streaming:
ffmpeg_output_movies off
stream_port 0

Does anyone have any suggestions on getting the image quality up to match raspistill? As you've noticed from the configs above, I'm purely interested in getting the best possible image, so I'm not concerned with movies or network streaming. At this point, I'm considering using a pir sensor and the gpio and passing on motion entirely, but I'd rather not give up just yet. ;)

Thanks!!!
Clint

clint9090
Posts: 7
Joined: Thu Jan 17, 2013 3:40 pm

Re: Motion - MMAL camera

Thu Aug 15, 2013 3:34 pm

Sorry, my actual config does not contain quotes around 'on' for the mmalcam_use_still setting. :oops:

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

Re: Motion - MMAL camera

Thu Aug 15, 2013 4:49 pm

clint9090 wrote:Second: I've got everything up and running smoothly thanks guys! However, I'd like to be able to capture images with the same quality/field of view that I get out of raspistill, but I end up with poorly exposed, mostly underexposed/dark images. I've tried setting:
mmalcam_use_still 'on'
framerate 2
minimum_frame_time 0.25
I've never tried the use_still option in mmal-motion, but I do know that you may get underexposed or black images from raspistill if you do not allow enough time for it to work. That is why raspistill has a 5-second default 'camera on' time for autoexposure to stabilize, before the real still frame is taken. I speculate that mmal-motion may be attempting to take 2 stills per second ('framerate 2') and then only using one out of every eight of those ('minimum_frame_time 0.25'). That might work OK with a typical video camera, but not with the RPi camera in stills mode where the process looses track of the autoexposure setting in between each invocation of the 'take still image' command.

There is another thread on a modification to rapsistill called "RaspiFastCamD" here: http://www.raspberrypi.org/phpBB3/viewt ... 43&t=50075
If my speculation is true, then a rewrite of mmal-motion to use the approach taken in RaspiFastCamD would solve this problem.

If you do end up switching to a PIR detector approach, you will probably want to use RaspiFastCamD to get a still immediately (supposedly < 10 msec latency) instead of waiting a second or more for raspistill to get around to capturing the frame.

joantune
Posts: 7
Joined: Fri Aug 16, 2013 9:38 pm

Re: Motion - MMAL camera

Sat Aug 17, 2013 12:22 am

dozencrows wrote: I don't think you'll be able to launch raspivid while motion is running, as motion-mmal will be using the Pi's camera. You'd have to kill motion and start raspivid, then restart motion when you've decided to stop recording video. The likelihood there is that you'll miss some initial portion of the detected motion in the recording, and if it is quick motion you could miss all of it!
Would this help:
http://www.lavrsen.dk/foswiki/bin/view/ ... backDevice
?


I have been reading this thread in one sitting, and this is exciting work!

Thanks for the contribution @dozencrows, also thanks to @jbeale for the insights and for everybody else that is making this community thrive and this question solved. I have some insights to share, as I was reading and googling for some alternatives. This thread has cut into my sleep time, so I should come back with a decent post tomorrow, under the penalty of being like a zombie tomorrow.

Cheers

joantune
Posts: 7
Joined: Fri Aug 16, 2013 9:38 pm

Re: Motion - MMAL camera

Sat Aug 17, 2013 12:28 am

PeterN wrote: I also added sitecopy proposed by jbeale to ftp avi file to web site and it works great.
@PeterN & @jbale, why not use rsync? I never tried sitecopy, but by the looks of its manual, it doesn't allow partial updates and it could be faster (?) oh, is it because rsync doesn't support ftp? I was just curious..

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

Re: Motion - MMAL camera

Sat Aug 17, 2013 7:02 am

Hi Joantune, Only reason I used sitecopy it was first package I came accross. rsync looks interesting and I will certainly investigate more. I read some posts about sitecopy and FTP in relation to FTP security or lack of. Cheers Peter

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

Re: Motion - MMAL camera

Sat Aug 17, 2013 11:40 pm

rsync is the way to go, it can work over ssh
see http://www.thegeekstuff.com/2011/07/rsy ... -password/

however copying to a second host would beg the question why not just use a CCTV camera, either over coax or a wifi camera directly connected to the second PC, the price difference is minimal, and the solution is much more robust.

the raspicam solution is great for simple motion detection where you just want a historical record, eg catching a squirrel eating nuts, but not to stream HD vids in real time.


Pete

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

Re: Motion - MMAL camera

Sun Aug 18, 2013 3:27 pm

For what it's worth- sitecopy does do partial updates. It only updates those files which have changed. If you're using a R-Pi, and you want a record of the image, you can't use a standard (analog) CCTV camera because there is no working analog video capture device. An IP camera would work fine, but they are typically more expensive (and in that case you would not need the R-Pi at all). The webcams in the RPi+CSI Camera price category do not have as good picture quality. The "HD IP Camera" listings on ebay I have seen below about $80 actually use a 640x480 sensor.

M_C
Posts: 4
Joined: Mon Aug 19, 2013 10:12 am

Re: Motion - MMAL camera

Mon Aug 19, 2013 10:14 am

Can motion use as input file/device named pipe connected to stream from raspivid?

joantune
Posts: 7
Joined: Fri Aug 16, 2013 9:38 pm

Re: Motion - MMAL camera

Tue Aug 20, 2013 10:53 am

M_C wrote:Can motion use as input file/device named pipe connected to stream from raspivid?
@M_C, read my advice on the post above, basicly, that looks like what the Loopback device of Motion was created for, I haven't tried it, but it looks like a good place to start:
http://www.lavrsen.dk/foswiki/bin/view/ ... backDevice

joantune
Posts: 7
Joined: Fri Aug 16, 2013 9:38 pm

Re: Motion - MMAL camera

Tue Aug 20, 2013 11:22 am

Ok, so, my understanding of the current state of things is:
  • Thanks to @dozencrows, we are able to use MMAL camera with motion. This is an improvement as using the MMAL api, you get automagically h.264 hardware encoding, right? but the actual motion detection algorithms of motion takes hella of a toll from the CPU;
  • Some optimization on the motion detection algorithm have beem made, again, by @dozencrows (thanks) and he is on his way to do some more.
  • @jbeal, very cleverly remembered us that the h.264 encoding proccess encompasses motion detection, to allow reusage of frames, thus reducing redundancy, thus attaining compression, and he even shared with us an article were researchers were able to use a h.264 video that was already encoded, to post analyze it and gather from it, without fully decoding it and without having to reanalyze it (? not sure on this last one), its motion vectors (that give you exactly what kind of 'motion' was discovered by the encoder) and using them for motion detection
My suggestions are:
  • Why not on the proccess of encoding the h.264 video, analyze at the same time the output of the motion vectors (if it is possible to access those) for motion detection. This would take away the need to post process the video. On this question, and regarding if its viable or not, I have some questions to the community (@dozencrows or someone else who can answer them):
    - How do you extract the video? would it be possible to use something of the sorts: https://github.com/codebrainz/motiondetector - is that GStreamer thing something that actually encodes the video? are we able to skip the use of OpenCV (I think it's kinda slow), or use the OpenCV only to process the motion vectors? do we have access to the motion vectors of the encoding process? if so, we might be able to use the algorithms proposed on the paper that @jbeal showed us, without the cost of redoing the work that the hardware encoder already does
So, I guess that's it, any feedback is welcomed.

@dozencrows: can u tell me the SVN revision where you extracted the motion code from? that would be helpful for me to do a diff to show how you added support for the MMAL camera so that I can start answering my own questions above.

lebeck
Posts: 4
Joined: Tue Aug 20, 2013 8:06 am

Re: Motion - MMAL camera

Tue Aug 20, 2013 9:26 pm

dozencrows wrote:@bjornatic, @yusuk - my apologies - I suspect you're wanting information on how to install and run my test version, which I haven't made clear.

I would recommend trying the following:
1. Install the standard Raspbian motion package via apt-get. This should add all the dependencies.

2. Unzip the motion-mmal tarball into your home folder.

3. Try running motion from your home folder -

Code: Select all

./motion -h
If that works, you should get the help text for the tool.

4. Then try running it with the provided config file -

Code: Select all

./motion -n -c motion-mmalcam.conf
. This should work - test it out by checking the webcam feed on your Pi on port 8081.

If you have a problem with libjpeg, see the post above about installing libjpeg62.

Let me know if you have any issues - please provide relevant details (e.g. command lines, changes to .conf files, error messages).
Hi dozen,

I've found a bit more detail steps but doesn't work here. Any idea of what is wrong.
(sorry but i'm new with pi and linux)

Type "sudo apt-get install -y libjpeg62 libjpeg62-dev libavformat53 libavformat-dev libavcodec53 libavcodec-dev libavutil51 libavutil-dev libc6-dev zlib1g-dev libmysqlclient18 libmysqlclient-dev libpq5 libpq-dev".
Type "wget https://www.dropbox.com/s/xdfcxm5hu71s9 ... mal.tar.gz".
Type "tar zxvf motion-mmal.tar.gz".
Type "sudo nano /home/pi/motion-mmalcam.conf".

Code: Select all

Edit “width” to: 640
Edit “height” to: 480
Edit “mmalcam_use_still” to: mmalcam_use_still on
Edit “threshold 1500” to: threshold 3000
Edit “output_pictures” to: output_pictures on
Edit “quality 75” to: quality 100
Edit “ffmpeg_output_movies” to: ffmpeg_output_movies off
Edit “target_dir” to: target_dir /home/pi
Edit “stream_port” to: stream_port 0
Edit “webcontrol_port” to: webcontrol_port 0
Press CTRL+X, then y, and then enter to save the changes to your configuration file.

Navigate to "/home/pi".
Type "sudo nano sar.sh".
Type exactly what’s below:

Code: Select all

#!/bin/sh
.	/motion -n -c motion-mmalcam.conf
Press CTRL+X, then y, and then enter to save the changes to your configuration file.
Type "sudo nano rm.sh".
Type exactly what’s below:

Code: Select all

#!/bin/bash
cd /home/pi
while true; do
rm -f *.jpg
sleep 60
done
Press CTRL+X, then y, and then enter to save the changes to your configuration file.
Type "sudo chmod +x /home/pi/sar.sh".
Type "sudo chmod +x /home/pi/rm.sh".
Type "sudo nano /etc/xdg/lxsession/LXDE/autostart".
Under @xscreensaver -no-splash, type what’s below:

Code: Select all

@/home/pi/sar.sh
@/home/pi/rm.sh

Press CTRL+X, then y, and then enter to save the changes to your configuration file.
Type "sudo reboot".

lebeck
Posts: 4
Joined: Tue Aug 20, 2013 8:06 am

Re: Motion - MMAL camera

Tue Aug 20, 2013 9:50 pm

ok worked when i typed ./motion -n -c motion-mmalcam.conf

but i still get a small problem.
dunno if anyone had the same
the video gets darker and darker until it gets totally black in around 1 minutes.

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

Re: Motion - MMAL camera

Sat Aug 24, 2013 2:55 pm

@lebeck: please try with mmalcam_use_still turned off - there's an issue with how still capture is configured, which causes the darkening you're experiencing.

@joantune: Have a look at my motion repository on GitHub. This was forked from the "official" motion repository, and the master branch is the original source from which I created the mmal-test branch. I think the sackmotion repository is created from the SVN.

Oxtailsoup
Posts: 1
Joined: Tue Sep 03, 2013 4:51 pm

Re: Motion - MMAL camera

Tue Sep 03, 2013 4:57 pm

Hey guys,

did you achieve some success in order to solve the darkness problem with using raspistill in motion?
I'm still not very happy with the pictures motion is taking.

dave_p
Posts: 28
Joined: Sun Mar 04, 2012 2:11 pm

Re: Motion - MMAL camera

Thu Sep 05, 2013 6:54 am

I've had the same problem others have reported in not being able to view the .avi files in VLC - only the first frame is shown. The solution in my case was to change the following line in motion-mmalcam.conf:

Code: Select all

ffmpeg_video_codec mpeg4
to

Code: Select all

ffmpeg_video_codec msmpeg4
The files now play correctly in VLC on my laptop - I haven't tried viewing them directly on the Pi.

Pachops427
Posts: 6
Joined: Sun Dec 16, 2012 9:15 pm

Re: Motion - MMAL camera

Fri Sep 06, 2013 5:55 pm

Hi, when I set up motion-mmal it doesn't appear to have a config file, so if somebody would be kind enough to post their one I would be really grateful, as it keeps trying to load a USB webcam, not the Raspberry Pi Camera Board, and I really want to get this working.
Thanks!

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

Re: Motion - MMAL camera

Sat Sep 07, 2013 12:13 pm

The config file should be present in the motion-mmal tarball, called motion-mmalcam.conf - you need to run it using a command line like this:

Code: Select all

./motion -n -c motion-mmalcam.conf
This assumes the current working directory contains the extracted tarball files. Note the ./ at the start of the line - this ensures the version of motion in this directory is run, not any installed Raspbian package.

If this doesn't help, please post the command line you're using to run motion, along with any output.

cursor2000
Posts: 11
Joined: Tue Jan 29, 2013 9:55 pm
Location: Herefordshire
Contact: Website

Re: Motion - MMAL camera

Wed Sep 18, 2013 9:17 pm

This is a great thread.

Ultimately I'd like to use motion-mmal and the RPi camera module to capture 25fps video when motion is detected. I've got good results with 2fps video but am I expecting too much from the RPi to get 25fps at 640x480? I thought I could trigger raspivid on motion detection but as another member pointed out, this isn't possible because motion-mmal is already using the camera.

If anyone has managed this I'd love to see the values they've used in the .conf file.

Thanks in advance!

PaoloUd
Posts: 3
Joined: Fri Sep 20, 2013 5:52 am

Re: Motion - MMAL camera

Fri Sep 20, 2013 6:05 am

Hi,
first of all thanks to dozencrows...
My problem...
When I run motion with :
./motion -n -c motion-mmalcam.conf
the program starts, but in the log file appears this:

Code: Select all

-1242528928] [NTC] [ALL] [Sep 19 23:22:41] motion_startup: Using log type (ALL) log level (WRN)
[0] [ERR] [STR] [Sep 19 23:22:41] httpd_run: motion-httpd failed bind() interface 127.0.0.1 / port 8080, retrying:
[0] [ERR] [STR] [Sep 19 23:22:41] httpd_run: motion-httpd socket failed interface 127.0.0.1 / port 8080, retrying:
[0] [ERR] [STR] [Sep 19 23:22:41] httpd_run: motion-httpd failed bind() interface 127.0.0.1 / port 8080, retrying:
[0] [ERR] [STR] [Sep 19 23:22:41] httpd_run: motion-httpd socket failed interface 127.0.0.1 / port 8080, retrying:
[0] [CRT] [STR] [Sep 19 23:22:41] httpd_run: motion-httpd ERROR bind() [interface 127.0.0.1 port 8080]:
[1] [ALR] [VID] [Sep 19 23:22:41] mmalcam_start: MMAL Camera thread starting... for camera (vc.ril.camera) of 640 x 480 at 2 fps
[1] [ALR] [VID] [Sep 19 23:22:41] mmalcam_start: MMAL Camera using video capture
[1] [CRT] [STR] [Sep 19 23:22:41] http_bindsock: motion-stream bind() failed, retrying:
[1] [ERR] [STR] [Sep 19 23:22:41] http_bindsock: motion-stream socket failed, retrying:
[1] [CRT] [STR] [Sep 19 23:22:41] http_bindsock: motion-stream creating socket/bind ERROR:
[1] [ERR] [ALL] [Sep 19 23:22:41] motion_init: Problem enabling motion-stream server in port 8081:
[1] [ALR] [VID] [Sep 19 23:22:41] MMAL Camera cleanup
[1] [ALR] [VID] [Sep 19 23:22:42] mmalcam_start: MMAL Camera thread starting... for camera (vc.ril.camera) of 640 x 480 at 2 fps
[1] [ALR] [VID] [Sep 19 23:22:42] mmalcam_start: MMAL Camera using video capture
[1] [CRT] [STR] [Sep 19 23:22:42] http_bindsock: motion-stream bind() failed, retrying:
[1] [ERR] [STR] [Sep 19 23:22:42] http_bindsock: motion-stream socket failed, retrying:
[1] [CRT] [STR] [Sep 19 23:22:42] http_bindsock: motion-stream creating socket/bind ERROR:
[1] [ERR] [ALL] [Sep 19 23:22:42] motion_init: Problem enabling motion-stream server in port 8081:
[1] [ALR] [VID] [Sep 19 23:22:42] MMAL Camera cleanup
etc...

Anyone able to tell me what I have done wrong?

Thanks
Paolo

Return to “Camera board”