resizer.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import os
  2. import cv2
  3. import time
  4. import argparse
  5. import numpy as np
  6. from multiprocessing import Pool, cpu_count
  7. def creat_dirs(dir_path):
  8. os.makedirs(dir_path, exist_ok=True)
  9. def chunk(length, n):
  10. for i in range(0, length, n):
  11. yield (i, i + n)
  12. def ResizeWithAspectRatio(curr_dim, resize_to: int = 320):
  13. """returns new h and new w which maintains the aspect ratio"""
  14. h, w = curr_dim
  15. if h > w:
  16. r = resize_to / float(h)
  17. size = (int(w * r), resize_to)
  18. else:
  19. r = resize_to / float(w)
  20. size = (resize_to, int(h * r))
  21. return size[::-1]
  22. def operation(data=None):
  23. prc_id = data["id"]
  24. images_paths = data["images_paths"]
  25. params = data["params"]
  26. SRC_DIR = params["SRC_DIR"]
  27. DST_DIR = params["DST_DIR"]
  28. IMG_SIZE = params["IMG_SIZE"]
  29. print(f"[INFO] starting process {prc_id}")
  30. for image_name in images_paths:
  31. src_image_path = os.path.join(SRC_DIR, image_name)
  32. dst_image_path = os.path.join(DST_DIR, image_name)
  33. image_true = cv2.imread(src_image_path, cv2.IMREAD_COLOR)
  34. imH, imW = image_true.shape[:2]
  35. if not (max(imW, imH) < IMG_SIZE):
  36. asp_h, asp_w = ResizeWithAspectRatio(curr_dim=(imH, imW), resize_to=IMG_SIZE)
  37. image_true = cv2.resize(image_true, (asp_w, asp_h), interpolation=cv2.INTER_NEAREST)
  38. cv2.imwrite(dst_image_path, image_true)
  39. print(f"[INFO] finishing process {prc_id}")
  40. def transform_images_xmls(source_dir, dest_dir, image_size=320):
  41. SRC_DIR = source_dir
  42. DST_DIR = dest_dir
  43. IMG_SIZE = image_size
  44. images_paths = []
  45. IMAGE_FORMATS = (".jpeg", ".JPEG", ".png", ".PNG", ".jpg", ".JPG")
  46. creat_dirs(SRC_DIR)
  47. creat_dirs(DST_DIR)
  48. params = {
  49. "SRC_DIR": SRC_DIR,
  50. "DST_DIR": DST_DIR,
  51. "IMG_SIZE": IMG_SIZE,
  52. }
  53. images_paths = [i for i in os.listdir(SRC_DIR) if i.endswith(IMAGE_FORMATS)]
  54. length = len(images_paths)
  55. procs = cpu_count()
  56. # procIDs = list(range(procs))
  57. print("Procs:", procs)
  58. numImagesPerProc = length / procs
  59. numImagesPerProc = int(np.ceil(numImagesPerProc))
  60. print("numImagesPerProc:", numImagesPerProc)
  61. chunked_image_paths = []
  62. for start, end in chunk(length, numImagesPerProc):
  63. chunked_image_paths.append(images_paths[start:end])
  64. payloads = []
  65. # loop over the set chunked image paths
  66. for i, imagePaths in enumerate(chunked_image_paths):
  67. data = {"id": i, "images_paths": imagePaths, "params": params}
  68. payloads.append(data)
  69. print("[INFO] Directory:", SRC_DIR)
  70. print("[INFO] Total images:", length)
  71. print(f"[INFO] launching pool using {procs} processes")
  72. pool = Pool(processes=procs)
  73. pool.map(operation, payloads)
  74. # close the pool and wait for all processes to finish
  75. print("[INFO] waiting for processes to finish...")
  76. pool.close()
  77. pool.join()
  78. print("[INFO] multiprocessing complete")
  79. def main():
  80. parser = argparse.ArgumentParser(description="Create Training and Validatin splits")
  81. parser.add_argument("-s", "--source-dir", required=True, type=str, help="Input Source folder path")
  82. parser.add_argument("-d", "--destination-dir", required=True, type=str, help="Output destination folder path")
  83. parser.add_argument("-x", "--img-size", required=True, type=int, help="size of resized Image ")
  84. args = parser.parse_args()
  85. src = args.source_dir
  86. dst = args.destination_dir
  87. image_size = args.img_size
  88. start = time.perf_counter()
  89. transform_images_xmls(src, dst, image_size=image_size)
  90. print("\nTime Taken: ", round(time.perf_counter() - start, 3), "s")
  91. if __name__ == "__main__":
  92. main()