Installation Guide for motion detection with webcam


26 posts   Page 1 of 2   1, 2
by tupf » Mon Sep 24, 2012 3:52 pm
Hi, here an installation guide for motion detection with webcam and wifi. I put it in here for now. Let me know if there are better places .
Tupf.

Needed HW
  • Lenovo W500 (or other HW to programm your SD card)
  • Raspberry Pi
  • HDMI cable, ethernet cable, usb mouse and keyboard
  • power supply with mini usb (I use Vivanco PAH3)
  • Webcam Microsoft VX 800
  • SD card SAMSUNG MB-SS8GA, or other
  • wifi usb EW-7811UN

Install Driver on W500 for the built in SD drive

I read and heard a couple of times that I cannot use built in SD drives for SD card setup. It works for me though. But I had to follow the steps below.

Ref.: http://forums.lenovo.com/t5/W-Series-ThinkPad-Laptops/W500-card-reader-problems-with-a-wrinkle/td-p/202550

  • Start Windows XP and log on with a user ID authorized as an administrator
  • Click Start and then click Control Panel
  • Click Performance and Maintenance
  • Click System
  • Click the Hardware tab
  • Click Device Manager
  • Double-click the IDE ATA/ATAPI Controller section
  • Double-click Ricoh SD Bus Host Adapter
  • Click the Driver tab
  • Click the Update Driver button
  • Select No, not this time and click Next
  • Select Install from a list or specific location and click Next
  • Select Don't search. I will choose the driver to install and click Next
  • Select SDA Standard Compliant SD Host Controller and click Next
  • Follow the instructions on the screen to complete installation
  • Restart the computer

Format SD card with FAT32. Use explorer for this (right click on SD card - format ...)

Retrieve image

Ref.: http://www.raspberrypi.org/downloads

such as 2012-08-16-wheezy-raspbian.zip, this one works fine.

Setup SD card

Ref.: http://elinux.org/RPi_Easy_SD_Card_Setup

see Section "Copying the image to an SD Card on Windows if first option isn't successful"


First time boot
  • connect to tv, mouse, keyboard and power supply
  • boot prompts for boot settings: change password + ssh server. You want ssh server enabled


Editor (emacs)

Ref.: http://romkey.com/2012/06/03/gnu-emacs-on-rasperry-pi/

  • sudo apt-get update
  • sudo apt-get install emacs

Timezone
  • sudo cp /usr/share/zoneinfo/Europe/Berlin /etc/localtime

Install vnc

Ref.: http://www.raspberrypi.org/phpBB3/viewtopic.php?t=7395

  • sudo apt-get install tightvncserver
  • vncserver and enter password, and enter view only password
  • cd /etc/init.d
  • sudo emacs tightvncserver
  • copy this into the file

#! /bin/sh
# /etc/init.d/tightvncserver
#

# Carry out specific functions when asked to by the system
case "$1" in
start)
su pi -c '/usr/bin/vncserver'
echo "Starting VNC server "
;;
stop)
pkill vncserver
echo "VNC Server has been stopped (didn't double check though)"
;;
*)
echo "Usage: /etc/init.d/blah {start|stop}"
exit 1
;;
esac

exit 0


  • Make script executable: sudo chmod +x tightvncserver
  • Make it run during boottime: sudo update-rc.d tightvncserver defaults
  • Kill previous vnc server: sudo pkill Xtightvnc and check if vnc is still runninh with exeuting ps aux | grep vnc.
  • Start vnc again: sudo /etc/init.d/tightvncserver start
  • Open vnc client on other computer ...

Webcam

Ref.: http://www.chrisge.org/blog/2012-04-10/weather.chrisge.org_die_software_i/iii_motion

  • sudo apt-get install motion
  • sudo apt-get install libv4l-0
  • sudo apt-get install uvccapture
  • dmesg | tail, you should see you camera attached in the output message, if it is connected to your HW
  • edit /etc/default/motion and set "start_motion_daemon" to yes
  • edit /etc/motion/motion.conf with the following settings (these are just examples, which work for me. You will need to do optimizations yourself)

set "daemon on"
set "minimum_frame_time 5", this can be modified, depending how often you want to take picture
set "pre_capture 2"
set "post_capture 2"
set "output_normal on"
set "quality 100"
set "ffmpeg_cap_new on"
set "ffmpeg_timelapse 30"
set "ffmpeg_variable_bitrate 2"
set "get_dir /media/webcam/motion"
set "webcam_port 8080"
set "control_port 8081", this matters!!!
set "webcam_localhost off"
set "width 320", this matters!!!
set "height 240", this matters!!!


