An end-to-end AgroTech Machine Learning application that recommends the optimal crop to plant based on soil and environmental conditions.
Making the right crop choice is crucial for farmers to maximize yield and optimize resource usage. Smart Crop Prediction addresses this challenge by leveraging machine learning to analyze soil nutrients and environmental factors, providing data-driven recommendations for the best crop to plant.
Traditional crop selection often relies on intuition or outdated practices, leading to suboptimal harvests and resource waste. This application empowers farmers and agricultural professionals with:
- Instant predictions based on soil composition (N, P, K levels)
- Environmental analysis considering temperature, humidity, and rainfall
- Scientific backing through a trained ML model with 99.32% accuracy
- Algorithm: Random Forest Classifier (100 estimators)
- Training Data: 2,200 samples across 22 different crop types
- Features: Nitrogen (N), Phosphorus (P), Potassium (K), Temperature, Humidity, pH, Rainfall
- Accuracy: 99.32% on test set
| Initial Form | Prediction Result |
|---|---|
![]() |
![]() |
| Layer | Technology |
|---|---|
| Frontend | Next.js 16, React 19, Tailwind CSS 4, Axios |
| Backend | FastAPI, Python 3.11+, Uvicorn |
| Machine Learning | Scikit-Learn, Pandas, NumPy, Joblib |
| DevOps | Docker, Docker Compose |
| Data Visualization | Matplotlib, Seaborn (Jupyter Notebooks) |
The easiest way to get started. Just make sure you have Docker installed.
# Clone the repository
git clone https://github.com/cauegrassi7/smart-crop-prediction.git
cd smart-crop-prediction
# Build and run with Docker Compose
docker-compose up --buildOnce running:
- π Frontend: http://localhost:3000
- β‘ Backend API: http://localhost:8000
- π API Docs: http://localhost:8000/docs
# Navigate to backend directory
cd backend
# Create virtual environment
python -m venv venv
# Activate virtual environment
# On macOS/Linux:
source venv/bin/activate
# On Windows:
# venv\Scripts\activate
# Install dependencies
pip install -r requirements.txtThe model is pre-trained and included in the repository. If you want to retrain it:
# From the backend directory
python train_model.pyThis will generate the crop_prediction_model.pkl file in the models/ folder.
# From the backend directory
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000Open a new terminal:
# Navigate to frontend directory
cd frontend
# Install dependencies
npm install
# Start development server
npm run devsmart-crop-prediction/
β
βββ π backend/ # FastAPI Backend
β βββ app/
β β βββ __init__.py
β β βββ main.py # API routes & FastAPI app
β β βββ schemas.py # Pydantic data models
β βββ models/
β β βββ crop_prediction_model.pkl # Trained ML model
β βββ train_model.py # Model training script
β βββ requirements.txt # Python dependencies
β βββ Dockerfile
β
βββ π frontend/ # Next.js Frontend
β βββ app/
β β βββ page.tsx # Main application page
β β βββ layout.tsx # Root layout
β β βββ globals.css # Global styles
β βββ public/ # Static assets
β βββ package.json
β βββ Dockerfile
β
βββ π data/ # Dataset
β βββ Crop_recommendation.csv # Training data (2,200 samples)
β
βββ π notebooks/ # Jupyter Notebooks
β βββ 01_model_training.ipynb # EDA & model development
β
βββ docker-compose.yml # Multi-container orchestration
βββ README.md
The API documentation is automatically generated and available via Swagger UI when the backend is running:
π http://localhost:8000/docs
| Method | Endpoint | Description |
|---|---|---|
GET |
/ |
Health check & welcome message |
GET |
/health |
Health check for Docker/K8s |
POST |
/predict |
Get crop recommendation |
curl -X POST "http://localhost:8000/predict" \
-H "Content-Type: application/json" \
-d '{
"N": 90,
"P": 42,
"K": 43,
"temperature": 20.8,
"humidity": 82.0,
"ph": 6.5,
"rainfall": 202.9
}'{
"input": {
"N": 90.0,
"P": 42.0,
"K": 43.0,
"temperature": 20.8,
"humidity": 82.0,
"ph": 6.5,
"rainfall": 202.9
},
"recommended_crop": "rice"
}| Feature | Description | Unit |
|---|---|---|
| N | Nitrogen content in soil | kg/ha |
| P | Phosphorus content in soil | kg/ha |
| K | Potassium content in soil | kg/ha |
| Temperature | Average temperature | Β°C |
| Humidity | Relative humidity | % |
| pH | Soil pH level | 0-14 |
| Rainfall | Annual rainfall | mm |
The model can predict among 22 different crop types, including:
Rice, Wheat, Maize, Chickpea, Kidney Beans, Pigeon Peas, Moth Beans, Mung Bean, Black Gram, Lentil, Pomegranate, Banana, Mango, Grapes, Watermelon, Muskmelon, Apple, Orange, Papaya, Coconut, Cotton, Jute, Coffee
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Feel free to open issues or submit pull requests.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
Made with π for sustainable agriculture



