Marconcini commited on
Commit
836740a
·
verified ·
1 Parent(s): 734cef3

LeafScan in-browser demo (code @ 54b1aaf)

Browse files
Files changed (2) hide show
  1. README.md +22 -5
  2. index.html +175 -17
README.md CHANGED
@@ -1,10 +1,27 @@
1
  ---
2
- title: Leafscan
3
- emoji: 📊
4
- colorFrom: red
5
- colorTo: yellow
6
  sdk: static
 
7
  pinned: false
 
 
 
 
8
  ---
9
 
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: LeafScan
3
+ emoji: 🌿
4
+ colorFrom: green
5
+ colorTo: gray
6
  sdk: static
7
+ app_file: index.html
8
  pinned: false
9
+ license: mit
10
+ short_description: Plant disease from a leaf photo, runs in your browser
11
+ models:
12
+ - Marconcini/leafscan-convnext-tiny
13
  ---
14
 
15
+ # LeafScan
16
+
17
+ Upload a leaf photo: top-3 diagnoses with calibrated probabilities, and a **"Not sure"** answer
18
+ when confidence is below a threshold fitted on field photos. The model (ConvNeXt-T, int8 ONNX,
19
+ 32 MB) runs **entirely in your browser** with ONNX Runtime Web: nothing is uploaded.
20
+
21
+ About 72% right on held-out field photos; it answers about two thirds of them, ~85% of those
22
+ right. A research demo, not a diagnostic tool.
23
+
24
+ Code, experiments and limitations: [github.com/marconcini/leafscan](https://github.com/marconcini/leafscan) ·
25
+ Model: [Marconcini/leafscan-convnext-tiny](https://huggingface.co/Marconcini/leafscan-convnext-tiny)
26
+
27
+ The Python/Gradio version of the demo (with Grad-CAM) runs locally: `python -m leafscan.app`.
index.html CHANGED
@@ -1,19 +1,177 @@
1
  <!doctype html>
2
- <html>
3
- <head>
4
- <meta charset="utf-8" />
5
- <meta name="viewport" content="width=device-width" />
6
- <title>My static Space</title>
7
- <link rel="stylesheet" href="style.css" />
8
- </head>
9
- <body>
10
- <div class="card">
11
- <h1>Welcome to your static Space!</h1>
12
- <p>You can modify this app directly by editing <i>index.html</i> in the Files and versions tab.</p>
13
- <p>
14
- Also don't forget to check the
15
- <a href="https://huggingface.co/docs/hub/spaces" target="_blank">Spaces documentation</a>.
16
- </p>
17
- </div>
18
- </body>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  </html>
 
1
  <!doctype html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1">
6
+ <title>LeafScan</title>
7
+ <style>
8
+ :root {
9
+ --bg: #f7f8f6; --card: #ffffff; --ink: #1f2328; --muted: #59636e; --line: #e3e6e1;
10
+ --accent: #2f7d4f; --accent-soft: #e4f1e8; --warn: #9a5b00; --warn-soft: #fbf0dc;
11
+ }
12
+ @media (prefers-color-scheme: dark) {
13
+ :root { --bg: #111412; --card: #1a1f1c; --ink: #e8ece9; --muted: #9aa49d; --line: #2c332e;
14
+ --accent: #6cc28e; --accent-soft: #1f3327; --warn: #f0b860; --warn-soft: #3a2e17; }
15
+ }
16
+ * { box-sizing: border-box; }
17
+ body { margin: 0; background: var(--bg); color: var(--ink);
18
+ font: 16px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif; }
19
+ main { max-width: 860px; margin: 0 auto; padding: 32px 16px 48px; }
20
+ h1 { font-size: 1.6rem; margin: 0 0 4px; }
21
+ .sub { color: var(--muted); margin: 0 0 24px; }
22
+ .grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
23
+ @media (max-width: 680px) { .grid { grid-template-columns: 1fr; } }
24
+ .card { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 16px; }
25
+ label.drop { display: flex; flex-direction: column; align-items: center; justify-content: center;
26
+ min-height: 260px; border: 2px dashed var(--line); border-radius: 10px; cursor: pointer;
27
+ color: var(--muted); text-align: center; padding: 12px; overflow: hidden; }
28
+ label.drop:hover { border-color: var(--accent); }
29
+ label.drop img { max-width: 100%; max-height: 320px; border-radius: 8px; }
30
+ input[type=file] { display: none; }
31
+ .verdict { font-size: 1.15rem; margin: 0 0 12px; min-height: 1.5em; }
32
+ .verdict.unsure { background: var(--warn-soft); color: var(--warn); padding: 10px 12px; border-radius: 8px; font-size: 1rem; }
33
+ .bar { margin: 10px 0; }
34
+ .bar .name { display: flex; justify-content: space-between; font-size: .92rem; }
35
+ .bar .track { height: 8px; background: var(--accent-soft); border-radius: 4px; margin-top: 4px; }
36
+ .bar .fill { height: 8px; background: var(--accent); border-radius: 4px; width: 0; transition: width .3s; }
37
+ .status { color: var(--muted); font-size: .9rem; }
38
+ .about { margin-top: 24px; color: var(--muted); font-size: .92rem; }
39
+ .about strong { color: var(--ink); }
40
+ a { color: var(--accent); }
41
+ </style>
42
+ </head>
43
+ <body>
44
+ <main>
45
+ <h1>🌿 LeafScan</h1>
46
+ <p class="sub">Plant disease from a leaf photo — runs entirely in your browser, nothing is uploaded.</p>
47
+
48
+ <div class="grid">
49
+ <div class="card">
50
+ <label class="drop" id="drop">
51
+ <span id="dropText">Click or drop a leaf photo here</span>
52
+ <input type="file" id="file" accept="image/*">
53
+ </label>
54
+ </div>
55
+ <div class="card">
56
+ <p class="verdict" id="verdict"></p>
57
+ <div id="bars"></div>
58
+ <p class="status" id="status">Loading the model (≈ 32 MB, once)…</p>
59
+ </div>
60
+ </div>
61
+
62
+ <div class="about">
63
+ <p><strong>What this is.</strong> A ConvNeXt-T trained on lab photos (PlantVillage), then
64
+ fine-tuned on web and field photos (PlantDoc), for <strong>38 crop / disease classes</strong>
65
+ of 14 crops. This page runs an int8-quantised copy of the model with ONNX Runtime Web.</p>
66
+ <p><strong>How much to trust it.</strong> On held-out field photos it is right about
67
+ <strong>72%</strong> of the time. Below a confidence threshold fitted on field photos it says
68
+ "Not sure": it answers about two thirds of field photos, and about <strong>85%</strong> of those
69
+ answers are right. It only knows these 38 classes and is <strong>not</strong> a substitute for
70
+ an agronomist.</p>
71
+ <p>Code, experiments and limitations:
72
+ <a href="https://github.com/marconcini/leafscan">github.com/marconcini/leafscan</a> ·
73
+ model: <a href="https://huggingface.co/Marconcini/leafscan-convnext-tiny">Marconcini/leafscan-convnext-tiny</a></p>
74
+ </div>
75
+ </main>
76
+
77
+ <script src="https://cdn.jsdelivr.net/npm/onnxruntime-web@1.20.1/dist/ort.min.js"></script>
78
+ <script>
79
+ // Model files: the Hugging Face model repo by default, or ?base=<url> (local testing).
80
+ const BASE = new URLSearchParams(location.search).get("base")
81
+ || "https://huggingface.co/Marconcini/leafscan-convnext-tiny/resolve/main/";
82
+ const MEAN = [0.485, 0.456, 0.406], STD = [0.229, 0.224, 0.225];
83
+ ort.env.wasm.wasmPaths = "https://cdn.jsdelivr.net/npm/onnxruntime-web@1.20.1/dist/";
84
+
85
+ let session, config;
86
+ const $ = (id) => document.getElementById(id);
87
+
88
+ function prettyLabel(label) { // same as leafscan.data.pretty_label
89
+ const [crop, disease] = label.split("___");
90
+ return `${crop.replaceAll("_", " ").replaceAll(",", "").trim()} — ${disease.replaceAll("_", " ").trim()}`;
91
+ }
92
+
93
+ // Same geometry as the evaluation transform: shorter side -> round(1.14 * size), centre crop.
94
+ function preprocess(img, size) {
95
+ const short = Math.floor(size * 1.14);
96
+ const scale = short / Math.min(img.naturalWidth, img.naturalHeight);
97
+ const w = Math.round(img.naturalWidth * scale), h = Math.round(img.naturalHeight * scale);
98
+ const canvas = document.createElement("canvas");
99
+ canvas.width = size; canvas.height = size;
100
+ const ctx = canvas.getContext("2d", { willReadFrequently: true });
101
+ ctx.imageSmoothingEnabled = true; ctx.imageSmoothingQuality = "high";
102
+ ctx.drawImage(img, -Math.round((w - size) / 2), -Math.round((h - size) / 2), w, h);
103
+ const px = ctx.getImageData(0, 0, size, size).data;
104
+ const data = new Float32Array(3 * size * size);
105
+ for (let i = 0; i < size * size; i++)
106
+ for (let c = 0; c < 3; c++)
107
+ data[c * size * size + i] = (px[4 * i + c] / 255 - MEAN[c]) / STD[c];
108
+ return new ort.Tensor("float32", data, [1, 3, size, size]);
109
+ }
110
+
111
+ async function predict(img) {
112
+ const { file, temperature, threshold } = config.onnx_int8;
113
+ const out = await session.run({ image: preprocess(img, config.img_size) });
114
+ const logits = Array.from(out.logits.data).map((z) => z / temperature);
115
+ const m = Math.max(...logits);
116
+ const exps = logits.map((z) => Math.exp(z - m));
117
+ const sum = exps.reduce((a, b) => a + b, 0);
118
+ const probs = exps.map((e) => e / sum);
119
+ const top = probs.map((p, i) => [config.classes[i], p]).sort((a, b) => b[1] - a[1]).slice(0, 3);
120
+ return { top, confident: top[0][1] >= threshold, threshold };
121
+ }
122
+
123
+ function show(pred) {
124
+ const [name, p] = pred.top[0];
125
+ const v = $("verdict");
126
+ if (pred.confident) {
127
+ v.className = "verdict";
128
+ v.innerHTML = `<strong>${prettyLabel(name)}</strong> (confidence ${Math.round(100 * p)}%)`;
129
+ } else {
130
+ v.className = "verdict unsure";
131
+ v.innerHTML = `<strong>Not sure.</strong> Best guess: ${prettyLabel(name)} (${Math.round(100 * p)}%),
132
+ below the ${Math.round(100 * pred.threshold)}% confidence needed to answer.
133
+ Try a closer photo of a single leaf, or ask an expert.`;
134
+ }
135
+ $("bars").innerHTML = pred.top.map(([c, q]) => `
136
+ <div class="bar"><div class="name"><span>${prettyLabel(c)}</span><span>${Math.round(100 * q)}%</span></div>
137
+ <div class="track"><div class="fill" style="width:${(100 * q).toFixed(1)}%"></div></div></div>`).join("");
138
+ }
139
+
140
+ async function handleFile(file) {
141
+ if (!file || !session) return;
142
+ const img = new Image();
143
+ img.src = URL.createObjectURL(file);
144
+ await img.decode();
145
+ $("dropText").replaceWith(Object.assign(img, { id: "dropText" }));
146
+ $("status").textContent = "Thinking…";
147
+ const t0 = performance.now();
148
+ show(await predict(img));
149
+ $("status").textContent = `Done in ${Math.round(performance.now() - t0)} ms, on your device.`;
150
+ }
151
+
152
+ // Test hook: predict from an image URL (used to compare with the Python implementation).
153
+ window.leafscanPredictUrl = async (url) => {
154
+ const img = new Image(); img.crossOrigin = "anonymous"; img.src = url; await img.decode();
155
+ return predict(img);
156
+ };
157
+
158
+ (async () => {
159
+ try {
160
+ config = await (await fetch(BASE + "config.json")).json();
161
+ session = await ort.InferenceSession.create(BASE + config.onnx_int8.file,
162
+ { executionProviders: ["wasm"] });
163
+ $("status").textContent = "Ready. Choose a photo.";
164
+ window.leafscanReady = true;
165
+ } catch (e) {
166
+ $("status").textContent = "Could not load the model: " + e;
167
+ window.leafscanError = String(e);
168
+ }
169
+ })();
170
+
171
+ $("file").addEventListener("change", (e) => handleFile(e.target.files[0]));
172
+ const drop = $("drop");
173
+ drop.addEventListener("dragover", (e) => e.preventDefault());
174
+ drop.addEventListener("drop", (e) => { e.preventDefault(); handleFile(e.dataTransfer.files[0]); });
175
+ </script>
176
+ </body>
177
  </html>