Computer Vision to Tell Dairy Cows Apart
How to tell dairy cows apart on barn cameras by the spots on their backs and track rumination: computer vision design for a Swiss agritech startup, 2020.
In short: in spring 2020 I designed a computer vision system for AiTend, a Swiss startup that watched dairy cows through ordinary barn cameras instead of collars. A camera under a barn roof never sees a cow's face, so the design identifies each cow by the spots on her back, anchors her identity at the milking robot, uses how far she can walk to catch tracking errors, and reads rumination, an early sign of illness, against the cow's own normal.
Before designing anything, we ran the client's footage through a detector, went through every alternative from collars and Bluetooth beacons to thermal cameras, and checked what Cainthus and CattleEye were trying at the same time.

| Project | Dairy cow identification |
|---|---|
| Client | Agritech startup for dairy farms, Switzerland |
| Year | 2020, one month |
| My role | Computer vision consultant at dvhb: research, solution design and proposal |
| Team | 3 people including me: data scientist, backend lead |
| Stage | Solution design and proposal |
| Tech | Multi-camera tracking, YOLOv3, CNN + SVM, I3D action recognition, pose estimation, optical flow, RFID, CVAT |
The problem
A farmer can't know hundreds of cows by sight, yet each one needs checks and treatment on her own schedule. Health shows in behaviour: a healthy cow chews the cud for eight to nine hours a day, and less points to illness or poor feed. The startup's pilot barn in Switzerland had been recording since October 2019, and finding cows in a frame was solved. Telling them apart and reading what each one did was not.
Why cameras, and not collars or chips
Wearables already measured rumination in 2020, but every cow needs her own device, with batteries and repairs, and we didn't want to put anything new on the animal or into it. Cameras cost the same for any herd. We went through every shortcut with the client:

Others were hitting the same walls at the time:

Sources: Cainthus's 2017 dome-camera pilot and 2020 change of course, and CattleEye's launch.
What we designed
People are recognised by their faces, cows by their backs

Barn cameras see cows from above and from behind. An off-the-shelf detector found the client's cows from the side but didn't know them from behind, so a cow had to be told apart by her back. Research backs it: cows have been identified from top-down images of their backs and even from images of the tailhead alone. A cow's real "face" biometric, the muzzle print, needs a close-up that no barn camera gets.

Only about 40% of the client's cows were spotted, and coat-pattern identification works poorly on solid-coloured breeds such as Jersey and Brown Swiss. Light added fake spots: on sunny afternoons the sun through the slatted walls blinded the cameras and laid stripes across the cows. We limited the first stage to spotted breeds.

Know each cow for sure, then don't lose her
The milking robot reads each cow's RFID tag two or three times a day, and from there the cameras follow her. For the moments when tracking loses her, I suggested using the physics of the barn, the principle behind the k-shortest paths tracker from Pascal Fua's lab at EPFL: she can reappear only as far as she can walk in that time. Matching her only against the cows inside that circle cuts the options and catches wrong IDs.

Rumination: catch enough of it, not all of it

The system watches the jaw of a cow it has already identified, with jaw keypoints and a classifier, optical flow, or an action recognition network such as I3D, fine-tuned on the client's footage because no labelled dataset of cow behaviour existed. Night footage and cows lying with their backs to the camera hide the jaw, so I proposed not to chase every minute: if the cameras reliably catch a third to a half of a cow's chewing, a drop below her own usual share is the alert.

Lameness, routine and a diary for every cow
Pose estimation was planned for a leg held bent and the line of the back as the cow walks, and for the time she spends eating, standing and lying. Each cow gets an hourly diary that writes "not determined" when no camera saw her, and limits set by a vet trigger an alert to the farm manager.

Architecture
The plan built on what the client already had: IP cameras at 4 frames a second, their own cow detection and footage labelled in CVAT. Our service would add each cow's ID and position on the barn floor and serve them through an API, in three stages that started with a two-week test of the hypotheses on the client's data.
Lessons
- More data often beats a better model. Two months later the client made identification work in-house with thousands more labelled crops of cows, not with a new model. The hard question became how to label a new farm quickly.
- Physics is a free feature. A cow can't teleport, and limits like walking speed catch errors that a model can't see.
- Compare an animal with herself. Behaviour a camera sees only part of the time is still a signal when you track the change from her own normal.
- A new field can be learned fast with the right partner. After computer vision projects since 2012 on Kinect and OpenCV, this was my first deep learning one: the data scientist brought the models and papers, and I turned the farm's problems into tasks, stages and a plan.
Related work
More computer vision cases: real-time monitoring of drilling rigs at the edge and sub-second tracking of online games from a screen stream. On farms, my agritech projects include computer vision for yield forecasting at Lima Labs in Kenya and a satellite prototype that finds fields prone to waterlogging.
Working on something similar?

R&D consultant, computer vision AI
I design computer vision for farms and other places where cameras have to tell similar things apart and follow them over time.