slice_image.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import argparse
  15. from tqdm import tqdm
  16. def slice_data(image_dir, dataset_json_path, output_dir, slice_size,
  17. overlap_ratio):
  18. try:
  19. from sahi.scripts.slice_coco import slice
  20. except Exception as e:
  21. raise RuntimeError(
  22. 'Unable to use sahi to slice images, please install sahi, for example: `pip install sahi`, see https://github.com/obss/sahi'
  23. )
  24. tqdm.write(
  25. f" slicing for slice_size={slice_size}, overlap_ratio={overlap_ratio}")
  26. slice(
  27. image_dir=image_dir,
  28. dataset_json_path=dataset_json_path,
  29. output_dir=output_dir,
  30. slice_size=slice_size,
  31. overlap_ratio=overlap_ratio, )
  32. def main():
  33. parser = argparse.ArgumentParser()
  34. parser.add_argument(
  35. '--image_dir', type=str, default=None, help="The image folder path.")
  36. parser.add_argument(
  37. '--json_path', type=str, default=None, help="Dataset json path.")
  38. parser.add_argument(
  39. '--output_dir', type=str, default=None, help="Output dir.")
  40. parser.add_argument(
  41. '--slice_size', type=int, default=500, help="slice_size")
  42. parser.add_argument(
  43. '--overlap_ratio', type=float, default=0.25, help="overlap_ratio")
  44. args = parser.parse_args()
  45. slice_data(args.image_dir, args.json_path, args.output_dir, args.slice_size,
  46. args.overlap_ratio)
  47. if __name__ == "__main__":
  48. main()