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.

t_ocr.py 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 os
  14. import sys
  15. sys.path.insert(
  16. 0,
  17. os.path.abspath(
  18. os.path.join(
  19. os.path.dirname(
  20. os.path.abspath(__file__)),
  21. '../../')))
  22. from deepdoc.vision.seeit import draw_box
  23. from deepdoc.vision import OCR, init_in_out
  24. import argparse
  25. import numpy as np
  26. def main(args):
  27. ocr = OCR()
  28. images, outputs = init_in_out(args)
  29. for i, img in enumerate(images):
  30. bxs = ocr(np.array(img))
  31. bxs = [(line[0], line[1][0]) for line in bxs]
  32. bxs = [{
  33. "text": t,
  34. "bbox": [b[0][0], b[0][1], b[1][0], b[-1][1]],
  35. "type": "ocr",
  36. "score": 1} for b, t in bxs if b[0][0] <= b[1][0] and b[0][1] <= b[-1][1]]
  37. img = draw_box(images[i], bxs, ["ocr"], 1.)
  38. img.save(outputs[i], quality=95)
  39. with open(outputs[i] + ".txt", "w+", encoding='utf-8') as f:
  40. f.write("\n".join([o["text"] for o in bxs]))
  41. if __name__ == "__main__":
  42. parser = argparse.ArgumentParser()
  43. parser.add_argument('--inputs',
  44. help="Directory where to store images or PDFs, or a file path to a single image or PDF",
  45. required=True)
  46. parser.add_argument('--output_dir', help="Directory where to store the output images. Default: './ocr_outputs'",
  47. default="./ocr_outputs")
  48. args = parser.parse_args()
  49. main(args)