跳到正文
DEV Community· Pavan Kumar Sadashiv·· 16 小時前AI 評分37

BAI507P 植物壓力偵測與早期健康監測系統:後端、前端、機器學習及 Windows 完整設定教學

AI-Based Plant Stress Detection & Early Health Monitor (BAI507P): Complete Backend, Frontend, ML & Windows Setup Guide

AI 導讀

BAI507P 植物壓力偵測與早期健康監測系統採用 OpenCV 特徵提取及 Scikit-learn RandomForest 的兩階段架構,並提供完整建置教學。

正文

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.


1. End-to-End Architecture: How Everything Connects

Browser (static/index.html)
  │  1. Captures / uploads a leaf photo + soil/weather slider values
  │  2. Sends multipart FormData via fetch("POST /api/analyze")
  ▼
FastAPI Server (app.py)
  │  3. Reads uploaded image bytes + validates sensor inputs
  │  4. Calls analyze_plant_image() in engine.py
  ▼
OpenCV Computer Vision Pipeline (engine.py)
  │  5. Segments plant tissue (HSV) & auto-centers the leaf (_center_plant_frame)
  │  6. Quantifies 6 visual symptoms:
  │     - Healthy Green %, Yellowing %, Browning %, Dark Spots %, Pale %, Wilting Index
  │  7. Draws color-coded segmentation overlay & encodes JPEG as base64
  ▼
Scikit-learn Multi-Modal Fusion (engine.py)
  │  8. Compares sensor values against the selected species' ideal range (deltas)
  │  9. Combines 6 visual features + 5 environmental features = 11 features
  │ 10. RandomForestClassifier predicts stress class + probabilities
  │ 11. Computes 0–100 Severity Score, Risk Level, Trend (Improving/Stable/Worsening) & Care Plan
  ▼
SQLite Database (db.py → plant_monitor.db)
  │ 12. Saves observation for historical progression charts & CSV export
  ▼
Browser (static/index.html)
     13. Renders Plant Health Report, Highlighted Overlay, Symptom Bars, Care Plan & History Chart

2. Answering the #1 Viva Question: "Why Did the Model Train in Under 1 Second Instead of Hours?"

Most 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.

For 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:

Approach Input to Model Training Hardware & Time Explainability to Guide / Farmers
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
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

