Metadata-Version: 2.4
Name: picture-irball-detect
Version: 0.1.0
Summary: Portable image-based infrared marker-ball detection
License-Expression: MIT
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy>=2.0
Requires-Dist: opencv-python-headless<5,>=4.10
Requires-Dist: onnxruntime<2,>=1.20
Requires-Dist: pillow>=10
Provides-Extra: gpu
Requires-Dist: onnxruntime-gpu[cuda,cudnn]<2,>=1.20; (sys_platform == "win32" and platform_machine == "AMD64") and extra == "gpu"
Requires-Dist: onnxruntime-gpu[cuda,cudnn]<2,>=1.20; (sys_platform == "linux" and platform_machine == "x86_64") and extra == "gpu"
Dynamic: license-file

# picture_irball_detect

`picture_irball_detect` is a pip-installable image detector for infrared marker balls.
It packages the trained MarkerNet ONNX weights, the production post-processing settings, a
Python API, a command-line interface, and a Tkinter desktop interface.

The default installation is self-contained: the model is shipped inside the wheel, and no
model download is required. The default backend is `auto`: it tests the existing CUDA environment
by running a small inference with the selected model. CUDA is used only if that probe succeeds;
otherwise the detector uses CPU ONNX Runtime. If ONNX Runtime itself is missing or broken, `auto`
uses the installed OpenCV CPU backend instead.

Detector startup never downloads, installs, uninstalls, or repairs dependencies. It does not
require network access, `nvidia-smi`, or NVIDIA Python package metadata to select a backend.

## Example

This historical evaluation frame contains eight infrared marker balls. The detector found all
eight balls with no false positives in that evaluation run.

Both images are included in the repository root and the source distribution:

- `example_original.jpg`: the original, unannotated JPEG to use as detector input.
- `example.jpg`: the annotated historical detection result for visual comparison.

The original is copied byte-for-byte from capture `2026-11-13_20-40-27.011729+0800`,
frame `raw_0000100.jpg`.

### Original image

![Original image containing eight infrared marker balls without annotations](example_original.jpg)

### Annotated result

![Eight infrared marker balls detected in an example frame](example.jpg)

After installation, run this from the repository root to detect the balls in the original image:

```powershell
uv run --no-sync picture-irball-detect example_original.jpg --output-dir results
```

## Install with uv

```powershell
uv venv
uv pip install .
```

For development:

```powershell
uv sync --group dev
uv run --no-sync pytest
uv run --no-sync ruff check src tests
```

The package requires Python 3.10 or newer. The Tkinter GUI also requires a Python distribution
with Tk support. The standard Windows Python installer includes it.

These installation commands can download dependencies when you explicitly run them. The optional
`gpu` extra is also available for manual environment setup; the detector never installs it itself.
Using `uv run --no-sync` after setup prevents uv from synchronizing dependencies before startup.

### Optional GPU dependencies (manual installation)

For an NVIDIA GPU on Windows x64 or Linux x86_64, first complete the base installation above, then
close any running detector or Python process using this environment. Install or update the NVIDIA
driver separately; the commands below install Python runtime libraries, not the GPU driver.
The CUDA and cuDNN downloads can be large. Run these commands yourself when you want GPU support.

From the repository root, use PowerShell on Windows:

```powershell
uv pip uninstall --python .venv\Scripts\python.exe onnxruntime
uv pip install --python .venv\Scripts\python.exe --reinstall-package onnxruntime-gpu "onnxruntime-gpu[cuda,cudnn]>=1.20,<2"
```

On Linux, use:

```bash
uv pip uninstall --python .venv/bin/python onnxruntime
uv pip install --python .venv/bin/python --reinstall-package onnxruntime-gpu "onnxruntime-gpu[cuda,cudnn]>=1.20,<2"
```

The `[cuda,cudnn]` extras install the CUDA and cuDNN runtime dependencies as well as GPU ONNX
Runtime. Do not keep both `onnxruntime` and `onnxruntime-gpu` installed: they share the
`onnxruntime` Python module. The first command removes the CPU distribution; the second forces
reinstallation of the GPU distribution to restore shared files even if it was already installed.

