Utilities¶
Shared helpers used across processors and examples: IoU for bounding-box overlap, JSON extraction and type coercion for cleaning up local-model output, and confidence clamping to keep scores in the [0, 1] range.
utils ¶
Utility modules for gaze.
compute_iou ¶
Compute Intersection over Union (IoU) of two bounding boxes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
box1
|
Sequence[float]
|
First bounding box as [x1, y1, x2, y2] |
required |
box2
|
Sequence[float]
|
Second bounding box as [x1, y1, x2, y2] |
required |
Returns:
| Type | Description |
|---|---|
float
|
IoU score between 0 and 1 |
Raises:
| Type | Description |
|---|---|
ValueError
|
If boxes don't have exactly 4 coordinates |
Source code in src/gaze/utils/iou.py
coerce_json_types ¶
Coerce response values to match JSON schema types, in place.
Recurses into nested objects and array items to arbitrary depth. The
response dict is mutated in place and also returned, so the call can be
used fluently: parsed = coerce_json_types(parsed, schema).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response
|
dict[str, Any]
|
Parsed JSON response dict (mutated in place). |
required |
schema
|
dict[str, Any]
|
The |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The same |
Source code in src/gaze/utils/json_coerce.py
extract_json_from_text ¶
Extract JSON object from model output text.
Handles common formats:
- Markdown code blocks (json ...)
- Raw JSON objects
- JSON embedded in surrounding text
Uses Python's JSONDecoder.raw_decode() for robust parsing that correctly handles JSON strings containing braces.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text that may contain a JSON object |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any] | None
|
Parsed JSON dict, or None if no valid JSON found |
Source code in src/gaze/utils/json_extract.py
clamp_confidence ¶
Clamp a confidence value to [0.0, 1.0].
Local models often emit confidence on non-standard scales (e.g. 0-100 or 0-5) or as word labels (e.g. "high", "medium"). Rather than rejecting these as invalid, we normalize to the expected range so the rest of the pipeline can proceed.
Returns None for boolean, NaN, or infinite inputs.