AI-Based Plant Stress Detection & Early Health Monitor (BAI507P): Complete Backend, Frontend, ML & Windows Setup Guide 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. 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. 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 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 | 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. 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 engine.py — Computer Vision + Machine Learning Core PLANT PROFILES dictionary moisture , temp , humidity , light for 12 plants across 3 categories: center plant frame bgr, plant mask — Auto-Centering for Accuracy cv2.boundingRect of the detected leaf pixels. 480×360 canvas. analyze plant image image bytes — OpenCV Symptom Extraction 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. PlantStressEngine — Scikit-learn Random Forest & Severity Fusion 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 . severity score = 0.58 visual stress + 0.42 env stress Improving / Stable / Worsening : severity score with the plant's previous recorded observation: = 6 points → ±6 points → db.py — SQLite Persistence Layer 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 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 | static/index.html — Clean Zero-Framework Frontend navigator.mediaDevices.getUserMedia + HTML5