CPP_Birdtracker
A C++ object tracking program specifically for lunar bird tracking
|
LunAero is a hardware and software project using OpenCV to automatically track birds as they migrate in front of the moon. This repository contains software used to track bird silhouettes from video produced by the LunAero moontracking robot. For information on the hardware and software for the robot, please see https://github.com/BlueNalgene/LunAero_C .
Created by , working in the lab of -S-Bridge.
The Birdtracker_CPP repository is an updated version of the LunAero silhouette tracking software. The previous implementation (archived at https://github.com/BlueNalgene/LunAero) was a Python 3 script. This updated version is C++ code designed for Linux, and includes many improvements, additional features, and is generally more mature code.
To follow my instructions, you need to use the Linux terminal, where you can copy and paste the following scripts. To open the Linux terminal, press ctrl
+alt
+t
.
When you install this for the first time on to a Linux system, you will need certain repositories installed to make it. If you are using a Debian-like operating system (e.g. Ubuntu or Mate), use the following command in your terminal. (Note: this looks way more daunting than it really is, your OS will have most of these installed with a default configuration):
Then comes the step that is actually daunting. This program requires OpenCV compiled with contrib
. This means you MUST compile OpenCV from source. This has been tested for OpenCV version 4.4.0-pre
. Older versions may not work. There are many options for compiling, I used the following on my Ubuntu box:
Next, use git
to pull this repository. Once the LunAero git
repository is pulled, you can compile it. To do this, execute the lines in the terminal:
This script downloads the everything you need to compile the program from this git
repository. Once you have the repository on your device and have installed all of the relevant packages from the apt
command above, enter the LunAero_C folder, and issue the following command:
The make
command follows the instructions from the Makefile
to compile your program. If you ever make changes to the source material, remember to edit the Makefile
to reflect changes to the packages used or the required C++ files. If make
runs correctly, your terminal will print some text that looks like g++ blah blah -o frame_extractor.o
spanning a few lines. If the output is significantly longer than that and includes words like ERROR or WARNING, something may have gone wrong, and you should read the error messages to see if something needs to be fixed.
To run the frame_extractor.o
, use the Linux terminal. There, you enter the command for the program and information about the video you want to run the program on. It is convenient if you navigate to the directory where you made the file:
A file called settings.cfg
has been provided to give the user the ability to modify settings without needing to recompile. Before running, LunAero for the first time, it is prudent to check that you are happy with the default settings. This is especially true for the General Settings at to top of the file.
A python3 script called fog_removal.py
has been added to simplify selecting the appropriate value for BLACKOUT_THRESH. Run this script prior to running a video to visually determine the appropriate value for this variable.
The following options are available for command line switches:
Full Command | Short Command | Expected Input | Description |
---|---|---|---|
--help | -h | none | Show this info |
--version | -v | none | Print version info to terminal |
--input | -i | path to vid file | Specify path to input video |
--config-file | -c | path to config | Specify config file |
--osf-path | -osf | url to osf vid | Specify path to osf video |
The "Short Command" is just a helpful shorter version to replace the full command. Using either the --help
or --version
commands will print the relevant info to the terminal and exit, without running the full program. Each command or input must be separated by a space.
Each of the commands --input
, --config-file
, and --osf-path
require input arguments to follow the switch. These inputs are paths to the local storage location (in the case of --input
and --config-file
) or a url path (in the case of --osf-path
). If any of these switches are issued without an appropriate path argument following it, the program will crash. You may not issue both the --input
and --osf-path
in the same terminal command, as these switches conflict. (You can only have one source video!). You do not need to issue a --config-file
switch if you are using the settings.cfg
file stored in the default location.
Some example commands:
Remember: any time you want to check the usage information to view what each command switch does, use --help
in terminal, e.g.:
Run Birdtracker_CPP on videos directly from storage repositories on Open Science Framework (OSF), you need to do some extra steps to pull in the API and enable access.
OSFClient
must be installed on your system. To install it, use the pip
package manager e.g.: .bashrc
file to contain the export command. OSFPROJECT
in settings.cfg.The best choice for file format is MP4, as that is what everything is configured in the code to use. Other formats, such as the raw H264 video format which is output by LunAero, are converted to MP4 prior to starting the run. This video conversion may add time to your run, so consider this if you run this on shared systems with restricted time access.
Video conversion is done using ffmpeg
, and this program is thus a prerequisite for use. If you are on a Linux system, you probably have ffmpeg
installed already though. So don't sweat it.
This program executes birdtracking efficiently by splitting tasks between CPU threads using the fork()
command. Data which are shared between the forks such as frame numbers and matrices, is shared using memory mapping such that they can be accessed easily using pointers. The wait
command is used to sync across forks, and the script executes code concurrently. When all frames of the video are completed, the data are passed through post-processing and the program exits. A simplified flowchart explains the process visually:
Individual steps with more detail, but still simplified:
While there are a few image processing functions peppered through the code, most of these are simple color conversions and cropping. The meat of the image processing occurs within the "Tiers". This is where contours of silhouettes crossing the moon are detected. They all operate on the same frames, but some "Tiers" offer different capabilities. Each have strengths and weaknesses.
See the descriptions and code in the documentation here.
Each Tier process has values which may be manipulated in settings.cfg
to customize the behavior during runtime.
On the test computer (Intel(R) Core(TM) i7-7500U CPU @ 2.70GHz), the run time of this program is approximately 7 times the length of the input video, when the video is 1920x1080 at 30fps. So a 5 minute video will run in about 35 minutes.
Why does it take so long? Most of the computation time is spent opening and closing image files. This cannot be avoided.
This program outputs several files during the run. The files are stored by default in the folder Birdtracker_Output
. Data files are stored in the data
directory, if the settings are configured to output the frames, a frames
directory is created containing png
images of each frame in the video, this will also create an output.mp4
slideshow of the frames.
The data files in the data
directory are as follows:
Filename | Description |
---|---|
log.log | Debugging log of run |
metadata.csv | Input video metadata |
boxes.csv | Boundary values of largest contour in frame |
ellipses.csv | Properties of the largest contour in frame |
Tier1.csv | Tier 1 detected silhouettes |
Tier2.csv | Tier 2 detected silhouettes |
Tier3.csv | Tier 3 detected silhouettes |
Tier4.csv | Tier 4 detected silhouettes |
mixed_tiers.csv | All tier data mixed into a single file |
offscreen_moon.csv | Number of pixels where the moon is touching the screen edge |
DEBUG_COUT
is set to true
in settings.cfg, this log file will be created. Debugging messages are written to this file over the course of the run. Note, that this does not include any STDOUT or STDERR messages. If there is an error message written to the terminal, for example, you must fetch that another way.adaptiveThreshold
method. See tier_one()adaptiveThreshold
method.CONCAT_TIERS
is set to true
.SIMP_ELL
is set to true
.If the OUTPUT_FRAMES
value in settings is set to true
, the Birdtracker will output every cropped frame to the frames
directory as a png
file with the naming format based on the frame number. These frames will remain in the directory unless the setting EMPTY_FRAMES
is set to true
.
If the program is outputting frames, and GEN_SLIDESHOW
is set to true
, the frames will be contatenated into a slideshow saved as output.mp4
.
If the program generates a slideshow, and TIGHT_CROP
is set to true
, the output video will be modified at the end of the run to crop each frame to a smaller size. The size of these frames is based on the extreme edges of the moon as reported in boxes.csv
+ 10 pixels. The cropped video is saved as output.mp4
.
Something always goes wrong. It is the way of things. When something inevitably does go wrong with LunAero program, you should first check the logs. If the setting DEBUG_COUT
in settings.cfg
is set to true
the program will attempt to save a log file in the same directory where the videos are saved for each run. This is very detailed, so searching for keywords like WARNING
and ERROR
are suggested.
If you need help, raise an Issue on this git
repository with descriptive information so the package maintainer can help you.
The source code is documented with the Doxygen
standard. Every function and most variables are heavily commented to make it easy for you. You can view the documentation online by going to: https://bluenalgene.github.io/CPP_Birdtracker/html/index.html .