Files
clip-annotator/README.md

206 lines
7.3 KiB
Markdown
Raw Normal View History

2026-05-01 10:28:48 +02:00
# River Annotation Tool
2026-05-01 09:24:14 +02:00
2026-05-20 13:26:03 +02:00
A desktop application for manually annotating river video clips as part of the [HydroScan](https://github.com/HydroScan) project. Annotators draw pixel-level water masks over river footage and answer structured survey questions about flow conditions, lighting, and scene quality.
2026-05-01 09:24:14 +02:00
2026-05-01 10:28:48 +02:00
## Requirements
2026-05-01 09:24:14 +02:00
2026-05-01 10:28:48 +02:00
- Python 3.12
- [uv](https://docs.astral.sh/uv/) (recommended) or pip
## Installation
2026-05-01 09:24:14 +02:00
```sh
2026-05-01 10:28:48 +02:00
# Clone the repository
git clone <repo-url>
cd river-annotation-tool
2026-05-20 13:26:03 +02:00
# Install with uv (creates the virtual environment automatically)
2026-05-01 10:28:48 +02:00
uv sync
# Or with pip
python -m venv .venv
2026-05-20 13:26:03 +02:00
.venv\Scripts\activate # Windows
# source .venv/bin/activate # macOS/Linux
2026-05-01 10:28:48 +02:00
pip install -e .
2026-05-01 09:24:14 +02:00
```
## Setup
Before running, create your config and clip list from the provided examples:
```sh
cp config/config.example.yaml config/config.yaml
cp config/clips.example.txt config/clips.txt
```
Edit `config/config.yaml` to set your `data_dir` and `out_dir`, then edit `config/clips.txt` to list the clips you want to annotate.
2026-05-01 10:28:48 +02:00
## Usage
2026-05-01 09:24:14 +02:00
```sh
python -m river_annotation_tool.annotation_script
2026-05-01 09:24:14 +02:00
```
2026-05-20 13:26:03 +02:00
### Arguments
2026-05-01 10:28:48 +02:00
| Argument | Default | Description |
|---|---|---|
| `--config` | `config/config.yaml` | Path to the config YAML file |
| `--data` | *(from config)* | Override `data_dir` from config |
| `--out` | *(from config)* | Override `out_dir` from config |
| `--clips` | *(from config)* | Override `clips_file` from config |
| `--clip` | *(first unannotated in list)* | Open a specific clip by stem name (e.g. `left_20230501`) |
2026-05-20 13:26:03 +02:00
| `--extras` | off | Also save GIFs and extra PNGs (see Output section) |
### Typical workflows
```sh
# Annotate clips listed in config/clips.txt (default)
python -m river_annotation_tool.annotation_script
2026-05-01 10:28:48 +02:00
# Use a different config file
python -m river_annotation_tool.annotation_script --config config/my_config.yaml
2026-05-20 13:26:03 +02:00
# Override paths from the command line
python -m river_annotation_tool.annotation_script --data data/clips --out data/out
2026-05-20 13:26:03 +02:00
# Annotate a single specific clip
python -m river_annotation_tool.annotation_script --clip left_20230615T120000
```
## Configuration
All settings live in `config/config.yaml`. Copy `config/config.example.yaml` to get started.
```yaml
display_max: 720 # longest side in pixels for display
fps_fallback: 25 # FPS to use if the video header is missing
max_frames: 100 # max frames to extract per clip
data_dir: data/clips # directory containing ZIP archives
out_dir: data/annotation_results
clips_file: config/clips.txt
questions:
- section: River
items:
- key: flow
label: "Flow Regime"
options: [Turbulent, Laminar, Uncertain]
default: Laminar
# add more items or sections as needed
```
Add, remove, or reorder questions directly in the YAML — the UI rebuilds automatically. `key` is what gets saved in `metadata.json`; `default` selects the pre-checked option (omit or set to `null` to leave unselected).
## Clip list file
`config/clips.txt` lists the clip filenames to annotate, one per line. Lines starting with `#` are ignored. Clips are processed in order; already-annotated clips (those with an existing `mask.png`) are skipped automatically.
```
# Example clips.txt
left_20230501T120000.zip
left_20230502T120000.zip
2026-05-20 13:26:03 +02:00
```
Copy `config/clips.example.txt` as a starting point.
2026-05-20 13:26:03 +02:00
## Controls
The window shows the video on the left (auto-playing) and the survey panel on the right.
2026-05-01 10:28:48 +02:00
| Action | How |
|---|---|
2026-05-20 13:26:03 +02:00
| Draw water mask | Click and drag on the video |
2026-05-01 10:28:48 +02:00
| Erase mask | Toggle **Eraser** button, then drag |
2026-05-20 13:26:03 +02:00
| Undo last stroke | **Undo** |
| Clear entire mask | **Clear** |
| Adjust brush size | Slider next to the erase controls |
| Save and continue | **Next** — saves current clip and loads the next one |
| Skip without saving | **Skip** — discards changes and loads the next one |
| Save only | **Save** — writes to disk without advancing |
| Restore last save | **Reload Saved** — reverts mask and answers to what was last written |
2026-05-01 10:28:48 +02:00
## Output
Each annotated clip produces a folder `<out_dir>/<clip_stem>/` with:
2026-05-01 09:24:14 +02:00
2026-05-01 10:28:48 +02:00
```
2026-05-20 13:26:03 +02:00
mask.png # Binary water mask at full source resolution (always)
metadata.json # Survey answers as JSON (always)
frame.png # Middle frame of the clip (always)
overlay.png # That frame with the mask blended in green (always)
# Only with --extras:
mask_vis.png # Mask rendered as a greyscale PNG
video_original_hires.gif # All frames at display resolution
video_original_lowres.gif # All frames at 50% of display resolution
video_overlay_hires.gif # Overlay GIF at display resolution
video_overlay_lowres.gif # Overlay GIF at 50% of display resolution
```
### Survey answers (`metadata.json`)
Keys and values are determined by the `questions` section in `config/config.yaml`. With the default config:
2026-05-20 13:26:03 +02:00
```json
{
"flow": "Turbulent | Laminar | Uncertain",
"shadows": "Yes | No | Uncertain",
"artifacts": "Yes | No | Uncertain",
"lighting": "Day | Night | Uncertain",
"exposure": "Overexposed | Underexposed | Both | Normal | Uncertain",
"snowing": "Yes | No | Uncertain",
"snow_on_ground":"Yes | No | Uncertain"
}
2026-05-01 10:28:48 +02:00
```
2026-05-20 13:26:03 +02:00
## How it works
### Clip format
Each clip is a ZIP archive containing a `left.mp4` video. The filename encodes the recording timestamp (e.g. `left_20230615T120000.zip`).
2026-05-20 13:26:03 +02:00
### Frame loading
Up to `max_frames` frames are extracted from the video and scaled so the longest side is `display_max` px. This display-resolution copy is what the annotator works on; the full-resolution dimensions are remembered separately so the saved mask is upscaled back to the original size on export.
2026-05-20 13:26:03 +02:00
### Mask drawing
The mask is a binary NumPy array matching the display frame size. Each brush stroke stamps a filled circle of the selected radius, setting pixels to 1 (draw) or 0 (erase). The history stack stores a copy of the mask before each stroke, enabling unlimited undo. On save the mask is resized to the original video resolution with nearest-neighbour interpolation and written as an 8-bit PNG (0 or 255).
### Resuming
When a clip is loaded that already has a saved `mask.png` and `metadata.json`, the mask is restored at display resolution and the survey answers are pre-filled. **Reload Saved** lets you revert to the last save at any point during the current session.
## Repository structure
2026-05-01 10:28:48 +02:00
```
config/
config.yaml # Your local config (git-ignored, copy from example)
config.example.yaml # Example config to copy and edit
clips.txt # Your clip list (git-ignored, copy from example)
clips.example.txt # Example clip list
2026-05-01 10:28:48 +02:00
src/river_annotation_tool/
annotation_script.py # Entry point — argument parsing and app launch
annotator.py # Main QMainWindow — orchestrates all components
clip_selector.py # Reads the clip list and picks the next clip
mask_canvas.py # Drawing widget — brush, undo, erase, mouse events
video_loader.py # ZIP extraction and frame resizing
config.py # AppConfig dataclass and YAML loader
__init__.py # Package version
pyproject.toml # Project metadata and dependencies
2026-05-01 09:24:14 +02:00
```
2026-05-01 10:28:48 +02:00
## Development
```sh
# Install pre-commit hooks
pre-commit install
2026-05-20 13:26:03 +02:00
pre-commit run --all-files # Run manually once
2026-05-01 10:28:48 +02:00
# Add a dependency
uv add <package>
uv add --dev <package> # Development-only
```