blanchon commited on
Commit
02fd27c
·
1 Parent(s): 97e15b8

adjust_bbox_to_divisible_16

Browse files
Files changed (1) hide show
  1. app.py +45 -22
app.py CHANGED
@@ -89,6 +89,39 @@ def remove_padding(image, original_size):
89
  return image.crop((left, top, right, bottom))
90
 
91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
  @spaces.GPU(duration=150)
93
  def infer(
94
  furniture_image_input: Image.Image,
@@ -120,11 +153,17 @@ def infer(
120
  mask_bbox_x_min, mask_bbox_y_min, mask_bbox_x_max, mask_bbox_y_max = (
121
  room_mask.getbbox(alpha_only=False)
122
  )
123
- # Add MASK_CONTEXT_PADDING (16 pixels) for the context
124
- mask_bbox_x_min = max(mask_bbox_x_min - MASK_CONTEXT_PADDING, 0)
125
- mask_bbox_y_min = max(mask_bbox_y_min - MASK_CONTEXT_PADDING, 0)
126
- mask_bbox_x_max = min(mask_bbox_x_max + MASK_CONTEXT_PADDING, room_mask.width)
127
- mask_bbox_y_max = min(mask_bbox_y_max + MASK_CONTEXT_PADDING, room_mask.height)
 
 
 
 
 
 
128
 
129
  bbox_longest_side = max(
130
  mask_bbox_x_max - mask_bbox_x_min,
@@ -175,19 +214,6 @@ def infer(
175
  room_image_cropped.save("room_image_cropped.png")
176
  room_mask_cropped.save("room_mask_cropped.png")
177
 
178
- # _room_image = ImageOps.fit(
179
- # _room_image,
180
- # (max_dimension, max_dimension),
181
- # method=Image.Resampling.LANCZOS,
182
- # centering=(0.5, 0.5),
183
- # )
184
- # _room_image.save("room_image.png")
185
- # _room_mask_with_white_background = Image.new(
186
- # "RGB", _room_mask.size, (255, 255, 255)
187
- # )
188
- # _room_mask_with_white_background.paste(_room_mask, (0, 0), _room_mask)
189
- # _room_mask_with_white_background.save("room_mask.png")
190
-
191
  furniture_image = ImageOps.pad(
192
  furniture_image_input,
193
  (max_dimension, max_dimension),
@@ -196,8 +222,6 @@ def infer(
196
  centering=(0.5, 0.5),
197
  )
198
 
199
- # _furniture_image.save("furniture_image.png")
200
-
201
  furniture_mask = Image.new("RGB", (max_dimension, max_dimension), (255, 255, 255))
202
 
203
  image = Image.new(
@@ -285,7 +309,7 @@ intro_markdown = r"""
285
  </div>
286
  <br>
287
  <div style="display: flex; text-align: center; font-size: 14px; padding-right: 300px; padding-left: 300px;">
288
- AnyFurnish is a tool that allows you to generate furniture images using Flux.1 Fill Dev.
289
  You can upload a furniture image and a room image, and the tool will generate a new image with the furniture in the room.
290
  </div>
291
  </div>
@@ -358,7 +382,6 @@ with gr.Blocks(css=css) as demo:
358
  sources=["upload"],
359
  image_mode="RGBA",
360
  layers=False,
361
- # crop_size="1:1",
362
  brush=gr.Brush(colors=["#FFFFFF"], color_mode="fixed"),
363
  height=500,
364
  )
 
89
  return image.crop((left, top, right, bottom))
90
 
91
 
92
+ def adjust_bbox_to_divisible_16(
93
+ x_min, y_min, x_max, y_max, width, height, padding=MASK_CONTEXT_PADDING
94
+ ):
95
+ # Add padding
96
+ x_min = max(x_min - padding, 0)
97
+ y_min = max(y_min - padding, 0)
98
+ x_max = min(x_max + padding, width)
99
+ y_max = min(y_max + padding, height)
100
+
101
+ # Calculate current bbox width and height
102
+ bbox_width = x_max - x_min
103
+ bbox_height = y_max - y_min
104
+
105
+ # Ensure bbox dimensions are divisible by 16
106
+ if bbox_width % 16 != 0:
107
+ adjustment = 16 - (bbox_width % 16)
108
+ x_min = max(x_min - adjustment // 2, 0)
109
+ x_max = min(x_max + adjustment // 2, width)
110
+
111
+ if bbox_height % 16 != 0:
112
+ adjustment = 16 - (bbox_height % 16)
113
+ y_min = max(y_min - adjustment // 2, 0)
114
+ y_max = min(y_max + adjustment // 2, height)
115
+
116
+ # Ensure bbox is still within bounds
117
+ x_min = max(x_min, 0)
118
+ y_min = max(y_min, 0)
119
+ x_max = min(x_max, width)
120
+ y_max = min(y_max, height)
121
+
122
+ return x_min, y_min, x_max, y_max
123
+
124
+
125
  @spaces.GPU(duration=150)
126
  def infer(
127
  furniture_image_input: Image.Image,
 
153
  mask_bbox_x_min, mask_bbox_y_min, mask_bbox_x_max, mask_bbox_y_max = (
154
  room_mask.getbbox(alpha_only=False)
155
  )
156
+ mask_bbox_x_min, mask_bbox_y_min, mask_bbox_x_max, mask_bbox_y_max = (
157
+ adjust_bbox_to_divisible_16(
158
+ mask_bbox_x_min,
159
+ mask_bbox_y_min,
160
+ mask_bbox_x_max,
161
+ mask_bbox_y_max,
162
+ room_mask.width,
163
+ room_mask.height,
164
+ padding=MASK_CONTEXT_PADDING,
165
+ )
166
+ )
167
 
168
  bbox_longest_side = max(
169
  mask_bbox_x_max - mask_bbox_x_min,
 
214
  room_image_cropped.save("room_image_cropped.png")
215
  room_mask_cropped.save("room_mask_cropped.png")
216
 
 
 
 
 
 
 
 
 
 
 
 
 
 
217
  furniture_image = ImageOps.pad(
218
  furniture_image_input,
219
  (max_dimension, max_dimension),
 
222
  centering=(0.5, 0.5),
223
  )
224
 
 
 
225
  furniture_mask = Image.new("RGB", (max_dimension, max_dimension), (255, 255, 255))
226
 
227
  image = Image.new(
 
309
  </div>
310
  <br>
311
  <div style="display: flex; text-align: center; font-size: 14px; padding-right: 300px; padding-left: 300px;">
312
+ AnyFurnish is a tool that allows you to generate furniture images using Flux.1 Fill Dev.
313
  You can upload a furniture image and a room image, and the tool will generate a new image with the furniture in the room.
314
  </div>
315
  </div>
 
382
  sources=["upload"],
383
  image_mode="RGBA",
384
  layers=False,
 
385
  brush=gr.Brush(colors=["#FFFFFF"], color_mode="fixed"),
386
  height=500,
387
  )