AI-Based Nitrogen Deficiency Detection and Precision Fertilization System
System Demonstration
This project presents a proof-of-concept precision agriculture system that combines deep learning, web technologies, and embedded systems to detect nitrogen (N) deficiency in corn leaves and recommend fertilizer application based on actual plant requirements.
Nitrogen deficiency is a major limiting factor in crop productivity. Conventional fertilizer application methods typically apply nitrogen uniformly across fields, which can result in excessive fertilizer use, increased cost, and environmental pollution. This project explores how computer vision and AI-driven decision-making can enable need-based nitrogen management, improving efficiency and sustainability.
Dataset and Model Development
The system is trained using a publicly available corn leaf nitrogen deficiency dataset from Mendeley Data https://data.mendeley.com/datasets/g7xnn2bm4g/1 . A ResNet-18 convolutional neural network was implemented and trained using PyTorch to classify corn leaf images into different nitrogen deficiency levels, such as high deficiency, mild deficiency, and no deficiency.
After training, the model was saved as a .pth file and optimized for inference in a lightweight application environment suitable for deployment on edge devices.
Web-Based Inference System
A Flask-based web application was developed to deploy the trained deep learning model. The web interface allows users to drag and drop or upload corn leaf images, which are then analyzed in real time. The system displays:
- Predicted nitrogen deficiency level
- Suggested nitrogen fertilizer amount (e.g., no spraying, 2 g, or 5 g nitrogen)
- This interface simulates how AI-assisted crop monitoring can be performed in a practical farming scenario.
Embedded System Integration
To demonstrate system-level integration, the prediction results generated by the Flask application are transmitted wirelessly to an ESP8266 microcontroller. The ESP8266 displays the classification result and fertilizer recommendation on an OLED display, such as:
“Mild N Deficiency – Spray 2 g Nitrogen”
Although no physical spraying mechanism is currently implemented, this step validates real-time communication between AI software and embedded hardware, which is critical for field deployment.
Real-World Implementation Scenario
In a real agricultural deployment, this system can be integrated with a side-dressing tractor or fertilizer application machine to enable real-time, plant-specific nitrogen spraying.
A camera mounted on the side of the tractor continuously captures images of corn leaves as the vehicle moves along crop rows. These images are sent in real time to an on-board edge computing device, such as an NVIDIA Jetson Nano or Raspberry Pi, installed on the machine.
The edge computer runs the trained deep learning model locally, allowing low-latency, offline inference without dependence on cloud connectivity. Each image is analyzed to determine the nitrogen deficiency level of the plant. Based on the model’s output, a control algorithm computes the required fertilizer dosage.
The computed decision is then converted into actuation commands, which are sent to electronically controlled sprayers or solenoid valves mounted on the tractor. This enables nitrogen fertilizer to be applied only where and in the amount needed, rather than uniformly across the field.
Significance and Future Scope
This project demonstrates the feasibility of transforming AI-based crop health analysis into actionable agricultural decisions. While the current implementation focuses on image classification and decision communication, the system architecture is fully extensible.
Future work may include:
- Integration with variable-rate sprayers or pumps
- Deployment on autonomous ground vehicles or drones
- Multi-nutrient deficiency detection
- Field validation under real environmental conditions
Technologies Used
- PyTorch (ResNet-18)
- Flask (Web Application)
- ESP8266 (IoT Communication)
- OLED Display
- Python, HTML, CSS
This mini-project serves as a foundational step toward AI-driven precision farming, demonstrating how deep learning, edge computing, and embedded systems can work together to enable efficient, sustainable agricultural practices.
Model Evaluation Metrics
The nitrogen deficiency classification model was evaluated using precision, recall, F1-score, and a confusion matrix to understand how reliably the system can support AI-driven, need-based fertilizer decisions.
- N0 (High Nitrogen Deficiency) shows strong performance (Precision = 0.793, Recall = 0.863, F1-score = 0.826). The high recall indicates that most severely nitrogen-deficient plants are correctly identified. This is particularly important in precision agriculture, as missing highly deficient plants would directly affect crop growth and yield.
- N75 (Mild Nitrogen Deficiency) exhibits lower performance (Precision = 0.600, Recall = 0.300, F1-score = 0.400). The confusion matrix indicates that many mildly deficient samples are misclassified as either high deficiency (N0) or no deficiency (NFull). This reflects the visual similarity of intermediate nitrogen stress levels and highlights a known challenge in fine-grained nutrient deficiency classification.
- NFull (No Nitrogen Deficiency) achieves very high recall (0.938) with a solid F1-score (0.777), meaning the model is highly reliable at identifying healthy plants where no fertilizer application is required. This directly supports the system’s goal of avoiding unnecessary nitrogen spraying, reducing cost and environmental impact.
Overall, the confusion matrix shows that the model performs best at detecting extreme conditions—high deficiency (N0) and no deficiency (NFull)—which are the most critical for real-world actuation decisions. Despite limitations in mild deficiency detection, the results validate the feasibility of using computer vision–based AI models for actionable, plant-specific nitrogen management in precision farming systems.
Explainable AI Using Grad-CAM
To ensure the transparency and interpretability of the nitrogen deficiency detection model, Grad-CAM (Gradient-weighted Class Activation Mapping) was employed. Grad-CAM is a visualization technique that highlights the regions of an input image that are most influential in the model’s decision-making process.
In this project, Grad-CAM generates heatmaps over corn leaf images, indicating the specific leaf areas that the ResNet-18 model focuses on when classifying nitrogen deficiency levels. For instance:
- In N0 (high deficiency) leaves, Grad-CAM highlights the most chlorotic (yellowed) regions, showing that the model correctly identifies the stressed areas.
- For NFull (healthy) leaves, the heatmaps emphasize uniformly green regions, reinforcing that the model is basing its predictions on healthy leaf features rather than irrelevant background.
- In N75 (mild deficiency), Grad-CAM can reveal why some samples are misclassified, as the highlighted areas may overlap with both healthy and severely deficient patterns, reflecting the visual ambiguity in intermediate deficiency levels.