geotrax.utils.file_utils¶
Where each stage writes its results, how they are written safely, and small helpers for location IDs, orthophoto folders and videos. The result files themselves are described in the extract and georeference guides.
Result paths¶
build_result_path
¶
build_result_path(source: Path, result_type: str, output_cfg: dict | None = None, viz_mode: int | None = None, ext: str | None = None) -> Path | None
Return the path where a stage writes one kind of result for an input video.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Path
|
Input video. |
required |
result_type
|
str
|
One of |
required |
output_cfg
|
dict | None
|
The |
None
|
viz_mode
|
int | None
|
Visualization mode, for |
None
|
ext
|
str | None
|
Video file extension without the dot, for |
None
|
Returns:
| Type | Description |
|---|---|
Path | None
|
The path, which need not exist yet, or |
Examples:
>>> build_result_path(Path("D1/A1.mp4"), "georeferenced")
PosixPath('D1/results/A1.csv')
>>> build_result_path(Path("D1/A1.mp4"), "visualized", viz_mode=2, ext="mp4")
PosixPath('D1/results/A1_mode_2.mp4')
Source code in geotrax/utils/file_utils.py
check_if_results_exist
¶
check_if_results_exist(file: Path, result_type: str, viz_mode: int | None = None, ext: str | None = None, output_cfg: dict | None = None) -> tuple[bool, Path | None]
Check whether a result of an input video already exists; batch skips the stage if so.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
Path
|
Input video. |
required |
result_type
|
str
|
A result type accepted by
|
required |
viz_mode
|
int | None
|
Visualization mode, for |
None
|
ext
|
str | None
|
Video file extension, for |
None
|
output_cfg
|
dict | None
|
The |
None
|
Returns:
| Type | Description |
|---|---|
tuple[bool, Path | None]
|
|
Source code in geotrax/utils/file_utils.py
get_output_dir
¶
Return the output directory for an input file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Path
|
Input video (or any file whose results are looked up). |
required |
output_cfg
|
dict | None
|
The |
None
|
Returns:
| Type | Description |
|---|---|
Path
|
|
Source code in geotrax/utils/file_utils.py
output_folder_exclusion
¶
Return how a directory scan should skip the configured output folder.
A relative folder is recreated next to every input, so it is skipped by name. An absolute
folder is one shared directory, so it is skipped by its resolved path only: skipping its name as
well would also drop every unrelated input folder that shares it, e.g. -of /mnt/out/D1
dropping every D1 drone folder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_cfg
|
dict | None
|
The |
None
|
Returns:
| Type | Description |
|---|---|
tuple[str | None, Path | None]
|
|
Source code in geotrax/utils/file_utils.py
read_recorded_stab_anchor
¶
Return the stabilization anchor frame that extract recorded for an input video.
Stabilized track coordinates are relative to the processing.cut_frame_left that extract ran
with, which it records in the run-metadata YAML. Stages that run separately (georeference,
visualize) must use that frame rather than their own cut_frame_left.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Path
|
Input video. |
required |
output_cfg
|
dict | None
|
The |
None
|
Returns:
| Type | Description |
|---|---|
int | None
|
The recorded frame index, or |
Source code in geotrax/utils/file_utils.py
no_vehicles_detected
¶
Tell whether extract finished on an input video without finding any vehicle.
Such a run writes no tracks file, the file whose presence marks a finished extraction, so its
absence alone cannot separate a video in which nothing was detected from one that was never
extracted. The run-metadata YAML settles it: extract records the number of saved vehicles
there, and writes it even when there are none. batch uses this to count such a video as done
instead of failed, and georeference and visualize to stop without an error.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Path
|
Input video. |
required |
output_cfg
|
dict | None
|
The |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in geotrax/utils/file_utils.py
DEFAULT_OUTPUT
module-attribute
¶
DEFAULT_OUTPUT = {'folder': 'results', 'tracks_postfix': '', 'georeferenced_postfix': '', 'stab_transform_postfix': '_vid_transf', 'geo_transform_postfix': '_geo_transf', 'visualization_postfix': '', 'metadata_postfix': ''}
Fallback output section, reproducing the historical results/ layout.
Used only when no config is passed to the path helpers (output_cfg=None), or for a key missing
from a custom config. The pipeline stages always pass the loaded output section.
Writing files¶
atomic_output
¶
Write a file atomically: yield a temporary path that replaces path only if the block completes.
batch treats any existing result as complete, so an interrupted write (Ctrl+C, a SLURM kill, a
full disk) must not leave a truncated file behind. Inside this context, path holds either its
previous content or the complete new one, and the temporary file is always removed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
Final destination. |
required |
keep_suffix
|
bool
|
Name the temporary file |
False
|
Yields:
| Type | Description |
|---|---|
Path
|
The temporary path to write to, in the same directory as |
Examples:
Source code in geotrax/utils/file_utils.py
convert_to_serializable
¶
Recursively convert paths and argparse namespaces into YAML/JSON-serializable values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
A value, possibly nesting dicts, lists, |
required |
Returns:
| Type | Description |
|---|---|
Any
|
|
Source code in geotrax/utils/file_utils.py
detect_delimiter
¶
Guess whether a text file is comma, space or tab separated.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
Path
|
File to inspect. |
required |
lines_to_check
|
int
|
Number of leading lines to count delimiters in. |
5
|
Returns:
| Type | Description |
|---|---|
str
|
The most frequent of |
Source code in geotrax/utils/file_utils.py
Locations¶
determine_location_id
¶
Extract the location ID from a filename: its first run of letters.
The location ID selects the orthophoto, segmentation and master frame of a recording site. A
digit, _ or - ends the run.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Path
|
Video or result file named after its location. |
required |
logger
|
Logger
|
Receives the detected ID; without one, an error is printed instead. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
The location ID. |
Raises:
| Type | Description |
|---|---|
SystemExit
|
The filename contains no letters. |
Examples:
>>> determine_location_id(Path("A1.mp4"))
'A'
>>> determine_location_id(Path("2025-01-01_A_PM1.mp4"))
'A'
>>> determine_location_id(Path("A1_AV.csv"))
'A'
Source code in geotrax/utils/file_utils.py
get_ortho_folder
¶
get_ortho_folder(source: Path, ortho_folder: Path | None, logger: Logger, critical: bool = True) -> Path
Return the orthophoto folder for an input, given explicitly or found from the project layout.
Without an explicit folder, the nearest ancestor of source named PROCESSED or DATASET is
looked up, and its sibling ORTHOPHOTOS folder is used.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Path
|
Input video or result file. |
required |
ortho_folder
|
Path | None
|
Explicit folder ( |
required |
logger
|
Logger
|
Receives the outcome. |
required |
critical
|
bool
|
Exit on failure instead of returning |
True
|
Returns:
| Type | Description |
|---|---|
Path
|
The orthophoto folder, or |
Raises:
| Type | Description |
|---|---|
SystemExit
|
The folder cannot be found and |
Source code in geotrax/utils/file_utils.py
Videos¶
get_video_dimensions
¶
Return the frame size of a video.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
video_path
|
Path
|
Video file. |
required |
Returns:
| Type | Description |
|---|---|
tuple[int, int]
|
|
Source code in geotrax/utils/file_utils.py
get_keyframe_times
¶
Return the presentation times of a video's keyframes.
Reads ffprobe's per-packet pts_time,flags output and keeps the packets flagged K. ffprobe is
run with an argument list rather than through a shell, so any path works on every platform.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
video_path
|
Path
|
Video file; |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Keyframe times in seconds, in stream order. |
Raises:
| Type | Description |
|---|---|
CalledProcessError
|
ffprobe failed. |
Source code in geotrax/utils/file_utils.py
determine_suffix_and_fourcc
¶
Return the container suffix and FourCC codec for annotated videos on this platform.
Returns:
| Type | Description |
|---|---|
tuple[str, str]
|
|