Post process
Post-processing functions.
detection_folder_separation(json_file, img_path, destination_path, confidence_threshold)
¶
Processes detection data from a JSON file to sort images into 'Animal' or 'No_animal' directories based on detection categories and confidence levels.
This function reads a JSON formatted file containing annotations of image detections. Each image is checked for detections with category '0' and a confidence level above the specified threshold. If such detections are found, the image is categorized under 'Animal'. Images without any category '0' detections above the threshold, including those with no detections at all, are categorized under 'No_animal'.
Parameters: - json_file (str): Path to the JSON file containing detection data. - destination_path (str): Base path where 'Animal' and 'No_animal' folders will be created and into which images will be sorted and copied. - source_images_directory (str): Path to the directory containing the source images to be processed. - confidence_threshold (float): The confidence threshold to consider a detection as valid.
Effects:
- Reads from the specified json_file
.
- Copies files from source_images_directory
to either destination_path/Animal
or
destination_path/No_animal
based on the detection data and confidence level.
Note:
- The function assumes that the JSON file structure includes keys 'annotations', each containing
'img_id', 'bbox', 'category', and 'confidence'. It does not handle missing keys or unexpected
JSON structures and may raise an exception in such cases.
- Directories Animal
and No_animal
are created if they do not already exist.
- Images are copied, not moved; original images remain in the source directory.
Source code in PytorchWildlife/utils/post_process.py
save_crop_images(results, output_dir, input_dir=None, overwrite=False)
¶
Save cropped images based on the detection bounding boxes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
results
|
list
|
Detection results containing image ID and detections. |
required |
output_dir
|
str
|
Directory to save the cropped images. |
required |
input_dir
|
str
|
Directory containing the input images. Default to None. |
None
|
overwrite
|
bool
|
Whether overwriting existing image folders. Default to False. |
False
|
Source code in PytorchWildlife/utils/post_process.py
save_detection_classification_json(det_results, clf_results, output_path, det_categories=None, clf_categories=None, exclude_file_path=None)
¶
Save classification results to a JSON file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
det_results
|
list
|
Detection results containing image ID, bounding boxes, detection category, and confidence. |
required |
clf_results
|
list
|
classification results containing image ID, classification category, and confidence. |
required |
output_path
|
str
|
Path to save the output JSON file. |
required |
det_categories
|
list
|
List of categories for detected objects. Defaults to None. |
None
|
clf_categories
|
list
|
List of categories for classified objects. Defaults to None. |
None
|
exclude_file_path
|
str
|
We can exclude the some path sections from the image ID. Defaults to None. |
None
|
Source code in PytorchWildlife/utils/post_process.py
save_detection_classification_timelapse_json(det_results, clf_results, output_path, det_categories=None, clf_categories=None, exclude_file_path=None, info={'detector': 'megadetector_v5'})
¶
Save detection and classification results to a JSON file in the specified format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
det_results
|
list
|
Detection results containing image ID, bounding boxes, detection category, and confidence. |
required |
clf_results
|
list
|
Classification results containing image ID, classification category, and confidence. |
required |
output_path
|
str
|
Path to save the output JSON file. |
required |
det_categories
|
dict
|
Dictionary of categories for detected objects. Defaults to None. |
None
|
clf_categories
|
dict
|
Dictionary of categories for classified objects. Defaults to None. |
None
|
exclude_file_path
|
str
|
We can exclude the some path sections from the image ID. Defaults to None. |
None
|
Source code in PytorchWildlife/utils/post_process.py
save_detection_images(results, output_dir, input_dir=None, overwrite=False)
¶
Save detected images with bounding boxes and labels annotated.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
results
|
list or dict
|
Detection results containing image ID, detections, and labels. |
required |
output_dir
|
str
|
Directory to save the annotated images. |
required |
input_dir
|
str
|
Directory containing the input images. Default to None. |
None
|
overwrite
|
bool
|
Whether overwriting existing image folders. Default to False. |
False
|
Source code in PytorchWildlife/utils/post_process.py
save_detection_images_dots(results, output_dir, input_dir=None, overwrite=False)
¶
Save detected images with bounding boxes and labels annotated.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
results
|
list or dict
|
Detection results containing image ID, detections, and labels. |
required |
output_dir
|
str
|
Directory to save the annotated images. |
required |
input_dir
|
str
|
Directory containing the input images. Default to None. |
None
|
overwrite
|
bool
|
Whether overwriting existing image folders. Default to False. |
False
|
Source code in PytorchWildlife/utils/post_process.py
save_detection_json(det_results, output_dir, categories=None, exclude_category_ids=[], exclude_file_path=None)
¶
Save detection results to a JSON file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
det_results
|
list
|
Detection results containing image ID, bounding boxes, category, and confidence. |
required |
output_dir
|
str
|
Path to save the output JSON file. |
required |
categories
|
list
|
List of categories for detected objects. Defaults to None. |
None
|
exclude_category_ids
|
list
|
List of category IDs to exclude from the output. Defaults to []. Category IDs can be found in the definition of each models. |
[]
|
exclude_file_path
|
str
|
We can exclude the some path sections from the image ID. Defaults to None. |
None
|
Source code in PytorchWildlife/utils/post_process.py
save_detection_json_as_dots(det_results, output_dir, categories=None, exclude_category_ids=[], exclude_file_path=None)
¶
Save detection results to a JSON file in dots format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
det_results
|
list
|
Detection results containing image ID, bounding boxes, category, and confidence. |
required |
output_dir
|
str
|
Path to save the output JSON file. |
required |
categories
|
list
|
List of categories for detected objects. Defaults to None. |
None
|
exclude_category_ids
|
list
|
List of category IDs to exclude from the output. Defaults to []. Category IDs can be found in the definition of each models. |
[]
|
exclude_file_path
|
str
|
We can exclude the some path sections from the image ID. Defaults to None. |
None
|
Source code in PytorchWildlife/utils/post_process.py
save_detection_timelapse_json(det_results, output_dir, categories=None, exclude_category_ids=[], exclude_file_path=None, info={'detector': 'megadetector_v5'})
¶
Save detection results to a JSON file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
det_results
|
list
|
Detection results containing image ID, bounding boxes, category, and confidence. |
required |
output_dir
|
str
|
Path to save the output JSON file. |
required |
categories
|
list
|
List of categories for detected objects. Defaults to None. |
None
|
exclude_category_ids
|
list
|
List of category IDs to exclude from the output. Defaults to []. Category IDs can be found in the definition of each models. |
[]
|
exclude_file_path
|
str
|
Some time, Timelapse has path issues. We can exclude the some path sections from the image ID. Defaults to None. |
None
|
info
|
dict
|
Default Timelapse info. Defaults to {"detector": "megadetector_v5}. |
{'detector': 'megadetector_v5'}
|