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