onnxtr 0.3.1__py3-none-any.whl → 0.3.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -67,11 +67,12 @@ class PreProcessor(NestedObject):
67
67
  if x.dtype not in (np.uint8, np.float32):
68
68
  raise TypeError("unsupported data type for numpy.ndarray")
69
69
  x = shape_translate(x, "HWC")
70
+
71
+ # Resizing
72
+ x = self.resize(x)
70
73
  # Data type & 255 division
71
74
  if x.dtype == np.uint8:
72
75
  x = x.astype(np.float32) / 255.0
73
- # Resizing
74
- x = self.resize(x)
75
76
 
76
77
  return x
77
78
 
@@ -95,13 +96,12 @@ class PreProcessor(NestedObject):
95
96
  raise TypeError("unsupported data type for numpy.ndarray")
96
97
  x = shape_translate(x, "BHWC")
97
98
 
98
- # Data type & 255 division
99
- if x.dtype == np.uint8:
100
- x = x.astype(np.float32) / 255.0
101
99
  # Resizing
102
100
  if (x.shape[1], x.shape[2]) != self.resize.output_size:
103
101
  x = np.array([self.resize(sample) for sample in x])
104
-
102
+ # Data type & 255 division
103
+ if x.dtype == np.uint8:
104
+ x = x.astype(np.float32) / 255.0
105
105
  batches = [x]
106
106
 
107
107
  elif isinstance(x, list) and all(isinstance(sample, np.ndarray) for sample in x):
onnxtr/transforms/base.py CHANGED
@@ -5,8 +5,8 @@
5
5
 
6
6
  from typing import Tuple, Union
7
7
 
8
- import cv2
9
8
  import numpy as np
9
+ from PIL import Image, ImageOps
10
10
 
11
11
  __all__ = ["Resize", "Normalize"]
12
12
 
@@ -17,64 +17,51 @@ class Resize:
17
17
  def __init__(
18
18
  self,
19
19
  size: Union[int, Tuple[int, int]],
20
- interpolation=cv2.INTER_LINEAR,
20
+ interpolation=Image.Resampling.BILINEAR,
21
21
  preserve_aspect_ratio: bool = False,
22
22
  symmetric_pad: bool = False,
23
23
  ) -> None:
24
- super().__init__()
25
- self.size = size
24
+ self.size = size if isinstance(size, tuple) else (size, size)
26
25
  self.interpolation = interpolation
27
26
  self.preserve_aspect_ratio = preserve_aspect_ratio
28
27
  self.symmetric_pad = symmetric_pad
29
28
  self.output_size = size if isinstance(size, tuple) else (size, size)
30
29
 
31
- if not isinstance(self.size, (int, tuple, list)):
32
- raise AssertionError("size should be either a tuple, a list or an int")
30
+ if not isinstance(self.size, (tuple, int)):
31
+ raise AssertionError("size should be either a tuple or an int")
33
32
 
34
- def __call__(
35
- self,
36
- img: np.ndarray,
37
- ) -> np.ndarray:
38
- if img.ndim == 3:
39
- h, w = img.shape[0:2]
40
- else:
41
- h, w = img.shape[1:3]
42
- sh, sw = self.size if isinstance(self.size, tuple) else (self.size, self.size)
33
+ def __call__(self, img: np.ndarray) -> np.ndarray:
34
+ img = (img * 255).astype(np.uint8) if img.dtype != np.uint8 else img
35
+ h, w = img.shape[:2] if img.ndim == 3 else img.shape[1:3]
36
+ sh, sw = self.size
43
37
 
44
- # Calculate aspect ratio of the image
45
- aspect = w / h
38
+ if not self.preserve_aspect_ratio:
39
+ return np.array(Image.fromarray(img).resize((sw, sh), resample=self.interpolation))
46
40
 