Verify GPU inference with the original example image:

```powershell
uv run --no-sync picture-irball-detect example_original.jpg --backend cuda --output-dir results
```

A successful run reports `"backend": "cuda"` and `"provider": "CUDAExecutionProvider"` in the
JSON output. This explicit `cuda` check fails if GPU inference is unavailable; it cannot silently
pass on CPU. For everyday use, omit `--backend cuda` to keep automatic CPU fallback.

Keep using `uv run --no-sync` after this manual setup. The base project declares the CPU
`onnxruntime` dependency, so a plain `uv sync` or reinstalling the base project can bring it back;
repeat the two manual setup commands if that happens. The application itself never runs them.

## Python API

```python
from picture_irball_detect import MarkerDetector, annotate
import cv2

image = cv2.imread("frame.jpg")
with MarkerDetector() as detector:
    balls = detector.detect(image)
    overlay = annotate(image, balls)
    cv2.imwrite("frame_overlay.png", overlay)

for ball in balls:
    print(ball.as_dict())
```

`detect` accepts a `uint8` BGR OpenCV image. Each `Ball` contains `center_x`, `center_y`,
`radius`, and `score` in original image pixels. `score` is a heatmap response, not a calibrated
probability.

## Custom settings and weights

The packaged default settings are available with:

```powershell
picture-irball-detect --print-default-config
```

Copy that JSON file and edit it, then pass the copy to the API or CLI:

```powershell
picture-irball-detect frame.jpg --config my_detector.json --model my_marker_net.onnx
```

The `model_path` setting may also point to a custom ONNX file. A relative model path in a custom
configuration is resolved relative to that configuration file. Custom models must produce the
same `(1, 4, 32, 32)` output layout as the bundled MarkerNet model.

The main settings are `threshold`, `min_radius`, `max_radius`, `max_side`, `flip_ensemble`,
`backend`, `threads`, `scale_filter`, `pattern_filter`, and `parallel_views`.

## CLI

Detect one image and print JSON:

```powershell
picture-irball-detect frame.jpg
```

Write JSON results and an orange-yellow overlay for each input:

```powershell
picture-irball-detect frame.jpg another.jpg --output-dir results
```

Useful overrides:

```powershell
picture-irball-detect frame.jpg --backend onnxruntime --threshold 0.35 --no-pattern-filter
picture-irball-detect frame.jpg --backend cuda
picture-irball-detect frame.jpg --backend opencv
```

### Backend selection

- `auto` probes CUDA using the selected model and a small 64x64 sample. The probe must complete
  inference with valid, finite output, with CUDA still active and CPU fallback disabled. The
  validated session is reused for detection. A failed probe selects CPU ONNX Runtime, or OpenCV
  CPU if ONNX Runtime is unusable.
- `cuda` performs the same probe, but reports an error instead of falling back if it fails.
- `onnxruntime` uses CPU ONNX Runtime directly, without probing CUDA.
- `opencv` uses OpenCV's CPU ONNX implementation directly, without importing ONNX Runtime.

Existing CUDA libraries are loaded through ONNX Runtime's default search paths. A listed CUDA
provider or an installed GPU package alone is not treated as proof that GPU inference works.
Fallback reasons are reported on stderr and in `runtime_info["fallback_reason"]`.

Legacy `auto_install_gpu` configuration/API arguments and the `--no-auto-install-gpu` flag are
accepted but ignored. `PICTURE_IRBALL_AUTO_INSTALL_GPU` is no longer read. None of these options
can enable installation.

## Tkinter GUI

```powershell
picture-irball-detect-gui
```

The GUI can open an image, load a JSON configuration, select a model file, choose the backend,
run detection, display the overlay, and save the annotated image. The configuration controls are
intended for quick experiments; the Python API and JSON file are better for reproducible use.

## Model provenance

The bundled model is the MarkerNet model used by the related image-aware detection service.
It predicts a center heatmap, sub-cell center offsets, and marker radius. The default post-
processing includes mirror-view fusion, isolated-scale filtering, and a local printed-grid
filter to reduce checkerboard false positives.

The exact packaged model hash and training metadata are in
`src/picture_irball_detect/models/metadata.json`.
