bhimrazy commited on
Commit
10a7d83
1 Parent(s): 30fdb7f

Updates utlity functions

Browse files
Files changed (1) hide show
  1. src/utils.py +34 -7
src/utils.py CHANGED
@@ -1,10 +1,12 @@
1
  import os
 
2
 
3
  import cv2
4
  import matplotlib.image as mpimg
5
  import matplotlib.pyplot as plt
6
  import numpy as np
7
  from PIL import Image, ImageOps
 
8
 
9
 
10
  def crop_and_pad_image(image_path, threshold=20, target_size=(512, 512)):
@@ -50,7 +52,7 @@ def crop_and_pad_image(image_path, threshold=20, target_size=(512, 512)):
50
  return squared_img
51
 
52
 
53
- def track_files(folder_path, extensions=('.jpg', '.jpeg', '.png')):
54
  """
55
  Track all the files in a folder and its subfolders.
56
 
@@ -83,7 +85,6 @@ def track_files(folder_path, extensions=('.jpg', '.jpeg', '.png')):
83
  return file_list
84
 
85
 
86
-
87
  def crop_circle_roi(image_path):
88
  """
89
  Crop the circular Region of Interest (ROI) from a fundus image.
@@ -104,7 +105,9 @@ def crop_circle_roi(image_path):
104
  _, thresholded_image = cv2.threshold(gray_image, 50, 255, cv2.THRESH_BINARY)
105
 
106
  # Find contours in the binary image
107
- contours, _ = cv2.findContours(thresholded_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
 
 
108
 
109
  # Assuming the largest contour corresponds to the ROI
110
  contour = max(contours, key=cv2.contourArea)
@@ -113,10 +116,11 @@ def crop_circle_roi(image_path):
113
  x, y, w, h = cv2.boundingRect(contour)
114
 
115
  # Crop the circular ROI using the bounding rectangle
116
- cropped_roi = image[y:y+h, x:x+w]
117
 
118
  return cropped_roi
119
 
 
120
  def plot_image_grid(image_paths, roi_crop=False):
121
  """
122
  Create a grid plot with a maximum of 16 images.
@@ -138,9 +142,32 @@ def plot_image_grid(image_paths, roi_crop=False):
138
  else:
139
  img = mpimg.imread(image_paths[i])
140
  ax.imshow(img)
141
- ax.axis('off')
142
  else:
143
- ax.axis('off')
144
 
145
  plt.tight_layout()
146
- plt.show()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
+ from datetime import datetime
3
 
4
  import cv2
5
  import matplotlib.image as mpimg
6
  import matplotlib.pyplot as plt
7
  import numpy as np
8
  from PIL import Image, ImageOps
9
+ from zoneinfo import ZoneInfo
10
 
11
 
12
  def crop_and_pad_image(image_path, threshold=20, target_size=(512, 512)):
 
52
  return squared_img
53
 
54
 
55
+ def track_files(folder_path, extensions=(".jpg", ".jpeg", ".png")):
56
  """
57
  Track all the files in a folder and its subfolders.
58
 
 
85
  return file_list
86
 
87
 
 
88
  def crop_circle_roi(image_path):
89
  """
90
  Crop the circular Region of Interest (ROI) from a fundus image.
 
105
  _, thresholded_image = cv2.threshold(gray_image, 50, 255, cv2.THRESH_BINARY)
106
 
107
  # Find contours in the binary image
108
+ contours, _ = cv2.findContours(
109
+ thresholded_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE
110
+ )
111
 
112
  # Assuming the largest contour corresponds to the ROI
113
  contour = max(contours, key=cv2.contourArea)
 
116
  x, y, w, h = cv2.boundingRect(contour)
117
 
118
  # Crop the circular ROI using the bounding rectangle
119
+ cropped_roi = image[y : y + h, x : x + w]
120
 
121
  return cropped_roi
122
 
123
+
124
  def plot_image_grid(image_paths, roi_crop=False):
125
  """
126
  Create a grid plot with a maximum of 16 images.
 
142
  else:
143
  img = mpimg.imread(image_paths[i])
144
  ax.imshow(img)
145
+ ax.axis("off")
146
  else:
147
+ ax.axis("off")
148
 
149
  plt.tight_layout()
150
+ plt.show()
151
+
152
+
153
+ def generate_run_id(zone: ZoneInfo = ZoneInfo("Asia/Kathmandu")) -> str:
154
+ """Generate a unique run ID using current UTC date and time.
155
+
156
+ Args:
157
+ zone (ZoneInfo, optional): Timezone information. Defaults to Indian Standard Time.
158
+
159
+ Returns:
160
+ str: A unique run ID in the format 'run-YYYY-MM-DD-HH-MM-SS'.
161
+ """
162
+ try:
163
+ current_utc_time = datetime.utcnow().astimezone(zone)
164
+ formatted_time = current_utc_time.strftime("%Y-%m-%d-%H-%M-%S")
165
+ return f"run-{formatted_time}"
166
+ except Exception as e:
167
+ # Handle exceptions gracefully
168
+ print(f"Error generating run ID: {e}")
169
+ return None # Or raise an exception if appropriate
170
+
171
+
172
+ if __name__ == "__main__":
173
+ print(generate_run_id())