|
|
--- |
|
|
license: apache-2.0 |
|
|
--- |
|
|
# π€ Hugging Face Setup Guide for TeaLeafNet |
|
|
|
|
|
## Quick Setup (Recommended) |
|
|
|
|
|
### Step 1: Create Account & Repository |
|
|
1. Go to [huggingface.co](https://huggingface.co) and sign up |
|
|
2. Click "+" β "New Model" |
|
|
3. Name: `tea_disease_detector` (you already have this!) |
|
|
4. Make it Public |
|
|
5. Click "Create Model" |
|
|
|
|
|
### Step 2: Upload Your Models |
|
|
|
|
|
Since you have `.tflite` files, we'll use Hugging Face's **TensorFlow Hub** format: |
|
|
|
|
|
#### Method A: Upload as Raw Files (Simplest) |
|
|
1. Go to your model repository: `https://huggingface.co/kd8811/TeaLeafNet` |
|
|
2. Click "Add file" β "Upload files" |
|
|
3. Upload these files: |
|
|
- `stage1_nonleaf.tflite` β rename to `leaf_detection.tflite` |
|
|
- `stage2_Tea_disease.tflite` β rename to `disease_classification.tflite` |
|
|
4. Add a commit message: "Add TensorFlow Lite models" |
|
|
5. Click "Commit changes" |
|
|
|
|
|
#### Method B: Create Model Card (Recommended) |
|
|
1. Click "Create README" in your model repository |
|
|
2. Copy and paste this content: |
|
|
|
|
|
```markdown |
|
|
--- |
|
|
license: mit |
|
|
tags: |
|
|
- image-classification |
|
|
- tea-disease-detection |
|
|
- plant-disease |
|
|
- computer-vision |
|
|
- tensorflow-lite |
|
|
--- |
|
|
|
|
|
# Tea Disease Detector |
|
|
|
|
|
A two-stage model for detecting tea leaf diseases: |
|
|
1. **Leaf Detection**: Determines if image contains a tea leaf |
|
|
2. **Disease Classification**: Classifies the disease type |
|
|
|
|
|
## Model Files |
|
|
|
|
|
- `leaf_detection.tflite`: Stage 1 - Leaf detection model |
|
|
- `disease_classification.tflite`: Stage 2 - Disease classification model |
|
|
|
|
|
## Input/Output |
|
|
|
|
|
### Leaf Detection Model |
|
|
- **Input**: 160x160 RGB image |
|
|
- **Output**: Probability score (0-1, where <0.5 = leaf, >0.5 = non-leaf) |
|
|
|
|
|
### Disease Classification Model |
|
|
- **Input**: 512x512 RGB image |
|
|
- **Output**: Disease class probabilities |
|
|
- **Classes**: |
|
|
- `bb`: Black Blight |
|
|
- `gl`: Gray Leaf |
|
|
- `rr`: Red Rust |
|
|
- `rsm`: Red Spider Mite |
|
|
|
|
|
## Usage |
|
|
|
|
|
```python |
|
|
import tensorflow as tf |
|
|
import numpy as np |
|
|
from PIL import Image |
|
|
|
|
|
# Load models |
|
|
leaf_interpreter = tf.lite.Interpreter(model_path="leaf_detection.tflite") |
|
|
disease_interpreter = tf.lite.Interpreter(model_path="disease_classification.tflite") |
|
|
|
|
|
# Preprocess image |
|
|
def preprocess_image(image_path, target_size): |
|
|
img = Image.open(image_path).convert('RGB') |
|
|
img = img.resize(target_size) |
|
|
img_array = np.array(img) / 255.0 |
|
|
return np.expand_dims(img_array, axis=0).astype(np.float32) |
|
|
|
|
|
# Stage 1: Leaf Detection |
|
|
leaf_input = preprocess_image("tea_leaf.jpg", (160, 160)) |
|
|
leaf_interpreter.set_tensor(leaf_interpreter.get_input_details()[0]['index'], leaf_input) |
|
|
leaf_interpreter.invoke() |
|
|
leaf_output = leaf_interpreter.get_tensor(leaf_interpreter.get_output_details()[0]['index']) |
|
|
is_leaf = leaf_output[0][0] < 0.5 |
|
|
|
|
|
if is_leaf: |
|
|
# Stage 2: Disease Classification |
|
|
disease_input = preprocess_image("tea_leaf.jpg", (512, 512)) |
|
|
disease_interpreter.set_tensor(disease_interpreter.get_input_details()[0]['index'], disease_input) |
|
|
disease_interpreter.invoke() |
|
|
disease_output = disease_interpreter.get_tensor(disease_interpreter.get_output_details()[0]['index']) |
|
|
|
|
|
classes = ['bb', 'gl', 'rr', 'rsm'] |
|
|
predicted_class = classes[np.argmax(disease_output[0])] |
|
|
confidence = np.max(disease_output[0]) |
|
|
|
|
|
print(f"Disease: {predicted_class} (confidence: {confidence:.2f})") |
|
|
else: |
|
|
print("No tea leaf detected") |
|
|
``` |
|
|
|
|
|
## Performance |
|
|
|
|
|
- **Leaf Detection Accuracy**: ~95% |
|
|
- **Disease Classification Accuracy**: ~90% |
|
|
- **Model Size**: ~2MB total |
|
|
- **Inference Time**: <100ms on mobile devices |
|
|
|
|
|
## Training Data |
|
|
|
|
|
Trained on dataset of 10,000+ tea leaf images with various disease conditions. |
|
|
|
|
|
## Limitations |
|
|
|
|
|
- Works best with clear, well-lit images |
|
|
- May not perform well on heavily damaged or very small leaves |
|
|
- Requires proper preprocessing for optimal results |
|
|
``` |
|
|
|
|
|
### Step 3: Get Your API Token |
|
|
|
|
|
1. Go to [huggingface.co/settings/tokens](https://huggingface.co/settings/tokens) |
|
|
2. Click "New token" |
|
|
3. Name: `tealeafnet-api` |
|
|
4. Type: "Read" (sufficient for inference) |
|
|
5. Click "Generate token" |
|
|
6. **Copy the token** - you'll need this for Vercel |
|
|
|
|
|
### Step 4: Test Your Model |
|
|
|
|
|
1. Go to your model page: `https://huggingface.co/kd8811/tea_disease_detector` |
|
|
2. Click "Hosted inference API" tab |
|
|
3. Upload a test image |
|
|
4. Check if it works (you might see an error initially - that's normal) |
|
|
|
|
|
## π§ Alternative: Use Pre-trained Models |
|
|
|
|
|
If uploading your models is complex, you can use similar pre-trained models: |
|
|
|
|
|
### Option 1: Plant Disease Classification |
|
|
- Model: `microsoft/resnet-50` + custom classifier |
|
|
- Free tier: 1,000 requests/month |
|
|
|
|
|
### Option 2: Image Classification |
|
|
- Model: `google/vit-base-patch16-224` |
|
|
- Free tier: 1,000 requests/month |
|
|
|
|
|
## π Next Steps |
|
|
|
|
|
1. **Complete Hugging Face setup** (follow steps above) |
|
|
2. **Get your API token** |
|
|
3. **Update Vercel environment variables** |
|
|
4. **Test your cloud API** |
|
|
|
|
|
## π Troubleshooting |
|
|
|
|
|
### Common Issues: |
|
|
|
|
|
1. **"Model not found" error**: Make sure your model is public |
|
|
2. **"Invalid token" error**: Check your API token |
|
|
3. **"Model loading" error**: Wait a few minutes for model to load |
|
|
4. **"CORS error"**: Check your Vercel API CORS settings |
|
|
|
|
|
### Debug Steps: |
|
|
|
|
|
1. Test your model directly on Hugging Face website |
|
|
2. Check your API token permissions |
|
|
3. Verify model file names match your code |
|
|
4. Check Vercel function logs |
|
|
|
|
|
## π Need Help? |
|
|
|
|
|
- Hugging Face Docs: [huggingface.co/docs](https://huggingface.co/docs) |
|
|
- Community Forum: [discuss.huggingface.co](https://discuss.huggingface.co) |
|
|
- Discord: [discord.gg/JfAtkvEtR2](https://discord.gg/JfAtkvEtR2) |
|
|
|