File size: 2,048 Bytes
2d67c36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from __future__ import annotations

from dataclasses import dataclass
from typing import Any, Dict, List, Optional, Tuple


@dataclass(frozen=True)
class CellStruct:
    id: str
    bbox: Tuple[int, int, int, int]  # (x1, y1, x2, y2)
    bbox_norm: Tuple[float, float, float, float]
    row: int
    col: int
    text: Optional[str] = None
    value: Optional[float] = None
    is_highlight: bool = False
    confidence: Optional[float] = None

    def to_dict(self) -> Dict[str, Any]:
        return {
            "id": self.id,
            "bbox": [int(self.bbox[0]), int(self.bbox[1]), int(self.bbox[2]), int(self.bbox[3])],
            "bbox_norm": [
                float(self.bbox_norm[0]),
                float(self.bbox_norm[1]),
                float(self.bbox_norm[2]),
                float(self.bbox_norm[3]),
            ],
            "row": int(self.row),
            "col": int(self.col),
            "text": self.text,
            "value": self.value,
            "is_highlight": bool(self.is_highlight),
            "confidence": self.confidence,
        }


@dataclass(frozen=True)
class TableStruct:
    image_path: str
    image_size: Tuple[int, int]  # (W, H)
    n_rows: int
    n_cols: int
    cells: List[CellStruct]
    table_bbox: Optional[Tuple[int, int, int, int]] = None
    confidence: Optional[float] = None

    def to_dict(self) -> Dict[str, Any]:
        out: Dict[str, Any] = {
            "image_path": self.image_path,
            "image_size": [int(self.image_size[0]), int(self.image_size[1])],
            "n_rows": int(self.n_rows),
            "n_cols": int(self.n_cols),
            "cells": [c.to_dict() for c in self.cells],
        }
        if self.table_bbox is not None:
            out["table_bbox"] = [
                int(self.table_bbox[0]),
                int(self.table_bbox[1]),
                int(self.table_bbox[2]),
                int(self.table_bbox[3]),
            ]
        if self.confidence is not None:
            out["confidence"] = float(self.confidence)
        return out