Sunday, 16 November 2014

Part 2: How to make your own Raspberry Pi Trail Camera 'PiTrailCam': Basic design model

In part 1 of this series, I outlined how a Trail Camera is made, and what might be used to make a home-brew version.  

I've put together a basic functional trail camera, a very early prototype if you like. 
I'm testing with a standard Raspberry Pi camera, not the IR version.  I do have an IR version, but haven't got around to using it yet.  For now, I'm limited to daylight triggering since I've not yet incorporated my IR illuminators since the box (tupperware - no expense spared) was too small to incorporate all the required bits.

This shows the prototype build so far (lack of hot glue gun skills notwithstanding).  This will be hardwired into my home network, and uses power-over ethernet (PoE) hence the wire going to into it.

Simple front view showing PIR and RasPi camera with LDR detailed in insert

PiTrailCam ver1 - Inside
This is a cut-down version of the version I had planned out on my desk, which included a relay that triggered a separately powered IR LED array, which unfortunately does not fit in the box.  Version 2 will probably be in a wooden box that is a bit more roomy and durable, and can accommodate a relay switch for the IR LED arrays.

Kit used in RasPi TrailCam version 1
Raspberry Pi model B
Raspberry Pi camera + supplied ribbon cable
Power over ethernet & connectors
I'm planning on directly splicing a hacked microUSB cable to the TP-Link power-out but bought the wrong cables...so have to make do with more bulky cables following this guide
  Micro USB (male) to USB cable (female)
  USB (male) to barrel power connector
Small prototyping breadboard
Female to male breadboard wires (various)
PIR motion sensor: Components, wiring and code described here
Mechanism to measure light level: code + components from here
Not strictly necessary in this build, but will be used to measure ilght level to activate IR led array in version 2:
  1x 2.2 kOhm resistor
  1x 1uF capacitor
  1x Light Dependent resistor

Prototype board layout as follows.  Note, I significantly slimmed this down to a small prototype board, but tend to use a larger breadboard with GPIO breakout at the 'drawing board ' stage.  this also shows a possible layout that will incorporate a relay to operate a pair of IR LED arrays:


For now, I'm just using the PIR (motion sensor) and LDR (measure light level) aspects .

First imageSome things I'm very happy with, others not I'm less so..

The observant among you will spot something wrong with this image...
Whats really cool is that I've got the time and date stamp included at the top (erm, bottom?) of this image.

Timstamp - just to prove it....
How the image capture works
I've coded the scripting side of things in Python.  I'm relatively new to this, so its a fun way of learning a new programming language, and to be fair most of this is mercilessly cribbed from other websites/blogs.

The Raspberry Pi camera is a described here, and can be used to capture video or stills, or both at the same time (may come back to this).  You'll mostly see the two programs Raspistill and Raspivid referred to which are used to capture still images and video respectively.  There are also several third party libraries built for it.  For this application I've used the PiCamera library.
My limited Python skills will likely show here, but as I understand it this offers a way for python to directly access the camera hardware, versus using calls to external software (such as to Raspistill and Raspivid).

In your python code, you have the option of either calling Raspistill as follows:
os.system ("raspistill -o /mnt/SHARE/captures/TrailCam.jpg")

or using PiCamera module, where you can see I've added a camera.hflip and camera.vflip command to fix the upside down and back to front image above.
def recordImage2():
 
 timestamp = dt.datetime.now().strftime('%Y%m%d%H%M%S')
 print "Motion Detected: " , dt.datetime.now()  
 
 with picamera.PiCamera() as camera:
  camera.led = False
  camera.resolution = (2592, 1944)
  camera.framerate = (1, 1)
  camera.vflip = True
  camera.hflip = True
  camera.quality = 100
  camera.exposure_mode = 'auto'
  camera.awb_mode = 'auto'
  camera.image_effect = 'none'
  camera.color_effects = None
  camera.start_preview()
  time.sleep(2)
  camera.annotate_bg = True
  camera.annotate_text = dt.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  camera.capture_sequence(['/mnt/savelocation/TrailCam_'+timestamp+'_image%02d.jpg' % i for i in range(3)])

