Allow inference on multiple individual videos via `sleap-track`
Discussed in https://github.com/talmolab/sleap/discussions/1439
Problem Description
Originally posted by roomrys August 4, 2023
Currently, to run inference on multiple videos via the sleap-track command, users need to call this command many times either manually or in a script. It might be a nice feature to allow multiple videos/inputs.
Feature Proposal
- Add an option for a folder of files in the cli instead of only an individual file path
- Later expand this to also include a csv of file paths
Implementation Details
Currently, sleap_track takes in an argument data_path from the command line that is the file path to a .slp file, this means that the user must run this function once for every video they wish to run an inference on either manually or through a script. We would like to allow the argument data_path to also take in a path to a folder of .slp files and run an inference on each of these files. This will all be acomplished in sleap/nn/inference.py
1. Create an optional flag to the cli to specify if data_path is a folder
- [ ] Add this component to the
_make_cli_parser()function - [ ] Also add a description to help docs https://github.com/talmolab/sleap/blob/43a4f13ab28d2726af4a9c1858e8e2cde06d53cc/sleap/nn/inference.py#L5051-L5068
2. Make data_path a list to enable iteration
- [ ] Check if the flag was called in args to communicate that args.data_path is a folder
- [ ] If it is, enter the folder and add all compatible files to
data_path - [ ] Else,
data_path = [args.data_path]
Note: The "labels" cli argument has been deprecated and will not need to be edited to accomodate this new function.
https://github.com/talmolab/sleap/blob/43a4f13ab28d2726af4a9c1858e8e2cde06d53cc/sleap/nn/inference.py#L5292-L5296
3. Add a loop to file loading lines
- [ ] Iterate through
data_path. The loop will encompass the entire code section shown below. - [ ] Change
providerto a list to store a value for each item indata_pathhttps://github.com/talmolab/sleap/blob/43a4f13ab28d2726af4a9c1858e8e2cde06d53cc/sleap/nn/inference.py#L5304-L5328
4. Add a loop to main() for running inference and tracking
-
[ ] Iterate through
data_pathin the section of main shown below. The loop will start at line 5476, before we run the inference but after the predictor.tracker is set. https://github.com/talmolab/sleap/blob/43a4f13ab28d2726af4a9c1858e8e2cde06d53cc/sleap/nn/inference.py#L5473-L5485 -
[ ] transplate the following lines of code into the above loop. This will need to be run for each item in
data_pathhttps://github.com/talmolab/sleap/blob/43a4f13ab28d2726af4a9c1858e8e2cde06d53cc/sleap/nn/inference.py#L5510-L5541
5. Add an aditional loop to main() for just running tracking
-
[ ] Iterate through
data_pathfor the following code. The loop will start after theelifand contain the rest of the attatched lines. https://github.com/talmolab/sleap/blob/43a4f13ab28d2726af4a9c1858e8e2cde06d53cc/sleap/nn/inference.py#L5487-L5500 -
[ ] Again, we will have to transplant the following lines of code into the loop. https://github.com/talmolab/sleap/blob/43a4f13ab28d2726af4a9c1858e8e2cde06d53cc/sleap/nn/inference.py#L5510-L5541
Great job @emdavis02!
- We could try it with and without the additional argument to determine if the input is a directory. It might be sufficient to use
Path.isdir()andPath.isfile()documented here. - There is either inference alone, or inference with tracking.
- Make sure our current implementation of the CLI works so that these changes are backwards-compatible.
- Please add examples of intended use cases and test that the new implementation behaves as expected. These examples will be added to the CLI documentation.
- Add necessary tests for
inference.pyto make sure changes are covered.
- We could try it with and without the additional argument to determine if the input is a directory. It might be sufficient to use
os.path.isdir()and os.path.isfile()` documented here.
Just jumping in to say: please use pathlib instead of os.path APIs!
Thanks! Here is the correspondence between os.path and Path: correspondence to tools in the os module
Long awaited, but finally integrated. This issue has been fixed in the SLEAP 1.4.1 release!