result.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. # Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import math
  15. import os
  16. import random
  17. import subprocess
  18. import tempfile
  19. from typing import Dict, Tuple
  20. import numpy as np
  21. from PIL import Image, ImageDraw
  22. from ....utils import logging
  23. from ....utils.deps import class_requires_deps, function_requires_deps, is_dep_available
  24. from ....utils.fonts import PINGFANG_FONT
  25. from ...common.result import BaseCVResult, JsonMixin
  26. from ...models.formula_recognition.result import (
  27. crop_white_area,
  28. draw_box_txt_fine,
  29. draw_formula_module,
  30. env_valid,
  31. generate_pdf_file,
  32. generate_tex_file,
  33. pdf2img,
  34. )
  35. if is_dep_available("opencv-contrib-python"):
  36. import cv2
  37. @class_requires_deps("opencv-contrib-python")
  38. class FormulaRecognitionResult(BaseCVResult):
  39. """Formula Recognition Result"""
  40. def _to_img(self) -> Dict[str, Image.Image]:
  41. """
  42. Converts the internal data to a PIL Image with detection and recognition results.
  43. Returns:
  44. Dict[str, Image.Image]: An image with detection boxes, texts, and scores blended on it.
  45. """
  46. image = Image.fromarray(self["doc_preprocessor_res"]["output_img"][:, :, ::-1])
  47. res_img_dict = {}
  48. model_settings = self["model_settings"]
  49. if model_settings["use_doc_preprocessor"]:
  50. res_img_dict.update(**self["doc_preprocessor_res"].img)
  51. layout_det_res = self["layout_det_res"]
  52. if len(layout_det_res) > 0:
  53. res_img_dict["layout_det_res"] = layout_det_res.img["res"]
  54. try:
  55. env_valid()
  56. except subprocess.CalledProcessError as e:
  57. logging.warning(
  58. "Please refer to 2.3 Formula Recognition Pipeline Visualization in Formula Recognition Pipeline Tutorial to install the LaTeX rendering engine at first."
  59. )
  60. res_img_dict["formula_res_img"] = image
  61. return res_img_dict
  62. if len(layout_det_res) <= 0:
  63. image = np.array(image.convert("RGB"))
  64. rec_formula = self["formula_res_list"][0]["rec_formula"]
  65. xywh = crop_white_area(image)
  66. if xywh is not None:
  67. x, y, w, h = xywh
  68. image = image[y : y + h, x : x + w]
  69. image = Image.fromarray(image)
  70. image_width, image_height = image.size
  71. box = [
  72. [0, 0],
  73. [image_width, 0],
  74. [image_width, image_height],
  75. [0, image_height],
  76. ]
  77. try:
  78. img_formula = draw_formula_module(
  79. image.size, box, rec_formula, is_debug=False
  80. )
  81. img_formula = Image.fromarray(img_formula)
  82. render_width, render_height = img_formula.size
  83. resize_height = render_height
  84. resize_width = int(resize_height * image_width / image_height)
  85. image = image.resize((resize_width, resize_height), Image.LANCZOS)
  86. new_image_width = image.width + int(render_width) + 10
  87. new_image = Image.new(
  88. "RGB", (new_image_width, render_height), (255, 255, 255)
  89. )
  90. new_image.paste(image, (0, 0))
  91. new_image.paste(img_formula, (image.width + 10, 0))
  92. res_img_dict["formula_res_img"] = new_image
  93. return res_img_dict
  94. except subprocess.CalledProcessError as e:
  95. logging.warning("Syntax error detected in formula, rendering failed.")
  96. res_img_dict["formula_res_img"] = image
  97. return res_img_dict
  98. h, w = image.height, image.width
  99. img_left = image.copy()
  100. img_right = np.ones((h, w, 3), dtype=np.uint8) * 255
  101. random.seed(0)
  102. draw_left = ImageDraw.Draw(img_left)
  103. self["formula_res_list"]
  104. for tno in range(len(self["formula_res_list"])):
  105. formula_res = self["formula_res_list"][tno]
  106. formula_res["formula_region_id"]
  107. formula = str(formula_res["rec_formula"])
  108. dt_polys = formula_res["dt_polys"]
  109. x1, y1, x2, y2 = list(dt_polys)
  110. try:
  111. color = (
  112. random.randint(0, 255),
  113. random.randint(0, 255),
  114. random.randint(0, 255),
  115. )
  116. box = [x1, y1, x2, y1, x2, y2, x1, y2]
  117. box = np.array(box).reshape([-1, 2])
  118. pts = [(x, y) for x, y in box.tolist()]
  119. draw_left.polygon(pts, outline=color, width=8)
  120. draw_left.polygon(box, fill=color)
  121. img_right_text = draw_box_formula_fine(
  122. (w, h),
  123. box,
  124. formula,
  125. is_debug=False,
  126. )
  127. pts = np.array(box, np.int32).reshape((-1, 1, 2))
  128. cv2.polylines(img_right_text, [pts], True, color, 1)
  129. img_right = cv2.bitwise_and(img_right, img_right_text)
  130. except subprocess.CalledProcessError as e:
  131. logging.warning("Syntax error detected in formula, rendering failed.")
  132. continue
  133. img_left = Image.blend(image, img_left, 0.5)
  134. img_show = Image.new("RGB", (int(w * 2), h), (255, 255, 255))
  135. img_show.paste(img_left, (0, 0, w, h))
  136. img_show.paste(Image.fromarray(img_right), (w, 0, w * 2, h))
  137. res_img_dict["formula_res_img"] = img_show
  138. return res_img_dict
  139. def _to_str(self, *args, **kwargs) -> Dict[str, str]:
  140. """Converts the instance's attributes to a dictionary and then to a string.
  141. Args:
  142. *args: Additional positional arguments passed to the base class method.
  143. **kwargs: Additional keyword arguments passed to the base class method.
  144. Returns:
  145. Dict[str, str]: A dictionary with the instance's attributes converted to strings.
  146. """
  147. data = {}
  148. data["input_path"] = self["input_path"]
  149. data["page_index"] = self["page_index"]
  150. data["model_settings"] = self["model_settings"]
  151. if self["model_settings"]["use_doc_preprocessor"]:
  152. data["doc_preprocessor_res"] = self["doc_preprocessor_res"].str["res"]
  153. if len(self["layout_det_res"]) > 0:
  154. data["layout_det_res"] = self["layout_det_res"].str["res"]
  155. data["formula_res_list"] = []
  156. for tno in range(len(self["formula_res_list"])):
  157. rec_formula_dict = {
  158. "rec_formula": self["formula_res_list"][tno]["rec_formula"],
  159. "formula_region_id": self["formula_res_list"][tno]["formula_region_id"],
  160. }
  161. if "dt_polys" in self["formula_res_list"][tno]:
  162. rec_formula_dict["dt_polys"] = (
  163. self["formula_res_list"][tno]["dt_polys"],
  164. )
  165. data["formula_res_list"].append(rec_formula_dict)
  166. return JsonMixin._to_str(data, *args, **kwargs)
  167. def _to_json(self, *args, **kwargs) -> Dict[str, str]:
  168. """
  169. Converts the object's data to a JSON dictionary.
  170. Args:
  171. *args: Positional arguments passed to the JsonMixin._to_json method.
  172. **kwargs: Keyword arguments passed to the JsonMixin._to_json method.
  173. Returns:
  174. Dict[str, str]: A dictionary containing the object's data in JSON format.
  175. """
  176. data = {}
  177. data["input_path"] = self["input_path"]
  178. data["page_index"] = str(self["page_index"])
  179. data["model_settings"] = self["model_settings"]
  180. if self["model_settings"]["use_doc_preprocessor"]:
  181. data["doc_preprocessor_res"] = self["doc_preprocessor_res"].str["res"]
  182. if len(self["layout_det_res"]) > 0:
  183. data["layout_det_res"] = self["layout_det_res"].str["res"]
  184. data["formula_res_list"] = []
  185. for tno in range(len(self["formula_res_list"])):
  186. rec_formula_dict = {
  187. "rec_formula": self["formula_res_list"][tno]["rec_formula"],
  188. "formula_region_id": self["formula_res_list"][tno]["formula_region_id"],
  189. }
  190. if "dt_polys" in self["formula_res_list"][tno]:
  191. rec_formula_dict["dt_polys"] = (
  192. self["formula_res_list"][tno]["dt_polys"],
  193. )
  194. data["formula_res_list"].append(rec_formula_dict)
  195. return JsonMixin._to_json(data, *args, **kwargs)
  196. @function_requires_deps("opencv-contrib-python")
  197. def draw_box_formula_fine(
  198. img_size: Tuple[int, int], box: np.ndarray, formula: str, is_debug: bool = False
  199. ) -> np.ndarray:
  200. """draw box formula for pipeline"""
  201. """
  202. Draw box formula for pipeline.
  203. This function generates a LaTeX formula image and transforms it to fit
  204. within a specified bounding box on a larger image. If the rendering fails,
  205. it will write "Rendering Failed" inside the box.
  206. Args:
  207. img_size (Tuple[int, int]): The size of the image (width, height).
  208. box (np.ndarray): A numpy array representing the four corners of the bounding box.
  209. formula (str): The LaTeX formula to render.
  210. is_debug (bool, optional): If True, enables debug mode. Defaults to False.
  211. Returns:
  212. np.ndarray: An image array with the rendered formula inside the specified box.
  213. """
  214. box_height = int(
  215. math.sqrt((box[0][0] - box[3][0]) ** 2 + (box[0][1] - box[3][1]) ** 2)
  216. )
  217. box_width = int(
  218. math.sqrt((box[0][0] - box[1][0]) ** 2 + (box[0][1] - box[1][1]) ** 2)
  219. )
  220. with tempfile.TemporaryDirectory() as td:
  221. tex_file_path = os.path.join(td, "temp.tex")
  222. pdf_file_path = os.path.join(td, "temp.pdf")
  223. img_file_path = os.path.join(td, "temp.jpg")
  224. generate_tex_file(tex_file_path, formula)
  225. if os.path.exists(tex_file_path):
  226. generate_pdf_file(tex_file_path, td, is_debug)
  227. formula_img = None
  228. if os.path.exists(pdf_file_path):
  229. formula_img = pdf2img(pdf_file_path, img_file_path, is_padding=False)
  230. if formula_img is not None:
  231. formula_h, formula_w = formula_img.shape[:-1]
  232. resize_height = box_height
  233. resize_width = formula_w * resize_height / formula_h
  234. formula_img = cv2.resize(
  235. formula_img, (int(resize_width), int(resize_height))
  236. )
  237. formula_h, formula_w = formula_img.shape[:-1]
  238. pts1 = np.float32(
  239. [[0, 0], [box_width, 0], [box_width, box_height], [0, box_height]]
  240. )
  241. pts2 = np.array(box, dtype=np.float32)
  242. M = cv2.getPerspectiveTransform(pts1, pts2)
  243. formula_img = np.array(formula_img, dtype=np.uint8)
  244. img_right_text = cv2.warpPerspective(
  245. formula_img,
  246. M,
  247. img_size,
  248. flags=cv2.INTER_NEAREST,
  249. borderMode=cv2.BORDER_CONSTANT,
  250. borderValue=(255, 255, 255),
  251. )
  252. else:
  253. img_right_text = draw_box_txt_fine(
  254. img_size, box, "Rendering Failed", PINGFANG_FONT.path
  255. )
  256. return img_right_text