My tomato was sick. My phone knew the cure. That sounds like a sales pitch, and I wish it was, because then at least somebody would be paying me for it. It is what happened after I spent too long staring at a tomato leaf, telling myself the brown rings were probably fine, while the plant quietly got worse behind my back.
So I built a plant-disease doctor. You take a photo of a leaf, and it tells you what is wrong, how bad it is, and what to do about it, in plain language. Just: this is early blight, this much of the leaf is affected, pick these leaves off and spray this.
This is plant disease detection using yolov11, or at least it is heading that way. The version in this project runs offline in demo mode, with no model download, using sample leaves and a colour-based classifier that stands in for the real model. The full YOLOv11 path is in the code and the README, so you can train the real thing on PlantVillage data and drop it in.

Why leaves lie to the naked eye
Your eyes are great at seeing a leaf, and terrible at measuring one. You look at a tomato plant and your brain says “green, mostly, looks fine.” Your brain is rounding. A small patch of brown here and a yellow halo there add up, and by the time your brain notices, the disease has already moved on to the next leaf.
Sick leaves also all look roughly the same if you do not know what you are looking for. Brown spot, dark patch, yellow edge — to most of us, that is one disease called “the plant looks sad.” To the plant, it matters which one it is, because early blight and late blight are not the same problem, they do not spread the same way, and they do not want the same treatment.
What you need
You need almost nothing. The demo runs on a normal computer, offline, with no model to download and no account to create. You need Python, the two libraries in requirements.txt, and the project folder. The code uses Pillow to open the photo and NumPy to count pixels. If you want the web version, you also need Gradio, but that is optional.
The project comes with four sample leaves in sample_data/, so you can run it before you have photographed anything. They are synthetic leaves, drawn by a script, not real photos — I will say that again later, because it matters — but they are real files the classifier really measures, and the numbers below come from measuring them. If you use your own photos later, take them in daylight, fill the frame with one leaf, and keep the background plain.
How it works
First, the photo gets resized and the background gets ignored. The code looks for near-white pixels and calls those background. Everything else is leaf.
Then it sorts every leaf pixel into a bucket by colour. Green means healthy leaf. Yellow means yellowing. Brown means brown spots. Dark means very dark patches, the water-soaked kind. Some pixels fit no bucket — veins, odd in-between colours — and those count as leaf but not as a symptom.
Then it adds up the symptom buckets. Yellow plus brown plus dark, as a share of all the leaf pixels, is the severity. If a quarter of the leaf is brown and yellow, the severity is about a quarter. Your eyes would have said “a bit spotty.” The code says a number.
Then it applies rules, in order, to name the disease. If the symptom share is at or under a small threshold, the leaf is healthy. If dark patches cover enough of the leaf, and there is at least as much dark as brown, it calls it potato late blight. If brown covers enough of the leaf, it calls it tomato early blight. Anything else symptomatic gets called pepper bacterial spot.
Finally, it looks up the treatment in treatments.py, a table that answers in plain language: what caused it, what to do today, what to spray, and what to watch for.

