Creating a Wildlife Camera With a Raspberry PI, Python OpenCV and Tensorflow
By: Armindo Cachada
Tags: Python, TensorFlow, Raspberry Pi 4, Programming, Raspberry Pi

I am a great believer that for you to learn, you need to create. And to create you need to have fun!

First I want to give you a background on why I am trying to build a wildlife camera with a Raspberry Pi.

I live in London and my garden gets visited often by local wildlife. It is so often, that as an avid gardener I get a bit annoyed. Broken plant pots, plants dug out, missing plants, missing fruits...

In my garden, I have seen small foxes, the cutest foxes that you could ever see, big foxes, cats(not mine), and birds. I have even received a visit from a Sparrowhawk.

And who knows what else shows up, lurking in the dark?

You must have eagle eyes if you can identify the animal in the picture

What more excuses do I need, to build a wildlife camera with a Raspberry PI, Python, Tensorflow, and who knows what else? And it will be an awesome camera!

You might argue, but why not just buy a wildlife camera, one that is already in the market and should do the job pretty well?

Good point. But that would take half the fun!

Raspberry PI Camera Modules

So let's get started.

The first thing we need to do is to understand the types of cameras that are available for the Raspberry Pi.

The cameras that are most popular are the camera modules that directly connect to the Raspberry PI using the MIPI connector. The advantage of this type of camera is that data transfer between the camera module and the Raspberry Pi is very fast. And there is a simple to use Raspberry Camera API that I can call from Python to interact with the camera.

I have tried this API and it has many useful functions that allow me to capture movement and record at the same time. I can do cool things like detecting motion and record in parallel using an intuitive API.

There are three versions of the Raspberry PI Camera Module available:

Screenshot from RaspberryPI.org

Above are the official Raspberry PI cameras but you can also get camera modules from other vendors that still work with the Raspberry Pi. The Raspberry PI Camera modules rely on the Raspberry PI GPU to do the image processing from the camera sensor.

Because the Raspberry PI itself needs to do the image processing, the options available in terms of camera sensors are quite limited. Each camera sensor comes with a different set of APIs, so it is not straightforward to support all different types of camera sensors.

Recording in Low Light

Since I am building a wildlife camera, it should be capable enough to operate during the day and night. It needs a camera sensor capable of recording in a low light environment, preferably in True color. The Raspberry v1 and v2 camera doesn’t work well in low light. To see in low light you need to use an IR light and remove the IR filter in the Raspberry PI Camera, depending on which version you buy. But then an additional complication you get is that during the day you get a pinkish image. So you need to have a mechanism to add the IR filter when there is sunlight and automatically remove the IR filter when there is no sunlight.

Picture from Raspberry PI camera with sunlight in dark environment

There is a new Raspberry PI Camera out there! The Raspberry Camera HQ but I am not 100% certain about its lowlight credentials. The Sony IMX477 is supposed to be better in low light than the previous versions of course. Whether or not it is capable of recording in true color in the dark, that is something I will test very soon in my channel. My initial investigation in terms of specs is that this is unlikely to be the case.

Sony Starvis, a remarkable camera sensor

Sony has a special family of camera sensors, primarily used for surveillance, called the Sony Starvis. The Sony Starvis sensor is an extraordinary advance in technology and is capable of recording with true color in low light at only 0.001 lux. Lux is a measurement of how much light it is available in a given environment.

Just to put it into perspective. The lowest LUX you can get is when it is totally dark, no sunlight, no moon, no stars, or almost none, on an overcast day. And no artificial light. That is unbelievably dark(0.0001 lux)! No camera sensor that I am aware of will be able to capture an image when it is that dark.

But if you have a clear sky, the stars will provide 0.002 lux. That’s still very dark. The Sony Starvis sensor can record in low light with 0.001 lux, half of that. Isn’t that mindblowing?

See the table below, which I grabbed off Wikipedia for a better idea of what LUX really means.

Wikipedia

So you will hopefully agree that the Sony Starvis camera sensor is perfect for a wildlife camera.

The bad news is that the Raspberry PI camera doesn’t support the Sony Starvis camera sensor.

But the good news is that if I get my hands on a USB camera or even an IP camera with the Sony Starvis sensor, then I will be able to leverage in combination with the Raspberry PI

That’s exactly what I have done!!!

But I went cheap so I only found a decent IP camera with Sony Starvis on Alixepress. It didn’t cost me more than £20 if memory serves me right.

And the difference between the Raspberry PI camera and this camera is like the difference between night and day. Literally. Judge for yourself.

Sony Starvis IMX307 Camera Sensor — Dark room

Raspberry PI v2 Camera — Dark Room — different perspective

For a wildlife camera, or even a security camera it's important to record good quality footage in a lowlight environment.

You might say that it is not a big deal to record in color at low light, but in my specific situation, I can’t use IR light as I am going to be placing the camera indoors facing the garden. It is going to have a window placed right in front of it. But IR light is not capable of going through a window. Did you know that? If you shine IR light at a window, it behaves in the same way as a mirror, and the camera will go blind.

Going back to my point. It is always nice to use the Raspberry PI camera for some simple projects or just to learn. But as soon as you try to do something half-serious, then you do need to have access to better cameras.

But if you are willing to use a USB camera(or even an IP camera), then you are opening a whole new set of possibilities. You will also be freeing up the Raspberry PI to do more important things with AI.

Installing The Raspberry PI Camera

Now it is time to set up the Raspberry PI Camera v2. The setup process is very simple.

Raspberry PI v2 Camera

The Raspberry Pi Camera comes with a white and blue ribbon.

You need to connect the ribbon to the CSI connector to the Raspberry PI board. The blue side facing the back of the board.

I used a 3D Printed case for the Raspberry PI camera, downloaded from Thingiverse, but you should be able to buy a Raspberry PI Camera case cheaply off Amazon.

Now it is time to power on the camera

Once the Raspberry PI is powered on, open a terminal window.

And we execute:

$ sudo apt update

.... and next $ sudo apt full upgrade

....

This is to ensure that the Raspberry PI is running in the latest version of Raspbian and it has all the latest patches/updates available to date.

After this, we need to execute also in the terminal

sudo raspi-config

You will need to select the Interfacing Options-P1 Camera. Then select Finish and reboot.

Taking a picture using Raspistill

Now the Raspberry PI Camera should be set up, let’s do a quick test with Raspistill. Let's open a terminal window again and try the following:

raspistill -v -o test.jpg

My Raspberry PI camera took this decent photo:

Recording a video with Raspivid

Taking a picture is very nice, but much better is to record a video. To do that, we can use raspivid.

raspivid -o vid.h264

This records a 5 second video.

If you want a longer video, then you need to pass the -t parameter with the number of milliseconds

raspivid -o vid.h264 -t 30000

This records a 30s video.

Creating a Livestream

Now the fun part starts. To see what the camera is recording in realtime we can try:

raspivid -o - -t 0 -n | cvlc -vvv stream:///dev/stdin --sout '#rtp{sdp=rtsp://:8554/}' :demux=h264

This creates an RTSP stream from the Raspberry PI camera that is accessible from the local network.

Conclusion

There is so much more that we need to do that it is not going to fit in this article. I need to also setup the Raspberry PI 4 swith Tensorflow, OpenCV and Python of course to start developing.

If you can’t wait until my next article in this series, why not subscribe to my Youtube Channel and see how I am getting on with this build? What are you waiting for?

Resources: