Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

__init__.py 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #
  2. # Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import pdfplumber
  17. from .ocr import OCR
  18. from .recognizer import Recognizer
  19. from .layout_recognizer import LayoutRecognizer4YOLOv10 as LayoutRecognizer
  20. from .table_structure_recognizer import TableStructureRecognizer
  21. def init_in_out(args):
  22. from PIL import Image
  23. import os
  24. import traceback
  25. from api.utils.file_utils import traversal_files
  26. images = []
  27. outputs = []
  28. if not os.path.exists(args.output_dir):
  29. os.mkdir(args.output_dir)
  30. def pdf_pages(fnm, zoomin=3):
  31. nonlocal outputs, images
  32. pdf = pdfplumber.open(fnm)
  33. images = [p.to_image(resolution=72 * zoomin).annotated for i, p in
  34. enumerate(pdf.pages)]
  35. for i, page in enumerate(images):
  36. outputs.append(os.path.split(fnm)[-1] + f"_{i}.jpg")
  37. def images_and_outputs(fnm):
  38. nonlocal outputs, images
  39. if fnm.split(".")[-1].lower() == "pdf":
  40. pdf_pages(fnm)
  41. return
  42. try:
  43. images.append(Image.open(fnm))
  44. outputs.append(os.path.split(fnm)[-1])
  45. except Exception:
  46. traceback.print_exc()
  47. if os.path.isdir(args.inputs):
  48. for fnm in traversal_files(args.inputs):
  49. images_and_outputs(fnm)
  50. else:
  51. images_and_outputs(args.inputs)
  52. for i in range(len(outputs)):
  53. outputs[i] = os.path.join(args.output_dir, outputs[i])
  54. return images, outputs
  55. __all__ = [
  56. "OCR",
  57. "Recognizer",
  58. "LayoutRecognizer",
  59. "TableStructureRecognizer",
  60. "init_in_out",
  61. ]