47
- # Compute scaling and padding sizes
48
- if self.preserve_aspect_ratio:
49
- if aspect > 1: # Horizontal image
50
- new_w = sw
51
- new_h = int(sw / aspect)
52
- elif aspect < 1: # Vertical image
53
- new_h = sh
54
- new_w = int(sh * aspect)
55
- else: # Square image
56
- new_h, new_w = sh, sw
57
-
58
- img_resized = cv2.resize(img, (new_w, new_h), interpolation=self.interpolation)
59
-
60
- # Calculate padding
61
- pad_top = max((sh - new_h) // 2, 0)
62
- pad_bottom = max(sh - new_h - pad_top, 0)
63
- pad_left = max((sw - new_w) // 2, 0)
64
- pad_right = max(sw - new_w - pad_left, 0)
65
-
66
- # Pad the image
67
- img_resized = cv2.copyMakeBorder( # type: ignore[call-overload]
68
- img_resized, pad_top, pad_bottom, pad_left, pad_right, borderType=cv2.BORDER_CONSTANT, value=0
69
- )
70
-
71
- # Ensure the image matches the target size by resizing it again if needed
72
- img_resized = cv2.resize(img_resized, (sw, sh), interpolation=self.interpolation)
41
+ actual_ratio = h / w
42
+ target_ratio = sh / sw
43
+
44
+ if target_ratio == actual_ratio:
45
+ return np.array(Image.fromarray(img).resize((sw, sh), resample=self.interpolation))
46
+
47
+ if actual_ratio > target_ratio:
48
+ tmp_size = (int(sh / actual_ratio), sh)
73
49
  else:
74
- # Resize the image without preserving aspect ratio
75
- img_resized = cv2.resize(img, (sw, sh), interpolation=self.interpolation)
50
+ tmp_size = (sw, int(sw * actual_ratio))
51
+
52
+ img_resized = Image.fromarray(img).resize(tmp_size, resample=self.interpolation)
53
+ pad_left = pad_top = 0
54
+ pad_right = sw - img_resized.width
55
+ pad_bottom = sh - img_resized.height
56
+
57
+ if self.symmetric_pad:
58
+ pad_left = pad_right // 2
59
+ pad_right -= pad_left
60
+ pad_top = pad_bottom // 2
61
+ pad_bottom -= pad_top
76
62
 
77
- return img_resized
63
+ img_resized = ImageOps.expand(img_resized, (pad_left, pad_top, pad_right, pad_bottom))
64
+ return np.array(img_resized)
78
65
 
79
66
  def __repr__(self) -> str:
80
67
  interpolate_str = self.interpolation
onnxtr/utils/fonts.py CHANGED
@@ -5,14 +5,16 @@
5
5
 
6
6
  import logging
7
7
  import platform
8
- from typing import Optional
8
+ from typing import Optional, Union
9
9
 
10
10
  from PIL import ImageFont
11
11
 
12
12
  __all__ = ["get_font"]
13
13
 
14
14
 
15
- def get_font(font_family: Optional[str] = None, font_size: int = 13) -> ImageFont.ImageFont:
15
+ def get_font(
16
+ font_family: Optional[str] = None, font_size: int = 13
17
+ ) -> Union[ImageFont.FreeTypeFont, ImageFont.ImageFont]:
16
18
  """Resolves a compatible ImageFont for the system
17
19
 
18
20
  Args:
@@ -29,7 +31,7 @@ def get_font(font_family: Optional[str] = None, font_size: int = 13) -> ImageFon
29
31
  try:
30
32
  font = ImageFont.truetype("FreeMono.ttf" if platform.system() == "Linux" else "Arial.ttf", font_size)
31
33
  except OSError: # pragma: no cover
32
- font = ImageFont.load_default()
34
+ font = ImageFont.load_default() # type: ignore[assignment]
33
35
  logging.warning(
34
36
  "unable to load recommended font family. Loading default PIL font,"
35
37
  "font size issues may be expected."
onnxtr/version.py CHANGED
@@ -1 +1 @@
1
- __version__ = 'v0.3.1'
1
+ __version__ = 'v0.3.2'
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: onnxtr
3
- Version: 0.3.1
3
+ Version: 0.3.2
4
4
  Summary: Onnx Text Recognition (OnnxTR): docTR Onnx-Wrapper for high-performance OCR on documents.
5
5
  Author-email: Felix Dittrich <felixdittrich92@gmail.com>
6
6
  Maintainer: Felix Dittrich
@@ -1,7 +1,7 @@
1
1
  onnxtr/__init__.py,sha256=h7Wc2tuHLsaoCk5xNpEFEK-g11A6SJA7nAasA76TQ_Y,100
2
2
  onnxtr/file_utils.py,sha256=WjUKalEdR53aoeIY4e-ihy3r7J_C9qFxL40JHGPfutc,1107
3
3
  onnxtr/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
- onnxtr/version.py,sha256=ywGt0EZ270HVUZZWcczHcQLiONeX32LjZhVbRTZg_qk,23
4
+ onnxtr/version.py,sha256=Mqv-IS8XNTfhjRfomiPmqTyHtOUKl9tLeE2KEmYIkeM,23
5
5
  onnxtr/contrib/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
6
6
  onnxtr/contrib/artefacts.py,sha256=tdmfhvfXVRYEH7uj4_hqf2cuUGoTieyNK8bXsD3zHwo,5383
7
7
  onnxtr/contrib/base.py,sha256=KyJ8_zDSKEWSFBszgCbLjEeI7SKg4N_iH_ZQNf90SWQ,3288
@@ -39,7 +39,7 @@ onnxtr/models/predictor/__init__.py,sha256=XL25XkRkgyK7mldF-CWhg2MMakSdP5vLpDLwL
39
39
  onnxtr/models/predictor/base.py,sha256=VUs1OIsb8FW91U1ehB1sBaxG4Suz8iS-Ut50Zt6_SHo,8860
40
40
  onnxtr/models/predictor/predictor.py,sha256=etxgAvT8cYhboPyHiDRO0BL1rBoTw5lL1vhZP4dHWqw,6247
41
41
  onnxtr/models/preprocessor/__init__.py,sha256=ERmmOxz_9mUkIuccNbzUa5Y6gVLLVDdyc4cCxbCCUbY,20
42
- onnxtr/models/preprocessor/base.py,sha256=f0t0rMCzvuxwgq7jlKvcVWyjeDOx7yCLUw52quEaETM,3990
42
+ onnxtr/models/preprocessor/base.py,sha256=8ZCKsB-o9uRaUm0x4x9FYpYxLXpwHyq2nVv_TlRgaMw,3990
43
43
  onnxtr/models/recognition/__init__.py,sha256=h1bZs55iLJBMATtzS4ntTKwfD6OGXBiiqGv_hEnOFnE,41
44
44
  onnxtr/models/recognition/core.py,sha256=0Q1dVXqRcDUr_ycT5tpoSH9-zuDF58GtnmxWpUS8Ibo,739
45
45
  onnxtr/models/recognition/utils.py,sha256=04abbjx-_OuF5iEANWIAOK3tQQl1tExPmBQx4IG04Lc,3569
@@ -54,20 +54,20 @@ onnxtr/models/recognition/predictor/__init__.py,sha256=ERmmOxz_9mUkIuccNbzUa5Y6g
54
54
  onnxtr/models/recognition/predictor/_utils.py,sha256=ZNm5I7ibiWfTlz302uiifCkUOu65YWa-oUBUMPrrUuQ,3406
55
55
  onnxtr/models/recognition/predictor/base.py,sha256=YvqSNEM3rCEttxl6hsC9zl1R97N9zO2WZfD5_-nfkR0,2483
56
56
  onnxtr/transforms/__init__.py,sha256=ERmmOxz_9mUkIuccNbzUa5Y6gVLLVDdyc4cCxbCCUbY,20
57
- onnxtr/transforms/base.py,sha256=KohBfq5qNkw9aznZtlGlphNlfKSRBhm5An6TcUiFA7M,3965
57
+ onnxtr/transforms/base.py,sha256=sVQIIQLzPRl0Uc6OyDGrJ4H_f6CMune5j0C9VVRAV0s,3577
58
58
  onnxtr/utils/__init__.py,sha256=pESRJKtcQyjRxiMgZPhtPYeLbCj-YSGyMVRHTbcMONU,94
59
59
  onnxtr/utils/common_types.py,sha256=eC_NyIwbo9qVF33LiNPqHKfyabWq9mYEKD9gAloo5UU,601
60
60
  onnxtr/utils/data.py,sha256=Dh0mgeHJhyPwmm63J90uDVmIYbrp63hh1_SnYLnpgJI,4354
61
- onnxtr/utils/fonts.py,sha256=OiOHFwkjN4L7QBrzMi7Ex7qj_KcTEJ1sHEJWSfiGNZU,1281
61
+ onnxtr/utils/fonts.py,sha256=27v0cojgUrVxNF8Krb1FybSoykoxFy1XjG8lHRUuiEY,1353
62
62
  onnxtr/utils/geometry.py,sha256=u9ei6WW8Yd29rtwnrDYercAY-tWkOLkzBd5Oi6NNyDI,17774
63
63
  onnxtr/utils/multithreading.py,sha256=30T7AylM3rb52ZEI3Pk1pfB0VYraTbc7yO2vNODVVFY,2011
64
64
  onnxtr/utils/reconstitution.py,sha256=Hx1_ddLevKLzuxXc19UelPdsGlAwqi4f6vRSYKHDUB4,2617
65
65
  onnxtr/utils/repr.py,sha256=kfbjGL6KymGT8spo2UL4FJXZ0XRwa7CO7Y1dTVR8dIk,2129
66
66
  onnxtr/utils/visualization.py,sha256=CX09qvDnNIw3BFW5F3jM4R9OcpLWAeZyoDyTAOGRvls,9925
67
67
  onnxtr/utils/vocabs.py,sha256=SCQ4XQjbHSxunj1tg2iHRiPfE8OaTAMhcJbKq5BNvFs,3138
68
- onnxtr-0.3.1.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
69
- onnxtr-0.3.1.dist-info/METADATA,sha256=5bzc3Hu83w_57K6-kow6sJ8rf25eJE9KxJtzGpSUkKU,29802
70
- onnxtr-0.3.1.dist-info/WHEEL,sha256=mguMlWGMX-VHnMpKOjjQidIo1ssRlCFu4a4mBpz1s2M,91
71
- onnxtr-0.3.1.dist-info/top_level.txt,sha256=r_MSUTpspp4pWEEWvly-s7ZkfCg1KwrK6-kBlXkWKU8,7
72
- onnxtr-0.3.1.dist-info/zip-safe,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
73
- onnxtr-0.3.1.dist-info/RECORD,,
68
+ onnxtr-0.3.2.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
69
+ onnxtr-0.3.2.dist-info/METADATA,sha256=I9n5apYunvpxpSy36h-UtfNYl8eW3WOLpnPujaKUzgo,29802
70
+ onnxtr-0.3.2.dist-info/WHEEL,sha256=y4mX-SOX4fYIkonsAGA5N0Oy-8_gI4FXw5HNI1xqvWg,91
71
+ onnxtr-0.3.2.dist-info/top_level.txt,sha256=r_MSUTpspp4pWEEWvly-s7ZkfCg1KwrK6-kBlXkWKU8,7
72
+ onnxtr-0.3.2.dist-info/zip-safe,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
73
+ onnxtr-0.3.2.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (70.1.1)
2
+ Generator: setuptools (70.2.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5