{"slug": "ai-based-plant-stress-detection-early-health-monitor-bai507p-complete-backend-ml", "title": "AI-Based Plant Stress Detection & Early Health Monitor (BAI507P): Complete Backend, Frontend, ML & Windows Setup Guide", "summary": "A developer built BAI507P, an AI-based plant stress detection and early health monitoring system that pairs an OpenCV pipeline extracting six visual leaf symptoms with a scikit-learn RandomForest classifier fusing those features with five environmental sensor readings into 11 inputs. The FastAPI and SQLite application predicts a stress class, a 0-100 severity score, risk level, trend and care plan, running on a standard laptop CPU in under 0.3 seconds, with an optional MobileNetV2 transfer-learning script as an upgrade path.", "body_md": "This guide walks through **every single file, function, ML decision, and setup step** behind our **AI-Based Plant Stress Detection and Early Health Monitoring System** (`BAI507P` Mini Project). It is written so you can open the code in **VS Code**, trace every line from the browser to Python and SQLite, run it on **Windows**, and answer any viva question confidently.\n\n```\nBrowser (static/index.html)\n  │  1. Captures / uploads a leaf photo + soil/weather slider values\n  │  2. Sends multipart FormData via fetch(\"POST /api/analyze\")\n  ▼\nFastAPI Server (app.py)\n  │  3. Reads uploaded image bytes + validates sensor inputs\n  │  4. Calls analyze_plant_image() in engine.py\n  ▼\nOpenCV Computer Vision Pipeline (engine.py)\n  │  5. Segments plant tissue (HSV) & auto-centers the leaf (_center_plant_frame)\n  │  6. Quantifies 6 visual symptoms:\n  │     - Healthy Green %, Yellowing %, Browning %, Dark Spots %, Pale %, Wilting Index\n  │  7. Draws color-coded segmentation overlay & encodes JPEG as base64\n  ▼\nScikit-learn Multi-Modal Fusion (engine.py)\n  │  8. Compares sensor values against the selected species' ideal range (deltas)\n  │  9. Combines 6 visual features + 5 environmental features = 11 features\n  │ 10. RandomForestClassifier predicts stress class + probabilities\n  │ 11. Computes 0–100 Severity Score, Risk Level, Trend (Improving/Stable/Worsening) & Care Plan\n  ▼\nSQLite Database (db.py → plant_monitor.db)\n  │ 12. Saves observation for historical progression charts & CSV export\n  ▼\nBrowser (static/index.html)\n     13. Renders Plant Health Report, Highlighted Overlay, Symptom Bars, Care Plan & History Chart\n```\n\nMost students assume every image AI project has to train a heavy **Deep Learning CNN** (like ResNet or MobileNetV2) on 50,000+ raw RGB images — which requires a GPU and takes hours.\n\nFor this Mini Project, we deliberately chose a **Two-Stage Hybrid Architecture (Computer Vision Feature Extraction + Tabular Machine Learning)**, which is faster, explainable, and runs cleanly on any student laptop CPU:\n\n| Approach | Input to Model | Training Hardware & Time | Explainability to Guide / Farmers | \n|---|---|---|---|\n| **End-to-End CNN** (e.g., MobileNetV2) | Raw `224×224×3` pixels (150,528 values/image) | GPU, 1–4 hours on 50k images | Black box — hard to combine with soil/temp sensors | \n| **Our 2-Stage Pipeline** (`OpenCV + RandomForest` ) | **11 structured numeric features** (6 OpenCV leaf metrics + 5 sensor deviations) | Standard laptop CPU, **< 0.3 seconds** | 100% transparent — shows exact yellowing %, brown %, spot %, and sensor deviations | \n\n`RandomForestClassifier`)` analyze_plant_image()`), you simply append those rows to the training DataFrame — and we also included `train_cnn_optional.py` (`MobileNetV2` transfer learning in TensorFlow/Keras) as an optional upgrade path if you expand to a full deep-learning ensemble later.\nThe entire backend is intentionally clean and modular — **only 3 Python files + 1 frontend HTML file**:\n\n```\nplant-stress-monitor/\n├── app.py                  # FastAPI web server & REST API endpoints\n├── engine.py               # OpenCV leaf analysis + Scikit-learn RandomForest engine\n├── db.py                   # SQLite database (plants & scan history) + CSV export\n├── static/\n│   ├── index.html          # Responsive frontend UI (HTML + CSS + Vanilla JS)\n│   └── logo.svg            # Project leaf brand logo\n├── test_app.py             # Automated self-check test suite\n└── requirements.txt        # Exact Python package list\n```\n\n`engine.py` — Computer Vision + Machine Learning Core\n`PLANT_PROFILES` dictionary\n\n`moisture`, `temp`, `humidity`, `light`) for 12 plants across 3 categories:\n`_center_plant_frame(bgr, plant_mask)` — Auto-Centering for Accuracy\n\n`cv2.boundingRect`) of the detected leaf pixels.` 480×360` canvas.\n`analyze_plant_image(image_bytes)` — OpenCV Symptom Extraction\n\n`H: 5–95`, `S >= 28`) and applies morphological closing (` cv2.morphologyEx`) so dark spots inside the leaf aren't excluded.` green_pct`: Healthy chlorophyll pixels (` H: 36–88`)` yellow_pct`: Chlorosis / yellowing (` H: 18–35`, high brightness)` brown_pct`: Necrosis / dry scorch (` H: 4–20`, lower brightness)` spot_pct`: Fungal spots / lesions detected via local Lab lightness drop (`local_mean_L - L > 24`)` pale_pct`: Bleaching / pale discoloration (low saturation, high brightness)`wilt_index`: Contour solidity (` area / convex_hull_area`) + Canny edge density (` cv2.Canny`) to detect curling and wrinkling` Yellow = Chlorosis`, `Red-Orange = Browning`, `Purple = Dark Spots`, `Green Contour = Leaf Boundary`) and returns base64 JPEG images to the UI.\n`PlantStressEngine` — Scikit-learn Random Forest & Severity Fusion\n\n`6 visual + 5 environmental deltas`: `moisture_delta`, `temp_delta`, `light_delta`, `rain_wetness`).` RandomForestClassifier(n_estimators=120, max_depth=12, random_state=42)` and evaluates it using `cross_val_score(..., cv=5)`.\n\n```\n severity_score = 0.58 * visual_stress + 0.42 * env_stress\n```\n\n`Improving` / `Stable` / `Worsening`):` severity_score` with the plant's previous recorded observation:\n`>= 6` points → `±6` points → `db.py` — SQLite Persistence Layer\n`sqlite3` (zero database installation needed on demo day!).`plants` (`id`, `name`, `plant_type`, `category`, `zone`, `created_at`)` observations` (`plant_id`, `timestamp`, `sensors_json`, `vision_json`, `stress_type`, `confidence`, `severity_level`, `risk_level`, `trend`, `recommendations_json`, `overlay_b64`)` app.py` — FastAPI REST Backend\nKey endpoints you can show in VS Code (or test live at `http://localhost:8000/docs`):\n\n| Endpoint | Method | Purpose | \n|---|---|---|\n| `/` | `GET` | Serves the web UI ( `static/index.html` ) | \n| `/api/analyze` | `POST` | Runs OpenCV + RandomForest diagnosis, saves to SQLite, returns JSON + overlay | \n| `/api/plants` | `GET` /`POST` | Lists all tracked plants or registers a new plant | \n| `/api/plants/{id}/history` | `GET` | Returns full timeline + overall progression verdict for a plant | \n| `/api/plants/{id}/export.csv` | `GET` | Downloads full plant history as a CSV file (compatible with Excel / MySQL / Firebase) | \n| `/api/sample-leaf/{kind}` | `GET` | Generates clean OpenCV demo leaves ( `healthy` ,`yellowing` ,`drought` ,`spotted` ) | \n\n`static/index.html` — Clean Zero-Framework Frontend\n`navigator.mediaDevices.getUserMedia()` + HTML5 `<canvas>` snapshot.`Ideal / Too Low / Too High` pills in real time as sliders move relative to the chosen plant's profile.\nFollow these exact steps on a **Windows 10 / 11 laptop**:\n\n`<> Code`` https://github.com/<your-username>/plant-stress-monitor.git`).`` Ctrl + ` `` to open the integrated PowerShell terminal and run:\n\n```\n   git clone https://github.com/<your-username>/plant-stress-monitor.git\n   cd plant-stress-monitor\n```\n\n`File → Open Folder → plant-stress-monitor`.\nIn the VS Code PowerShell terminal:\n\n```\npython -m venv .venv\n.\\.venv\\Scripts\\Activate.ps1\npip install -r requirements.txt\n```\n\n*(If PowerShell blocks script execution, run once: `Set-ExecutionPolicy -Scope CurrentUser RemoteSigned` and retry activation.)*\n\n```\n# Optional: verify all backend + OpenCV + ML checks pass\npython test_app.py\n\n# Start the web server\npython app.py\n```\n\nOpen **[http://localhost:8000](http://localhost:8000)** in Chrome or Edge! You can also open **[http://localhost:8000/docs](http://localhost:8000/docs)** to show the interactive FastAPI backend API documentation to your guide.\n\n`_center_plant_frame()` in `Export CSV` endpoint (`/api/plants/{id}/export.csv`) that produces clean structured records ready for MySQL import, Firebase, or research analysis.", "url": "https://wpnews.pro/news/ai-based-plant-stress-detection-early-health-monitor-bai507p-complete-backend-ml", "canonical_source": "https://dev.to/hrlpavan/ai-based-plant-stress-detection-early-health-monitor-bai507p-complete-backend-frontend-ml--3edk", "published_at": "2026-11-21 07:27:53+00:00", "updated_at": "2026-11-21 07:42:03.933813+00:00", "lang": "en", "topics": ["machine-learning", "computer-vision", "ai-tools", "developer-tools"], "entities": ["OpenCV", "scikit-learn", "FastAPI", "SQLite", "MobileNetV2", "TensorFlow", "Keras", "RandomForestClassifier"], "also_reported_by": [], "takeaway": "Combining six OpenCV leaf-symptom metrics with five sensor deviations into 11 tabular features lets a RandomForest classify plant stress in under 0.3 seconds on a laptop CPU, trading a black-box CNN for fully explainable outputs.", "recommendation": null, "alternates": {"html": "https://wpnews.pro/news/ai-based-plant-stress-detection-early-health-monitor-bai507p-complete-backend-ml", "markdown": "https://wpnews.pro/news/ai-based-plant-stress-detection-early-health-monitor-bai507p-complete-backend-ml.md", "text": "https://wpnews.pro/news/ai-based-plant-stress-detection-early-health-monitor-bai507p-complete-backend-ml.txt", "jsonld": "https://wpnews.pro/news/ai-based-plant-stress-detection-early-health-monitor-bai507p-complete-backend-ml.jsonld"}}