Metadata-Version: 2.5
Name: OTAnalytics
Version: 0.7.6
Summary: OTAnalytics is a core module of the OpenTrafficCam framework to perform traffic analysis on trajectories of road users.
Project-URL: Homepage, https://opentrafficcam.org/
Project-URL: Documentation, https://opentrafficcam.org/overview/
Project-URL: Repository, https://github.com/OpenTrafficCam/OTAnalytics
Project-URL: Issues, https://github.com/OpenTrafficCam/OTAnalytics/issues
Project-URL: Changelog, https://github.com/OpenTrafficCam/OTAnalytics/releases
Author-email: OpenTrafficCam contributors <team@opentrafficcam.org>, platomo GmbH <info@platomo.de>
License-Expression: GPL-3.0-only
License-File: LICENSE
Keywords: OpenTrafficCam,Traffic Analysis,Traffic Counting,Trajectories
Classifier: Development Status :: 4 - Beta
Classifier: License :: OSI Approved :: GNU General Public License v3 (GPLv3)
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Requires-Python: ==3.12.*
Requires-Dist: aioboto3==15.5.0
Requires-Dist: av==13.0.0
Requires-Dist: customtkinter==5.2.2
Requires-Dist: fastapi==0.128.0
Requires-Dist: ijson==3.3.0
Requires-Dist: matplotlib==3.11.2
Requires-Dist: more-itertools==10.7.0
Requires-Dist: nicegui==2.24.1
Requires-Dist: numpy==2.1.1
Requires-Dist: opencv-python-headless==4.10.0.84
Requires-Dist: openpyxl==3.1.5
Requires-Dist: pandas==2.3.3
Requires-Dist: pillow==13.0.0
Requires-Dist: polars==1.32.3
Requires-Dist: pyarrow==23.0.1
Requires-Dist: pywin32==310; sys_platform == 'win32'
Requires-Dist: rasterio==1.5.1
Requires-Dist: requests==2.33.0
Requires-Dist: seaborn==0.13.2
Requires-Dist: shapely==2.0.7
Requires-Dist: starlette==0.49.1
Requires-Dist: tqdm==4.67.1
Requires-Dist: ujson==5.13.0
Requires-Dist: uvicorn==0.32.1
Description-Content-Type: text/markdown

# OTAnalytics

