File size: 825 Bytes
e735bf3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
from dataclasses import asdict
from typing import Any

from models import FingerQualityResult


def _round_value(value: Any) -> Any:
    if isinstance(value, float):
        return round(value, 2)
    if isinstance(value, dict):
        return {k: _round_value(v) for k, v in value.items()}
    if isinstance(value, (list, tuple)):
        return [_round_value(v) for v in value]
    return value


def finger_quality_result_to_json(result: FingerQualityResult) -> str:
    data = asdict(result)

    # Make bbox frontend-friendly
    if data["bbox"] is not None:
        data["bbox"] = {
            "x": data["bbox"][0],
            "y": data["bbox"][1],
            "width": data["bbox"][2],
            "height": data["bbox"][3],
        }

    data = _round_value(data)
    return json.dumps(data, indent=2)