You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

__init__.py 2.1KB

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