Creating a motion-detecting security cam with a Raspberry Pi – Part 1

As mentioned in these previous posts, I've been spending some time developing a social media-enabled security cam using a Raspberry Pi and a standard webcam. The eventual idea is that the security cam will check visitors against a database of photos of a homeowners' friends extracted from Facebook.

I have a lot of the needed "social" components in place – more on those in a future post – but I did just want to document some of the steps needed to create a functional security cam that simply uploads captured videos to Google Drive and sends an email with both a link to the video and an attached image frame (to make quick identification of the visitor easier, especially when reading the email on a mobile device).

Most of the components needed for this were in place – and have been used to good effect in several other comparable projects out there – but I thought I'd just gather some key links in one place (some of which are repeated from last time):

Raspberry Pi + Debian = RaspbianRaspbian "wheezy" has a number of apparent advantages over its counterpart OS distributions – it includes support for a lot of things out-of-the-box – but also has a key disadvantage in that it doesn't have webcam support. So I had to jump through a few hoops to get that working – no doubt more than were strictly needed, so I won't list them here. I'm thinking of trying Arch Linux ARM next, which does come with the all-important webcam support (and while it's apparently targeted at more advanced users, I feel like I'm gradually getting to the point where I can handle it :-).

Logitech QuickCam Communicate DeluxeOne of the key issues I've been hitting is around resolution: I initially found I had to increase the Motion capture resolution to 960 x 720 for my Logitech Quickcam Communicate Deluxe to work properly, but then found that for the device to work reliably over a period of time (i.e. seconds), I had to reduce that down to 352 x 288 (the default resolution for the Motion sub-system). At a higher resolution the Pi just stopped working – I had to cycle the power to reboot it – which implied a pretty significant throughput issue. Even now I'm finding the webcam stops responding after a period of working – which seems to be due to another problem, possibly due to some webcams requiring a powered USB hub as they otherwise consume too much of the Pi's own supply – but I'm trying to work through that, now. It's possible the stability issues are more related to the use of Raspbian – I'll hopefulyl find out more once I've tried Arch Linux ARM.

When managing Motion, I found this post to be extremely helpful in terms of setting up a script to kiMotionll it when running in daemon mode. I also found this introduction to be very good, too. I'll publish my working configuration file for Motion in the next post – hopefully with step-by-step instructions on getting the whole system up and running.

The notification system – which takes the video created by Motion, uploads it to Google Drive and then uses Gmail to send a link to the video to the chosen recipient – was already pretty complete. The main change I wanted to make was to take the "best" frame extracted from the video (and Motion allows you to automatically save off the frame with the most changed pixels from the stream, which is a great way to do this) and attach that to the email.

I used the information in this page to create my own remix of the Uploader.py file (again, which I'll publish next time).

17 responses to “Creating a motion-detecting security cam with a Raspberry Pi – Part 1”

  1. Have you given up on AutoCAD?

  2. Far from it: 6 of my last 8 posts were about AutoCAD.

    Kean

  3. I never heard some security cam like that and i feel really amaze with it.In Finland most of i seen are alarm system and CCTV cameras which help many business and home to be more secure 24/7.

  4. Hi Kean,
    thanks for your post. It's great and very useful.
    greeting from germany.

  5. I like your posts regarding this topic! I have been attempting to create my own trail cams for hunting. I have decided to try using raspberry pi and the new camera feature it will be releasing soon. I'd like to incorporate your idea of the security cams with my trail cams. But my trail cams will need an IR light, usb motion detector(i'm assuming i need that) and SSD, and batteries and a solar panel. the ultimate goal is to have multiple units that talk with each other to create a network and send the data back to the house via long range, scheduled uploads... What are your thoughts?
    Thanks, -rich

  6. You may or may not need a hardware motion detector... the "motion" sub-system is pretty capable. I'd definitely start by checking that out. Not sure about the implications of lighting it all by IR.

    Sounds like a fun project!

    Kean

  7. This is great. Some douche recently keyed my girlfriend's car. I'm hoping to catch the guy with a similar setup.

  8. Tricia Denning Avatar

    Got my Pi up and cam running stable...I can get uploader.py to work from a command line but not from within motion.conf. when I try the sample test that was in the article that starts with ./ I get bad interpreter no file or directory. It only works if I type it: python ....then the line. I do not know linux or python at all but I get the feeling I don't have the uploader.py file in the right directory.(/home/pi) Any help? am I even close? LOL thanks 🙂

  9. I put mine in /etc/motion (as far as I recall from looking at part 2).

    It should work if you follow the steps to the letter...

    Kean

  10. I had to add sudo to my on movie end setting so that it reads "on_movie_end sudo python /etc/motion/uploader.py /etc/motion/uploader.cfg %f". Try that and see if it helps.

  11. Kean,

    First, thank you for posting this! Sometimes I like to dig in and learn something before trying but sometimes I just want to "plug it in and make it work" and then find the details later.

    Anyway, I have followed the steps you outline and all the pieces work independently but I found that the motion.conf file you posted does not have anything configured for "on_movie_end" so I added a line (based on what Tricia wrote above). So that got me one step further. Now what I see is a single .avi file in my ~/motion folder but a number of .jpg files. And when the python script runs it complains that none of the .jpg files match the .avi file - due to the fact that the .jpg files have a -00 or -01 appended to the base name. I'm guessing this is something I need to configure in the motion.conf file but I was wondering if you have updated your copy to include this?

    Thanks,
    -Keith

  12. Kean Walmsley Avatar

    Keith,

    Are you sure you have the right file? This file has an entry for on_movie_end, so I have a doubt...

    Regards,

    Kean

  13. Devina christabela Suhendra Avatar
    Devina christabela Suhendra

    Hello, mr.Kean...
    First I want to say thank you for your post, it's inspiring me..
    I recently learning python for gaming and now I got a homework from my college to make a project, and I interest with your project...
    And my questions is python can support to make software like 'motion' and is there any tutorial or step by step to make it??
    thank you for your time...

  14. Hi Devina,

    I'd expect most serious low-level software components (such as Motion), would be implemented in a language that's "closer to the metal", such as C or C++. That said, you can do some fairly impressive things in Python (I'll be posting some code in the coming days that probably fits that description).

    Regards,

    Kean

  15. Hi Mr. Kean,

    Since Motion cannot capture at the full capability of the Raspberry PI camera, is it possible that Motion can just trigger raspivid to take video for 10 seconds with full hd on motion detection rather than doing itself?

  16. Possibly, although I don't know if Motion can just pass across device access like that. I haven't done anything like this myself.

    Kean

  17. Well, I have got the best information from here the site is fully stuffed with the knowledgeable information.

    view site here

Leave a Reply to Severi Aalto Cancel reply

Your email address will not be published. Required fields are marked *