note that webcam_port, control_port, width, height do matter. Wrong settings can cause the camera not to work.

  • give /media dir access rights: sudo chmod 777 /media
  • start motion with sudo /etc/init.d/motion start
  • check if all is ok with tail -f /var/log/syslog
  • if you want to stop motion: sudo /etc/init.d/motion stop
  • check you browser if picture is displayed: http://xx.xx.xx.xx:8080/
  • controls can be also set here: http://xx.xx.xx.xx:8081/

Wireless

Ref.: http://www.raspberrypi.org/phpBB3/viewtopic.php?f=31&t=7471
Ref.: http://www.ctrl-alt-del.cc/2012/05/raspberry-pi-meets-edimax-ew-7811un-wireless-ada.html
Ref.: http://dl.dropbox.com/u/80256631/install-rtl8188cus.txt

  • cd /boot
  • get script: sudo wget http://dl.dropbox.com/u/80256631/ install-rtl8188cus-latest.sh -O /boot/install-rtl8188cus-latest.sh
  • plug in EW-7811UN
  • run script
  • enter SSID
  • enter password

done!!!!!
Posts: 14
Joined: Mon Sep 24, 2012 1:55 pm
by tupf » Sat Oct 06, 2012 7:54 am
I need to include something here, because I figured that the SD becomes full pretty fast. So I recommend to add another partition, right after Editor (Emacs). Since I used a 8GB SD card, there is plenty space:

Add another partition

Ref.: http://elinux.org/RPi_Easy_SD_Card_Setup

  • The wheezy version I used has already parted installed, otherwhise: sudo su, and apt-get install parted
  • parted /dev/mmcblk0 (this starts parted, another command line appears)
  • unit chs
  • print (you will see a number of partitions printed, I have two.)
  • mkpart primary a,b,c x,y,z (while a, b, c, x, y, z are cylinders, heads and sectors). So at the Start you need to go one sector up from the End of you last printed partition. For x,y,z you choose whatever size you want to have for you partition. Check the size of Cylinder from the print output.
  • print (you partition should be added)
  • quit (this leaves the parted command prompt.)
  • mkfs.ext4 /dev/mmcblk0p3 (this depends on how many partitions you have)
  • e2label /dev/mmcblk0p3 data
  • emacs /etc/fstab (or other editor)
  • add at the end of the file fstab: /def/mmclblk0p3 /data ext4 defaults 1 2
  • mkdir /data
  • mount /data
  • cd /data
  • mkdir webcam
  • cd webcam
  • mkdir motion

Now one modification in Webcam

Instead of setting up this in the motion.conf file

set "get_dir /media/webcam/motion"

you should setup this

set "get_dir /data/webcam/motion"

This is better!
Posts: 14
Joined: Mon Sep 24, 2012 1:55 pm
by pluggy » Sat Oct 06, 2012 9:58 am
Why not stretch the existing partition to use all of the SD card rather than putting another one on and then changing a lot of stuff so it gets used ?
Don't judge Linux by the Pi.......
I must not tread on too many sacred cows......
User avatar
Posts: 3636
Joined: Thu May 31, 2012 3:52 pm
Location: Barnoldswick, Lancashire,UK
by shuckle » Sat Oct 06, 2012 11:18 am
Do you get good quality picture?
Posts: 547
Joined: Sun Aug 26, 2012 11:49 am
Location: Finland
by tupf » Sun Oct 07, 2012 7:47 am
sure I could stretch the partition. Probably there are many ways. I just showed one way which works for me and I am happy with it.

I added a cronjob to delete old files in /data/webcam/motion if the the size becomes too large. I am testing this out now, and will update after I see it works.

The quality of pictures is alright. You cannot expect too much for a 10Euro webcam with a fixed aperture. I did not test out the maximum pixel size yet, But I will do.

It does capture a person when he walks by, but I had to change the minimum frame time in motion.conf from set "minimum_frame_time 5" to set "minimum_frame_time 3". I am still optimizing around.

I had to spent effort in keeping the camera in the shadow by attaching a shield around it. Direct sunlight causes overexposed pictures. Also sudden cloud covers causes motion to capture pictures. But I was able to minimize it, and it works ok now. I will also put some light absorbing paint around the camera, and see if this helps. Probably a better camera with automatic aperture would improve all this, but I dont want to spent money for it.
Posts: 14
Joined: Mon Sep 24, 2012 1:55 pm
by tupf » Wed Oct 10, 2012 6:55 pm
One more thing:

I had the problem that jpg and swf files from motion added up about 100M of space every day, so I want to delete old files automatically.

I solved this by adding the entries below in the crontab file and it works fine:

  • sudo su
  • cd /etc
  • emacs crontab
  • append the entries below
0 12 * * * root find /data/webcam/motion/* -mtime +2 -exec rm {} \;
0 23 * * * root ifdown wlan0
30 5 * * * root ifup wlan0


The first item deletes all files older than 2 days from the /data/webcam/motion/. The last two crontab items switch off wlan at 11 pm and turn it on at 5.30 am.
Posts: 14
Joined: Mon Sep 24, 2012 1:55 pm
by 5up Mushroom » Sun Oct 21, 2012 2:26 pm
For the file storage, you could use NFS (assuming you have a linux box elsewhere on the same network).

On the other linux box:
1. sudo apt-get install nfs-kernel-server
2. sudo {your editor of choice} /etc/exports - this file tells your nfs server which computers are allowed to connect to which folder on your nfs server
3. To allow any computer on your network to connect to a folder on your nfs server (assuming your router assigned IPs in the 192.168.1.1-254 range... you might have to change that to 192.168.0.0 depending on the DHCP settings on your router.)

/the/folder/ 192.168.1.0/255.255.255.0(rw,fsid=0,insecure,no_subtree_check,async)

On your PI:
4. Make a new directory to link to your share folder: mkdir ~/nfs
5. sudo apt-get install nfs-client
6. sudo {your editor of choice} /etc/fstab
7. To mount the nfs share on raspberry startup toss this line in fstab:

{your nfs servers ip}:/the/folder /home/pi/nfs nfs soft,intr,rsize=8192,wsize=8192

8. Now just reboot and you should be golden. sudo shutdown -r now

You should now be able to navigate to /home/pi/nfs and see everything in the folder on your other computer. You can just create a bash script to move older recordings to the folder and toss in a new cron job to kick of the script.
Posts: 1
Joined: Sun Oct 21, 2012 1:12 pm
by properphatboy » Sat Dec 01, 2012 10:52 am
Awesome guide tupf.

I'm just struggling on 2 things. The first is that I can't find the "get_dir /data/webcam/motion" anywhere in the file.

The second is that I'm not familiar with VNC. Having set it up as shown above, how do I then access it from my windows laptop?

Thanks,

Dee.
Dee
Posts: 14
Joined: Wed Nov 21, 2012 6:42 pm
Location: Bristol
by tupf » Sat Dec 08, 2012 5:24 pm
Thanks Dee.

about the vnc viewer. Just google for vncviewer you will find many download sites. You will need to install on your windows laptop.

about the motion.conf, looks like it is a typo. It should be target_dir ... and not get_dir. Thanks for pointing it out.

Tupf.
Posts: 14
Joined: Mon Sep 24, 2012 1:55 pm
by skyemoor » Wed Oct 23, 2013 8:12 pm
I was trying to figure out how to use Motion in combination with raspivid, but this obviates the need for the latter altogether, saving quite a bit of processing power.

Good thinking, thanks!
Posts: 11
Joined: Wed Oct 16, 2013 1:21 pm
by caporeira » Sun Jan 26, 2014 10:58 pm
I can't install motion on my Rasbian. Anyone have idea why ?

Code: Select all
pi@raspberrypi ~ $ sudo apt-get install motion
Reading package lists... Done
Building dependency tree
Reading state information... Done
motion is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
1 not fully installed or removed.
After this operation, 0 B of additional disk space will be used.
Do you want to continue [Y/n]? Y
Setting up watchdog (5.12-1) ...
/run/udev or .udevdb or .udev presence implies active udev.  Aborting MAKEDEV in                              vocation.
insserv: warning: script 'mathkernel' missing LSB tags and overrides
insserv: There is a loop between service watchdog and mathkernel if stopped
insserv:  loop involving service mathkernel at depth 2
insserv:  loop involving service watchdog at depth 1
insserv: Stopping mathkernel depends on watchdog and therefore on system facilit                              y `$all' which can not be true!
insserv: exiting now without changing boot order!
update-rc.d: error: insserv rejected the script header
dpkg: error processing watchdog (--configure):
 subprocess installed post-installation script returned error exit status 1
Errors were encountered while processing:
 watchdog
E: Sub-process /usr/bin/dpkg returned an error code (1)


And when I want expand sd card:
Code: Select all
pi@raspberrypi ~ $ sudo raspi-config

Command (m for help):
Disk /dev/mmcblk0: 16.1 GB, 16147021824 bytes
4 heads, 16 sectors/track, 492768 cylinders, total 31537152 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000981cb

        Device Boot      Start         End      Blocks   Id  System
/dev/mmcblk0p1            8192      122879       57344    c  W95 FAT32 (LBA)
/dev/mmcblk0p2          122880    31537151    15707136   83  Linux

Command (m for help): Partition number (1-4):
Command (m for help): Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): Partition number (1-4, default 2): First sector (2048-31537151, default 2048): Last sector, +sectors or +size{K,M,G} (122880-31537151, default 31537151): Using default value 31537151

Command (m for help):
Disk /dev/mmcblk0: 16.1 GB, 16147021824 bytes
4 heads, 16 sectors/track, 492768 cylinders, total 31537152 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000981cb

        Device Boot      Start         End      Blocks   Id  System
/dev/mmcblk0p1            8192      122879       57344    c  W95 FAT32 (LBA)
/dev/mmcblk0p2          122880    31537151    15707136   83  Linux

Command (m for help): The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
update-rc.d: using dependency based boot sequencing
update-rc.d: warning: default start runlevel arguments (2 3 4 5) do not match resize2fs_once Default-Start values (2 3 4 5 S)
update-rc.d: warning: default stop runlevel arguments (0 1 6) do not match resize2fs_once Default-Stop values (none)
insserv: warning: script 'mathkernel' missing LSB tags and overrides
insserv: There is a loop between service watchdog and mathkernel if stopped
insserv:  loop involving service mathkernel at depth 2
insserv:  loop involving service watchdog at depth 1
insserv: Stopping mathkernel depends on watchdog and therefore on system facility `$all' which can not be true!
insserv: exiting now without changing boot order!
update-rc.d: error: insserv rejected the script header
Posts: 71
Joined: Sun Aug 25, 2013 5:58 pm
by DougieLawson » Sun Jan 26, 2014 11:08 pm
Start by fixing the /etc/init.d/mathkernel script

Either
sudo apt-get purge wolfram*
or
copy the first ten lines from /etc/init.d/skeleton to /etc/init.d/mathkernel
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
RPi owner since '12.
Twitter: @DougieLawson

2B, B+, A+, 5Bs, zero, 3B

Please post ALL technical questions on the forum.Do not send private messages.
User avatar
Posts: 22267
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
by caporeira » Sun Jan 26, 2014 11:16 pm
The first line on this two files looks the same: "PATH=/sbin:/usr/sbin:/bin:/usr/bin"

Ok, I was used this command: sudo apt-get purge wolfram*
and now is installed. I'm config from this tutorial -> Installing the motion detection software (http://www.codeproject.com/Articles/665 ... nce-camera)

But raspberryip:8080 and 8081 port dosen't work :(
Posts: 71
Joined: Sun Aug 25, 2013 5:58 pm
by griffij10 » Thu Jun 05, 2014 10:47 pm
Hi there,
Is there any way that I can change the place where the images save so that I can view them and delete them easier from a Windows computer that is attached to the pi?

Thanks

Joel
Posts: 5
Joined: Sun May 25, 2014 11:02 pm
by roedel » Fri Jun 06, 2014 9:24 am
Just have a look at the documentation:
http://www.lavrsen.dk/foswiki/bin/view/ ... le_Options

You are looking for the config option target_dir.

best,
Stephan
Posts: 4
Joined: Tue Sep 24, 2013 11:23 am
by griffij10 » Mon Aug 04, 2014 8:42 am
Hi Stephan,

I have changed the target_dir from /tmp/motion to /home/pi/Desktop/Windows-Share but instead of saving the image to there, the camera works for a second and then turns off. Is there anything else I need to change in order for it to work?
Thanks
Posts: 5
Joined: Sun May 25, 2014 11:02 pm
by griffij10 » Wed Sep 03, 2014 7:19 pm
Can anyone tell me how to save motion pictures onto a usb stick so I can then view them on another machine? As I'm kinda new to Linux can you give me a step by step walkthrough please
Thanks! :)
Posts: 5
Joined: Sun May 25, 2014 11:02 pm
by Ivan Tham » Thu Sep 04, 2014 8:48 am
Does anyone knows how to use motion with fswebcam. I always get bad quality pictures with uvccapture.
Posts: 75
Joined: Mon Jun 24, 2013 7:06 am
by harold95 » Mon Sep 22, 2014 8:41 pm
i had this working, but something went knuts

Code: Select all
Sep 22 16:39:10 raspberrypi motion: [1] Retrying until successful connection with camera
Sep 22 16:39:10 raspberrypi motion: [1] connect returned error: Operation now in progress
Sep 22 16:39:10 raspberrypi motion: [1] Failed to open camera - check your config and that netcamera is online
Sep 22 16:39:10 raspberrypi motion: [1] Failed to read first camera header - giving up for now



camera is plugged in, but it won't work. as i said, it WAS working.

any ideas?

thanks
harold
1. B pi, usb ext pwrd hdd 500gb, pwrd usb hub 2a (pwrs pi), wifi, vga adptr, wired kb/mouse, usb audio.
2. B pi, hdlss. adaptd for use as an IRLP node. IRLP rev 3 board wired to gpio, usb audio, Alinco DR135 2 meter ham radio(www.irlp.net)
Posts: 20
Joined: Thu Feb 06, 2014 11:28 pm
Location: Pawtucket Rhode Island USA
by neilld » Sat May 23, 2015 11:39 am
I have been using Motion software on my PI as a security camera with a standard PC webcam as the source. I am not using motion detection but the 8GB SD card is now full.
It seems that Motion is creating files whether or not motion detection is used.
Anyone know where are these files located so that I can delete & free up some space on the card?

thanks in advance

DFN
Posts: 29
Joined: Mon Dec 22, 2014 3:03 pm
by Slacsteve » Sat Jun 27, 2015 1:43 pm
Avoiding having to delete files:

I changed the file name of mpgs to just %H-timelapse.mpg so that I only save 24 1 hour movies. Mode is set to hourly so the the file is finalized after the end of the hour. I then use the movie end trigger (External Command section of motion.conf) to run ncftpput the file to my FTP server. They are overwritten daily. Next I will use the above mentioned crontab control of WLAN to avoid uploading nighttime videos.
Posts: 2
Joined: Sat Jun 27, 2015 1:30 pm
by jwbrock » Fri Jul 24, 2015 7:23 am
I switched from Ubuntu to Raspbian for my motion detection system and after that the video's on my iPhone are blank (they do work VLC). I tried adding -profile -level -refs, etc to ffmpeg directly (i.e. w/ a script) , but with no luck so far.
I also tried to compile ffmpeg, but again with no luck.

How do I generate movies viewable on an iPhone?
Posts: 1
Joined: Fri Jul 24, 2015 7:11 am
by fitzy89 » Sat Dec 19, 2015 10:15 pm
Hello

Thanks very much for your guide, after months of trying to get this working with Windows apps I've finally got it to do what I want with the Pi.

One issue I seem to be encountering though is the slight 'combing' on the live stream image. I have read through the config files and got it performing very well with my camera which is a Sunluxy analogue CCTV camera through a USB capture card.

This combing seems to happen in stripes across various parts of the screen, but never over the whole screen. I have tried enabling the deinterlace settings in the config file and restarting motion but this didn't seem to make any difference at all.

Can anyone advise what I could do to resolve this? I am aiming to get a nice smooth clear playback via a web browser so I have set the framerate to 100, although it should be noted that the results are the same even if it's set as low as 5 fps. The Pi (and client watching the stream) is connected by ethernet for the fastest and most reliable networking which should rule out any network speed issues.

My config file is here: http://paste.debian.net/351906/

And an example screenshot is: http://rob.fitzgerald.tech/screenshots/stripes.PNG

Kind regards
Rob
Posts: 17
Joined: Mon Jul 15, 2013 10:27 am
by steve8428 » Sun Jan 31, 2016 9:26 am
I need some help getting motion to work. I have installed it and edited the config file with the following

daemon on
web control_port 8081
web control_localhost off
stream_port 8080
width 640
height 480
target_dir /home/pi/m-video
output_pictures off
text_left Pi-cam %t
logfile /home/pi/mmal/motion.log

and when i start motion with sudo service motion start I get the command line so assume it has started.

But when going to my browser in firfox Pi Ip:8080 or 8081 nothing happens.

The camera is a pi module camera and can take still images with the following command

raspistill -o cam.jpg

so I assume again this camera is set up correct. I have port forwarded both ports one at a time on my network router.

I am also running the Pi on a wireless dongle so not sure if this is a problem.
I have apache server running on this Pi wich works all ok and I can access this over the internet

Any one have any ideas what I can try.
My goal was to have a CCTV image on the apache server html page on the pi with is updated when motion is detected.

thanks :?
Posts: 31
Joined: Mon Jan 05, 2015 11:28 pm
by PiJay » Mon Mar 07, 2016 7:10 pm
Great writeup - thank you! I have it working but have a separate question: Is their any way to output the video to the (LCD) console?
Posts: 6
Joined: Mon Mar 07, 2016 7:08 pm