All code can be found on GitHub
A student was doing a research project that involved counting bees as they ate sugar solution contained in a small dish sitting above certain plants. The object was to see which plants they were attracted to. This involved counting them as they approached the dish. This is a small proof of concept project to see if this was possible with a Raspberry Pi and the official camera as I thought that it would be easier than standing there all day counting them. The project uses Python and CV2 to achieve this and allows the live image and results to be displayed on a website utilising Flask and socketio. It was surprising how easy it was to produce something that worked and was relatively simple leveraging the power of numpy arrays and CV2.
The experiment had a series of metal containers containing plants covered in a mesh. The mesh stopped the bee being able to see the plant so was only attracted by the scent/smell. In the centre of the mesh was a small dish containing sugar solution which was the reward for the bee.
To write the software I took a picture of one of the tops of the metal containers and had a picture of a bee sellotaped to a cable tie. The Raspberry Pi camera was then fitted above this. The camera has a fixed long focus by default when you buy it, but this can easily be changed by carefully cutting away the glue holding the lens in. The focal length can then be changed by rotating the lens.
Run the software with app.py. This starts up the webserver and bee detection. By default you can connect on the flask default port of 5000. The image below shows how it looks.
The actual detection is done by looking for anything yellow in the red box. This constitutes a bee. The colour mask can be adjusted in the code. When a bee is detected the screen looks like this
It should be emphasised that this was just a proof of concept to see if it was possible and it worked very well.