Kipcam

As the new year stood before us, so did, although more literally, the untouched garden of our new house. It was 40 Square meters of 5 different types of stone tiles, randomly arranged to prevent the weeds from overtaking the backyard. To improve upon this dreadful sight, we spend the next two months digging, building and planting the garden so we could enjoy it in spring. The main feature was the chicken-run that would be the new home for two chickens.

Having saved two chickens from a premature end-of-life, we gave them names that would symbolically free them from their anonymous life: Kipchoge and Kipchumba.

Kipcam

Chickens are curious creatures. They like to digg, eat everything that looks edible and lay eggs like there is no tomorrow. All these eggs need to be collected, but from the outside you never know if there are any eggs in the nest. To save me from the occasional disappointment, I installed a camera in the henhouse: the kipcam!

I quickly moved past any privacy concerns regarding the chickens and started planning the setup. I choose for a Wanscam HW0021 with WiFi and a dual-axis rotating camera to adjust the view. It does not have power over ethernet, so the only cable to connect was a power cable.

Snapshot of the Kipcam

Egg detection

Great! We can make snapshots and and view the nest by logging in to the IP camera from my local network. But what if we could automatically detect how many eggs lay in the nest? Enter phase two.

With object detection it all comes down to what differentiates that object from its surroundings. Looking at an egg in a nest, there are many ways to go about detection: color, surface area, edges, eliptical shape and even size.

Before writing an algorithm myself, I looked for people who tried to solve the same problem and found the startup Eggs Iting. They have developed a clever way to detect eggs using Local Binary Patterns. It is a fairly complex approach, but results in a robust detection. First I wanted to try a simpler solution because of two reasons:

  • First of all, they use a flash when taking snapshots to have a consistent light intensity and distribution on the photos they do detection on. This is especially useful when using LBP, but not possible with my setup.
  • Second, to know whether there is an egg, you need to compare an LBP histogram with that of a reference and calculate the minimum acceptable difference. That means needing a lot of labeled egg images..

Therefore, I look at how well I could detect eggs without using reference images

Hough Transform

The first idea was to use the hard edge, eliptical shape and consistent size to detect the eggs with a circular hough transform. This requires input parameters on the expected radius of the circle and is executed on binary image from canny edge detection. After playing with the parameters, the accumulator of the hough transform was too messy to clearly identify the center of each egg, altough there are some weak bright spots at the center of each egg, it gets lost in the noise.  

Distance transform

Looking at the canny edge detection output, I thought well from here it is pretty clear where the eggs lie. Mainly because of the matte surface, there are no edges detected inside the eggs. So instead of trying to the rid of the background, I can use high frequency noise to detect the eggs.

A distance transform calculates the distance to the nearest white pixel (edge pixel). This simple calculation clearly shows three islands with the highest value in the center as this is farthest from the edge in a circle. Using some constraints on the size and shape of the islands, it was easy to label them and detect eggs fairly accurately.

Telegram bot

The beauty of looking through the camera and detecting eggs was still confined to my local network and a Python script I had to run manually. To make this more user-friendly and available outside the comfort of my home, I wrote a telegram bot.

Telegram offers a very accesible API to create bots and there was even a Python wrapper available to make common tasks easier. The bot runs on a Raspberry Pi Model 3B+ that is connected to my local network and IP camera, to prevent port forwarding of the camera directly. The bot has two commands that fire the corresponding python scripts:

  • /snap : take a snapshot with the camera
  • /detect : detect how many eggs lay in the nest

Conclusion

It was great fun to make the hardware and software work together and show friends the magic of realtime egg detection on my phone. The detection is not as robust as I would like, but I'm still very pleased with the performance for such a simple algorithm. I am happy, let's see if the chickens will also thrive in their new home.