The recommended processing pipeline uses a detection model with only one generic class ("insect"). The low input resolution enables a high inference speed, which is necessary to reliably track moving/flying insects. Images of the detected and tracked insects are cropped from synchronized HQ frames in real time.
By using the provided script for automated monitoring, cropped detections of individual insects are saved as .jpg files and relevant metadata is saved to .csv for each recording interval. The insect images can be classified in a subsequent step on your local PC, by using a classification model exported to ONNX format for faster CPU inference. The prediction results are added to the merged metadata .csv files for post-processing and analysis in the last step.
Optional: Set up an isolated Python environment
You can create a virtual environment before installing the required packages for YOLOv5 to avoid version and dependency conflicts of the packages, especially if you are working on different Python projects.
Navigate to the
Create a new Python environment (folder in your current directory):
Activate the environment by running:
If you cannot run the activate script because of your Execution Policy settings, open a new PowerShell as administrator and run the following:
Now all packages will be installed only in the virtual environment, which you have to activate everytime you want to run a script that depends on these packages.
You can deactivate the virtual environment by running:
- Create the new folder
yolov5fork and extract it to the
Open a new Terminal (PowerShell) and navigate to the
Install the required packages by running:
If you are running the image classification on a computer with CUDA-enabled GPU, please edit the
requirements.txtfile and change
onnxruntime-gpubefore installing the packages.
Run image classification¶
We will use the modified
script from the custom
together with the provided classification model
to classify all insect images in the
insect-detect/data folder (camera trap output)
and add the prediction results to the merged metadata .csv files.
insect-detect-mlrepo and extract it to the
- Copy your
insect-detect/datafolder, saved from the Raspberry Pi's SD card to the
YOLOv5-clsfolder. Make sure that only cropped detections are present! If you additionally saved full HQ frames, delete them before running the classification script.
Navigate to the
YOLOv5-clsfolder and start the classification script by running:
--nameof your prediction run accordingly, e.g. by including information about the camera trap location and collection date of the images. Instead of copying the
insect-detect/datafolder to the
YOLOv5-clsfolder, you can also insert its full path after
--sort-top1sort images to folders with predicted top1 class as folder name and do not write results on to image as text (which is the default configuration)
--sort-probsort images first by probability and then by top1 class (requires
--concat-csvconcatenate all metadata .csv files and append classification results to new columns
--new-csvcreate a new .csv file with classification results, e.g. if no metadata .csv files are available
--save-txtto save the classification results to individual .txt files for each image
Image Not Found
While running the classification script, in some cases you might run into the following error:
This error can be caused by corrupt .jpg images, that are rarely generated during image capture. Run the
check_images.pyscript to find corrupt images in the data folder and move them to a new folder with:
After removing the corrupt .jpg images, the classification script should now run without throwing an error.
All results are saved to
*metadata_classified.csv still contains multiple rows for each
tracked insect (=
track_ID). During post-processing and analysis
in the last step, the respective class with the overall highest probability is
calculated for each tracked insect. This will create the final .csv file, in
which each row corresponds to an individual tracked insect.
If you used
--sort-top1 as optional argument, the insect images are sorted
to folders with the predicted top1 class as folder name. This allows for a
quick identification of edge cases and these images can be used to
classification model. By using
--sort-prob additionally, the images are
sorted by top1 probability first and then by top1 class. This can give you
more information about cases where the model is unsure about its prediction.
Adding images with a low probability to your training dataset can increase
classification accuracy over time.