You have probably seen a nature documentary in which you can see stunning photos and videos of rare animals. While those certainly can be captured by dedicated photographers who speed weeks sitting motionless in the field, they are often taken by photo traps. A photo trap is a special camera meant to be left outdoors for extended period of time and which can automatically snap a photo or video when an animal is nearby. Those photo traps have been used to capture a number of rare animals on film over the years, but you can also utilize the technology to snap photos of more mundane animals. Sebastian Staacks used a Raspberry Pi to control a photo trap that caught some adorable squirrels.
The camera Staacks used for this photo trap was a Sony Alpha NEX-5T and you will need something similar if you want to follow his guide exactly. The key feature of that camera is a shutter release that can be triggered over WiFi. The camera also needs to be able to send a live preview over WiFi to a computer on the network. In this case, Staacks has the camera placed on a tripod and pointed at a nut box to entice hungry squirrels. When a squirrel comes into frame, it is detected by the wirelessly-connected Raspberry Pi 3 Model B single-board computer. The Raspberry Pi then triggers the camera’s shutter release and a picture is taken.
The most obvious way to achieve results like this would be to run some kind of object recognition machine learning model on the Raspberry Pi, but Staacks’s solution is much cleverer and requires far less processing power. The photo is framed in such a way that the nut box is in focus in the foreground while the background is completely out of focus. A fairly simple algorithm in OpenCV software running on the Raspberry Pi can determine how much of the photo is in focus at any given time. Normally, that is only about 25 percent of the photo — just the nut box. But when an animal, like a squirrel, enters the frame, the size of the in-focus portion of the photo jumps up. That change can be detected and is what Staacks’s script uses to trigger the shutter release. Calculating the amount of focus is much less resource intensive then running a machine learning model. It’s a really ingenious solution and is perfect for snapping photos of animals if you know exactly where they will be.