Change dataset to checkered + processing pipeline
Browse files- app.py +17 -20
- image_processing_pipeline.py +9 -20
app.py
CHANGED
@@ -31,7 +31,7 @@ google_analytics_tracking_id = os.getenv("GOOGLE_ANALYTICS_TRACKING_ID")
|
|
31 |
logging.basicConfig(level=logging.INFO)
|
32 |
|
33 |
# Load datasets and initialize database
|
34 |
-
dataset = load_dataset("bgsys/background-removal-arena-
|
35 |
fill_database_once()
|
36 |
|
37 |
# Directory setup for JSON dataset
|
@@ -136,23 +136,20 @@ def get_notice_markdown():
|
|
136 |
"""
|
137 |
|
138 |
def compute_mask_difference(segmented_a, segmented_b):
|
139 |
-
"""Compute the
|
140 |
-
mask_a = np.asarray(segmented_a)
|
141 |
-
mask_b = np.asarray(segmented_b)
|
142 |
-
|
143 |
-
#
|
144 |
-
|
145 |
-
|
146 |
-
#
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
)
|
153 |
-
|
154 |
-
# Compute the absolute difference between the masks
|
155 |
-
return np.abs(mask_a_1d - mask_b_1d)
|
156 |
|
157 |
js = r"""
|
158 |
function load_zoom() {
|
@@ -169,7 +166,7 @@ function load_zoom() {
|
|
169 |
});
|
170 |
|
171 |
// Choose a scale factor
|
172 |
-
const scale =
|
173 |
|
174 |
function handleMouseMove(e) {
|
175 |
const rect = e.currentTarget.getBoundingClientRect();
|
@@ -297,7 +294,7 @@ def gradio_interface():
|
|
297 |
image_a = gr.Image(value=segmented_a, label="Image A", width=image_width, height=image_height)
|
298 |
image_b = gr.Image(value=segmented_b, label="Image B", width=image_width, height=image_height)
|
299 |
input_image_display = gr.AnnotatedImage(
|
300 |
-
value=(input_image, [(mask_difference
|
301 |
width=image_width,
|
302 |
height=image_height
|
303 |
)
|
|
|
31 |
logging.basicConfig(level=logging.INFO)
|
32 |
|
33 |
# Load datasets and initialize database
|
34 |
+
dataset = load_dataset("bgsys/background-removal-arena-green_v0_clothing_checkered", split='train')
|
35 |
fill_database_once()
|
36 |
|
37 |
# Directory setup for JSON dataset
|
|
|
136 |
"""
|
137 |
|
138 |
def compute_mask_difference(segmented_a, segmented_b):
|
139 |
+
"""Compute the difference between two images across all channels."""
|
140 |
+
mask_a = np.asarray(segmented_a, dtype=np.float32) / 255.0
|
141 |
+
mask_b = np.asarray(segmented_b, dtype=np.float32) / 255.0
|
142 |
+
|
143 |
+
# Compute absolute difference across all channels (RGBA)
|
144 |
+
difference = np.abs(mask_a - mask_b)
|
145 |
+
|
146 |
+
# Take the maximum difference across channels at each pixel
|
147 |
+
max_diff = np.max(difference, axis=-1)
|
148 |
+
|
149 |
+
# Apply non-linear transformation to enhance visibility of smaller differences
|
150 |
+
# Using square root to compress the range while maintaining continuity
|
151 |
+
# Multiply by 3 to make it more visible
|
152 |
+
return np.sqrt(max_diff) * 3
|
|
|
|
|
|
|
153 |
|
154 |
js = r"""
|
155 |
function load_zoom() {
|
|
|
166 |
});
|
167 |
|
168 |
// Choose a scale factor
|
169 |
+
const scale = 5;
|
170 |
|
171 |
function handleMouseMove(e) {
|
172 |
const rect = e.currentTarget.getBoundingClientRect();
|
|
|
294 |
image_a = gr.Image(value=segmented_a, label="Image A", width=image_width, height=image_height)
|
295 |
image_b = gr.Image(value=segmented_b, label="Image B", width=image_width, height=image_height)
|
296 |
input_image_display = gr.AnnotatedImage(
|
297 |
+
value=(input_image, [(mask_difference, button_name)]),
|
298 |
width=image_width,
|
299 |
height=image_height
|
300 |
)
|
image_processing_pipeline.py
CHANGED
@@ -11,9 +11,9 @@ from utils.removebg import iterate_over_directory as removebg_iterate
|
|
11 |
from utils.photoroom import iterate_over_directory as photoroom_iterate
|
12 |
from utils.bria_rmbg20 import iterate_over_directory as bria_iterate
|
13 |
from utils.clipdrop import iterate_over_directory as clipdrop_iterate
|
14 |
-
from utils.add_green_background import process_directory as add_green_background_process
|
15 |
from utils.upload_to_dataset import upload_to_dataset
|
16 |
from utils.resize_processed_images import process_images as downsize_processed_images
|
|
|
17 |
|
18 |
def check_env_variables():
|
19 |
"""Check if the necessary environment variables are loaded."""
|
@@ -30,7 +30,7 @@ def check_env_variables():
|
|
30 |
|
31 |
def copy_images(source_dir, dest_dir):
|
32 |
os.makedirs(dest_dir, exist_ok=True)
|
33 |
-
valid_extensions = ('.png', '.jpg', '.jpeg')
|
34 |
|
35 |
# Walk through the source directory
|
36 |
for root, _, files in os.walk(source_dir):
|
@@ -57,7 +57,6 @@ def main():
|
|
57 |
parser = argparse.ArgumentParser(description="Image Processing Pipeline")
|
58 |
parser.add_argument("--input-dir", type=str, default="original-images", help="Input directory for images")
|
59 |
parser.add_argument("--work-dir", type=str, default="workdir", help="Working directory for intermediate images")
|
60 |
-
parser.add_argument("--output-dir", type=str, default="final-images", help="Output directory for final images")
|
61 |
parser.add_argument("--dataset-name", type=str, help="Name of the dataset to upload to Hugging Face Hub")
|
62 |
parser.add_argument("--push-dataset", action="store_true", help="Push the dataset to the Hugging Face Hub")
|
63 |
|
@@ -66,10 +65,10 @@ def main():
|
|
66 |
# Define intermediate directories within the work directory
|
67 |
input_resized_dir = os.path.join(args.work_dir, "resized")
|
68 |
bg_removed_dir = os.path.join(args.work_dir, "background-removed")
|
69 |
-
|
70 |
|
71 |
# Ensure all directories exist
|
72 |
-
for directory in [input_resized_dir, bg_removed_dir,
|
73 |
os.makedirs(directory, exist_ok=True)
|
74 |
|
75 |
# Step 4: Move images to final output directory
|
@@ -100,23 +99,13 @@ def main():
|
|
100 |
executor.submit(bria_iterate, input_resized_dir, bg_removal_dirs["bria"])
|
101 |
executor.submit(clipdrop_iterate, input_resized_dir, bg_removal_dirs["clipdrop"])
|
102 |
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
target_width = 800
|
108 |
-
subdirectories = ["bria", "photoroom", "clipdrop", "removebg"]
|
109 |
-
os.makedirs(args.output_dir, exist_ok=True)
|
110 |
-
for subdir in subdirectories:
|
111 |
-
input_directory = os.path.join(green_bg_dir, subdir)
|
112 |
-
output_directory = os.path.join(args.output_dir, subdir)
|
113 |
-
downsize_processed_images(input_directory, output_directory, target_width)
|
114 |
-
|
115 |
-
original_output_directory = os.path.join(args.output_dir, "web-original-images")
|
116 |
-
downsize_processed_images(input_resized_dir, original_output_directory, target_width)
|
117 |
|
118 |
if args.dataset_name:
|
119 |
-
upload_to_dataset(
|
120 |
else:
|
121 |
print("Please provide a dataset name using --dataset-name")
|
122 |
|
|
|
11 |
from utils.photoroom import iterate_over_directory as photoroom_iterate
|
12 |
from utils.bria_rmbg20 import iterate_over_directory as bria_iterate
|
13 |
from utils.clipdrop import iterate_over_directory as clipdrop_iterate
|
|
|
14 |
from utils.upload_to_dataset import upload_to_dataset
|
15 |
from utils.resize_processed_images import process_images as downsize_processed_images
|
16 |
+
from utils.add_checkered_background import process_directory as add_checkered_background_process
|
17 |
|
18 |
def check_env_variables():
|
19 |
"""Check if the necessary environment variables are loaded."""
|
|
|
30 |
|
31 |
def copy_images(source_dir, dest_dir):
|
32 |
os.makedirs(dest_dir, exist_ok=True)
|
33 |
+
valid_extensions = ('.png', '.jpg', '.jpeg', '.webp')
|
34 |
|
35 |
# Walk through the source directory
|
36 |
for root, _, files in os.walk(source_dir):
|
|
|
57 |
parser = argparse.ArgumentParser(description="Image Processing Pipeline")
|
58 |
parser.add_argument("--input-dir", type=str, default="original-images", help="Input directory for images")
|
59 |
parser.add_argument("--work-dir", type=str, default="workdir", help="Working directory for intermediate images")
|
|
|
60 |
parser.add_argument("--dataset-name", type=str, help="Name of the dataset to upload to Hugging Face Hub")
|
61 |
parser.add_argument("--push-dataset", action="store_true", help="Push the dataset to the Hugging Face Hub")
|
62 |
|
|
|
65 |
# Define intermediate directories within the work directory
|
66 |
input_resized_dir = os.path.join(args.work_dir, "resized")
|
67 |
bg_removed_dir = os.path.join(args.work_dir, "background-removed")
|
68 |
+
checkered_bg_dir = os.path.join(args.work_dir, "checkered-background")
|
69 |
|
70 |
# Ensure all directories exist
|
71 |
+
for directory in [input_resized_dir, bg_removed_dir, checkered_bg_dir]:
|
72 |
os.makedirs(directory, exist_ok=True)
|
73 |
|
74 |
# Step 4: Move images to final output directory
|
|
|
99 |
executor.submit(bria_iterate, input_resized_dir, bg_removal_dirs["bria"])
|
100 |
executor.submit(clipdrop_iterate, input_resized_dir, bg_removal_dirs["clipdrop"])
|
101 |
|
102 |
+
|
103 |
+
print("Adding checkered background...")
|
104 |
+
add_checkered_background_process(bg_removed_dir, checkered_bg_dir)
|
105 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
|
107 |
if args.dataset_name:
|
108 |
+
upload_to_dataset(input_resized_dir, checkered_bg_dir, args.dataset_name, dry_run=not args.push_dataset)
|
109 |
else:
|
110 |
print("Please provide a dataset name using --dataset-name")
|
111 |
|