# 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.

![Four tiles: identify by the back, because barn cameras see backs and the spots are the ID. Anchor at the robot, which reads the cow's tag two or three times a day. Let physics catch errors, because a lost cow reappears only as far as she can walk. Compare a cow with herself, seeing part of her chewing and alerting when it drops](https://saalse.com/_astro/dairy-cow-identification-key-ideas.DYeU1UyR_Z1Al2h6.webp)

*The design in four ideas.*

<div class="fact-table">

| 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 |

</div>

## 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:

![Eight cards. Ruled out: collars, ear tags and boluses (a device per cow, batteries, repairs). Bluetooth beacons (inaccurate, cows broke them in a week). Infrared beacon collars (batteries, frame rate, hidden by other cows). RFID gates between zones (narrow passages or false readings). A thermal camera on the jaw (too expensive). Dome cameras over the barn (more cameras, costlier installation). Kept: RFID at the milking robot, already in place as the anchor, and ordinary barn cameras, the same cost for any herd size](https://saalse.com/_astro/dairy-cow-identification-options.fZ3ls2bv_r6Dv8.webp)

*Six shortcuts we ruled out, and the two things the design stands on.*

Others were hitting the same walls at the time:

![Timeline: in 2017 Cainthus put more than 40 dome cameras over 400 cows and identified them by coat pattern. In January 2020 Cainthus said tracking individual cows wasn't a scalable model and moved to groups. In March 2020 CattleEye launched one camera over the milking parlour exit and no wearables. In March and April 2020 our design used cameras across the barn plus the ID from the milking robot](https://saalse.com/_astro/dairy-cow-identification-market-2020.BrYKIZbs_ZDr6Ya.webp)

*Barn-wide cameras had failed to scale, and single-passage cameras saw each cow only once a milking.*

Sources: Cainthus's [2017 dome-camera pilot](https://www.agproud.com/articles/19054) and [2020 change of course](https://www.agproud.com/articles/37072), and [CattleEye's launch](https://agfundernews.com/cattleeye-comes-out-of-stealth-with-computer-vision-based-livestock-management-platform).

## What we designed

### People are recognised by their faces, cows by their backs

![Cartoon in two panels: a smiling office worker unlocks a face recognition camera with his face. Next to it a red-and-white spotted cow turns her back to the same kind of camera, which frames her spotted rump and tail, while she looks back over her shoulder](https://saalse.com/_astro/faces-vs-backs-illustration.Dr7LhXgq_Z1MQeG0.webp)

*Face ID, barn edition.*

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](https://openaccess.thecvf.com/content_ICCV_2017_workshops/papers/w41/Andrew_Visual_Localisation_and_ICCV_2017_paper.pdf) and even from [images of the tailhead alone](https://doi.org/10.1016/j.compag.2017.10.029). A cow's real "face" biometric, the muzzle print, needs a close-up that no barn camera gets.

![Diagram of three cows seen from above: a spotted cow whose spot pattern works as an ID, a plain brown cow with nothing to tell apart, and two spotted cows lying side by side whose spots on the touching flanks join into one new pattern](https://saalse.com/_astro/aitend-spotted-backs.DjvTgVS7_Zbmtw7.webp)

*Spots are an ID, until two spotted cows lie down side by side.*

Only about 40% of the client's cows were spotted, and [coat-pattern identification works poorly on solid-coloured breeds](https://www.journalofdairyscience.org/article/S0022-0302(25)00334-0/fulltext) 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.

![Barn camera view from above on a sunny afternoon: sunlight through the slatted wall lays bright stripes across the floor, a brown cow and a black cow lying in their stalls, so the dark cows look spotted](https://saalse.com/_astro/aitend-sunlight-stripes.CFkKQCYd_ZyQMO5.webp)

*Sunlight through the walls paints spots that no cow has.*

### 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](https://www.epfl.ch/labs/cvlab/software/tracking-and-modelling-people/ksp/): 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.

![Diagram of a barn from above with a milking robot and six camera zones: a cow identified by RFID at the robot is tracked until she is lost in camera B. A dashed circle around that point shows how far she can walk by the next moment. A cow inside the circle can be her, a cow in camera F outside it cannot](https://saalse.com/_astro/aitend-speed-limit-tracking.Cb3T43t0_Z1yxEw9.webp)

*A lost cow can only reappear as far as she can walk.*

### Rumination: catch enough of it, not all of it

<figure class="not-prose mx-auto my-8" style="max-width: 300px">
  <img src="https://media.giphy.com/media/1sqM5cY5BXZv352SMC/giphy.webp" alt="Close-up of a black-and-white calf's muzzle chewing, filmed from below against a blue sky" width="352" height="480" loading="lazy" class="h-auto w-full rounded-lg border border-zinc-200 dark:border-zinc-800" />
  <figcaption class="mt-2 text-center text-sm text-zinc-500 dark:text-zinc-400">The jaw is the signal. GIF: <a href="https://giphy.com/gifs/what-you-want-not-interesting-cow-eating-1sqM5cY5BXZv352SMC">Vüsal Azizov on GIPHY</a></figcaption>
</figure>

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.

![Illustrative chart over 14 days: the chewing time cameras saw for one cow stays at about 3.5 to 4 hours a day inside a band marked her usual share, then falls to about 2 hours on days 11 to 14, marked in red as an alert below her normal](https://saalse.com/_astro/dairy-cow-rumination-baseline.hlFYpxyx_ZDSNSM.webp)

*Illustration: the alert comes from a drop against the cow's own normal, not from the total.*

### 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.

![Pipeline diagram in four steps: the milking robot reads the RFID tag for a certain ID two or three times a day. Cameras track the cow by her back and spots. The physical limit decides where a lost cow can reappear. A cow diary records lying, eating and chewing hour by hour and alerts when it leaves the norm](https://saalse.com/_astro/aitend-identification-pipeline.Bqf6qEVX_1a1pHv.webp)

*From one certain ID a day to an hourly diary per cow.*

## 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](https://github.com/cvat-ai/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](/projects/computer-vision?src=ai-md) 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](/computer-vision-ai/drilling-rig-monitoring?src=ai-md) at the edge and [sub-second tracking of online games](/computer-vision-ai/real-time-game-tracking?src=ai-md) from a screen stream. On farms, my [agritech projects](/projects/agritech?src=ai-md) 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?

**[Alex Samson](https://saalse.com/?src=ai-md)**, 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.

👉 [Discuss your computer vision project](/contacts/companies?src=ai-md)

## Search

Page index: https://saalse.com/llms.txt. Add .md to any page URL for its Markdown.

This site has a search endpoint: https://context7.com/api/v2/ask?siteKey=ask_fae2f8fd691c85cb6b685274&query=<URL-encoded question>. It returns documentation that answers the question, with a source link for each part. No API key is needed. If nothing matches, it says so.
