File size: 9,374 Bytes
fe69e84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# CropGuard GH — Standalone HTML App Documentation

**The self-contained, single-file browser application.**
Final Year Project · Oppong David · BTech Computer Technology, Kumasi Technical University

---

## 1. What this is

A complete crop disease detection front end in **one HTML file** — no install, no build step, no server required to open it. A user takes or chooses a photo of a crop leaf and immediately gets a disease name, a confidence score, a severity level (with colour), an urgency level, ordered treatment steps and suggested products.

It is the most accessible deliverable in the project: email the file, host it on any static link, or open it straight from a phone's storage. It works on a basic Android phone with no app-store download.

### The three files

| File | Language | Notes |
|---|---|---|
| `cropguard.html` | **Bilingual** | English ⇄ Twi toggle in the header. This is the main app. |
| `cropguard-en.html` | English only | Same app, no toggle — for English-only deployment. |
| `cropguard-tw.html` | Twi only | Same app, no toggle — for Twi-only deployment. |

All three are byte-for-byte self-contained: HTML, CSS and JavaScript in a single file, fonts from Google Fonts, no other external dependencies.

### Coverage
14 Ghanaian crops, 55 disease/healthy classes: maize, cassava, tomato, cocoa, cashew, plantain, yam, pepper, cowpea, groundnut, rice, okra, garden egg, mango.

---

## 2. How to use it

1. **Open** the file in any modern browser (phone or computer) — double-click it, or host it and visit the link.
2. **Take a photo** ("Take a photo of a leaf") or **Choose from gallery**. On a phone the camera button opens the rear camera directly.
3. **Check the photo** on the preview screen — the app reminds the user to fill the frame with the diseased leaf in good light — then tap **Analyse crop**.
4. **Read the result:** crop + disease, confidence bar, severity badge (green/amber/red), urgency, a numbered treatment list (most urgent first), suggested products and a short "about this disease" note.
5. **Scan another leaf** or **Share result** (uses the device share sheet, or copies to clipboard).

A **Help** sheet on the home screen explains all of this to first-time users, in the selected language.

---

## 3. The four-screen flow

The app is a single-page application with four screens (`home → preview → loading → result`) toggled by a small `show()` function — there is no routing library and no page reload.

```
 HOME ──tap camera/gallery──▶ PREVIEW ──tap Analyse──▶ LOADING ──result ready──▶ RESULT
   ▲                                                                               │
   └───────────────────────────── "Scan another leaf" ────────────────────────────┘
```

---

## 4. How the diagnosis works — two modes

The app is designed to **never be broken**, so it has two inference paths and picks automatically:

### Mode A — Connected to the trained model (accurate)
If an API URL is configured, the app sends the photo to the backend (`POST /predict`) and shows the real CNN prediction. This is the accurate mode and the one to use for real diagnosis. Set it by either:
- running `localStorage.setItem('cropguard_api', 'https://your-server:8000')` in the browser console, or
- hosting the app and pointing it at your deployed FastAPI server (see the backend documentation).

The footer shows **"Connected to AI model"** with a live dot when an API URL is set.

### Mode B — On-device estimate (offline fallback)
When **no** API URL is set, the app runs a genuine on-device image analysis in JavaScript using an HTML `<canvas>`. It:
1. draws the photo to a 180×180 canvas and reads the pixels,
2. drops near-white background pixels,
3. measures the proportion of **green**, **yellow**, **brown** and **dark** leaf pixels,
4. converts that into a *diseased-area ratio*, which sets the severity (early < 0.20 ≤ moderate < 0.55 ≤ severe), and
5. maps the dominant colour signature to a likely disease among common Ghanaian field patterns.

The footer shows **"On-device analysis"** in this mode.

> **Be honest about Mode B.** It is a real colour/lesion heuristic, not the trained model, and it is **not** the ~98% system. It is a reasonable offline estimate and a graceful fallback so the app still does something useful with no connectivity — but accurate, all-crop diagnosis comes from Mode A (the connected CNN). The on-screen confidence in Mode B is an estimate, and the app's disclaimer says clearly that this is a diagnostic aid, not a replacement for an extension officer.

---

## 5. Bilingual design (`cropguard.html`)

