tdurbor commited on
Commit
683fa1b
1 Parent(s): cc82b64

Resized images smaller for faster loading + changed how mask is computed

Browse files
Files changed (3) hide show
  1. app.py +8 -7
  2. requirements.txt +2 -1
  3. utils/resize_processed_images.py +62 -0
app.py CHANGED
@@ -52,7 +52,7 @@ def update_rankings_table():
52
 
53
  def select_new_image():
54
  """Select a new image and its segmented versions."""
55
- image_paths = load_images_from_directory("data/resized-original-images")
56
  last_image_path = None
57
  max_attempts = 10
58
 
@@ -70,10 +70,10 @@ def select_new_image():
70
 
71
  image_filename = os.path.splitext(os.path.basename(random_image_path))[0] + ".png"
72
  segmented_image_paths = {
73
- "Photoroom": os.path.join("data/processed/photoroom", image_filename),
74
  #"Clipdrop": os.path.join("data/processed/clipdrop", image_filename),
75
- "RemoveBG": os.path.join("data/processed/removebg", image_filename),
76
- "BRIA RMBG 2.0": os.path.join("data/processed/bria", image_filename)
77
  }
78
 
79
  try:
@@ -107,14 +107,15 @@ def get_notice_markdown():
107
 
108
  ## 👇 Test now!
109
  """
 
110
  def compute_mask_difference(segmented_a, segmented_b):
111
  """Compute the absolute difference between two image masks."""
112
  mask_a = np.asarray(segmented_a)
113
  mask_b = np.asarray(segmented_b)
114
 
115
- # Set transparent pixels to zero and compute the sum in one step
116
- mask_a_1d = np.where(mask_a[..., 3] == 0, 0, np.sum(mask_a[..., :3], axis=-1))
117
- mask_b_1d = np.where(mask_b[..., 3] == 0, 0, np.sum(mask_b[..., :3], axis=-1))
118
 
119
  # Compute the absolute difference between the masks
120
  return np.abs(mask_a_1d - mask_b_1d)
 
52
 
53
  def select_new_image():
54
  """Select a new image and its segmented versions."""
55
+ image_paths = load_images_from_directory("data/web-original-images")
56
  last_image_path = None
57
  max_attempts = 10
58
 
 
70
 
71
  image_filename = os.path.splitext(os.path.basename(random_image_path))[0] + ".png"
72
  segmented_image_paths = {
73
+ "Photoroom": os.path.join("data/resized/photoroom", image_filename),
74
  #"Clipdrop": os.path.join("data/processed/clipdrop", image_filename),
75
+ "RemoveBG": os.path.join("data/resized/removebg", image_filename),
76
+ "BRIA RMBG 2.0": os.path.join("data/resized/bria", image_filename)
77
  }
78
 
79
  try:
 
107
 
108
  ## 👇 Test now!
109
  """
110
+
111
  def compute_mask_difference(segmented_a, segmented_b):
112
  """Compute the absolute difference between two image masks."""
113
  mask_a = np.asarray(segmented_a)
114
  mask_b = np.asarray(segmented_b)
115
 
116
+ # Create a binary mask where non-transparent pixels are marked as 1
117
+ mask_a_1d = np.where(mask_a[..., 3] != 0, 1, 0)
118
+ mask_b_1d = np.where(mask_b[..., 3] != 0, 1, 0)
119
 
120
  # Compute the absolute difference between the masks
121
  return np.abs(mask_a_1d - mask_b_1d)
requirements.txt CHANGED
@@ -7,4 +7,5 @@ pillow==11.0.0
7
  python-dotenv==1.0.1
8
  requests==2.32.3
9
  SQLAlchemy==2.0.36
10
- uvicorn==0.30.1
 
 
7
  python-dotenv==1.0.1
8
  requests==2.32.3
9
  SQLAlchemy==2.0.36
10
+ uvicorn==0.30.1
11
+ py-spy
utils/resize_processed_images.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from PIL import Image
2
+ import os
3
+
4
+ def create_directory(path):
5
+ """Create a directory if it doesn't exist."""
6
+ os.makedirs(path, exist_ok=True)
7
+
8
+ def resize_image(input_path, output_path, target_width):
9
+ """Resize an image to the target width while maintaining aspect ratio."""
10
+ with Image.open(input_path) as img:
11
+ # Calculate the new height to maintain the aspect ratio
12
+ width_percent = target_width / img.width
13
+ target_height = int(img.height * width_percent)
14
+
15
+ # Resize the image
16
+ img = img.resize((target_width, target_height), Image.LANCZOS)
17
+
18
+ # Save the resized image in the same format as the input
19
+ img.save(output_path, format=img.format)
20
+
21
+ def process_images(input_directory, output_directory, target_width):
22
+ """Process and resize images from the input directory to the output directory."""
23
+ create_directory(output_directory)
24
+
25
+ for root, _, files in os.walk(input_directory):
26
+ for file in files:
27
+ if file.lower().endswith(('.png', '.jpg', '.jpeg', '.webp', '.heic')):
28
+ file_path = os.path.join(root, file)
29
+ result_file_name = os.path.splitext(file)[0] + os.path.splitext(file)[1]
30
+ result_path = os.path.join(output_directory, result_file_name)
31
+
32
+ # Check if the output file already exists
33
+ if not os.path.exists(result_path):
34
+ print(f"Resizing {file_path} to {result_path}")
35
+ resize_image(file_path, result_path, target_width)
36
+ else:
37
+ print(f"Skipped {file_path}, already resized.")
38
+
39
+ def main():
40
+ """Main function to resize images in specified subdirectories."""
41
+ # Process images in the processed directory
42
+ base_input_directory = "../data/processed"
43
+ base_output_directory = "../data/resized"
44
+ target_width = 800 # Set the desired width for web display
45
+
46
+ # List of subdirectories to process
47
+ subdirectories = ["bria", "photoroom", "clipdrop", "removebg"]
48
+
49
+ for subdir in subdirectories:
50
+ input_directory = os.path.join(base_input_directory, subdir)
51
+ output_directory = os.path.join(base_output_directory, subdir)
52
+ process_images(input_directory, output_directory, target_width)
53
+
54
+ # Additionally, process images in the resized-original-images directory
55
+ original_input_directory = "../data/resized-original-images"
56
+ original_output_directory = "../data/web-original-images"
57
+ process_images(original_input_directory, original_output_directory, target_width)
58
+
59
+ print("Image resizing completed.")
60
+
61
+ if __name__ == "__main__":
62
+ main()