Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 io
  17. import pdfplumber
  18. from .ocr import OCR
  19. from .recognizer import Recognizer
  20. from .layout_recognizer import LayoutRecognizer4YOLOv10 as LayoutRecognizer
  21. from .table_structure_recognizer import TableStructureRecognizer
  22. def init_in_out(args):
  23. from PIL import Image
  24. import os
  25. import traceback
  26. from api.utils.file_utils import traversal_files
  27. images = []
  28. outputs = []
  29. if not os.path.exists(args.output_dir):
  30. os.mkdir(args.output_dir)
  31. def pdf_pages(fnm, zoomin=3):
  32. nonlocal outputs, images
  33. pdf = pdfplumber.open(fnm)
  34. images = [p.to_image(resolution=72 * zoomin).annotated for i, p in
  35. enumerate(pdf.pages)]
  36. for i, page in enumerate(images):
  37. outputs.append(os.path.split(fnm)[-1] + f"_{i}.jpg")
  38. def images_and_outputs(fnm):
  39. nonlocal outputs, images
  40. if fnm.split(".")[-1].lower() == "pdf":
  41. pdf_pages(fnm)
  42. return
  43. try:
  44. fp = open(fnm, 'rb')
  45. binary = fp.read()
  46. fp.close()
  47. images.append(Image.open(io.BytesIO(binary)).convert('RGB'))
  48. outputs.append(os.path.split(fnm)[-1])
  49. except Exception:
  50. traceback.print_exc()
  51. if os.path.isdir(args.inputs):
  52. for fnm in traversal_files(args.inputs):
  53. images_and_outputs(fnm)
  54. else:
  55. images_and_outputs(args.inputs)
  56. for i in range(len(outputs)):
  57. outputs[i] = os.path.join(args.output_dir, outputs[i])
  58. return images, outputs
  59. __all__ = [
  60. "OCR",
  61. "Recognizer",
  62. "LayoutRecognizer",
  63. "TableStructureRecognizer",
  64. "init_in_out",
  65. ]