Why this is even better for our problem statement:

  1. Visual symptoms alone are ambiguous (yellowing can be caused by nitrogen deficiency or overwatering or heat). A pure image CNN cannot see soil moisture or air temperature.
  2. Stage 1 (OpenCV) converts any leaf photo into 6 deterministic botanical measurements in ~30 milliseconds — no training required for color/spot/contour math.
  3. Stage 2 (Scikit-learn RandomForestClassifier) trains 120 decision trees on 1,440 domain-calibrated agronomic feature rows (covering 7 stress classes across Crops, Garden Plants, and Trees) with 5-fold cross-validation in under a second on startup.
  4. How to scale with larger real datasets later: Whenever real labeled farm/garden data is collected (or PlantVillage images are processed through 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.

3. Complete Code Walkthrough (Show This in VS Code)

The entire backend is intentionally clean and modular — only 3 Python files + 1 frontend HTML file:

plant-stress-monitor/
├── app.py                  # FastAPI web server & REST API endpoints
├── engine.py               # OpenCV leaf analysis + Scikit-learn RandomForest engine
├── db.py                   # SQLite database (plants & scan history) + CSV export
├── static/
│   ├── index.html          # Responsive frontend UI (HTML + CSS + Vanilla JS)
│   └── logo.svg            # Project leaf brand logo
├── test_app.py             # Automated self-check test suite
└── requirements.txt        # Exact Python package list

A. engine.py — Computer Vision + Machine Learning Core

  1. PLANT_PROFILES dictionary

    • Stores species-specific ideal ranges (moisture, temp, humidity, light) for 12 plants across 3 categories:
      • Agricultural Crops: Tomato, Paddy (Rice), Chilli, Banana
      • Garden Plants: Rose, Hibiscus, Tulsi, Money Plant
      • Trees: Mango Tree, Coconut Palm, Neem Tree, General Plant
  2. _center_plant_frame(bgr, plant_mask) — Auto-Centering for Accuracy

    • Finds the bounding box (cv2.boundingRect) of the detected leaf pixels.
    • Crops tightly with a 14% margin and places the leaf dead-center on a standardized 480×360 canvas.
    • Result: Even if a user uploads an off-center phone photo, measurements stay consistent and accurate.
  3. analyze_plant_image(image_bytes) — OpenCV Symptom Extraction

    • Converts image from BGR → HSV (Hue, Saturation, Value) and CIE-Lab (Lightness + color channels).
    • Plant Masking: Isolates organic plant hues (H: 5–95, S >= 28) and applies morphological closing (cv2.morphologyEx) so dark spots inside the leaf aren't excluded.
    • 6 Quantitative Visual Features:
      • 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
    • Visual Overlay: Blends semi-transparent color masks onto the leaf (Yellow = Chlorosis, Red-Orange = Browning, Purple = Dark Spots, Green Contour = Leaf Boundary) and returns base64 JPEG images to the UI.
  4. PlantStressEngine — Scikit-learn Random Forest & Severity Fusion

    • Uses 11 features (6 visual + 5 environmental deltas: moisture_delta, temp_delta, humidity, light_delta, rain_wetness).
    • Trains RandomForestClassifier(n_estimators=120, max_depth=12, random_state=42) and evaluates it using cross_val_score(..., cv=5).
    • Severity Score (0–100): Weighted fusion of visual and environmental stress:
     severity_score = 0.58 * visual_stress + 0.42 * env_stress
    
  • Progression Tracking (Improving / Stable / Worsening): Compares the current severity_score with the plant's previous recorded observation:
    • Drop of >= 6 points → Improving
    • Increase of >= 6 points → Worsening
    • Within ±6 points → Stable

B. db.py — SQLite Persistence Layer

  • Uses Python's built-in sqlite3 (zero database installation needed on demo day!).
  • Two core tables:
    • plants (id, name, plant_type, category, zone, created_at)
    • observations (id, plant_id, timestamp, sensors_json, vision_json, stress_type, confidence, severity_score, severity_level, risk_level, trend, recommendations_json, overlay_b64)
  • On first startup, seeds 3 realistic demo plants (Tomato Crop, Terrace Hibiscus, Campus Mango Sapling) with multi-day history so the Plant History chart works immediately.

C. app.py — FastAPI REST Backend

Key endpoints you can show in VS Code (or test live at http://localhost:8000/docs):

Endpoint Method Purpose
/ GET Serves the web UI (static/index.html)
/api/analyze POST Runs OpenCV + RandomForest diagnosis, saves to SQLite, returns JSON + overlay
/api/plants GET / POST Lists all tracked plants or registers a new plant
/api/plants/{id}/history GET Returns full timeline + overall progression verdict for a plant
/api/plants/{id}/export.csv GET Downloads full plant history as a CSV file (compatible with Excel / MySQL / Firebase)
/api/sample-leaf/{kind} GET Generates clean OpenCV demo leaves (healthy, yellowing, drought, spotted)

D. static/index.html — Clean Zero-Framework Frontend

  • Pure HTML5 + CSS3 + Vanilla JavaScript (zero React/Node build tools required, loads instantly offline in college labs).
  • Webcam Capture: Uses browser navigator.mediaDevices.getUserMedia() + HTML5 <canvas> snapshot.
  • Live Sensor Status Badges: Updates Ideal / Too Low / Too High pills in real time as sliders move relative to the chosen plant's profile.
  • Interactive SVG Trend Chart: Plots Stress Score (Red) vs. Healthy Green Area % (Green) over time with zero external charting libraries.

4. Step-by-Step Windows Setup & GitHub Fork Guide (For VS Code Demo)

Follow these exact steps on a Windows 10 / 11 laptop:

Step 1: Install Prerequisites

  1. Install Python 3.11+ from python.org — make sure to check "Add Python to PATH" during install.
  2. Install Git from git-scm.com.
  3. Install VS Code from code.visualstudio.com.

Step 2: Fork & Clone the Repository

  1. Open the project repository link on GitHub and click Fork (top right) → Create fork under your own GitHub account.
  2. Click the green <> Code button on your forked repo and copy the HTTPS URL (e.g., https://github.com/<your-username>/plant-stress-monitor.git).
  3. Open VS Code → press Ctrl + ` to open the integrated PowerShell terminal and run:
   git clone https://github.com/<your-username>/plant-stress-monitor.git
   cd plant-stress-monitor
  1. Open the folder in VS Code: File → Open Folder → plant-stress-monitor.

Step 3: Create Virtual Environment & Install Packages

In the VS Code PowerShell terminal:

python -m venv .venv
.\.venv\Scripts\Activate.ps1
pip install -r requirements.txt

(If PowerShell blocks script execution, run once: Set-ExecutionPolicy -Scope CurrentUser RemoteSigned and retry activation.)

Step 4: Run the Application & Automated Tests

# Optional: verify all backend + OpenCV + ML checks pass
python test_app.py

# Start the web server
python app.py

Open http://localhost:8000 in Chrome or Edge! You can also open http://localhost:8000/docs to show the interactive FastAPI backend API documentation to your guide.


5. Top HOD Viva Questions & Crisp Answers

  1. Q: Why did you combine image analysis with environmental sensors instead of using only images? A: Multiple plant stresses (like nitrogen deficiency, overwatering, and heat stress) cause similar yellowing on leaves. Fusing OpenCV visual symptoms with soil moisture, temperature, humidity, light, and wetness lets the ML model separate the actual root cause accurately.
  2. Q: Why is Random Forest trained so quickly here? A: Because OpenCV handles raw image feature extraction deterministically (reducing hundreds of thousands of pixels into 6 botanical metrics), so the Random Forest only trains on 11 structured numerical features across 1,440 samples — taking under 0.3 seconds on CPU with high cross-validation accuracy.
  3. Q: How does the system detect whether a plant is improving or worsening? A: Every scan is stored in SQLite (observations table) linked to plant_id. On each new scan, engine.py compares the new severity score against the previous scan: a drop of 6+ points is marked **Improving, an increase of 6+ points is **Worsening, and within ±6 points is **Stable.
  4. Q: What happens if a user uploads an off-center leaf photo? A: _center_plant_frame() in engine.py detects the leaf contour bounding box, crops out excess background with a 14% margin, and re-centers the leaf on a standard 480×360 canvas before computing symptom percentages.
  5. Q: How do you export or migrate the database to MySQL / Firebase? A: All data logic is isolated inside db.py, and every plant has a one-click Export CSV endpoint (/api/plants/{id}/export.csv) that produces clean structured records ready for MySQL import, Firebase, or research analysis.

來源:DEV Community · dev.to