By calling recordImage2() on a PIR activation event causes 3 images (image1, image2, image3) to be saved to a network location that I've mounted to a folder in /mnt, and am currently saving files there.  Not sure how that will pan out if I switch to video though.

How to get text overlay working with picamera python library
I'm quite pleased that I have been able to get the text overlay working.  The next thing to do is see if I can get a dark background behind the time & datestamp, as date and time in white over a white sky isn't much use to anyone.

At first I could not image overlay to work, and could not see why the instructions here did not work.  Turns out I was running an version 1.5 of PiCamera, and need at least the current version (1.8 at this time).  You can tell which version you're running by following these instructions here.  You can update that by doing sudo update then sudo upgrade, you may need to update the Raspberry Pi's firmware : sudo rpi-update.

How to mount network drive to Raspberry Pi
For info, to setup network mounting you need to edit /etc/fstab as follows:

sudo nano /etc/fstab

Then add the following line:
//XXX.XXX.XX.XX/SHARE/TrailCamPi /mnt/SHARE cifs username=RasPiUser,password=RasPiPwd,uid=1000,_netdev 0 0

Ctrl & O, then Ctrl & X exits the nano text editor.
At a reboot, (or sudo mount -all) the network folder will be mounted at /mnt/SHARE

/XXX.XXX.XX.XX/SHARE/TrailCamPi = IP address of target PC with save file destination folder.  Although its not strictly necessary, and you could setup a network share with no user password protection, I've set mine up with a dedicated user RasPi user + password on the destination machine, and pass those credentials with the mount command.

I'll sign off with another badger video from my commercial Trail cam, captured a couple of days ago:




Next step is to swop out the standard RasPi camera to the PiNoir, and add some LED arrays.