The maths
There are two numbers in the output, confidence and severity, and they measure two different things.
Severity is the easy one. Count the leaf pixels. Count how many of them are yellow, brown, or dark. Divide the second number by the first, and turn it into a percentage. That percentage is the severity: the share of the leaf showing symptoms. The code also turns that percentage into a word. Under 5% is mild. From 5% up to 15% is moderate. From 15% up to 30% is severe. Over 30% is critical.
Confidence is the other number, and it is not severity wearing a different hat. Confidence says how sure the rule match is, and it is built from the dominant symptom colour. The more of the leaf that colour covers, the surer the match. Each disease has its own formula, and each formula is capped, so the demo never claims to be completely sure. Healthy caps at 98%, the two blights cap at 96%, and bacterial spot caps at 95%.
Here is the worked example, using the tomato early blight sample leaf, with the real numbers from the demo run. The leaf had 66,826 leaf pixels. Green covered 71.9% of them, yellow covered 12.6%, brown covered 14.9%, and dark covered 0.0%. The leftover is the veins and in-between colours that fit no bucket.
Severity first. Add the symptom buckets using the unrounded values: yellow 12.56% plus brown 14.87% plus dark 0.00% gives 27.43%, which rounds to 27.4%. That is the severity: 27.4% of the leaf affected. It falls in the 15% to 30% band, so the word is severe. Not a guess. A count.
Confidence next. The early blight formula starts at 62, then adds 180 times the brown share and 50 times the yellow share, using the shares as fractions. Brown as a fraction is 0.1487, so 180 times that is 26.77. Yellow as a fraction is 0.1256, so 50 times that is 6.28. Add them up: 62 plus 26.77 plus 6.28 gives 95.04, which rounds to 95.0%. That is the confidence: 95.0%. It is below the 96% cap for early blight, so no capping was needed here.
Build it
Install the demo dependencies. The demo needs Pillow and NumPy, and nothing else.
pip install -r requirements.txtRun the demo on every sample leaf. This is the command the numbers in this article come from.
python3 diagnose.py --allDiagnose a single photo of your own.
python3 diagnose.py sample_data/tomato_early_blight.jpgIf you want the web version, run the app file and open the local address it prints. Gradio is imported only inside that file, so the command-line tool never needs it.
python3 app.pyThe heart of the project is doctor.py. The function that matters most is extract_features: it opens the photo, resizes it, throws out the near-white background, and sorts every remaining pixel into a colour bucket.
def extract_features(image_path):
img = Image.open(image_path).convert("RGB").resize((400, 400))
arr = np.asarray(img, dtype=np.int16)
r, g, b = arr[:, :, 0], arr[:, :, 1], arr[:, :, 2]
leaf = ~((r > BACKGROUND_MIN) & (g > BACKGROUND_MIN) & (b > BACKGROUND_MIN))
dark = leaf & (r < 95) & (g < 95) & (b < 95)
brown = leaf & ~dark & (r > 100) & (r < 195) & (g > 55) & (g < 125) & (r > g) & (g > b)
yellow = leaf & ~dark & ~brown & (r > 175) & (g > 150) & (b < 130) & (r > b + 50)
green = leaf & ~dark & ~brown & ~yellow & (g > r + 15) & (g > b + 15)
return Features(leaf_pixels=int(leaf.sum()), green_pct=pct(green),
yellow_pct=pct(yellow), brown_pct=pct(brown), dark_pct=pct(dark))The classifier itself is classify, and it is just the rules from the previous section, written as code.
def classify(features):
symptom = features.symptom_pct / 100.0
if symptom <= HEALTHY_SYMPTOM_MAX:
return "healthy", round(min(98.0, 90.0 + features.green_pct * 8.0), 1)
if features.dark_pct >= LATE_BLIGHT_DARK_MIN and features.dark_pct >= features.brown_pct:
return "potato_late_blight", round(min(96.0, 60.0 + features.dark_pct * 150.0 + features.brown_pct * 80.0), 1)
if features.brown_pct >= EARLY_BLIGHT_BROWN_MIN:
return "tomato_early_blight", round(min(96.0, 62.0 + features.brown_pct * 180.0 + features.yellow_pct * 50.0), 1)
return "pepper_bacterial_spot", round(min(95.0, 58.0 + features.dark_pct * 200.0 + features.yellow_pct * 120.0), 1)The thresholds come from config.py, not from inside the function. If you want to tune the doctor, you change one small config file, not the logic. The caps are right there in the open: 98, 96, 96, 95.
The treatment table is treatments.py: a dictionary from disease name to plain-language advice, written by a human and identical every time for the same disease.
The command-line tool is diagnose.py. It reads the sample list from sample_data/leaves.csv for the --all run, and it holds the YOLOv11 switch: when USE_YOLOV11 is set in config.py, it loads a trained model file and takes the top prediction’s class and confidence, while severity still comes from the pixel measurement.
Expected outcome
Here is what the demo actually prints, leaf by leaf, from running python3 diagnose.py --all on the four sample leaves. If you run the same command, you will get the same numbers, because the sample leaves are generated from fixed seeds.
The healthy tomato leaf came back Healthy, with 98.0% confidence and 0.0% of the leaf affected, rated mild. Its leaf pixels were 99.6% green, with 0.0% yellow, 0.0% brown, and 0.0% dark.
The tomato early blight leaf came back as Tomato early blight, with 95.0% confidence and 27.4% of the leaf affected, rated severe. Its leaf pixels were 71.9% green, 12.6% yellow, 14.9% brown, and 0.0% dark. The treatment says to pick off the worst leaves, bag them, bin them, and not compost them, then water at the base in the morning and spray copper fungicide or chlorothalonil at the label dose before the next rain.
The potato late blight leaf came back as Potato late blight, with 96.0% confidence and 29.4% of the leaf affected, rated severe. Its leaf pixels were 70.2% green, 3.0% yellow, 0.0% brown, and 26.4% dark. The treatment says to remove badly affected leaves at once, stop overhead watering, improve airflow, and spray copper fungicide or a blight-specific fungicide at the label dose, repeating after rain.
The pepper bacterial spot leaf came back as Pepper bacterial spot, with 79.9% confidence and 14.7% of the leaf affected, rated moderate. Its leaf pixels were 84.7% green, 9.3% yellow, 0.0% brown, and 5.3% dark. The treatment says to remove spotted leaves, keep leaves dry, space the plants for airflow, and know that copper spray can slow it but will not cure spots that are already there.

All four results above are from the built-in sample and demo dataset, not the accuracy of a trained YOLOv11 model. They are what a colour-rule classifier measured on four synthetic leaves. If you train the real model on PlantVillage data, quote those numbers from that run instead.
What broke
The first version of the classifier was confidently wrong, which is the worst kind of wrong. It called the pepper leaf healthy. The small dark spots and their yellow halos added up to enough symptom share to matter, but the rule order checked the wrong thing first, and the leaf slipped through as fine. A plant doctor that tells a sick pepper it is healthy is not a doctor. It is a very polite bystander.
The fix was boring, which is how you know it was the right fix. I reordered the rules so the healthy check happens first and only when the symptom share is genuinely tiny, then the dark-patch check, then the brown check, and only then the bacterial spot fallback. I also stopped letting the confidence formula borrow numbers from the wrong bucket, because an early version built a confidence score from a quantity that had nothing to do with the disease it was naming. The rule now is simple: confidence comes from the dominant symptom colour for that disease, and severity comes from the total symptom share. Two numbers, two sources, never swapped.
What is inside the folder
The project folder is small on purpose. doctor.py holds the classifier, severity scoring, and report text. diagnose.py is the command-line tool and the YOLOv11 switch. app.py is the optional Gradio web interface. treatments.py is the disease-to-treatment table. config.py holds the thresholds, severity bands, and YOLOv11 settings. generate_samples.py draws the sample leaves. requirements.txt lists Pillow and NumPy, with Gradio and Ultralytics commented out as optional. sample_data/ holds the four leaf images and leaves.csv. README.md covers setup, the demo run, and the real YOLOv11 training notes, including PlantVillage as the data source.

Download the full project
You can download the full project, code and sample leaves included, here:
Premium Download
Get the complete project code
Unzip it, run pip install -r requirements.txt, then run python3 diagnose.py --all. Then take a photo of a real leaf, point the tool at it, and see what it says.