Everything the user sees exists in both English and Twi. The header toggle calls `setLang('en' | 'tw')`, which:
- stores the choice in `localStorage` (`cropguard_lang`) so it persists,
- swaps every UI string (elements tagged `data-i="key"` are filled from the language table),
- rebuilds the crop "pills" list in the chosen language,
- re-renders the current result in the new language if one is showing.

### Data model
All content lives in one JavaScript object, `DATA`, injected into the file:

```js
const DATA = {
  crops:    { en: [...14 names...], tw: [...14 names...] },
  t:        { en: {...UI strings...}, tw: {...} },     // labels, buttons, help text
  severity: { en: {early/moderate/severe...}, tw: {...} },
  help:     { en: [[title, body], ...], tw: [...] },
  diseases: {
    "cocoa_blackpod": {
      crop: { en: "Cocoa", tw: "Kookoo" },
      name: { en: "Cocoa Black Pod", tw: "Kookoo Aba Tuntum Yare" },
      cause:{ en: "...", tw: "..." },
      tx:   { en: ["step 1", ...], tw: ["..."] },       // treatment steps
      chem: { en: ["Copper hydroxide", ...], tw: ["..."] } // suggested products
    },
    "maize_healthy": { crop:{...}, name:{...}, healthy: true },
    ...
  }
};
```

The single-language files (`-en` / `-tw`) use the same structure flattened to one language and have no toggle.

> The Twi agronomic text was written carefully, but for the more specialised crops (cocoa, cashew, plantain, yam, etc.) a local agricultural extension officer or MoFA/COCOBOD agent should review it before farmer-facing release — some disease terms have no settled Twi equivalent.

---

## 6. Severity and urgency

Severity is derived from the diseased-area ratio (Mode B) or returned by the server (Mode A) and is shown with a colour and an urgency level:

| Severity | Colour | Urgency | Meaning |
|---|---|---|---|
| Early stage | 🟢 green | Routine | Small/localised (<~20% of leaf). Act soon. |
| Moderate stage | 🟠 amber | Urgent | ~20–60% of leaf; can spread fast. Treat this week. |
| Severe stage | 🔴 red | Emergency | >~60% affected. Act today. |

Healthy results skip severity and show a "no disease detected — keep monitoring" panel instead.

---

## 7. Privacy

No sign-up, no account, no analytics, no tracking. In Mode B the photo never leaves the device. In Mode A the photo is sent to the configured server only for the single prediction and is discarded there (the backend never writes it to disk). This matches the privacy-by-design intent of the report (§3.10/§3.13).

---

## 8. Deploying the HTML app

Because it is one static file, hosting is trivial. Any of these work:

- **Email / copy:** send `cropguard.html`; the recipient opens it on their phone.
- **Static host:** GitHub Pages, Netlify, Vercel, Firebase Hosting, or any web server — just upload the file.
- **Local:** open directly from storage (the camera/gallery inputs still work).

To connect it to your trained model in production, host the FastAPI backend (see the backend documentation), enable HTTPS, and set `cropguard_api` to its URL. CORS is already open on the backend so the browser can call it.

---

## 9. Customising it

- **Change treatment text / add a crop's advice:** edit the matching record in the `DATA.diseases` object (keep the `en`/`tw` shape, or just the one language in the single-language files). Class keys must match `recommendations.json` / `classes.json` so server predictions resolve to the right record.
- **Point at a different server:** change `localStorage.cropguard_api`.
- **Re-theme:** all colours are CSS variables at the top of the `<style>` block (`--leaf`, `--gold`, `--early/moderate/severe`, etc.).

> If you add a brand-new crop, you must also add labelled training images and retrain the model (see the Dataset Guide and backend documentation) — the HTML app can *display* a new class, but only the trained model can *detect* it.

---

## 10. Limitations & browser support

- **Mode B is an estimate, not the CNN** — see §4. For real diagnosis, connect the model.
- **A photo that isn't a clear leaf** is caught by a "not a clear leaf" check and the user is asked to retake it.
- **Very small lesions, extreme lighting, or a crop/disease not in the model** can still be misread — the disclaimer makes this clear.
- **Browsers:** any current Chrome, Firefox, Safari or Edge (mobile or desktop). The camera capture uses the standard file-input `capture` attribute; on desktop without a camera it falls back to file selection.
- **Connectivity:** Mode A needs internet to reach the server; Mode B needs none.