[![PyPI version](https://img.shields.io/pypi/v/OTAnalytics.svg)](https://pypi.org/p/OTAnalytics/)
[![Tests](https://github.com/OpenTrafficCam/OTAnalytics/actions/workflows/test.yml/badge.svg?tag=latest)](https://github.com/OpenTrafficCam/OTAnalytics/actions/workflows/test.yml?query=tag%3Alatest)
[![Tests](https://github.com/OpenTrafficCam/OTAnalytics/actions/workflows/build-release.yml/badge.svg)](https://github.com/OpenTrafficCam/OTAnalytics/actions/workflows/build-release.yml)

OTAnalytics is a core module of the [OpenTrafficCam framework](https://github.com/OpenTrafficCam) designed to perform
traffic analysis on trajectories of road users tracked by [OTVision](https://github.com/OpenTrafficCam/OTVision) or
other tools in videos recorded by [OTCamera](https://github.com/OpenTrafficCam/OTCamera) or other camera systems.

## Overview

OTAnalytics provides tools for analyzing traffic data, including:

- Processing trajectory data of road users
- Defining and analyzing traffic flows
- Counting vehicles/road users assigned to flows
- Generating event lists and statistics
- Visualizing traffic data

The application offers multiple user interfaces:

- A graphical desktop interface (using CustomTkinter)
- A command-line interface for automation and batch processing

## Installation

### Requirements

- Python 3.12
- Dependencies listed in [pyproject.toml](pyproject.toml)

### Installation from GitHub Releases (Recommended)

The easiest way to install OTAnalytics is to download the latest release from
the [GitHub Releases page](https://github.com/OpenTrafficCam/OTAnalytics/releases).

1. Go to the [OTAnalytics Releases page](https://github.com/OpenTrafficCam/OTAnalytics/releases)
2. Download the latest release for your operating system
3. Extract the downloaded archive
4. Run the application using the instructions in the [Usage](#usage) section

### Installation from Source (Linux/macOS)

If you prefer to install from source:

1. Clone the repository:

   ```bash
   git clone https://github.com/OpenTrafficCam/OTAnalytics.git
   cd OTAnalytics
   ```

2. Run the installation script:
   ```bash
   ./install.sh
   ```

### Installation from Source (Windows)

If you prefer to install from source:

1. Clone the repository:

   ```cmd
   git clone https://github.com/OpenTrafficCam/OTAnalytics.git
   cd OTAnalytics
   ```

2. Run the installation script:
   ```cmd
   install.cmd
   ```

## Usage

### Starting the Application

#### Desktop GUI (Default)

On Linux/macOS:

```bash
./start_gui.sh
```

On Windows:

```cmd
start_gui.cmd
```

#### Command Line Interface

On Linux/macOS:

```bash
./start_gui.sh --cli
```

On Windows:

```cmd
start_gui.cmd --cli
```

### Configuration

OTAnalytics can be configured using:

1. Command-line arguments
2. Configuration files (YAML format)

Example configuration options:

- Specify track files for analysis
- Define flow and section configurations using otflow files
- Configure export formats and counting intervals
- Control parallelization with multiple processes
- Include or exclude specific road user classes

### Traffic Statistics and Geo Track Export

Two optional exports for the CLI, both off by default. Enable them in the
otconfig (under `analysis.export`, which is JSON) or with a CLI flag:

```json
{
  "analysis": {
    "export": {
      "traffic_statistics": {
        "enabled": true,
        "interval_minutes": 1,
        "max_speed_kmh": 200,
        "peak_window_minutes": 60,
        "peak_step_minutes": 15,
        "class_groups": {
          "LV": ["car", "car_with_trailer", "private_van", "private_van_with_trailer", "delivery_van", "delivery_van_with_trailer", "motorcyclist"],
          "SV": ["truck", "truck_with_trailer", "truck_with_semitrailer", "bus"],
          "BIKE": ["bicyclist", "bicyclist_with_trailer", "cargo_bike_driver", "scooter_driver"],
          "PED": ["pedestrian"]
        }
      },
      "geo_tracks": true
    }
  }
}
```

Both keys are optional and only the relevant part of the otconfig is shown. Defaults:
`enabled` true, `interval_minutes` 1; `class_groups` defaults to
every classification being its own group; `geo_tracks` defaults to false.

| Export             | otconfig key         | CLI flag               |
| ------------------ | -------------------- | ---------------------- |
| Traffic statistics | `traffic_statistics` | `--traffic-statistics` |
| Geo tracks         | `geo_tracks`         | `--geo-track-export`   |

`--traffic-statistics` uses the `traffic_statistics` block of the otconfig if there
is one, otherwise the defaults.

**Traffic statistics** are written per UTC day, because the timezone of the
camera is not known while processing. All times in the files are the UTC times
of the input files, written as in the counts CSV (`YYYY-MM-DD HH:MM:SS`, with
fractions of a second only where a time has them). Each day
has a JSON file and four CSV files in
`{save_dir}/{save_stem}.traffic_statistics_{YYYY-MM-DD}`, where `{i}` is
`interval_minutes`:

| File                           | Content                                                                                          |
| ------------------------------ | ------------------------------------------------------------------------------------------------ |
| `.json`                        | per flow and for all flows, per class group: totals, peak hour, average speed and v85 of the day |
| `.counts_{i}min.csv`           | counts per flow, class group and interval                                                        |
| `.speeds_{i}min.csv`           | speed values per flow, class group and interval                                                  |
| `.speeds_all_flows_{i}min.csv` | speed values per class group and interval over all flows                                         |
| `.speeds_total_{i}min.csv`     | speed values per interval over all flows and class groups, classification `total`                |

The CSV files have the format of the counts CSV export, with the class group in
the `classification` column. The speed files have the columns `speed count`
(number of plausible speeds), `avg speed` and `v85` (km/h) instead of `count`,
and the files over all flows have no flow and section columns.

A road user is counted at most once per day. Speed is the flow distance divided
by the time between the road user's start and end event, in km/h. Only speeds
above 0 and up to `max_speed_kmh` enter the speed figures. v85 is the 85th
percentile of the measured speeds (nearest rank). The JSON file contains results
plus `coverage` and `as_of`. To continue a day after a restart, pass the road
user assignment CSV files of the day with `--traffic-statistics-restore`. Counts
and speeds are rebuilt from them with the current settings, and coverage and
`as_of` are taken from an existing day file. A classification may belong to at
most one class group. Only the time ranges of the processed videos count as
covered: intervals between videos (e.g. a camera outage) have no rows in the CSV
files. A covered interval without plausible speeds has a `speed count` of 0 and
empty speed values.

**Geo tracks** are written to
`{save_dir}/{save_stem}_{YYYY-MM-DD_HH-MM-SS}.geo_tracks.json.bz2` (timestamp of
the earliest detection of the chunk, UTC). The bz2-compressed JSON file looks like
this:

```json
{"metadata": {"format": "otanalytics.geo_tracks", "version": 1},
 "data": {"detections": [
   {"track-id": "...", "class": "car", "geo_x": 455123.12, "geo_y": 5701234.56,
    "occurrence": 1761572520.04}
 ]}}
```

`occurrence` is the Unix timestamp in seconds. Detections without a geo coordinate
are left out, and if none remain no file is written.

## Features

- **Track Analysis**: Process and analyze trajectory data from various sources
- **Flow Definition**: Define and analyze traffic flows between sections
- **Counting**: Count road users assigned to flows with configurable time intervals
- **Event Detection**: Generate event lists when road users cross defined sections
- **Visualization**: Visualize tracks, flows, and sections
- **Export**: Export analysis results in various formats
- **Filtering**: Filter tracks by road user class and time
- **Parallelization**: Process data using multiple CPU cores

## Documentation

For detailed instructions on how to install and use OTAnalytics, please refer to
the [official documentation](https://opentrafficcam.org/OTAnalytics).

- [Installation Guide](https://opentrafficcam.org/OTAnalytics/installation/)
- [User Interface Guide](https://opentrafficcam.org/OTAnalytics/usage-ui/)

## Contributing

We appreciate your support in the form of both code and comments. Please have a look at
the [contribute](https://opentrafficcam.org/contribute) section of the OpenTrafficCam documentation for guidelines on
how to contribute to this project.

To set up a development environment, see [docs/dev.md](docs/dev.md).

## License

This software is licensed under the [GPL-3.0 License](LICENSE)

## Contact

- GitHub: [https://github.com/OpenTrafficCam](https://github.com/OpenTrafficCam)
- Email: [team@opentrafficcam.org](mailto:team@opentrafficcam.org)
