File size: 1,305 Bytes
790d9ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""Small helper utilities shared by debug capture and overlay modules."""

import numpy as np
import cv2

from config.runtime import (
    DEBUG_ARTIFACTS_MAX_DIMENSION,
    DEBUG_ARTIFACTS_PREVIEW_DIMENSION,
)


def _debug_resize_rgb(image_rgb: np.ndarray) -> np.ndarray:
    height, width = image_rgb.shape[:2]
    largest = max(height, width)
    if largest <= DEBUG_ARTIFACTS_MAX_DIMENSION:
        return image_rgb
    scale = DEBUG_ARTIFACTS_MAX_DIMENSION / largest
    return cv2.resize(
        image_rgb,
        (max(1, round(width * scale)), max(1, round(height * scale))),
        interpolation=cv2.INTER_AREA,
    )


def _debug_preview_rgb(image_rgb: np.ndarray) -> np.ndarray:
    height, width = image_rgb.shape[:2]
    largest = max(height, width)
    if largest <= DEBUG_ARTIFACTS_PREVIEW_DIMENSION:
        return image_rgb
    scale = DEBUG_ARTIFACTS_PREVIEW_DIMENSION / largest
    return cv2.resize(
        image_rgb,
        (max(1, round(width * scale)), max(1, round(height * scale))),
        interpolation=cv2.INTER_AREA,
    )


def _debug_json_default(value):
    if isinstance(value, np.generic):
        return value.item()
    if isinstance(value, np.ndarray):
        return value.tolist()
    raise TypeError(f"Unsupported debug manifest value: {type(value).__name__}")