14 comments:

  1. It looks like you are making great progress with this project, and polishing your Python skills at the same time!

    Here are a couple of comments;
    Measuring light levels. I noticed someone using a light sensor on the RPi forum and thought it might be an interesting alternative to use the camera itself for this. (see post 8/9 here: http://www.raspberrypi.org/forums/viewtopic.php?f=37&t=91532).
    POE; as POE is not standardised, it should be simple to come up with your own method. For example, apply 12V to the spare wires in the network cable, and use a 5V switching regulator to provide the necessary supply via the GPIO pins (5V & Gnd). This should be a pretty cheap solution, but also take up very little space in your camera box.

    Looking forward to part 3.

    ReplyDelete
  2. That's a pretty slick trail cam you have set up.

    Myself, I have done something very similar with a Raspberry Pi and a Pi Noir. Though my"prototype" box is NOWHERE near as nice as yours...

    I have a couple of questions/comments on some differences.

    You seem to have done a lot of custom coding in Python to do the detection and capturing. You you seen Motion at all? Myself, that is what I am using to get video/images from the camera. Any particular reason you chose to code it all in python manually? In my opinion, motion has the added bonus of being able to stream video over a network.

    I did a lot of searching for some reasonably priced IR LED arrays... I did find a nice site with some really awesome pricing in my searches. Here is the LED Array I purchased:
    http://pcboard.ca/store/ir-and-uv-products/ir-infra-red-48-led-ir-panel-assembled-and-tested-259952778.html

    I live in Canada, so the shipping was local to me. The product works like a charm, and really REALLY lights things up nicely. It took me a while to find something that would produce enough light to be of any real use. The couple of photos you uploaded look pretty lit... What did you use for a light source for them?

    I also came up with my own DIY PoE setup. Basically, I cut 1 short cable, crimped 2 pairs of the cables into an ethernet end, and left the other 2 pairs free. I did this to both halves of the original cable giving me 2 of these spliced cables. This way it allowed me to have the 2 pairs for 100Mbit ethernet, 1 pair for the 3.3v power required for the Pi, and the last pair for 12v power for the LED array. the 2 "splice" cables are then plugged into a female-femal ethernet adapter, and both of those are connected to each other with whatever length of ethernet cable I need/want to use to get my camera Pi out to wherever. As an added layer of protection from any possible power issues, I had an old 4-port 100Mbit switch kicking about, and I make sure to only plug the "splice" cables into this. I can then take a regular ethernet cable and connect this switch (and all Pis connected to it) to me newer, better router, and gain full network access without fear of damaging my good network equipment.

    In all you've got a pretty cool project going, and it is interesting to see the similarities and differences on similar setups with similar goals. I look forward to reading more on how this one goes, and hope to see what you can do for "version 2".

    ReplyDelete
  3. Hi Jonny M,
    I've got a couple of the very same IR illuminators, and a vcoltage step up controller, plus a two channel relay which I plan to use to control the IR illuminators. At the moment I'm trying to work out a neat way of assessing ambient light levels to control when to turn on the IR illuminators. I want more control over their switching than would be provided by the built-in LDRs on those IR arrays since I want to sync IR illuminators with an IR cut-out filter.
    I used the PIR as I thought it was neat - I havent used motion yet, but agree that it would be a possibility too.
    The manual coding is more of a self-teaching exercise since I dont know a lot of python and in a nerd-esque way quite like a bit of code that works...
    Version 1 really is rather basic yet - and I've yet to capture any wildlife using it - the pics / videos shown are all from my commercial (real) trailcam, however the early pictures that PiTrailCam is producing are better quality than the commercail one, but fiedl of view is a bit too wide for my liking

    ReplyDelete
  4. Hi SteveDee,
    i posted a new topic in the Python board on the Raspberry Pi forum about your idea to use the camera to measure light:
    http://www.raspberrypi.org/forums/viewtopic.php?f=32&t=92193

    ReplyDelete
    Replies
    1. There may be a Python image analysis library which has this functionality (i.e. provides image brightness without using ImageMagick "convert" command).

      Delete
    2. Doh! Don't know why I didn't think of that I the first place...!

      Delete
  5. give us more!!! Please tell us the final stage of your build and findings i would love to build some of these for personal use.

    ReplyDelete
  6. Also is it possible to have this build send images or video over email or text using the gsm and/or cellular network. Many trail cams use the gsm network by putting a 90 day cell phone chip in it to send using the cellular network. I have heard that att tmobile have a gsm network but also that verizon works.

    ReplyDelete
  7. Hello - thanks for the post.
    I am hoping to build a unit with a fast trigger - less than 0.3 seconds - to catch Leadbeaters (endangered Australian possums that live in logging zones.) This speed is faster than the affordable commercial versions. Any suggestions appreciated as is your great post.

    ReplyDelete
    Replies
    1. I guess it all depends on how you intend to trigger a camera. If power wasn't an issue I would be inclined to go for an approach that run a constant record buffer and only triggered a video capture in the event of a motions event. One example of this approach is here:
      https://www.raspberrypi.org/forums/viewtopic.php?f=43&t=115583. Image capture can be achieved by other methods, eg via a PIR (the method used by my trail cam), or by IR beam interruption (method used by my bird box entrance hole counter)... Latency issues my also come into play depending on what sort of camera, lighting conditions, etc...

      Delete
  8. This comment has been removed by a blog administrator.

    ReplyDelete
  9. Hi @NestBoxTech ! I' have just finished my first prototype of a PiTrailCam, and i didnt know your project. They are quite similar!

    I use a RPi2, 1LDR, 1PIR, 1PiNoir, 1 Relay, 1IR LED'S homeinvention -from old trail cam). I'm researching for code ideas, and, by now, i can detect light, activate IR LEDS, and record still images and videosequences, but i must improve results. Looking forward to see your third part of the project!

    ReplyDelete