Real-time wildlife detection for crop protection β runs on a $35 Raspberry Pi, no internet required.
π€ Live Demo Β· π Colab Notebook Β· π Report Bug Β· π‘ Request Feature
- π― Problem & Solution
- π¦ Classes Detected
- β¨ Features
- π Performance
- πΎ Real-World Impact
- ποΈ Architecture
- π Quick Start
- π± Deployment Targets
- π οΈ Tech Stack
- π¦ Project Structure
- π Training Details
- π§ Local Installation
- π€ Contributing
- π License
- π¨βπ» Author
- β Acknowledgments
Indian farmers lose 20-40% of crops annually to wildlife attacks β elephants trample fields, wild boars uproot vegetables, nilgai destroy wheat overnight. Traditional solutions like fencing (βΉ50,000/hectare) and night guards (βΉ8,000/month) are expensive and ineffective.
CropGuardAI deploys on a $35 Raspberry Pi with solar power to provide 24/7 automated wildlife detection. When animals approach, it sends SMS alerts to farmers and triggers deterrent lights β all without internet connectivity.
| Before CropGuardAI | After CropGuardAI |
|---|---|
| π« 40% crop loss | β 80% reduction |
| π° βΉ8,000/month guard | π° βΉ0/month operation |
| π΄ Human fatigue | β AI never sleeps |
| π‘ Internet required | β Completely offline |
| Class | Emoji | Threat Level |
|---|---|---|
| Boar | π | π΄ High β Uproots crops |
| Cow | π | π‘ Medium β Grazing damage |
| Dog | π | π’ Low β Stray animals |
| Elephant | π | π΄ Critical β Destroys entire field |
| Human | π€ | π‘ Medium β Potential theft |
| Monkey | π | π‘ Medium β Fruit destruction |
| Nilgai | π¦ | π΄ High β Nocturnal crop raider |
| Feature | Description |
|---|---|
| π¦ Ultra-Lightweight | 965 KB model β fits on microcontrollers |
| π― High Accuracy | 88.57% on validation set |
| β‘ Real-Time | <100ms inference on Raspberry Pi 4 |
| π Offline-First | Zero internet dependency |
| βοΈ Solar Powered | Runs on 5W β compatible with solar panels |
| π± SMS Alerts | Auto-notifies farmers of intrusions |
| π Cross-Platform | Raspberry Pi, Android, iOS, Web, ESP32 |
| π Open Source | MIT License β free for anyone |
| Metric | Value |
|---|---|
| Architecture | MobileNetV2 (Ξ±=0.35) |
| Input Size | 160Γ160Γ3 |
| Total Parameters | 492,647 |
| Trainable Parameters | 82,439 |
| Model Size (FP32) | 1.87 MB |
| Model Size (FP16) | 965 KB β |
| Model Size (INT8) | 685 KB |
| Validation Accuracy | 88.57% |
| Inference Time (RPi4) | <100ms |
| Power Consumption | ~5W |
| Version | Size | Accuracy | Use Case |
|---|---|---|---|
| Keras (H5) | 2.93 MB | 88.57% | Training/Development |
| FP32 TFLite | 1.87 MB | 88.57% | Desktop/Server |
| FP16 TFLite | 965 KB | 88.57% | Production β |
| INT8 TFLite | 685 KB | 80.00% | Microcontrollers |
Model convergence showing training and validation accuracy/loss over epochs
Confusion matrix across all 7 wildlife classes
| Item | Traditional | CropGuardAI | Savings |
|---|---|---|---|
| Setup Cost | βΉ50,000 (fencing) | βΉ5,000 (hardware) | βΉ45,000 |
| Monthly Cost | βΉ8,000 (guard) | βΉ0 | βΉ8,000 |
| Crop Loss/Year | βΉ40,000 | βΉ8,000 | βΉ32,000 |
| Annual Savings | β | β | βΉ1,28,000 |
| Component | Cost | Purpose |
|---|---|---|
| Raspberry Pi 4 | βΉ3,000 | AI Processing |
| Camera Module | βΉ500 | Image Capture |
| Solar Panel + Battery | βΉ1,500 | Power Supply |
βββββββββββββββββββββββββββββββββββββββββββββββ
β INPUT (160Γ160Γ3) β
β Wildlife Image from Camera β
ββββββββββββββββββββ¬βββββββββββββββββββββββββββ
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββ
β PREPROCESSING LAYER β
β mobilenet_v2.preprocess_input() β
β Scales to [-1, 1] range β
ββββββββββββββββββββ¬βββββββββββββββββββββββββββ
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββ
β MOBILENETV2 BACKBONE (Ξ±=0.35) β
β π Frozen (410,208 params) β
β Transfer Learning β
ββββββββββββββββββββ¬βββββββββββββββββββββββββββ
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββ
β GLOBAL AVERAGE POOLING (1280) β
ββββββββββββββββββββ¬βββββββββββββββββββββββββββ
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββ
β DROPOUT (0.3) β
ββββββββββββββββββββ¬βββββββββββββββββββββββββββ
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββ
β DENSE (64, ReLU) β
β 81,984 params β
ββββββββββββββββββββ¬βββββββββββββββββββββββββββ
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββ
β DENSE (7, Softmax) β OUTPUT β
β 455 params β
βββββββββββββββββββββββββββββββββββββββββββββββ
βΌ
π Boar π Cow π Dog π Elephant
π€ Human π Monkey π¦ Nilgai
pip install tensorflow numpy pillowimport tensorflow as tf, numpy as np
from PIL import Image
# Load model
interpreter = tf.lite.Interpreter(model_path="animal_classifier_fp16.tflite")
interpreter.allocate_tensors()
# Predict
img = np.expand_dims(np.array(Image.open("animal.jpg").resize((160,160))).astype(np.float32), 0)
interpreter.set_tensor(interpreter.get_input_details()[0]['index'], img)
interpreter.invoke()
pred = interpreter.get_tensor(interpreter.get_output_details()[0]['index'])[0]
classes = ["boar","cow","dog","elephant","human","monkey","nilgai"]
print(f"Detected: {classes[np.argmax(pred)]} ({np.max(pred)*100:.1f}%)")| Platform | Model | Size | Accuracy | Status |
|---|---|---|---|---|
| π₯§ Raspberry Pi 4 | FP16 | 965 KB | 88.57% | β Ready |
| π± Android/iOS | FP16 | 965 KB | 88.57% | β Ready |
| π Web Browser | FP16 | 965 KB | 88.57% | β Ready |
| π ESP32-S3 | INT8 | 685 KB | 80.00% | |
| π Edge TPU | INT8 | 685 KB | 80.00% |
| Category | Technology |
|---|---|
| ML Framework | TensorFlow 2.21, Keras |
| Model Architecture | MobileNetV2 (Ξ±=0.35) |
| Transfer Learning | ImageNet Pre-trained Weights |
| Quantization | FP16 (Recommended), INT8 |
| Deployment | TFLite, Hugging Face Spaces |
| Training Platform | Google Colab (T4 GPU) |
| Dataset Source | Roboflow (Custom Annotated) |
| Web Demo | Gradio 5.0 |
| Languages | Python, TFLite |
CropGuardAI/
β
βββ π models/ # Trained models
β βββ animal_classifier_fp16.tflite # Production model (965 KB) β
β βββ animal_classifier_fp32.tflite # Full precision (1.87 MB)
β βββ animal_classifier_int8.tflite # Ultra-compact (685 KB)
β βββ animal_classifier.h5 # Keras source (2.93 MB)
β βββ labels.json # Class label mapping
β
βββ π notebooks/ # Jupyter/Colab notebooks
β βββ cropguardai_training.ipynb # Complete training pipeline
β
βββ π examples/ # Usage examples
β βββ raspberry_pi_inference.py # Raspberry Pi deployment
β βββ mobile_inference.py # Mobile/Desktop inference
β βββ web_inference.html # Browser demo
β
βββ π docs/ # Documentation & visuals
β βββ training_curves.png # Training history plots
β βββ confusion_matrix.png # Confusion matrix heatmap
β
βββ π README.md # Project documentation
βββ π LICENSE # MIT License
βββ π .gitignore # Git ignore rules
- Source: Roboflow β Crop Protection Animal Dataset
- Images: 525 annotated images
- Split: 80% Training (420), 20% Validation (105)
- Augmentation: Random Flip, Rotation, Zoom
Optimizer: Adam (lr=1e-3)
Loss: Categorical Crossentropy
Epochs: 30 (EarlyStopping patiance=8)
Batch Size: 32
Hardware: Google Colab T4 GPU (Free)
Training Time: ~3 minutes# Clone repository
git clone https://github.com/adity0208/CropGuardAI.git
cd CropGuardAI
# Install dependencies
pip install -r requirements.txt
# Download model
wget https://github.com/adity0208/CropGuardAI/raw/main/models/animal_classifier_fp16.tflite
# Run inference
python examples/mobile_inference.py --image path/to/animal.jpgContributions are welcome! Here's how:
- π΄ Fork the repository
- πΏ Create feature branch (
git checkout -b feature/amazing-feature) - πΎ Commit changes (
git commit -m 'Add amazing feature') - π Push to branch (
git push origin feature/amazing-feature) - π¬ Open Pull Request
Distributed under the MIT License. See LICENSE for more information.
MIT License β Free for personal, academic, and commercial use.
Aditya Kushwaha
- TensorFlow β ML Framework
- MobileNetV2 β Model Architecture
- Roboflow β Dataset Platform
- Hugging Face β Model Hosting
- Google Colab β Training Infrastructure
Built with β€οΈ for farmers everywhere

