diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/NOTICE b/NOTICE new file mode 100644 index 0000000..9523f34 --- /dev/null +++ b/NOTICE @@ -0,0 +1,6 @@ +Automated Camera Trapping Identification and Organization Network (ACTION) +Copyright 2023 Morgan Humphrey, David Humphrey + +This software contains code derived from https://github.com/parlaynu/megadetector-v5-onnx, used under the MIT License (https://github.com/parlaynu/megadetector-v5-onnx/blob/main/LICENSE), and https://github.com/Tianxiaomo/pytorch-YOLOv4, used under the Apache-2.0 License (https://github.com/Tianxiaomo/pytorch-YOLOv4/blob/master/License.txt). + +This software uses derived machine learning models (ONNX formatted) from https://github.com/tamim662/YOLO-Fish (GPL-3.0 - https://github.com/tamim662/YOLO-Fish/blob/main/LICENSE) and https://github.com/microsoft/CameraTraps (MIT - https://github.com/microsoft/CameraTraps/blob/main/LICENSE), which are also made available. diff --git a/README.md b/README.md index 79b8bfd..75ba67a 100644 --- a/README.md +++ b/README.md @@ -1,19 +1,29 @@ # Automated Camera Trapping Identification and Organization Network (ACTION) +## Overview + +Action is a Python-based tool designed to bring the power of AI computer vision models to camera trap video analysis. Using Action, it's possible to process hours of raw video into clip segments where animals or fish appear. + +Whether you're monitoring aquatic life with underwater cameras or tracking terrestrial wildlife, Action can help you save time tediously scanning footage manually. + +## How it Works + +Action takes one or more video files as input, along with several optional parameters to customize the process, and analyzes each video. Depending on the environment specified by the user, an appropriate object detection model is used: [YOLO-Fish v4](https://github.com/tamim662/YOLO-Fish) for aquatic videos, or [Megadetector v5](https://github.com/microsoft/CameraTraps/blob/main/megadetector.md) for terrestrial. The videos are then processed using the AI model, and whenever animals or fish are detected, a clip is created. At the end of the process, the clips represent all the detections in the raw footage. + ## Setup Action is written in Python and requires a number of dependencies and large machine learning models (~778M) to be installed and downloaded. The easiest way to use it is with the [pixi](https://prefix.dev/docs/pixi/overview) package manager. Pixi installs everything you need into a local `.pixi` folder (i.e., at the root of the project), without needing to modify your system. +### Installation Steps + 1. Clone this repo using `git clone https://github.com/humphrem/action.git`. NOTE: the repo includes large `.onnx` model files, which will get downloaded if you have [`git-lfs`](https://git-lfs.com/) installed (otherwise we'll get them as part of the setup below). 2. [Install pixi](https://prefix.dev/docs/pixi/overview#installation) 3. Start a terminal and navigate to the root of the Action project folder you just cloned, `cd action` 4. Enter the command `pixi run setup` to download, install, and setup everything you'll need -## Using Action - -### Pixi Shell +### Using the Pixi Shell Environment Each time you want to use Action, you need to open a terminal and navigate to the Action folder, then start a shell with `pixi`: @@ -23,8 +33,80 @@ pixi shell This will make all of the dependencies installed with `pixi run setup` available. -You can exit the pixi shell by using: +When you are done, you can exit the pixi shell by using: ```sh exit ``` + +## Running Action + +With all dependencies installed, and the models downloaded to the `models/` directory, you can now run action: + +```sh +pixi shell +python3 action.py + +usage: action.py [-h] [-e {terrestrial,aquatic}] [-b BUFFER] [-c CONFIDENCE] + [-m MIN_DURATION] [-f SKIP_FRAMES] [-d] [-o OUTPUT_DIR] [-s] + [--log-level {DEBUG,INFO,WARNING,ERROR}] + filename [filename ...] +action.py: error: the following arguments are required: filename +``` + +> [!NOTE] +> On Unix systems you can also use `./action.py` without `python3`. + +### Options + +Action can be configured to run in different ways using various arguments and flags. + +| Option | Description | Example | +| --- | --- | --- | +| `filename` | Path to a video file, multiple video files, or a glob pattern | `./video/*.mov` | +| `-e`, `--environment` | Type of camera environment, either aquatic or terrestrial. Defaults to `aquatic` | `--environment terrestrial` | +| `-b`, `--buffer` | Number of seconds to add before and after detection. Cannot be negative | `--buffer 1.0` | +| `-c`, `--confidence` | Confidence threshold for detection. Must be greater than 0.0 and less than 1.0 | `--confidence 0.45` | +| `-m`, `--minimum-duration` | Minimum duration for clips in seconds. Must be greater than 0.0 | `--minimum-duration 2.0` | +| `-f`, `--frames-to-skip` | Number of frames to skip when detecting. Cannot be negative, defaults to half the frame rate | `--frames-to-skip 15` | +| `-d`, `--delete-previous-clips` | Whether to delete previous clips before processing video | `--delete-previous-clips` | +| `-o`, `--output-dir` | Output directory to use for all clips | `--output-dir ./output` | +| `-s`, `--show-detections` | Whether to show detection frames with bounding boxes | `--show-detections` | +| `--log-level` | Logging level. Can be `DEBUG`, `INFO`, `WARNING`, or `ERROR`. Defaults to `INFO` | `--log-level DEBUG` | + +> [!NOTE] +> The options with `-` or `--` are optional, while `filename` is a required argument. + +### Examples + +To process a video named `recording.mov` using all the default settings, specify only the filename: + +```sh +python3 action.py recording.mov +``` + +You can also include multiple filenames: + +```sh +python3 action.py recording1.mov recording2.mov recording3.mov +``` + +Or use a file pattern: + +```sh +python3 action.py ./videos/*.avi +``` + +Many other options can be altered (see above) to process videos in specific ways. For example: + +```sh +python3 action.py ./video/aquatic.mov -c 0.60 -m 3.0 -s -b 1.0 -d -e aquatic +``` + +This would process the file `./video/aquatic.mov`, deleting all previous detections, use YOLO-Fish for detections, set a confidence threshold of `0.60` (i.e., include fish detections with confidence `0.60` and higher), make all clips `3.0` seconds minimum with a `1.0` second buffer added to the start and end of the clip (i.e., `1.0` + `3.0` + `1.0` = `5.0` seconds), and show each initial detection visual (i.e., bounding boxes on the video frame). + +```sh +python3 action.py ./video/terrestrial.mov -c 0.45 -m 8.0 -b 2.0 -e terrestrial -f 25 +``` + +This would process the file `./video/terrestrial.mov`, use Megadetector for detections, set a confidence threshold of `0.45` (i.e., include animal detections with confidence `0.45` and higher), make all clips `8.0` seconds minimum with a `2.0` second buffer added to the start and end of the clip (i.e., `2.0` + `8.0` + `2.0` = `12.0` seconds), and run detections on every 25th frame in the video. diff --git a/action.py b/action.py index 899f528..773189e 100755 --- a/action.py +++ b/action.py @@ -1,376 +1,9 @@ -""" -This is the main module for ACTION. It handles parsing arguments from the user, -loading and managing resources, and processing detections into clips. -""" +#!/usr/bin/env python3 -import os -import sys -import time -import logging import argparse +from src.action import main -from clip_manager import ( - ClipManager, - remove_clips_dir, - remove_output_dir, - create_output_dir, -) -from yolo_fish_detector import YoloFishDetector -from megadetector_detector import MegadetectorDetector -from utils import format_time, format_percent, get_video_paths - -import cv2 - - -# We use converted ONNX models for YOLO-Fish (https://github.com/tamim662/YOLO-Fish) -# and Megadetector (https://github.com/microsoft/CameraTraps) -def load_detector(environment, min_duration, buffer, confidence, logger): - """ - Load the appropriate detector based on the environment provided. - - Args: - environment (str): The type of detector to load. Must be either "aquatic" or "terrestrial". - min_duration (float): The minimum duration of a generated clip - buffer (float): An optional number of seconds to add before/after a clip - confidence (float): The confidence level to use - logger (logging.Logger): The logger to use for logging messages. - - Returns: - detector (object): An instance of the appropriate detector. - - Raises: - TypeError: If the any args are not of the correct type - """ - - # Make sure any user-provided flags for detection are valid before we use them - if buffer and buffer < 0.0: - raise TypeError("Error: minimum buffer cannot be negative") - - if min_duration and min_duration <= 0.0: - raise TypeError("Error: minimum duration must be greater than 0.0") - - if confidence and (confidence <= 0.0 or confidence > 1.0): - raise TypeError("Error: confidence must be greater than 0.0 and less than 1.0") - - detector = None - if environment == "terrestrial": - detector = MegadetectorDetector(logger, min_duration, buffer, confidence) - elif environment == "aquatic": - detector = YoloFishDetector(logger, min_duration, buffer, confidence) - else: - raise TypeError("environment must be one of aquatic or terrestrial") - - detector.load() - return detector - - -# Defining the function process_frames, called in main -def process_frames( - video_path, - cap, - detector, - clips, - fps, - total_frames, - frames_to_skip, - logger, - args, -): - """ - Process frames from a video file and create clips based on detections. - - Args: - video_path (str): The path to the video file. - cap (cv2.VideoCapture): The video capture object. - detector (object): The detector to use for detecting objects in frames. - clips (ClipManager): The clip manager for managing clips. - fps (int): The frames per second of the video. - total_frames (int): The total number of frames in the video. - frames_to_skip (int): The number of frames to skip between detections - logger (logging.Logger): The logger to use for logging messages. - args (argparse.Namespace): The command line arguments. - - Returns: - None - """ - buffer_seconds = detector.buffer - min_detection_duration = detector.min_duration - show_detections = args.show_detections - - # Number of frames per minute of video time - frames_per_minute = 60 * fps - # Frame number for the next progress message - next_progress_frame = frames_per_minute - - # Track when there are is something in frame as detection events - detection_start_time = None - detection_highest_confidence = 0 - detection_event = False - - frame_count = 0 - - # Loop over all frames in the video - while cap.isOpened(): - ret, frame = cap.read() - # If there isn't another frame, we're done - if not ret: - break - - # If a detection is already happening, then we know we're going to - # record for a given period, so we can skip ahead to the frame where - # the detection period ends. However, we then need to check *all* the - # frames after this within the buffer period (i.e., so we don't overlap - # with the end buffer period in the previous clip). If we detect something - # in these frames, we should extend this detection period; otherwise - # we end it and create a new clip. - if detection_event: - # Calculate the number of frames to skip ahead. We know that we - # want to record for a minimum duration, and potentially we add - # a buffer period in seconds. - skip_ahead_frames = int((min_detection_duration + buffer_seconds) * fps) - logger.debug(f"Detection event, skipping ahead {skip_ahead_frames} frames") - - # Skip ahead the number of frames that will be in the clip - cap.set(cv2.CAP_PROP_POS_FRAMES, frame_count + skip_ahead_frames) - frame_count += skip_ahead_frames - - # Check some frames within the buffer period for a detection, to see if - # we should extend this detection period, or if it's OK to end it. NOTE: - # if `buffer_seconds` is 0, at least check the next frame. - for i in range(max(1, int(buffer_seconds * fps))): - ret, frame = cap.read() - if not ret: - break - frame_count += 1 - - # Only process frames that are multiples of frames_to_skip - # or the last frame in the video. - if i % frames_to_skip == 0 or frame_count == total_frames - 1: - logger.debug( - f"Checking frame {frame_count} before ending detection event" - ) - # If a detection is made, extend the current detection period - boxes = detector.detect(frame) - if len(boxes) > 0: - detection_highest_confidence = max( - detection_highest_confidence, max(box[4] for box in boxes) - ) - logger.info( - f"{detector.class_name} detected, extending detection event: {format_time(frame_count / fps + buffer_seconds)} (max confidence={format_percent(detection_highest_confidence)})" - ) - if show_detections: - detector.draw_detections(frame, boxes, video_path) - break - else: - # If no detection was made within the buffer period, and we didn't - # extend, end the detection period now and create a new clip. - detection_end_time = frame_count / fps + buffer_seconds - logger.info( - f"Detection period ended: {format_time(detection_end_time)} (duration={format_time(detection_end_time - detection_start_time)}, max confidence={format_percent(detection_highest_confidence)})" - ) - clips.create_clip( - detection_start_time, - detection_end_time, - video_path, - ) - - # Reset the detection period - detection_event = False - detection_highest_confidence = 0 - - # Start again reading the next frame - continue - - # If we're not already in a detection event, process every n frames - # vs. every frame for speed (e.g., every 15 of 30fps). We also check - # the last frame, so we don't miss anything at the edge. - if frame_count % frames_to_skip == 0 or frame_count == total_frames - 1: - boxes = detector.detect(frame) - - # If there are one ore more detections - if len(boxes) > 0: - detection_highest_confidence = max( - detection_highest_confidence, max(box[4] for box in boxes) - ) - - # If we're not already in a detection event, start one - if not detection_event: - detection_start_time = max(0, frame_count / fps - buffer_seconds) - logger.info( - f"{detector.class_name} detected, starting detection event: {format_time(frame_count / fps)} (max confidence={format_percent(detection_highest_confidence)})" - ) - if show_detections: - detector.draw_detections(frame, boxes, video_path) - detection_event = True - - # We've finished processing this frame - frame_count += 1 - - # Print a progress message every minute of video time so we know what's going on - if frame_count >= next_progress_frame: - logger.info( - f"\nProgress: {format_percent(frame_count / total_frames)} processed ({frame_count}/{total_frames} frames, {format_time(frame_count / fps)})\n" - ) - next_progress_frame += frames_per_minute - - # Before we finish the program, check if there's a detection event in progress - # and if there is, end it now so we don't lose the final clip. - if detection_event: - detection_end_time = frame_count / fps + buffer_seconds - logger.info( - f"Detection period ended: {format_time(detection_end_time)} (duration={format_time(detection_end_time - detection_start_time)}, max confidence={format_percent(detection_highest_confidence)})" - ) - clips.create_clip( - detection_start_time, - detection_end_time, - video_path, - ) - - -# Main part of program to do setup and start processing frames in each file -def main(args): - """ - The main function of the program. Sets up the logger, validates arguments, - loads the detector, and processes frames from each video file. - - Args: - args (argparse.Namespace): The command line arguments. - """ - - # Create a logger for this module and set the log level - logger = logging.getLogger(__name__) - logging.basicConfig(level=args.log_level, format="%(message)s") - - delete_clips = args.delete_clips - output_dir = args.output_dir - video_paths = get_video_paths(args.filename) - logger.debug(f"Got input files: {video_paths}") - - # Validate argument parameters from user before using them - if len(video_paths) < 1: - logger.error("Error: you must specify one or more video filenames to process") - sys.exit(1) - - # Load YOLO-Fish or Megadetector, based on `-e` value - detector = None - try: - detector = load_detector( - args.environment, args.min_duration, args.buffer, args.confidence, logger - ) - except Exception as e: - logger.error(f"There was an error: {e}") - sys.exit(1) - - cap = None - clips = None - - # Initialize the output_dir if specified - if output_dir: - # If `-d`` was specified, delete old clips first - if delete_clips: - remove_output_dir(output_dir, logger) - # Create the output directory if it doesn't exist - create_output_dir(output_dir) - - try: - # Create a queue manager for clips to be processed by ffmpeg - clips = ClipManager(logger, output_dir) - - # Keep track of total time to process all files, recording start time - total_time_start = time.time() - - # Loop over all the video file paths and process each one - for i, video_path in enumerate(video_paths, start=1): - # Make sure this video path actually exists before we try to use it - if not os.path.exists(video_path): - logger.info(f"Video path {video_path} does not exist, skipping.") - continue - - file_start_time = time.time() - - # If the user requests it via -d flag, and isn't using a common output_dir - # remove old clips first - if not output_dir and delete_clips: - remove_clips_dir(video_path, logger) - - # Setup video capture for this video file - cap = cv2.VideoCapture(video_path) - fps = cap.get(cv2.CAP_PROP_FPS) - frames_to_skip = args.skip_frames or int(fps / 2.0) - total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) - duration = total_frames / fps - - logger.info( - f"\nStarting file {i} of {len(video_paths)}: {video_path} - {format_time(duration)} - {total_frames} frames at {fps} fps, skipping every {frames_to_skip} frames" - ) - logger.info( - f"Using confidence threshold {detector.confidence}, minimum clip duration of {detector.min_duration} seconds, and {detector.buffer} seconds of buffer." - ) - - # If we're not using a common clips dir, reset the counter for future clips - if not output_dir: - clips.reset_clip_count() - - clip_count_before = clips.get_clip_count() - - # Process the video's frames into clips - process_frames( - video_path, - cap, - detector, - clips, - fps, - total_frames, - frames_to_skip, - logger, - args, - ) - - clip_count_after = clips.get_clip_count() - clips_processed = clip_count_after - clip_count_before - - file_end_time = time.time() - logger.info( - f"Finished file {i} of {len(video_paths)}: {video_path} (total time to process file {format_time(file_end_time - file_start_time)}). Processed {total_frames} frames into {clips_processed} clips" - ) - - # Clean-up the resources we have open, if necessary - if cap is not None: - cap.release() - - cv2.destroyAllWindows() - cv2.waitKey(1) - - # Keep track of total time to process all files, recording end time - total_time_end = time.time() - logger.info( - f"\nFinished. Total time for {len(video_paths)} files: {format_time(total_time_end - total_time_start)}" - ) - - except KeyboardInterrupt: - logger.warning("Interrupted by user, cleaning up...") - clips.stop() - except Exception as e: - logger.error(f"There was an error: {e}") - finally: - # Clean-up the resources we have open, if necessary - if cap is not None: - cap.release() - - cv2.destroyAllWindows() - cv2.waitKey(1) - - # Wait for the ffmpeg clip queue to complete before we exit - if clips is not None: - clips.cleanup() - - -# Define the command line arguments if __name__ == "__main__": - """ - The entry point of the program. Defines the command line arguments and - calls the main function. - """ parser = argparse.ArgumentParser( description="Automated Camera Trapping Identification and Organization Network (ACTION)" ) diff --git a/pixi.lock b/pixi.lock index cd6ea48..7ea9aa2 100644 --- a/pixi.lock +++ b/pixi.lock @@ -27,9 +27,9 @@ package: openssl: '>=3.0.7,<4.0a0' readline: '>=8.1.2,<9.0a0' libffi: '>=3.4,<4.0a0' - libsqlite: '>=3.40.0,<4.0a0' libgcc-ng: '>=12' libnsl: '>=2.0.0,<2.1.0a0' + libsqlite: '>=3.40.0,<4.0a0' libzlib: '>=1.2.13,<1.3.0a0' bzip2: '>=1.0.8,<2.0a0' libuuid: '>=2.32.1,<3.0a0' @@ -115,25 +115,6 @@ package: noarch: python size: 1386212 timestamp: 1690024763393 -- name: git-lfs - version: 3.4.0 - manager: conda - platform: linux-64 - dependencies: {} - url: https://conda.anaconda.org/conda-forge/linux-64/git-lfs-3.4.0-ha770c72_0.conda - hash: - md5: 8b8aef0a35f5b98937a65b67b7d3b536 - sha256: 77c871ba307297dab92ef9a2923ad44bd5d7dcf429b92c45cdbea2bc85209fb3 - optional: false - category: main - build: ha770c72_0 - arch: x86_64 - subdir: linux-64 - build_number: 0 - license: MIT - license_family: MIT - size: 3783189 - timestamp: 1690413607828 - name: git version: 2.42.0 manager: conda @@ -141,11 +122,11 @@ package: dependencies: openssl: '>=3.1.2,<4.0a0' pcre2: '>=10.40,<10.41.0a0' - libiconv: '>=1.17,<2.0a0' - libgcc-ng: '>=12' libzlib: '>=1.2.13,<1.3.0a0' - libexpat: '>=2.5.0,<3.0a0' + libiconv: '>=1.17,<2.0a0' curl: '*' + libexpat: '>=2.5.0,<3.0a0' + libgcc-ng: '>=12' gettext: '*' perl: 5.* url: https://conda.anaconda.org/conda-forge/linux-64/git-2.42.0-pl5321h86e50cf_0.conda @@ -212,6 +193,46 @@ package: license_family: MIT size: 4902978 timestamp: 1696896633112 +- name: libstdcxx-ng + version: 13.2.0 + manager: conda + platform: linux-64 + dependencies: {} + url: https://conda.anaconda.org/conda-forge/linux-64/libstdcxx-ng-13.2.0-h7e041cc_2.conda + hash: + md5: 9172c297304f2a20134fc56c97fbe229 + sha256: ab22ecdc974cdbe148874ea876d9c564294d5eafa760f403ed4fd495307b4243 + optional: false + category: main + build: h7e041cc_2 + arch: x86_64 + subdir: linux-64 + build_number: 2 + license: GPL-3.0-only WITH GCC-exception-3.1 + license_family: GPL + size: 3842773 + timestamp: 1695219454837 +- name: python_abi + version: '3.11' + manager: conda + platform: linux-64 + dependencies: {} + url: https://conda.anaconda.org/conda-forge/linux-64/python_abi-3.11-4_cp311.conda + hash: + md5: d786502c97404c94d7d58d258a445a65 + sha256: 0be3ac1bf852d64f553220c7e6457e9c047dfb7412da9d22fbaa67e60858b3cf + optional: false + category: main + build: 4_cp311 + arch: x86_64 + subdir: linux-64 + build_number: 4 + constrains: + - python 3.11.* *_cpython + license: BSD-3-Clause + license_family: BSD + size: 6385 + timestamp: 1695147338551 - name: libgcc-ng version: 13.2.0 manager: conda @@ -296,46 +317,6 @@ package: license_family: GPL size: 421133 timestamp: 1695219303065 -- name: libstdcxx-ng - version: 13.2.0 - manager: conda - platform: linux-64 - dependencies: {} - url: https://conda.anaconda.org/conda-forge/linux-64/libstdcxx-ng-13.2.0-h7e041cc_2.conda - hash: - md5: 9172c297304f2a20134fc56c97fbe229 - sha256: ab22ecdc974cdbe148874ea876d9c564294d5eafa760f403ed4fd495307b4243 - optional: false - category: main - build: h7e041cc_2 - arch: x86_64 - subdir: linux-64 - build_number: 2 - license: GPL-3.0-only WITH GCC-exception-3.1 - license_family: GPL - size: 3842773 - timestamp: 1695219454837 -- name: python_abi - version: '3.11' - manager: conda - platform: linux-64 - dependencies: {} - url: https://conda.anaconda.org/conda-forge/linux-64/python_abi-3.11-4_cp311.conda - hash: - md5: d786502c97404c94d7d58d258a445a65 - sha256: 0be3ac1bf852d64f553220c7e6457e9c047dfb7412da9d22fbaa67e60858b3cf - optional: false - category: main - build: 4_cp311 - arch: x86_64 - subdir: linux-64 - build_number: 4 - constrains: - - python 3.11.* *_cpython - license: BSD-3-Clause - license_family: BSD - size: 6385 - timestamp: 1695147338551 - name: libcblas version: 3.9.0 manager: conda @@ -605,12 +586,12 @@ package: platform: linux-64 dependencies: libssh2: '>=1.11.0,<2.0a0' - libzlib: '>=1.2.13,<1.3.0a0' + krb5: '>=1.21.2,<1.22.0a0' libcurl: ==8.4.0 hca28451_0 openssl: '>=3.1.3,<4.0a0' - zstd: '>=1.5.5,<1.6.0a0' - krb5: '>=1.21.2,<1.22.0a0' + libzlib: '>=1.2.13,<1.3.0a0' libgcc-ng: '>=12' + zstd: '>=1.5.5,<1.6.0a0' url: https://conda.anaconda.org/conda-forge/linux-64/curl-8.4.0-hca28451_0.conda hash: md5: 2bcf7689cae931dd35d9a45626f49fce @@ -647,40 +628,18 @@ package: license_family: BSD size: 271133 timestamp: 1685837707056 -- name: zstd - version: 1.5.5 - manager: conda - platform: linux-64 - dependencies: - libzlib: '>=1.2.13,<1.3.0a0' - libgcc-ng: '>=12' - libstdcxx-ng: '>=12' - url: https://conda.anaconda.org/conda-forge/linux-64/zstd-1.5.5-hfc55251_0.conda - hash: - md5: 04b88013080254850d6c01ed54810589 - sha256: 607cbeb1a533be98ba96cf5cdf0ddbb101c78019f1fda063261871dad6248609 - optional: false - category: main - build: hfc55251_0 - arch: x86_64 - subdir: linux-64 - build_number: 0 - license: BSD-3-Clause - license_family: BSD - size: 545199 - timestamp: 1693151163452 - name: libcurl version: 8.4.0 manager: conda platform: linux-64 dependencies: libssh2: '>=1.11.0,<2.0a0' - libzlib: '>=1.2.13,<1.3.0a0' + krb5: '>=1.21.2,<1.22.0a0' libnghttp2: '>=1.52.0,<2.0a0' openssl: '>=3.1.3,<4.0a0' - zstd: '>=1.5.5,<1.6.0a0' - krb5: '>=1.21.2,<1.22.0a0' + libzlib: '>=1.2.13,<1.3.0a0' libgcc-ng: '>=12' + zstd: '>=1.5.5,<1.6.0a0' url: https://conda.anaconda.org/conda-forge/linux-64/libcurl-8.4.0-hca28451_0.conda hash: md5: 1158ac1d2613b28685644931f11ee807 @@ -760,6 +719,28 @@ package: license_family: BSD size: 106190 timestamp: 1598867915 +- name: zstd + version: 1.5.5 + manager: conda + platform: linux-64 + dependencies: + libzlib: '>=1.2.13,<1.3.0a0' + libgcc-ng: '>=12' + libstdcxx-ng: '>=12' + url: https://conda.anaconda.org/conda-forge/linux-64/zstd-1.5.5-hfc55251_0.conda + hash: + md5: 04b88013080254850d6c01ed54810589 + sha256: 607cbeb1a533be98ba96cf5cdf0ddbb101c78019f1fda063261871dad6248609 + optional: false + category: main + build: hfc55251_0 + arch: x86_64 + subdir: linux-64 + build_number: 0 + license: BSD-3-Clause + license_family: BSD + size: 545199 + timestamp: 1693151163452 - name: krb5 version: 1.21.2 manager: conda @@ -1246,9 +1227,9 @@ package: manager: conda platform: linux-64 dependencies: - gettext: '>=0.21.1,<1.0a0' libgcc-ng: '>=12' libunistring: '>=0,<1.0a0' + gettext: '>=0.21.1,<1.0a0' url: https://conda.anaconda.org/conda-forge/linux-64/libidn2-2.3.4-h166bdaf_0.tar.bz2 hash: md5: 7440fbafd870b8bab68f83a064875d34 @@ -1474,10 +1455,10 @@ package: manager: conda platform: linux-64 dependencies: - libgcc-ng: '>=12' + libzlib: '>=1.2.13,<1.3.0a0' libuuid: '>=2.32.1,<3.0a0' freetype: '>=2.12.1,<3.0a0' - libzlib: '>=1.2.13,<1.3.0a0' + libgcc-ng: '>=12' expat: '>=2.5.0,<3.0a0' url: https://conda.anaconda.org/conda-forge/linux-64/fontconfig-2.14.2-h14ed4e7_0.conda hash: @@ -1519,13 +1500,13 @@ package: manager: conda platform: linux-64 dependencies: - graphite2: '*' + libgcc-ng: '>=12' libstdcxx-ng: '>=12' icu: '>=73.2,<74.0a0' + graphite2: '*' + freetype: '>=2.12.1,<3.0a0' libglib: '>=2.78.0,<3.0a0' cairo: '>=1.16.0,<2.0a0' - libgcc-ng: '>=12' - freetype: '>=2.12.1,<3.0a0' url: https://conda.anaconda.org/conda-forge/linux-64/harfbuzz-8.2.1-h3d44ed6_0.conda hash: md5: 98db5f8813f45e2b29766aff0e4a499c @@ -1540,18 +1521,38 @@ package: license_family: MIT size: 1526592 timestamp: 1695089914042 +- name: graphite2 + version: 1.3.13 + manager: conda + platform: linux-64 + dependencies: + libgcc-ng: '>=7.5.0' + libstdcxx-ng: '>=7.5.0' + url: https://conda.anaconda.org/conda-forge/linux-64/graphite2-1.3.13-h58526e2_1001.tar.bz2 + hash: + md5: 8c54672728e8ec6aa6db90cf2806d220 + sha256: 65da967f3101b737b08222de6a6a14e20e480e7d523a5d1e19ace7b960b5d6b1 + optional: false + category: main + build: h58526e2_1001 + arch: x86_64 + subdir: linux-64 + build_number: 1001 + license: LGPLv2 + size: 104701 + timestamp: 1604365484436 - name: libglib version: 2.78.0 manager: conda platform: linux-64 dependencies: - libgcc-ng: '>=12' - gettext: '>=0.21.1,<1.0a0' + libffi: '>=3.4,<4.0a0' libstdcxx-ng: '>=12' + gettext: '>=0.21.1,<1.0a0' pcre2: '>=10.40,<10.41.0a0' - libzlib: '>=1.2.13,<1.3.0a0' - libffi: '>=3.4,<4.0a0' libiconv: '>=1.17,<2.0a0' + libzlib: '>=1.2.13,<1.3.0a0' + libgcc-ng: '>=12' url: https://conda.anaconda.org/conda-forge/linux-64/libglib-2.78.0-hebfc3b9_0.conda hash: md5: e618003da3547216310088478e475945 @@ -1937,26 +1938,6 @@ package: license_family: MIT size: 385309 timestamp: 1695736061006 -- name: graphite2 - version: 1.3.13 - manager: conda - platform: linux-64 - dependencies: - libgcc-ng: '>=7.5.0' - libstdcxx-ng: '>=7.5.0' - url: https://conda.anaconda.org/conda-forge/linux-64/graphite2-1.3.13-h58526e2_1001.tar.bz2 - hash: - md5: 8c54672728e8ec6aa6db90cf2806d220 - sha256: 65da967f3101b737b08222de6a6a14e20e480e7d523a5d1e19ace7b960b5d6b1 - optional: false - category: main - build: h58526e2_1001 - arch: x86_64 - subdir: linux-64 - build_number: 1001 - license: LGPLv2 - size: 104701 - timestamp: 1604365484436 - name: fonts-conda-forge version: '1' manager: conda @@ -2384,25 +2365,6 @@ package: noarch: python size: 1386212 timestamp: 1690024763393 -- name: git-lfs - version: 3.4.0 - manager: conda - platform: osx-arm64 - dependencies: {} - url: https://conda.anaconda.org/conda-forge/osx-arm64/git-lfs-3.4.0-hce30654_0.conda - hash: - md5: c96c89fe47e4a4cb14929a04ba78488f - sha256: d4ecce07f6aa93f319d267460068b4dcf5058b665b4530ef2c5be9b7ca047cdb - optional: false - category: main - build: hce30654_0 - arch: aarch64 - subdir: osx-arm64 - build_number: 0 - license: MIT - license_family: MIT - size: 3741778 - timestamp: 1690414069499 - name: git version: 2.42.0 manager: conda @@ -3553,11 +3515,11 @@ package: dependencies: libexpat: '>=2.5.0,<3.0a0' fontconfig: '>=2.14.2,<3.0a0' - libzlib: '>=1.2.13,<1.3.0a0' - fonts-conda-ecosystem: '*' - freetype: '>=2.12.1,<3.0a0' fribidi: '>=1.0.10,<2.0a0' + fonts-conda-ecosystem: '*' harfbuzz: '>=8.1.1,<9.0a0' + libzlib: '>=1.2.13,<1.3.0a0' + freetype: '>=2.12.1,<3.0a0' url: https://conda.anaconda.org/conda-forge/osx-arm64/libass-0.17.1-hf7da4fe_1.conda hash: md5: 53fff6fc379154382f5121325c5fe2f5 @@ -3594,63 +3556,24 @@ package: license_family: MIT size: 237668 timestamp: 1674829263740 -- name: fonts-conda-ecosystem - version: '1' +- name: harfbuzz + version: 8.2.1 manager: conda platform: osx-arm64 dependencies: - fonts-conda-forge: '*' - url: https://conda.anaconda.org/conda-forge/noarch/fonts-conda-ecosystem-1-0.tar.bz2 + icu: '>=73.2,<74.0a0' + libcxx: '>=15.0.7' + graphite2: '*' + cairo: '>=1.16.0,<2.0a0' + freetype: '>=2.12.1,<3.0a0' + libglib: '>=2.78.0,<3.0a0' + url: https://conda.anaconda.org/conda-forge/osx-arm64/harfbuzz-8.2.1-hf1a6348_0.conda hash: - md5: fee5683a3f04bd15cbd8318b096a27ab - sha256: a997f2f1921bb9c9d76e6fa2f6b408b7fa549edd349a77639c9fe7a23ea93e61 + md5: 001c9b64c94fb7066525bff0dc0f30c2 + sha256: 4f1a8d3f2968d9d76ae1b75e021426f7bc53e88bd1efab82b22eb7fff15ace81 optional: false category: main - build: '0' - arch: aarch64 - subdir: osx-arm64 - build_number: 0 - license: BSD-3-Clause - license_family: BSD - noarch: generic - size: 3667 - timestamp: 1566974674465 -- name: fribidi - version: 1.0.10 - manager: conda - platform: osx-arm64 - dependencies: {} - url: https://conda.anaconda.org/conda-forge/osx-arm64/fribidi-1.0.10-h27ca646_0.tar.bz2 - hash: - md5: c64443234ff91d70cb9c7dc926c58834 - sha256: 4b37ea851a2cf85edf0a63d2a63266847ec3dcbba4a31156d430cdd6aa811303 - optional: false - category: main - build: h27ca646_0 - arch: aarch64 - subdir: osx-arm64 - build_number: 0 - license: LGPL-2.1 - size: 60255 - timestamp: 1604417405528 -- name: harfbuzz - version: 8.2.1 - manager: conda - platform: osx-arm64 - dependencies: - icu: '>=73.2,<74.0a0' - libcxx: '>=15.0.7' - graphite2: '*' - cairo: '>=1.16.0,<2.0a0' - freetype: '>=2.12.1,<3.0a0' - libglib: '>=2.78.0,<3.0a0' - url: https://conda.anaconda.org/conda-forge/osx-arm64/harfbuzz-8.2.1-hf1a6348_0.conda - hash: - md5: 001c9b64c94fb7066525bff0dc0f30c2 - sha256: 4f1a8d3f2968d9d76ae1b75e021426f7bc53e88bd1efab82b22eb7fff15ace81 - optional: false - category: main - build: hf1a6348_0 + build: hf1a6348_0 arch: aarch64 subdir: osx-arm64 build_number: 0 @@ -3684,11 +3607,11 @@ package: dependencies: icu: '>=73.2,<74.0a0' libpng: '>=1.6.39,<1.7.0a0' + __osx: '>=10.9' + freetype: '>=2.12.1,<3.0a0' libglib: '>=2.78.0,<3.0a0' libzlib: '>=1.2.13,<1.3.0a0' zlib: '*' - freetype: '>=2.12.1,<3.0a0' - __osx: '>=10.9' fontconfig: '>=2.14.2,<3.0a0' libcxx: '>=16.0.6' fonts-conda-ecosystem: '*' @@ -3711,9 +3634,9 @@ package: manager: conda platform: osx-arm64 dependencies: - pcre2: '>=10.40,<10.41.0a0' gettext: '>=0.21.1,<1.0a0' libcxx: '>=15.0.7' + pcre2: '>=10.40,<10.41.0a0' libffi: '>=3.4,<4.0a0' libiconv: '>=1.17,<2.0a0' libzlib: '>=1.2.13,<1.3.0a0' @@ -3752,6 +3675,27 @@ package: license_family: Other size: 79577 timestamp: 1686575471024 +- name: fonts-conda-ecosystem + version: '1' + manager: conda + platform: osx-arm64 + dependencies: + fonts-conda-forge: '*' + url: https://conda.anaconda.org/conda-forge/noarch/fonts-conda-ecosystem-1-0.tar.bz2 + hash: + md5: fee5683a3f04bd15cbd8318b096a27ab + sha256: a997f2f1921bb9c9d76e6fa2f6b408b7fa549edd349a77639c9fe7a23ea93e61 + optional: false + category: main + build: '0' + arch: aarch64 + subdir: osx-arm64 + build_number: 0 + license: BSD-3-Clause + license_family: BSD + noarch: generic + size: 3667 + timestamp: 1566974674465 - name: pixman version: 0.42.2 manager: conda @@ -3772,6 +3716,24 @@ package: license_family: MIT size: 213843 timestamp: 1695736518800 +- name: fribidi + version: 1.0.10 + manager: conda + platform: osx-arm64 + dependencies: {} + url: https://conda.anaconda.org/conda-forge/osx-arm64/fribidi-1.0.10-h27ca646_0.tar.bz2 + hash: + md5: c64443234ff91d70cb9c7dc926c58834 + sha256: 4b37ea851a2cf85edf0a63d2a63266847ec3dcbba4a31156d430cdd6aa811303 + optional: false + category: main + build: h27ca646_0 + arch: aarch64 + subdir: osx-arm64 + build_number: 0 + license: LGPL-2.1 + size: 60255 + timestamp: 1604417405528 - name: fonts-conda-forge version: '1' manager: conda @@ -3982,9 +3944,9 @@ package: tzdata: '*' openssl: '>=3.0.5,<4.0a0' libzlib: '>=1.2.13,<1.3.0a0' - libffi: '>=3.4.2,<3.5.0a0' vc: '>=14.1,<15' libsqlite: '>=3.39.4,<4.0a0' + libffi: '>=3.4.2,<3.5.0a0' tk: '>=8.6.12,<8.7.0a0' bzip2: '>=1.0.8,<2.0a0' xz: '>=5.2.6,<5.3.0a0' @@ -4062,25 +4024,6 @@ package: noarch: python size: 1386212 timestamp: 1690024763393 -- name: git-lfs - version: 3.4.0 - manager: conda - platform: win-64 - dependencies: {} - url: https://conda.anaconda.org/conda-forge/win-64/git-lfs-3.4.0-h57928b3_0.conda - hash: - md5: d8b2d400f3762539ccdf4138b18a61ef - sha256: 89acdfc86aac146263cc562fd41fed4d2fceefd099e93a2afc837bf8dda73ebc - optional: false - category: main - build: h57928b3_0 - arch: x86_64 - subdir: win-64 - build_number: 0 - license: MIT - license_family: MIT - size: 3650843 - timestamp: 1690414029264 - name: git version: 2.42.0 manager: conda @@ -4152,27 +4095,6 @@ package: license_family: MIT size: 4812037 timestamp: 1696897314194 -- name: python_abi - version: '3.11' - manager: conda - platform: win-64 - dependencies: {} - url: https://conda.anaconda.org/conda-forge/win-64/python_abi-3.11-4_cp311.conda - hash: - md5: 70513332c71b56eace4ee6441e66c012 - sha256: 67c2aade3e2160642eec0742384e766b20c766055e3d99335681e3e05d88ed7b - optional: false - category: main - build: 4_cp311 - arch: x86_64 - subdir: win-64 - build_number: 4 - constrains: - - python 3.11.* *_cpython - license: BSD-3-Clause - license_family: BSD - size: 6755 - timestamp: 1695147711935 - name: ucrt version: 10.0.22621.0 manager: conda @@ -4194,6 +4116,27 @@ package: license_family: PROPRIETARY size: 1283972 timestamp: 1666630199266 +- name: python_abi + version: '3.11' + manager: conda + platform: win-64 + dependencies: {} + url: https://conda.anaconda.org/conda-forge/win-64/python_abi-3.11-4_cp311.conda + hash: + md5: 70513332c71b56eace4ee6441e66c012 + sha256: 67c2aade3e2160642eec0742384e766b20c766055e3d99335681e3e05d88ed7b + optional: false + category: main + build: 4_cp311 + arch: x86_64 + subdir: win-64 + build_number: 4 + constrains: + - python 3.11.* *_cpython + license: BSD-3-Clause + license_family: BSD + size: 6755 + timestamp: 1695147711935 - name: vc version: '14.3' manager: conda @@ -4356,10 +4299,10 @@ package: platform: win-64 dependencies: ucrt: '>=10.0.20348.0' - libzlib: '>=1.2.13,<1.3.0a0' - libiconv: '>=1.17,<2.0a0' vc: '>=14.2,<15' + libzlib: '>=1.2.13,<1.3.0a0' vc14_runtime: '>=14.29.30139' + libiconv: '>=1.17,<2.0a0' url: https://conda.anaconda.org/conda-forge/win-64/libxml2-2.11.5-hc3477c8_1.conda hash: md5: 27974f880a010b1441093d9f737a949f @@ -4374,6 +4317,30 @@ package: license_family: MIT size: 1600640 timestamp: 1692960798126 +- name: libzlib + version: 1.2.13 + manager: conda + platform: win-64 + dependencies: + ucrt: '>=10.0.20348.0' + vc: '>=14.2,<15' + vc14_runtime: '>=14.29.30139' + url: https://conda.anaconda.org/conda-forge/win-64/libzlib-1.2.13-hcfcfb64_5.conda + hash: + md5: 5fdb9c6a113b6b6cb5e517fd972d5f41 + sha256: c161822ee8130b71e08b6d282b9919c1de2c5274b29921a867bca0f7d30cad26 + optional: false + category: main + build: hcfcfb64_5 + arch: x86_64 + subdir: win-64 + build_number: 5 + constrains: + - zlib 1.2.13 *_5 + license: Zlib + license_family: Other + size: 55800 + timestamp: 1686575452215 - name: libiconv version: '1.17' manager: conda @@ -4414,30 +4381,6 @@ package: license_family: BSD size: 17207 timestamp: 1688020635322 -- name: libzlib - version: 1.2.13 - manager: conda - platform: win-64 - dependencies: - ucrt: '>=10.0.20348.0' - vc: '>=14.2,<15' - vc14_runtime: '>=14.29.30139' - url: https://conda.anaconda.org/conda-forge/win-64/libzlib-1.2.13-hcfcfb64_5.conda - hash: - md5: 5fdb9c6a113b6b6cb5e517fd972d5f41 - sha256: c161822ee8130b71e08b6d282b9919c1de2c5274b29921a867bca0f7d30cad26 - optional: false - category: main - build: hcfcfb64_5 - arch: x86_64 - subdir: win-64 - build_number: 5 - constrains: - - zlib 1.2.13 *_5 - license: Zlib - license_family: Other - size: 55800 - timestamp: 1686575452215 - name: pthreads-win32 version: 2.9.1 manager: conda @@ -5018,27 +4961,6 @@ package: license: ISC size: 150013 timestamp: 1690026269050 -- name: libffi - version: 3.4.2 - manager: conda - platform: win-64 - dependencies: - vc: '>=14.1,<15.0a0' - vs2015_runtime: '>=14.16.27012' - url: https://conda.anaconda.org/conda-forge/win-64/libffi-3.4.2-h8ffe710_5.tar.bz2 - hash: - md5: 2c96d1b6915b408893f9472569dee135 - sha256: 1951ab740f80660e9bc07d2ed3aefb874d78c107264fd810f24a1a6211d4b1a5 - optional: false - category: main - build: h8ffe710_5 - arch: x86_64 - subdir: win-64 - build_number: 5 - license: MIT - license_family: MIT - size: 42063 - timestamp: 1636489106777 - name: libsqlite version: 3.43.2 manager: conda @@ -5060,6 +4982,27 @@ package: license: Unlicense size: 846363 timestamp: 1696959271392 +- name: libffi + version: 3.4.2 + manager: conda + platform: win-64 + dependencies: + vc: '>=14.1,<15.0a0' + vs2015_runtime: '>=14.16.27012' + url: https://conda.anaconda.org/conda-forge/win-64/libffi-3.4.2-h8ffe710_5.tar.bz2 + hash: + md5: 2c96d1b6915b408893f9472569dee135 + sha256: 1951ab740f80660e9bc07d2ed3aefb874d78c107264fd810f24a1a6211d4b1a5 + optional: false + category: main + build: h8ffe710_5 + arch: x86_64 + subdir: win-64 + build_number: 5 + license: MIT + license_family: MIT + size: 42063 + timestamp: 1636489106777 - name: tk version: 8.6.13 manager: conda @@ -5194,25 +5137,6 @@ package: noarch: python size: 1386212 timestamp: 1690024763393 -- name: git-lfs - version: 3.4.0 - manager: conda - platform: osx-64 - dependencies: {} - url: https://conda.anaconda.org/conda-forge/osx-64/git-lfs-3.4.0-h694c41f_0.conda - hash: - md5: 26e1938cad30e5ffbfd4a6e3b814aa9f - sha256: 7677cf73b481452625e71e9b29b0358d90ed56055a9fdabfff30c3e5c5f1936d - optional: false - category: main - build: h694c41f_0 - arch: x86_64 - subdir: osx-64 - build_number: 0 - license: MIT - license_family: MIT - size: 4049660 - timestamp: 1690413813491 - name: git version: 2.42.0 manager: conda @@ -6364,11 +6288,11 @@ package: dependencies: libexpat: '>=2.5.0,<3.0a0' fontconfig: '>=2.14.2,<3.0a0' + libzlib: '>=1.2.13,<1.3.0a0' fonts-conda-ecosystem: '*' + freetype: '>=2.12.1,<3.0a0' fribidi: '>=1.0.10,<2.0a0' harfbuzz: '>=8.1.1,<9.0a0' - libzlib: '>=1.2.13,<1.3.0a0' - freetype: '>=2.12.1,<3.0a0' url: https://conda.anaconda.org/conda-forge/osx-64/libass-0.17.1-h80904bb_1.conda hash: md5: 9ccad0aebe916aa3715fda9eefe92584 @@ -6405,6 +6329,45 @@ package: license_family: MIT size: 237068 timestamp: 1674829100063 +- name: fonts-conda-ecosystem + version: '1' + manager: conda + platform: osx-64 + dependencies: + fonts-conda-forge: '*' + url: https://conda.anaconda.org/conda-forge/noarch/fonts-conda-ecosystem-1-0.tar.bz2 + hash: + md5: fee5683a3f04bd15cbd8318b096a27ab + sha256: a997f2f1921bb9c9d76e6fa2f6b408b7fa549edd349a77639c9fe7a23ea93e61 + optional: false + category: main + build: '0' + arch: x86_64 + subdir: osx-64 + build_number: 0 + license: BSD-3-Clause + license_family: BSD + noarch: generic + size: 3667 + timestamp: 1566974674465 +- name: fribidi + version: 1.0.10 + manager: conda + platform: osx-64 + dependencies: {} + url: https://conda.anaconda.org/conda-forge/osx-64/fribidi-1.0.10-hbcb3906_0.tar.bz2 + hash: + md5: f1c6b41e0f56998ecd9a3e210faa1dc0 + sha256: 4f6db86ecc4984cd4ac88ca52030726c3cfd11a64dfb15c8602025ee3001a2b5 + optional: false + category: main + build: hbcb3906_0 + arch: x86_64 + subdir: osx-64 + build_number: 0 + license: LGPL-2.1 + size: 65388 + timestamp: 1604417213 - name: harfbuzz version: 8.2.1 manager: conda @@ -6457,10 +6420,10 @@ package: icu: '>=73.2,<74.0a0' libpng: '>=1.6.39,<1.7.0a0' libglib: '>=2.78.0,<3.0a0' + libzlib: '>=1.2.13,<1.3.0a0' + zlib: '*' freetype: '>=2.12.1,<3.0a0' __osx: '>=10.9' - zlib: '*' - libzlib: '>=1.2.13,<1.3.0a0' fontconfig: '>=2.14.2,<3.0a0' libcxx: '>=16.0.6' fonts-conda-ecosystem: '*' @@ -6483,9 +6446,9 @@ package: manager: conda platform: osx-64 dependencies: - libcxx: '>=15.0.7' pcre2: '>=10.40,<10.41.0a0' gettext: '>=0.21.1,<1.0a0' + libcxx: '>=15.0.7' libffi: '>=3.4,<4.0a0' libiconv: '>=1.17,<2.0a0' libzlib: '>=1.2.13,<1.3.0a0' @@ -6524,27 +6487,6 @@ package: license_family: Other size: 90764 timestamp: 1686575574678 -- name: fonts-conda-ecosystem - version: '1' - manager: conda - platform: osx-64 - dependencies: - fonts-conda-forge: '*' - url: https://conda.anaconda.org/conda-forge/noarch/fonts-conda-ecosystem-1-0.tar.bz2 - hash: - md5: fee5683a3f04bd15cbd8318b096a27ab - sha256: a997f2f1921bb9c9d76e6fa2f6b408b7fa549edd349a77639c9fe7a23ea93e61 - optional: false - category: main - build: '0' - arch: x86_64 - subdir: osx-64 - build_number: 0 - license: BSD-3-Clause - license_family: BSD - noarch: generic - size: 3667 - timestamp: 1566974674465 - name: pixman version: 0.42.2 manager: conda @@ -6565,24 +6507,6 @@ package: license_family: MIT size: 336190 timestamp: 1695736270076 -- name: fribidi - version: 1.0.10 - manager: conda - platform: osx-64 - dependencies: {} - url: https://conda.anaconda.org/conda-forge/osx-64/fribidi-1.0.10-hbcb3906_0.tar.bz2 - hash: - md5: f1c6b41e0f56998ecd9a3e210faa1dc0 - sha256: 4f6db86ecc4984cd4ac88ca52030726c3cfd11a64dfb15c8602025ee3001a2b5 - optional: false - category: main - build: hbcb3906_0 - arch: x86_64 - subdir: osx-64 - build_number: 0 - license: LGPL-2.1 - size: 65388 - timestamp: 1604417213 - name: fonts-conda-forge version: '1' manager: conda diff --git a/pixi.toml b/pixi.toml index ba7dfbe..29383d8 100644 --- a/pixi.toml +++ b/pixi.toml @@ -3,6 +3,9 @@ name = "action" version = "0.1.0" description = "Automated Camera Trapping Identification and Organization Network (ACTION)" repository = "https://github.com/humphrem/action" +readme = "README.md" +license = "Apache-2.0" +license-file = "LICENSE" authors = [ "Morgan Humphrey", "David Humphrey"] channels = ["conda-forge"] platforms = ["win-64", "linux-64", "osx-64", "osx-arm64"] diff --git a/aquatic-demo.sh b/scripts/aquatic-demo.sh similarity index 87% rename from aquatic-demo.sh rename to scripts/aquatic-demo.sh index 2aa92ae..5dbe5e0 100755 --- a/aquatic-demo.sh +++ b/scripts/aquatic-demo.sh @@ -1,3 +1,3 @@ -#!/bin/zsh +#!/bin/bash python3 action.py ./video/aquatic-demo.mov -c 0.45 -m 3.0 -s -b 1.0 -d -e aquatic diff --git a/terrestrial-demo.sh b/scripts/terrestrial-demo.sh similarity index 88% rename from terrestrial-demo.sh rename to scripts/terrestrial-demo.sh index b2b76a3..d70d713 100755 --- a/terrestrial-demo.sh +++ b/scripts/terrestrial-demo.sh @@ -1,3 +1,3 @@ -#!/bin/zsh +#!/bin/bash python3 action.py ./video/terrestrial-demo.mov -c 0.45 -m 3.0 -s -b 1.0 -d -e terrestrial diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 0000000..22a7d32 --- /dev/null +++ b/src/__init__.py @@ -0,0 +1 @@ +from .action import main # noqa: F401 diff --git a/src/action.py b/src/action.py new file mode 100755 index 0000000..b139ff6 --- /dev/null +++ b/src/action.py @@ -0,0 +1,364 @@ +""" +This is the main module for ACTION. It handles parsing arguments from the user, +loading and managing resources, and processing detections into clips. +""" + +import os +import sys +import time +import logging + +from .clip_manager import ( + ClipManager, + remove_clips_dir, + remove_output_dir, + create_output_dir, +) +from .yolo_fish_detector import YoloFishDetector +from .megadetector_detector import MegadetectorDetector +from .utils import format_time, format_percent, get_video_paths + +import cv2 + + +# We use converted ONNX models for YOLO-Fish (https://github.com/tamim662/YOLO-Fish) +# and Megadetector (https://github.com/microsoft/CameraTraps) +def load_detector(environment, min_duration, buffer, confidence, logger): + """ + Load the appropriate detector based on the environment provided. + + Args: + environment (str): The type of detector to load. Must be either "aquatic" or "terrestrial". + min_duration (float): The minimum duration of a generated clip + buffer (float): An optional number of seconds to add before/after a clip + confidence (float): The confidence level to use + logger (logging.Logger): The logger to use for logging messages. + + Returns: + detector (object): An instance of the appropriate detector. + + Raises: + TypeError: If the any args are not of the correct type + """ + + # Make sure any user-provided flags for detection are valid before we use them + if buffer and buffer < 0.0: + raise TypeError("Error: minimum buffer cannot be negative") + + if min_duration and min_duration <= 0.0: + raise TypeError("Error: minimum duration must be greater than 0.0") + + if confidence and (confidence <= 0.0 or confidence > 1.0): + raise TypeError("Error: confidence must be greater than 0.0 and less than 1.0") + + detector = None + if environment == "terrestrial": + detector = MegadetectorDetector(logger, min_duration, buffer, confidence) + elif environment == "aquatic": + detector = YoloFishDetector(logger, min_duration, buffer, confidence) + else: + raise TypeError("environment must be one of aquatic or terrestrial") + + detector.load() + return detector + + +# Defining the function process_frames, called in main +def process_frames( + video_path, + cap, + detector, + clips, + fps, + total_frames, + frames_to_skip, + logger, + args, +): + """ + Process frames from a video file and create clips based on detections. + + Args: + video_path (str): The path to the video file. + cap (cv2.VideoCapture): The video capture object. + detector (object): The detector to use for detecting objects in frames. + clips (ClipManager): The clip manager for managing clips. + fps (int): The frames per second of the video. + total_frames (int): The total number of frames in the video. + frames_to_skip (int): The number of frames to skip between detections + logger (logging.Logger): The logger to use for logging messages. + args (argparse.Namespace): The command line arguments. + + Returns: + None + """ + buffer_seconds = detector.buffer + min_detection_duration = detector.min_duration + show_detections = args.show_detections + + # Number of frames per minute of video time + frames_per_minute = 60 * fps + # Frame number for the next progress message + next_progress_frame = frames_per_minute + + # Track when there are is something in frame as detection events + detection_start_time = None + detection_highest_confidence = 0 + detection_event = False + + frame_count = 0 + + # Loop over all frames in the video + while cap.isOpened(): + ret, frame = cap.read() + # If there isn't another frame, we're done + if not ret: + break + + # If a detection is already happening, then we know we're going to + # record for a given period, so we can skip ahead to the frame where + # the detection period ends. However, we then need to check *all* the + # frames after this within the buffer period (i.e., so we don't overlap + # with the end buffer period in the previous clip). If we detect something + # in these frames, we should extend this detection period; otherwise + # we end it and create a new clip. + if detection_event: + # Calculate the number of frames to skip ahead. We know that we + # want to record for a minimum duration, and potentially we add + # a buffer period in seconds. + skip_ahead_frames = int((min_detection_duration + buffer_seconds) * fps) + logger.debug(f"Detection event, skipping ahead {skip_ahead_frames} frames") + + # Skip ahead the number of frames that will be in the clip + cap.set(cv2.CAP_PROP_POS_FRAMES, frame_count + skip_ahead_frames) + frame_count += skip_ahead_frames + + # Check some frames within the buffer period for a detection, to see if + # we should extend this detection period, or if it's OK to end it. NOTE: + # if `buffer_seconds` is 0, at least check the next frame. + for i in range(max(1, int(buffer_seconds * fps))): + ret, frame = cap.read() + if not ret: + break + frame_count += 1 + + # Only process frames that are multiples of frames_to_skip + # or the last frame in the video. + if i % frames_to_skip == 0 or frame_count == total_frames - 1: + logger.debug( + f"Checking frame {frame_count} before ending detection event" + ) + # If a detection is made, extend the current detection period + boxes = detector.detect(frame) + if len(boxes) > 0: + detection_highest_confidence = max( + detection_highest_confidence, max(box[4] for box in boxes) + ) + logger.info( + f"{detector.class_name} detected, extending detection event: {format_time(frame_count / fps + buffer_seconds)} (max confidence={format_percent(detection_highest_confidence)})" + ) + if show_detections: + detector.draw_detections(frame, boxes, video_path) + break + else: + # If no detection was made within the buffer period, and we didn't + # extend, end the detection period now and create a new clip. + detection_end_time = frame_count / fps + buffer_seconds + logger.info( + f"Detection period ended: {format_time(detection_end_time)} (duration={format_time(detection_end_time - detection_start_time)}, max confidence={format_percent(detection_highest_confidence)})" + ) + clips.create_clip( + detection_start_time, + detection_end_time, + video_path, + ) + + # Reset the detection period + detection_event = False + detection_highest_confidence = 0 + + # Start again reading the next frame + continue + + # If we're not already in a detection event, process every n frames + # vs. every frame for speed (e.g., every 15 of 30fps). We also check + # the last frame, so we don't miss anything at the edge. + if frame_count % frames_to_skip == 0 or frame_count == total_frames - 1: + boxes = detector.detect(frame) + + # If there are one ore more detections + if len(boxes) > 0: + detection_highest_confidence = max( + detection_highest_confidence, max(box[4] for box in boxes) + ) + + # If we're not already in a detection event, start one + if not detection_event: + detection_start_time = max(0, frame_count / fps - buffer_seconds) + logger.info( + f"{detector.class_name} detected, starting detection event: {format_time(frame_count / fps)} (max confidence={format_percent(detection_highest_confidence)})" + ) + if show_detections: + detector.draw_detections(frame, boxes, video_path) + detection_event = True + + # We've finished processing this frame + frame_count += 1 + + # Print a progress message every minute of video time so we know what's going on + if frame_count >= next_progress_frame: + logger.info( + f"\nProgress: {format_percent(frame_count / total_frames)} processed ({frame_count}/{total_frames} frames, {format_time(frame_count / fps)})\n" + ) + next_progress_frame += frames_per_minute + + # Before we finish the program, check if there's a detection event in progress + # and if there is, end it now so we don't lose the final clip. + if detection_event: + detection_end_time = frame_count / fps + buffer_seconds + logger.info( + f"Detection period ended: {format_time(detection_end_time)} (duration={format_time(detection_end_time - detection_start_time)}, max confidence={format_percent(detection_highest_confidence)})" + ) + clips.create_clip( + detection_start_time, + detection_end_time, + video_path, + ) + + +# Main part of program to do setup and start processing frames in each file +def main(args): + """ + The main function of the program. Sets up the logger, validates arguments, + loads the detector, and processes frames from each video file. + + Args: + args (argparse.Namespace): The command line arguments. + """ + + # Create a logger for this module and set the log level + logger = logging.getLogger(__name__) + logging.basicConfig(level=args.log_level, format="%(message)s") + + delete_clips = args.delete_clips + output_dir = args.output_dir + video_paths = get_video_paths(args.filename) + logger.debug(f"Got input files: {video_paths}") + + # Validate argument parameters from user before using them + if len(video_paths) < 1: + logger.error("Error: you must specify one or more video filenames to process") + sys.exit(1) + + # Load YOLO-Fish or Megadetector, based on `-e` value + detector = None + try: + detector = load_detector( + args.environment, args.min_duration, args.buffer, args.confidence, logger + ) + except Exception as e: + logger.error(f"There was an error: {e}") + sys.exit(1) + + cap = None + clips = None + + # Initialize the output_dir if specified + if output_dir: + # If `-d`` was specified, delete old clips first + if delete_clips: + remove_output_dir(output_dir, logger) + # Create the output directory if it doesn't exist + create_output_dir(output_dir) + + try: + # Create a queue manager for clips to be processed by ffmpeg + clips = ClipManager(logger, output_dir) + + # Keep track of total time to process all files, recording start time + total_time_start = time.time() + + # Loop over all the video file paths and process each one + for i, video_path in enumerate(video_paths, start=1): + # Make sure this video path actually exists before we try to use it + if not os.path.exists(video_path): + logger.info(f"Video path {video_path} does not exist, skipping.") + continue + + file_start_time = time.time() + + # If the user requests it via -d flag, and isn't using a common output_dir + # remove old clips first + if not output_dir and delete_clips: + remove_clips_dir(video_path, logger) + + # Setup video capture for this video file + cap = cv2.VideoCapture(video_path) + fps = cap.get(cv2.CAP_PROP_FPS) + frames_to_skip = args.skip_frames or int(fps / 2.0) + total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) + duration = total_frames / fps + + logger.info( + f"\nStarting file {i} of {len(video_paths)}: {video_path} - {format_time(duration)} - {total_frames} frames at {fps} fps, skipping every {frames_to_skip} frames" + ) + logger.info( + f"Using confidence threshold {detector.confidence}, minimum clip duration of {detector.min_duration} seconds, and {detector.buffer} seconds of buffer." + ) + + # If we're not using a common clips dir, reset the counter for future clips + if not output_dir: + clips.reset_clip_count() + + clip_count_before = clips.get_clip_count() + + # Process the video's frames into clips + process_frames( + video_path, + cap, + detector, + clips, + fps, + total_frames, + frames_to_skip, + logger, + args, + ) + + clip_count_after = clips.get_clip_count() + clips_processed = clip_count_after - clip_count_before + + file_end_time = time.time() + logger.info( + f"Finished file {i} of {len(video_paths)}: {video_path} (total time to process file {format_time(file_end_time - file_start_time)}). Processed {total_frames} frames into {clips_processed} clips" + ) + + # Clean-up the resources we have open, if necessary + if cap is not None: + cap.release() + + cv2.destroyAllWindows() + cv2.waitKey(1) + + # Keep track of total time to process all files, recording end time + total_time_end = time.time() + logger.info( + f"\nFinished. Total time for {len(video_paths)} files: {format_time(total_time_end - total_time_start)}" + ) + + except KeyboardInterrupt: + logger.warning("Interrupted by user, cleaning up...") + clips.stop() + except Exception as e: + logger.error(f"There was an error: {e}") + finally: + # Clean-up the resources we have open, if necessary + if cap is not None: + cap.release() + + cv2.destroyAllWindows() + cv2.waitKey(1) + + # Wait for the ffmpeg clip queue to complete before we exit + if clips is not None: + clips.cleanup() diff --git a/base_detector.py b/src/base_detector.py similarity index 100% rename from base_detector.py rename to src/base_detector.py diff --git a/clip_manager.py b/src/clip_manager.py similarity index 99% rename from clip_manager.py rename to src/clip_manager.py index e051c45..8885ae9 100644 --- a/clip_manager.py +++ b/src/clip_manager.py @@ -12,7 +12,7 @@ from multiprocessing import Process, Queue, Event from queue import Empty -from utils import format_time +from .utils import format_time def get_clips_dir(video_path): diff --git a/megadetector_detector.py b/src/megadetector_detector.py similarity index 99% rename from megadetector_detector.py rename to src/megadetector_detector.py index de068f7..a6841b2 100644 --- a/megadetector_detector.py +++ b/src/megadetector_detector.py @@ -8,7 +8,7 @@ import numpy as np from onnxruntime.capi._pybind_state import get_available_providers -from base_detector import BaseDetector +from .base_detector import BaseDetector # The Megadetector ONNX model should be in the ./models directory and needs to be pulled with git-lfs megadetector_model_path = "models/md_v5a_1_3_640_640_static.onnx" diff --git a/utils.py b/src/utils.py similarity index 100% rename from utils.py rename to src/utils.py diff --git a/yolo_fish_detector.py b/src/yolo_fish_detector.py similarity index 99% rename from yolo_fish_detector.py rename to src/yolo_fish_detector.py index 1c545aa..ad28ad9 100644 --- a/yolo_fish_detector.py +++ b/src/yolo_fish_detector.py @@ -7,7 +7,7 @@ import numpy as np -from base_detector import BaseDetector +from .base_detector import BaseDetector # Code inspired by https://github.com/Tianxiaomo/pytorch-YOLOv4, used under Apache-2.0 License # https://github.com/Tianxiaomo/pytorch-YOLOv4/blob/master/License.txt