ocr-stream 2.3.8__tar.gz → 2.4__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ocr_stream
3
- Version: 2.3.8
3
+ Version: 2.4
4
4
  Summary: Extrair textos de documentos digitalizados e imagens.
5
5
  Author: Bruno Chaves
6
6
  Requires-Python: >=3.11
@@ -12,7 +12,7 @@ Requires-Dist: pandas
12
12
  Requires-Dist: requests>=2.32.4
13
13
  Requires-Dist: opencv-python
14
14
  Requires-Dist: pytesseract
15
- Requires-Dist: convert_stream>=2.3.8
15
+ Requires-Dist: convert_stream>=2.4
16
16
  Requires-Dist: soup_files>=1.4
17
17
 
18
18
  # ocrlib
@@ -9,9 +9,9 @@ from ocr_stream.recognize import RecognizeImage, RecognizePdf
9
9
  from convert_stream import DocumentPdf, PageDocumentPdf
10
10
  from convert_stream.image import ImageObject, CollectionImages
11
11
  from convert_stream.mod_types import (
12
- ColumnsTable, ColumnBody, HeadCell, HeadValues, create_void_dict_map
12
+ ColumnsTable, ColumnBody, HeadCell, HeadValues, DictTextTable
13
13
  )
14
- from time import sleep
14
+
15
15
  from convert_stream.text import FindText
16
16
  from convert_stream.pdf import ConvertPdfToImages, CollectionPagePdf
17
17
  import pandas as pd
@@ -122,7 +122,7 @@ class SearchableTextImage(object):
122
122
  """
123
123
  def __init__(self, silent: bool = False):
124
124
  self.silent = silent
125
- self.elements: dict[str, ColumnBody] = create_void_dict_map()
125
+ self.elements: DictTextTable = DictTextTable.create_void_dict()
126
126
 
127
127
  def __repr__(self):
128
128
  return f'SearchableTextPdf: {self.elements}'
@@ -8,11 +8,13 @@ class LibOcr(Enum):
8
8
 
9
9
  PYTESSERACT = "pytesseract"
10
10
  PYOCR = "pyocr"
11
+ EASYOCR = "easyocr"
11
12
  NOT_IMPLEMENTED = "not_implemented"
12
13
 
13
14
 
14
15
  MOD_PYTESSERACT: bool = False
15
16
  MOD_PYOCR: bool = False
17
+ MOD_EASYOCR: bool = False
16
18
 
17
19
  try:
18
20
  from pytesseract import pytesseract, Output
@@ -27,16 +29,24 @@ try:
27
29
  import pyocr.tesseract
28
30
  MOD_PYOCR = True
29
31
  except Exception as err:
30
- print(err)
32
+ #print(err)
31
33
  pyocr = object
32
34
 
33
35
 
34
- if MOD_PYTESSERACT and MOD_PYOCR:
35
- ModuleOcr: TypeAlias = Union[pyocr, pytesseract]
36
+ try:
37
+ import easyocr
38
+ MOD_EASYOCR = True
39
+ except Exception as err:
40
+ easyocr = object
41
+
42
+ if MOD_PYTESSERACT and MOD_PYOCR and MOD_EASYOCR:
43
+ ModuleOcr: TypeAlias = Union[esasyocr, pytesseract, pyocr]
36
44
  DEFAULT_LIB_OCR = LibOcr.PYTESSERACT
37
45
  elif MOD_PYTESSERACT:
38
46
  ModuleOcr: TypeAlias = Union[pytesseract]
39
47
  DEFAULT_LIB_OCR = LibOcr.PYTESSERACT
48
+ elif MOD_EASYOCR:
49
+ ModuleOcr: TypeAlias = Union[easyocr]
40
50
  elif MOD_PYOCR:
41
51
  ModuleOcr: TypeAlias = Union[pyocr]
42
52
  DEFAULT_LIB_OCR = LibOcr.PYOCR
@@ -44,5 +54,3 @@ else:
44
54
  ModuleOcr: TypeAlias = None
45
55
  DEFAULT_LIB_OCR = LibOcr.NOT_IMPLEMENTED
46
56
 
47
-
48
- __all__ = ['LibOcr', 'DEFAULT_LIB_OCR']
@@ -13,16 +13,10 @@ from convert_stream.image import ImageObject
13
13
  from convert_stream.mod_types import MetaDataItem, MetaDataFile
14
14
  from convert_stream.mod_types.table_types import ColumnBody
15
15
  from ocr_stream.modules import (
16
- LibOcr, DEFAULT_LIB_OCR, pytesseract, Output, pyocr,
16
+ LibOcr, DEFAULT_LIB_OCR, pytesseract, Output, pyocr, easyocr
17
17
  )
18
18
  from ocr_stream.bin_tess import BinTesseract
19
19
 
20
- try:
21
- import pyocr.tesseract
22
- except Exception as err:
23
- print(err)
24
- pyocr = None
25
-
26
20
 
27
21
  class TextRecognized(object):
28
22
  """
@@ -142,6 +136,78 @@ class IPytesseract(ABCOcrTesseract):
142
136
  return TextRecognized(bt)
143
137
 
144
138
 
