formula_rec.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
  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 random
  16. import numpy as np
  17. import cv2
  18. import PIL
  19. from PIL import Image, ImageDraw, ImageFont
  20. from ...utils.fonts import PINGFANG_FONT_FILE_PATH
  21. from .base import CVResult
  22. class FormulaRecResult(CVResult):
  23. _HARD_FLAG = False
  24. def _to_str(self):
  25. rec_formula_str = ", ".join([str(formula) for formula in self['rec_formula']])
  26. return str(self).replace("\\\\","\\")
  27. def get_minarea_rect(self, points):
  28. bounding_box = cv2.minAreaRect(points)
  29. points = sorted(list(cv2.boxPoints(bounding_box)), key=lambda x: x[0])
  30. index_a, index_b, index_c, index_d = 0, 1, 2, 3
  31. if points[1][1] > points[0][1]:
  32. index_a = 0
  33. index_d = 1
  34. else:
  35. index_a = 1
  36. index_d = 0
  37. if points[3][1] > points[2][1]:
  38. index_b = 2
  39. index_c = 3
  40. else:
  41. index_b = 3
  42. index_c = 2
  43. box = np.array(
  44. [points[index_a], points[index_b], points[index_c], points[index_d]]
  45. ).astype(np.int32)
  46. return box
  47. def _to_img(
  48. self,
  49. ):
  50. """draw ocr result"""
  51. # TODO(gaotingquan): mv to postprocess
  52. drop_score = 0.5
  53. boxes = self["dt_polys"]
  54. formula = self["rec_formula"]
  55. image = self._img_reader.read(self["input_path"])
  56. if self._HARD_FLAG:
  57. image_np = np.array(image)
  58. image = Image.fromarray(image_np[:, :, ::-1])
  59. h, w = image.height, image.width
  60. img_left = image.copy()
  61. random.seed(0)
  62. draw_left = ImageDraw.Draw(img_left)
  63. if formula is None or len(formula) != len(boxes):
  64. formula = [None] * len(boxes)
  65. for idx, (box, txt) in enumerate(zip(boxes, formula)):
  66. try:
  67. color = (
  68. random.randint(0, 255),
  69. random.randint(0, 255),
  70. random.randint(0, 255),
  71. )
  72. box = np.array(box)
  73. if len(box) > 4:
  74. pts = [(x, y) for x, y in box.tolist()]
  75. draw_left.polygon(pts, outline=color, width=8)
  76. box = self.get_minarea_rect(box)
  77. height = int(0.5 * (max(box[:, 1]) - min(box[:, 1])))
  78. box[:2, 1] = np.mean(box[:, 1])
  79. box[2:, 1] = np.mean(box[:, 1]) + min(20, height)
  80. draw_left.polygon(box, fill=color)
  81. except:
  82. continue
  83. img_left = Image.blend(image, img_left, 0.5)
  84. img_show = Image.new("RGB", (w, h), (255, 255, 255))
  85. img_show.paste(img_left, (0, 0, w, h))
  86. return img_show
  87. def create_font(txt, sz, font_path):
  88. """create font"""
  89. font_size = int(sz[1] * 0.8)
  90. font = ImageFont.truetype(font_path, font_size, encoding="utf-8")
  91. if int(PIL.__version__.split(".")[0]) < 10:
  92. length = font.getsize(txt)[0]
  93. else:
  94. length = font.getlength(txt)
  95. if length > sz[0]:
  96. font_size = int(font_size * sz[0] / length)
  97. font = ImageFont.truetype(font_path, font_size, encoding="utf-8")
  98. return font