The Plant Growth Tracker is a Python package designed to help researchers and agronomists analyze plant growth by processing images and videos to calculate total plant areas and individual leaf areas. It leverages advanced image processing techniques and allows for custom training of segmentation models tailored to your dataset.
- Total Plant Area Calculation: Process images or videos to calculate the total area occupied by plants.
- Individual Leaf Area Calculation: Segment individual leaves and calculate their areas using a custom-trained model.
- Custom Model Training: Train a custom segmentation model using your own dataset for improved accuracy.
- Custom Preprocessing: Apply custom preprocessing steps to enhance image quality before analysis.
-
Python: Version 3.6 or higher.
-
Hardware: A GPU is recommended for training the custom model.
-
Dependencies: Install the required packages using:
pip install -r requirements.txt
requirements.txt:fastapi uvicorn[standard] opencv-python==4.7.0.72 numpy==1.24.4 pandas pydantic torch==2.0.1 torchvision==0.15.2 pillow pillow-heif scikit-image transformers tqdm matplotlib git+https://github.com/facebookresearch/segment-anything.git
pip install plant-growth-tracker-
Clone the Repository:
git clone https://github.com/yourusername/plant-growth-tracker.git
-
Navigate to the Project Directory:
cd plant-growth-tracker -
Install the Package in Editable Mode:
pip install -e .Note: Installing in editable mode (
-e) allows you to make changes to the source code without reinstalling the package.
To calculate the total plant area in images:
from plant_growth_tracker import process_images
image_folder_path = 'path/to/your/image_folder'
df = process_images(
image_folder_path=image_folder_path,
segmentation_type='total_plant_area'
)
# Save or print the results
print(df)
df.to_csv('total_plant_area_results.csv', index=False)To calculate individual leaf areas, you need to train a custom model first (see Training a Custom Model). Once you have a trained model, use it as follows:
from plant_growth_tracker import process_images
image_folder_path = 'path/to/your/image_folder'
custom_model_paths = {
'model_path': 'path/to/save/your_trained_model',
'processor_path': 'path/to/save/your_trained_processor'
}
df = process_images(
image_folder_path=image_folder_path,
segmentation_type='individual_leaf_area',
custom_model_paths=custom_model_paths
)
# Save or print the results
print(df)
df.to_csv('individual_leaf_area_results.csv', index=False)Before processing images, you can apply custom preprocessing steps to enhance image quality. This is particularly useful for improving segmentation accuracy.
from plant_growth_tracker import process_images, custom_preprocess
image_folder_path = 'path/to/your/image_folder'
df = process_images(
image_folder_path=image_folder_path,
segmentation_type='total_plant_area',
preprocessing_function=custom_preprocess # Apply custom preprocessing
)
# Save or print the results
print(df)
df.to_csv('total_plant_area_results_custom.csv', index=False)Note: The
custom_preprocessfunction allows you to define and apply your own image preprocessing pipeline. Refer to the Custom Preprocessing section for more details.
Organize your images and corresponding masks in separate directories:
dataset/
├── images/
│ ├── image1.jpg
│ ├── image2.jpg
│ └── ...
└── masks/
├── image1.png
├── image2.png
└── ...
- Images: Place all your training images in the
imagesdirectory. - Masks: Create binary masks for each image where the leaves are white (pixel value 255) and the background is black (pixel value 0).
- Naming Convention: Ensure that each mask filename corresponds exactly to its image filename (e.g.,
image1.jpgandimage1.png).
-
Create a Training Script
Create a Python script named
train_model.py:from plant_growth_tracker.services.model_training import train_custom_model import torch images_dir = 'dataset/images' masks_dir = 'dataset/masks' model_save_path = 'trained_model' processor_save_path = 'trained_processor' train_custom_model( images_dir=images_dir, masks_dir=masks_dir, model_save_path=model_save_path, processor_save_path=processor_save_path, num_epochs=10, batch_size=2, learning_rate=1e-5, device='cuda' if torch.cuda.is_available() else 'cpu' )
-
Run the Training Script
Execute the script:
python train_model.py
- Parameters:
num_epochs: Number of times the entire dataset is passed through the model.batch_size: Number of samples processed before the model is updated.learning_rate: Step size at each iteration while moving toward a minimum of a loss function.device: Specify'cuda'for GPU acceleration or'cpu'for CPU.
- Parameters:
-
Monitor Training
- The script will output the training loss after each epoch.
- Adjust parameters based on the training performance and available computational resources.
After training, use your custom model to process new images:
from plant_growth_tracker import process_images
image_folder_path = 'path/to/new/images'
custom_model_paths = {
'model_path': 'trained_model',
'processor_path': 'trained_processor'
}
df = process_images(
image_folder_path=image_folder_path,
segmentation_type='individual_leaf_area',
custom_model_paths=custom_model_paths
)
# Save or print the results
print(df)
df.to_csv('individual_leaf_area_results.csv', index=False)custom_model_paths: Dictionary containing the paths to your trained model and processor.- Output: The
process_imagesfunction returns a pandas DataFrame with the results.
To perform a custom analysis using the package, execute the test_package.py script from your terminal:
python tests/test_package.py- Description: This script runs custom preprocessing and model analysis on your dataset, generating results and visualizations.
- Output:
- CSV File:
tests/output/plant_area_results.csvcontaining the analysis results. - Visualizations: Saved in the
tests/output/visualizations/directory, showcasing preprocessing steps and detected contours.
- CSV File:
-
GPU Recommendation: For training and inference on large datasets, a GPU is highly recommended to speed up processing.
-
Data Quality: The accuracy of the segmentation depends on the quality of your training data. Ensure masks are accurately annotated.
-
Parameter Adjustment: You may need to adjust thresholds and parameters in the code to suit your specific dataset.
-
Dependencies: Ensure all required packages are installed. Use:
pip install -r requirements.txt
-
Error Handling: The package includes basic error handling. If you encounter issues, please check that all file paths and parameters are correct.
This tool package has been utilized in the following publication:
-
Arabidopsis transcriptome responses to low water potential using high-throughput plate assays
Citation:
Gonzalez, S., Swift, J., Yaaran, A., Xu, J., Miller, C., Illouz-Eliaz, N., Nery, J. R., Busch, W., Zait, Y., & Ecker, J. R. (2023). Arabidopsis transcriptome responses to low water potential using high-throughput plate assays. eLife, 12, e84747. https://doi.org/10.7554/eLife.84747
This project is licensed under the MIT License. See the LICENSE file for details.