139
+ # ======================================================================#
140
+ # Modulo easyocr
141
+ # ======================================================================#
142
+ class IEasyOcr(ABCOcrTesseract):
143
+
144
+ def __init__(self, bin_tess: BinTesseract, *, lang: str = 'pt', tess_data_dir: str = None):
145
+ super().__init__(bin_tess, lang=lang, tess_data_dir=tess_data_dir)
146
+ self.reader: easyocr.Reader = easyocr.Reader([self.lang], gpu=True)
147
+
148
+ def to_string(self, img: Union[File, ImageObject]) -> str:
149
+ if isinstance(img, File):
150
+ img = ImageObject(img)
151
+ result = self.reader.readtext(img.to_opencv())
152
+ text = '\n'.join([res[1] for res in result])
153
+ return text
154
+
155
+ def to_reconize(self, img: Union[File, ImageObject]) -> TextRecognized:
156
+ import cv2
157
+ import numpy as np
158
+ from PIL import Image
159
+ from io import BytesIO
160
+ from reportlab.pdfgen import canvas
161
+ from reportlab.lib.pagesizes import A4
162
+ from reportlab.lib.utils import ImageReader
163
+
164
+ # 1️⃣ Converter o objeto de entrada para imagem OpenCV
165
+ if isinstance(img, File):
166
+ img = ImageObject(img)
167
+ img_cv = img.to_opencv()
168
+ img_h, img_w = img_cv.shape[:2]
169
+
170
+ # 2️⃣ Reconhecer o texto com EasyOCR
171
+ results = self.reader.readtext(img_cv)
172
+
173
+ # 3️⃣ Converter a imagem para PDF (imagem de fundo)
174
+ buffer = BytesIO()
175
+ pdf = canvas.Canvas(buffer, pagesize=(img_w, img_h))
176
+
177
+ # Desenha a imagem original como fundo
178
+ img_pil = Image.fromarray(cv2.cvtColor(img_cv, cv2.COLOR_BGR2RGB))
179
+ pdf.drawImage(ImageReader(img_pil), 0, 0, width=img_w, height=img_h)
180
+
181
+ # 4️⃣ Adiciona o texto OCR como camada "invisível" sobre a imagem
182
+ pdf.setFont("Helvetica", 8)
183
+ pdf.setFillColorRGB(1, 1, 1, alpha=0) # texto invisível
184
+
185
+ for (bbox, text, conf) in results:
186
+ if not text.strip():
187
+ continue
188
+ # Coordenadas do bounding box (EasyOCR retorna 4 pontos)
189
+ (x1, y1), (x2, y2), (x3, y3), (x4, y4) = bbox
190
+
191
+ # Média da posição vertical
192
+ avg_y = (y1 + y2 + y3 + y4) / 4
193
+
194
+ # Posição invertida (PDF tem origem no canto inferior esquerdo)
195
+ y_pdf = img_h - avg_y
196
+
197
+ # Largura estimada
198
+ text_width = abs(x2 - x1)
199
+ pdf.drawString(x1, y_pdf, text)
200
+
201
+ pdf.showPage()
202
+ pdf.save()
203
+ buffer.seek(0)
204
+
205
+ # 5️⃣ Retorna como TextRecognized (bytes do PDF com texto embutido)
206
+ text_rec = TextRecognized(buffer.read())
207
+ text_rec.metadata = img.metadata if hasattr(img, "metadata") else MetaDataFile()
208
+ return text_rec
209
+
210
+
145
211
  # ======================================================================#
146
212
  # Modulo OCR pyocr
147
213
  # ======================================================================#
@@ -206,6 +272,10 @@ class TesseractOcr(object):
206
272
  md = IPytesseract(bin_tess, lang=lang, tess_data_dir=tess_data_dir)
207
273
  elif lib_ocr == LibOcr.PYOCR:
208
274
  md = IPyOcr(bin_tess, lang=lang, tess_data_dir=tess_data_dir)
275
+ elif lib_ocr == LibOcr.EASYOCR:
276
+ if lang is None:
277
+ lang = 'pt'
278
+ md = IEasyOcr(bin_tess, lang=lang, tess_data_dir=tess_data_dir)
209
279
  else:
210
280
  raise ValueError(f'{lib_ocr} is not supported')
211
281
  return cls(md)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ocr_stream
3
- Version: 2.3.8
3
+ Version: 2.4
4
4
  Summary: Extrair textos de documentos digitalizados e imagens.
5
5
  Author: Bruno Chaves
6
6
  Requires-Python: >=3.11
@@ -12,7 +12,7 @@ Requires-Dist: pandas
12
12
  Requires-Dist: requests>=2.32.4
13
13
  Requires-Dist: opencv-python
14
14
  Requires-Dist: pytesseract
15
- Requires-Dist: convert_stream>=2.3.8
15
+ Requires-Dist: convert_stream>=2.4
16
16
  Requires-Dist: soup_files>=1.4
17
17
 
18
18
  # ocrlib
@@ -5,5 +5,5 @@ pandas
5
5
  requests>=2.32.4
6
6
  opencv-python
7
7
  pytesseract
8
- convert_stream>=2.3.8
8
+ convert_stream>=2.4
9
9
  soup_files>=1.4
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "ocr_stream"
7
- version = "2.3.8"
7
+ version = "2.4"
8
8
  authors = [
9
9
  { name="Bruno Chaves"},
10
10
  ]
@@ -21,7 +21,7 @@ dependencies = [
21
21
  "requests>=2.32.4",
22
22
  "opencv-python",
23
23
  "pytesseract",
24
- "convert_stream>=2.3.8",
24
+ "convert_stream>=2.4",
25
25
  "soup_files>=1.4",
26
26
  ]
27
27
 
File without changes
File without changes