Weed detection and spot spraying
Spot spraying finds and treats individual weeds instead of an entire field, which turns weeding into a real-time object detection problem running on a moving machine with a fraction of a second to decide.
- 10 min read
- 3 reading levels
- Published
Read these first
On this page 6
One lesson, three depths. Pick the one that fits you today — you can switch any time.
Beginner — No maths. Plain English.
Spot spraying finds individual weeds in a field and treats only those spots, instead of spraying the whole field with herbicide.
Think about weeding a small vegetable patch by hand. You do not treat every plant the same. You glance at each one for a split second, and decide: vegetable or weed. You do this fast, without stopping to think hard about any single plant.
A spot-spraying machine tries to do exactly that, mounted on a tractor moving across a field at walking speed or faster.
Why it exists
Spraying an entire field with herbicide treats every square metre the same, whether or not weeds are actually growing there. Most of a field, most of the time, has no weeds in it at all. Blanket spraying wastes chemical, costs money, and puts herbicide onto soil and crops that never needed it.
Spot spraying only fires the nozzle where a weed is actually detected. Real deployments report herbicide savings often in the 70-90% range against blanket spraying. The exact number depends heavily on how weedy the field already is.
How it works
Camera on the sprayer -> Detector spots -> Nozzle fires only
sees the ground ahead each weed's location at that exact spot,
in the image timed to the machine's
forward speedThis is object detection — finding not only what is in an image, but exactly where. That location is what lets a nozzle be aimed and timed correctly. The whole pipeline — see, decide, spray — runs in a fraction of a second. The machine keeps moving the entire time.
Where you have already seen it
- "Precision agriculture" sprayers now sold commercially, mounted on tractors, using cameras and real-time detection.
- Automated lawn-care robots that avoid flower beds while mowing grass.
- Any "smart" agricultural machinery advertisement mentioning reduced chemical use — spot spraying is usually the technology behind that claim.
An honest warning
A weed and a young crop plant can look nearly identical in the first weeks after planting. Neither has grown its distinctive shape yet. Misclassifying a crop seedling as a weed means spraying herbicide directly onto the crop you are trying to grow.
Speed adds a second constraint most people underestimate. A detector that is accurate but too slow is useless on a moving machine. By the time it decides, the nozzle has already passed the spot. Real systems trade off some accuracy for the speed needed to keep up with the tractor.
Remember this
- Spot spraying is object detection running in real time on a moving machine, not only image classification.
- The payoff is real — large reductions in herbicide use — but only if the detector is both accurate and fast enough to matter.
- Young crop seedlings and weeds looking alike is the hardest and most consequential failure mode, because a mistake sprays the crop itself.
What to learn next
- Object detection — the general technique this lesson builds on.
- YOLO — the detector family most real-time spot sprayers are actually built from.
- AI for smallholder farms — why this technology is out of reach for most of the world's farmers today.
Developer — Code and libraries.
Setup
pip install numpyA real weed detector is a trained object-detection model running on camera images, which needs a labelled image dataset and real training time. This example demonstrates a smaller, older, still-used idea instead: crops are planted in straight rows, so a plant far from any row line is very likely a weed. It runs instantly and shows both the value and the sharp limit of a simple approach.
Minimal runnable code
import numpy as np
rng = np.random.RandomState(0)
# Simulate a small patch of field: crop plants sit in straight rows spaced 40 cm apart,
# with a little natural jitter. Weeds appear at random positions between rows.
row_spacing = 40 # cm between crop rows
n_crop = 12
n_weed = 6
crop_rows = rng.choice([0, 1, 2], size=n_crop) # which row each plant is near
crop_x = crop_rows * row_spacing + rng.normal(0, 3, size=n_crop) # small planting jitter
crop_y = rng.uniform(0, 200, size=n_crop)
weed_x = rng.uniform(-10, 90, size=n_weed) # weeds scattered anywhere
weed_y = rng.uniform(0, 200, size=n_weed)
all_x = np.concatenate([crop_x, weed_x])
all_y = np.concatenate([crop_y, weed_y])
true_label = np.array(["crop"] * n_crop + ["weed"] * n_weed)
# distance from each plant to the nearest expected row line
row_lines = np.array([0, 40, 80])
distance_to_nearest_row = np.min(np.abs(all_x[:, None] - row_lines[None, :]), axis=1)
# anything more than 8 cm from a row line is flagged for spot spraying
predicted = np.where(distance_to_nearest_row > 8, "weed", "crop")
for i in range(len(all_x)):
print(f"plant {i:2d} true={true_label[i]:5s} predicted={predicted[i]:5s} "
f"distance_to_row={distance_to_nearest_row[i]:5.1f} cm")
accuracy = (predicted == true_label).mean()
print(f"\naccuracy: {accuracy:.2f}")plant 0 true=crop predicted=crop distance_to_row= 4.1 cm plant 1 true=crop predicted=crop distance_to_row= 0.3 cm plant 2 true=crop predicted=crop distance_to_row= 7.3 cm plant 3 true=crop predicted=crop distance_to_row= 1.4 cm plant 4 true=crop predicted=crop distance_to_row= 1.4 cm plant 5 true=crop predicted=crop distance_to_row= 2.9 cm plant 6 true=crop predicted=crop distance_to_row= 3.8 cm plant 7 true=crop predicted=crop distance_to_row= 4.3 cm plant 8 true=crop predicted=crop distance_to_row= 0.2 cm plant 9 true=crop predicted=crop distance_to_row= 3.3 cm plant 10 true=crop predicted=crop distance_to_row= 0.3 cm plant 11 true=crop predicted=crop distance_to_row= 4.3 cm plant 12 true=weed predicted=crop distance_to_row= 5.0 cm plant 13 true=weed predicted=weed distance_to_row= 12.2 cm plant 14 true=weed predicted=weed distance_to_row= 11.4 cm plant 15 true=weed predicted=crop distance_to_row= 0.3 cm plant 16 true=weed predicted=crop distance_to_row= 5.0 cm plant 17 true=weed predicted=weed distance_to_row= 11.3 cm accuracy: 0.83
What actually happened
Every weed that happened to sprout close to a row line (plants 12, 15, 16) got misclassified as a crop. This is not a bug in the code — it is the honest limit of the method itself.
np.abs(all_x[:, None] - row_lines[None, :])computes the distance from every plant to every row line at once, using NumPy broadcasting. Adding[:, None]and[None, :]reshapes the two 1D arrays so they combine into a full grid of distances, one per plant-row pair.np.min(..., axis=1)then keeps only the distance to the closest row for each plant.- The threshold of 8 cm is another tuned decision boundary, exactly like the NDVI threshold in NDVI and vegetation indices — set here by eye, and in practice tuned against labelled field data.
Common mistakes
Trusting position alone for weeds growing inside the crop row. This is the exact failure shown above. An "in-row weed" that germinates right next to a crop plant is invisible to any method based only on distance from a row line. Real commercial systems use vision-based classification (does this look like a weed?) precisely because position alone cannot catch this case.
Ignoring machine speed when picking a model. A highly accurate but slow detector, timed against a tractor moving at several kilometres an hour, will make correct decisions about ground the sprayer has already passed. Detector choice on this kind of system is a genuine accuracy-versus-latency trade, covered generally in YOLO.
Assuming row spacing is exact. Real planting machinery has mechanical tolerance, and a field is rarely perfectly flat. Using too tight a threshold, like 2 cm instead of 8 cm here, will flag healthy crop plants as weeds on any field with normal planting variation.
Try it yourself
Add a seventh weed positioned at x = 1 (right on top of row 0, simulating an in-row weed). Rerun and confirm it gets classified as crop. That single failure is the reason real spot sprayers use trained visual classifiers rather than this position-only method.
What to learn next
- Object detection — the real technique behind production spot sprayers.
- YOLO — a widely used real-time detector family, and its speed-accuracy trade-offs.
- Convolutional neural networks — the architecture that lets a detector tell crop and weed apart by appearance, not only position.
Researcher — Mathematics and papers.
Formal task
Weed detection for spot spraying is typically posed as real-time object detection with a hard latency budget set by machine geometry:
t_available = d_nozzle_offset / v_machined_nozzle_offset— the physical distance between the camera's field of view and the spray nozzle, in metres.v_machine— the machine's forward speed, in metres per second.t_available— the time budget for the full see-decide-spray pipeline, typically tens to a few hundred milliseconds for field-relevant speeds and nozzle offsets.
This is a strict deployment constraint on top of the usual accuracy metrics, and it rules out detector architectures that are accurate but too slow regardless of raw accuracy numbers.
Detector families used in practice
| Family | Typical speed | Note |
|---|---|---|
| Colour/index thresholding (e.g. Excess Green Index) | Extremely fast, effectively free | Cannot distinguish crop from weed, only vegetation from soil — usable only where crop and weed are spatially separated, as in the row-based demo above |
| YOLO family (single-stage detector) | Real-time on embedded GPUs | Dominant choice in commercial spot sprayers as of the mid-2020s, trading some accuracy for speed |
| Two-stage detectors (Faster R-CNN family) | Slower | Higher accuracy, generally reserved for offline analysis or research rather than real-time nozzle control |
| Semantic segmentation (per-pixel crop/weed/soil) | Moderate | Gives finer spray-area control than bounding boxes, at higher compute cost per frame |
See object detection and YOLO for the underlying architectures.
Evaluation
Standard object-detection metrics apply — mean average precision (mAP) across IoU thresholds — but agricultural deployment adds asymmetric cost weighting rarely present in general object-detection benchmarks: a missed weed costs one weed's worth of continued growth, while a misclassified crop plant sprayed with herbicide can kill that plant outright. Several published systems explicitly report a "crop damage rate" alongside standard mAP for exactly this reason, and tune the detection threshold to bias toward under-spraying rather than over-spraying, given the asymmetric cost, as in choosing a threshold from costs.
Reported field results and their limits
Commercial and research spot-spraying systems (e.g. work summarised in Partel et al., 2019; and results from commercial platforms disclosed publicly by agricultural equipment manufacturers) report herbicide reductions frequently in the 70-90% range relative to blanket spraying, alongside detection precision typically in the 85-95% range on the specific crop, growth stage and weed species tested. These figures are specific to the reported conditions. Cross-crop, cross-region and cross-growth-stage generalisation is not automatic, echoing the domain-shift caveat in why crop disease models fail in the field — a detector trained on one crop and weed population needs revalidation, not blind reuse, before deployment on a different one.
Papers
- Partel, V., Kakarla, S. C., Ampatzidis, Y. (2019). Development and Evaluation of a Low-Cost and Smart Technology for Precision Weed Management Utilizing Artificial Intelligence. Computers and Electronics in Agriculture.
- Wu, X. et al. (2021). Review of Weed Detection Methods Based on Computer Vision. Sensors — a broad survey of the detector families above.
- Redmon, J. et al. (2016). You Only Look Once: Unified, Real-Time Object Detection. CVPR. Origin of the YOLO family used in most real-time agricultural detectors.
Current state
Vision-based, real-time spot spraying is commercially deployed at meaningful scale as of the mid-2020s, primarily on large mechanised farms in North America, Europe and Australia where the capital cost of the equipment is affordable relative to farm size. It remains largely inaccessible to smallholder farms for reasons covered in AI for smallholder farms.
What to learn next
- Object detection — full detail on the detection techniques referenced here.
- Choosing a threshold from costs — formalising the asymmetric cost of spraying a crop versus missing a weed.
- AI for smallholder farms — the access and cost gap this technology has not yet closed.