Skip to content

Commit eb8d633

Browse files
committed
[test] ocr.py example test and add one test figure
1 parent a855f0e commit eb8d633

File tree

2 files changed

+53
-24
lines changed

2 files changed

+53
-24
lines changed

EduNLP/SIF/parser/ocr.py

+53-24
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
import requests
55
from EduNLP.utils import image2base64
66

7+
78
class FormulaRecognitionError(Exception):
89
"""Exception raised when formula recognition fails."""
910
def __init__(self, message="Formula recognition failed"):
1011
self.message = message
1112
super().__init__(self.message)
1213

14+
1315
def ocr_formula_figure(image_PIL_or_base64, is_base64=False):
1416
"""
1517
Recognizes mathematical formulas in an image and returns their LaTeX representation.
@@ -19,26 +21,38 @@ def ocr_formula_figure(image_PIL_or_base64, is_base64=False):
1921
image_PIL_or_base64 : PngImageFile or str
2022
The PngImageFile if is_base64 is False, or the base64 encoded string of the image if is_base64 is True.
2123
is_base64 : bool, optional
22-
Indicates whether the image_PIL_or_base64 parameter is an PngImageFile or a base64 encoded string, by default False.
24+
Indicates whether the image_PIL_or_base64 parameter is an PngImageFile or a base64 encoded string.
2325
2426
Returns
2527
-------
2628
latex : str
27-
The LaTeX representation of the mathematical formula recognized in the image. Raises an exception if the image is not recognized as containing a mathematical formula.
29+
The LaTeX representation of the mathematical formula recognized in the image.
30+
Raises an exception if the image is not recognized as containing a mathematical formula.
2831
2932
Raises
3033
------
3134
FormulaRecognitionError
32-
If the HTTP request does not return a 200 status code, if there is an error processing the response, or if the image is not recognized as a mathematical formula.
35+
If the HTTP request does not return a 200 status code,
36+
if there is an error processing the response,
37+
if the image is not recognized as a mathematical formula.
3338
3439
Examples
3540
--------
41+
>>> import os
3642
>>> from PIL import Image
37-
>>> image_PIL = Image.open("path/to/your/image.jpg")
43+
>>> from EduNLP.utils import abs_current_dir, path_append
44+
>>> img_dir = os.path.abspath(path_append(abs_current_dir(__file__), "..", "..", "..", "asset", "_static"))
45+
>>> image_PIL = Image.open(path_append(img_dir, "item_ocr_formula.png", to_str=True))
3846
>>> print(ocr_formula_figure(image_PIL))
39-
Or
40-
>>> image_base64 = "base64_encoded_image_string"
47+
f(x)=\\left (\\frac {1}{3}\\right )^{x}-\\sqrt {x}}
48+
>>> import os
49+
>>> from PIL import Image
50+
>>> from EduNLP.utils import abs_current_dir, path_append, image2base64
51+
>>> img_dir = os.path.abspath(path_append(abs_current_dir(__file__), "..", "..", "..", "asset", "_static"))
52+
>>> image_PIL = Image.open(path_append(img_dir, "item_ocr_formula.png", to_str=True))
53+
>>> image_base64 = image2base64(image_PIL)
4154
>>> print(ocr_formula_figure(image_base64, is_base64=True))
55+
f(x)=\\left (\\frac {1}{3}\\right )^{x}-\\sqrt {x}}
4256
4357
Notes
4458
-----
@@ -74,41 +88,59 @@ def ocr_formula_figure(image_PIL_or_base64, is_base64=False):
7488
else:
7589
latex = None
7690
raise FormulaRecognitionError("Image is not recognized as a formula")
77-
91+
7892
return latex
79-
93+
94+
8095
def ocr(src, is_base64=False, figure_instances: dict = None):
8196
"""
82-
Recognizes mathematical formulas within figures from a given source, which can be either a base64 string or an identifier for a figure within a provided dictionary.
97+
Recognizes mathematical formulas within figures from a given source,
98+
which can be either a base64 string or an identifier for a figure within a provided dictionary.
8399
84100
Parameters
85101
----------
86102
src : str
87-
The source from which the figure is to be recognized. It can be a base64 encoded string of the image if is_base64 is True, or an identifier for the figure if is_base64 is False.
103+
The source from which the figure is to be recognized.
104+
It can be a base64 encoded string of the image if is_base64 is True,
105+
or an identifier for the figure if is_base64 is False.
88106
is_base64 : bool, optional
89107
Indicates whether the src parameter is a base64 encoded string or an identifier, by default False.
90108
figure_instances : dict, optional
91-
A dictionary mapping figure identifiers to their corresponding PngImageFile, by default None. This is only required and used if is_base64 is False.
109+
A dictionary mapping figure identifiers to their corresponding PngImageFile, by default None.
110+
This is only required and used if is_base64 is False.
92111
93112
Returns
94113
-------
95114
forumla_figure_latex : str or None
96-
The LaTeX representation of the mathematical formula recognized within the figure. Returns None if no formula is recognized or if the figure_instances dictionary does not contain the specified figure identifier when is_base64 is False.
115+
The LaTeX representation of the mathematical formula recognized within the figure.
116+
Returns None if no formula is recognized or
117+
if the figure_instances dictionary does not contain the specified figure identifier when is_base64 is False.
97118
98119
Examples
99120
--------
100-
>>> src_base64 = r"\FormFigureBase64{base64_encoded_image_string}"
101-
>>> print(ocr(src_base64, is_base64=True))
102-
Or
121+
>>> import os
103122
>>> from PIL import Image
104-
>>> image_PIL = Image.open("path/to/your/image.jpg")
105-
>>> figure_instances = {"figure1": image_PIL}
106-
>>> src_id = r"\FormFigureID{figure1}"
107-
>>> print(ocr(src_id, figure_instances=figure_instances))
123+
>>> from EduNLP.utils import abs_current_dir, path_append
124+
>>> img_dir = os.path.abspath(path_append(abs_current_dir(__file__), "..", "..", "..", "asset", "_static"))
125+
>>> image_PIL = Image.open(path_append(img_dir, "item_ocr_formula.png", to_str=True))
126+
>>> figure_instances = {"1": image_PIL}
127+
>>> src_id = r"$\\FormFigureID{1}$"
128+
>>> print(ocr(src_id[1:-1], figure_instances=figure_instances))
129+
f(x)=\\left (\\frac {1}{3}\\right )^{x}-\\sqrt {x}}
130+
>>> import os
131+
>>> from PIL import Image
132+
>>> from EduNLP.utils import abs_current_dir, path_append, image2base64
133+
>>> img_dir = os.path.abspath(path_append(abs_current_dir(__file__), "..", "..", "..", "asset", "_static"))
134+
>>> image_PIL = Image.open(path_append(img_dir, "item_ocr_formula.png", to_str=True))
135+
>>> image_base64 = image2base64(image_PIL)
136+
>>> src_base64 = r"$\\FormFigureBase64{%s}$" % (image_base64)
137+
>>> print(ocr(src_base64[1:-1], is_base64=True, figure_instances=True))
138+
f(x)=\\left (\\frac {1}{3}\\right )^{x}-\\sqrt {x}}
108139
109140
Notes
110141
-----
111-
This function relies on `ocr_formula_figure` for the actual OCR (Optical Character Recognition) process. Ensure that `ocr_formula_figure` is correctly implemented and can handle both base64 encoded strings and PngImageFile as input.
142+
This function relies on `ocr_formula_figure` for the actual OCR (Optical Character Recognition) process.
143+
Ensure that `ocr_formula_figure` is correctly implemented and can handle base64 encoded strings and PngImageFile.
112144
"""
113145
forumla_figure_latex = None
114146
if is_base64:
@@ -120,8 +152,5 @@ def ocr(src, is_base64=False, figure_instances: dict = None):
120152
if figure_instances is not None:
121153
figure = figure_instances[figure]
122154
forumla_figure_latex = ocr_formula_figure(figure, is_base64)
123-
124-
return forumla_figure_latex
125-
126-
127155

156+
return forumla_figure_latex

asset/_static/item_ocr_formula.png

5.69 KB
Loading

0 commit comments

Comments
 (0)