Skip to content

Commit 1a94bb1

Browse files
committed
feat: support to_json func (fixed issue #489)
1 parent ab20888 commit 1a94bb1

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

python/rapidocr/utils/output.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
# @Author: SWHL
33
# @Contact: liekkaskono@163.com
44
from dataclasses import dataclass, field
5-
from typing import List, Optional, Tuple, Union
5+
from typing import Any, Dict, List, Optional, Tuple, Union
66

77
import numpy as np
88

99
from .logger import Logger
10+
from .to_json import ToJSON
1011
from .to_markdown import ToMarkdown
1112
from .utils import save_img
1213
from .vis_res import VisRes
@@ -35,20 +36,23 @@ def __len__(self):
3536
return 0
3637
return len(self.txts)
3738

38-
def to_json(self):
39-
pass
39+
def to_json(self) -> Optional[List[Dict[Any, Any]]]:
40+
if any(v is None for v in (self.boxes, self.txts, self.scores)):
41+
logger.warning("The identified content is empty.")
42+
return None
43+
return ToJSON.to(self.boxes, self.txts, self.scores)
4044

4145
def to_markdown(self) -> str:
4246
return ToMarkdown.to(self.boxes, self.txts)
4347

44-
def vis(self, save_path: Optional[str] = None):
48+
def vis(self, save_path: Optional[str] = None) -> Optional[np.ndarray]:
4549
if self.img is None or self.boxes is None:
4650
logger.warning("No image or boxes to visualize.")
47-
return
51+
return None
4852

4953
if self.viser is None:
5054
logger.error("vis instance is None")
51-
return
55+
return None
5256

5357
if all(v is None for v in self.word_results):
5458
vis_img = self.viser(self.img, self.boxes, self.txts, self.scores)

python/rapidocr/utils/to_json.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# -*- encoding: utf-8 -*-
2+
# @Author: SWHL
3+
# @Contact: liekkaskono@163.com
4+
from typing import Any, Dict, List, Tuple
5+
6+
import numpy as np
7+
8+
9+
class ToJSON:
10+
@classmethod
11+
def to(
12+
cls, boxes: np.ndarray, txts: Tuple[str], scores: Tuple[float]
13+
) -> List[Dict[Any, Any]]:
14+
results = []
15+
for box, txt, score in zip(boxes, txts, scores):
16+
results.append({"box": box.tolist(), "txt": txt, "score": score})
17+
return results

python/rapidocr/utils/to_markdown.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
class ToMarkdown:
66
@classmethod
77
def to(cls, boxes, txts) -> str:
8-
# def to(cls, result: RapidOCROutput) -> str:
98
"""
109
根据 OCR 结果的坐标信息,将文本还原为近似原始排版的 Markdown。
1110

0 commit comments

Comments
 (0)